Repository: codenotary/immudb
File: embedded/sql/catalog_test.go (Go)

Code before:
				id INTEGER AUTO_INCREMENT,
				PRIMARY KEY(id)
			)`, nil)
		require.NoError(t, err)
		totalTablesCount++
	}

	t.Run("table count should be 3 on catalog reload", func(t *testing.T) {
		for i := 0; i < 3; i++ {
			tx, err := engine.NewTx(context.Background(), DefaultTxOptions())
			require.NoError(t, err)
			defer tx.Cancel()

			require.Equal(t, totalTablesCount, tx.catalog.tableCount)
		}
	})

	t.Run("table count should not increase on adding existing table", func(t *testing.T) {
		tx, err := engine.NewTx(context.Background(), DefaultTxOptions())
		require.NoError(t, err)
		defer tx.Cancel()
		catlog := tx.catalog

		for _, v := range []string{"table1", "table2", "table3"} {
			_, err := catlog.newTable(v, []*ColSpec{{colName: "id", colType: IntegerType}})
			require.ErrorIs(t, err, ErrTableAlreadyExists)
		}
		require.Equal(t, totalTablesCount, catlog.tableCount)
	})

	t.Run("table count should increase on using newTable function on catalog", func(t *testing.T) {
		tx, err := engine.NewTx(context.Background(), DefaultTxOptions())
		require.NoError(t, err)
		defer tx.Cancel()
		catlog := tx.catalog

		for _, v := range []string{"table4", "table5", "table6"} {
			_, err := catlog.newTable(v, []*ColSpec{{colName: "id", colType: IntegerType}})
			require.NoError(t, err)
		}
		require.Equal(t, totalTablesCount+3, catlog.tableCount)
	})

	t.Run("table count should increase on adding new table", func(t *testing.T) {
		for _, v := range []string{"table4", "table5", "table6"} {
			_, _, err = engine.Exec(
				context.Background(), nil,
				`
				CREATE TABLE `+v+` (
					id INTEGER AUTO_INCREMENT,
					PRIMARY KEY(id)

Review comment (suggestion):
it may be better to create from a sql statement as the actual creation is triggered from there.

Also it would be useful to have a transaction creating a table but being cancelled instead of committed, which may not increase the counter.

Additionally, check the case of two concurrent transactions creating tables

Code after:
			CREATE TABLE `+v+` (
				id INTEGER AUTO_INCREMENT,
				PRIMARY KEY(id)
			)`, nil)
		require.NoError(t, err)
		totalTablesCount++
	}

	t.Run("table count should be 3 on catalog reload", func(t *testing.T) {
		for i := 0; i < 3; i++ {
			tx, err := engine.NewTx(context.Background(), DefaultTxOptions())
			require.NoError(t, err)
			defer tx.Cancel()

			require.Equal(t, totalTablesCount, tx.catalog.tableCount)
		}
	})

	t.Run("table count should not increase on adding existing table", func(t *testing.T) {
		tx, err := engine.NewTx(context.Background(), DefaultTxOptions())
		require.NoError(t, err)
		defer tx.Cancel()
		catlog := tx.catalog

		for _, v := range []string{"table1", "table2", "table3"} {
			_, err := catlog.newTable(v, []*ColSpec{{colName: "id", colType: IntegerType}})
			require.ErrorIs(t, err, ErrTableAlreadyExists)
		}
		require.Equal(t, totalTablesCount, catlog.tableCount)
	})

	t.Run("table count should increase on using newTable function on catalog", func(t *testing.T) {
		tx, err := engine.NewTx(context.Background(), DefaultTxOptions())
		require.NoError(t, err)
		defer tx.Cancel()
		catlog := tx.catalog

		for _, v := range []string{"table4", "table5", "table6"} {
			_, err := catlog.newTable(v, []*ColSpec{{colName: "id", colType: IntegerType}})
			require.NoError(t, err)
		}
		require.Equal(t, totalTablesCount+3, catlog.tableCount)
	})

	t.Run("table count should increase on adding new table", func(t *testing.T) {
		for _, v := range []string{"table4", "table5", "table6"} {
			_, _, err = engine.Exec(
				context.Background(), nil,
				`
				CREATE TABLE `+v+` (
					id INTEGER AUTO_INCREMENT,