<commit_before>package db

import (
	"testing"
	"database/sql"
	. "gopkg.in/check.v1"
	_ "github.com/mattn/go-sqlite3"
)

func Test(t *testing.T) { TestingT(t) }

type TestRdbSuite struct{}

var _ = Suite(&TestRdbSuite{})

// Tests the panic(no panic handler)
func (suite *TestRdbSuite) TestOperateOnDbWithPanic(c *C) {
	testedCtrl := buildSampleDbController(c)
	defer testedCtrl.Release()

	var testedFunc = func () {
		testedCtrl.OperateOnDb(DbCallbackFunc(func (db *sql.DB) {
			panic("Test Panic")
		}))
	}

	defer func() {
		c.Assert(testedFunc, PanicMatches, "Test Panic")
	} ()
}

// Tests the operate on database
func (suite *TestRdbSuite) TestOperateOnDb(c *C) {
	var getCalled bool = false

	testedCtrl := buildSampleDbController(c)
	defer testedCtrl.Release()

	testedCtrl.OperateOnDb(DbCallbackFunc(func(db *sql.DB) {
		getCalled = true

		_, err := db.Exec("CREATE TABLE test_on_db(tob_id INT PRIMARY KEY)")
		c.Assert(err, IsNil)
	}))
	c.Assert(getCalled, Equals, true)
}

// Tests the releasing for resources on controller
//
// Also tests the re-release(panic)
func (suite *TestRdbSuite) TestRelease(c *C) {
	testedCtrl := buildSampleDbController(c)

	testedCtrl.Release()

	defer func() {
		panicObject := recover()
		c.Assert(panicObject, NotNil)
	} ()

	testedCtrl.Release()
}

// Tests the register of handler
func (suite *TestRdbSuite) TestRegisterPanicHandler(c *C) {
	testedCtrl := buildSampleDbController(c)
	defer testedCtrl.Release()

	var getCalled bool = false
	testedCtrl.RegisterPanicHandler(func(panicValue interface{}) {
		c.Logf("Got panic: %v", panicValue)
		getCalled = true
	})

	testedCtrl.Exec("No Such SQL Stmt")

	c.Assert(getCalled, Equals, true)
}

// Tests the execute of SQL
func (suite *TestRdbSuite) TestExec(c *C) {
	testedCtrl := buildSampleDbController(c)
	defer testedCtrl.Release()

	testedCtrl.Exec("CREATE TABLE test_exec_1(te_id int PRIMARY KEY)")

	testedResult := testedCtrl.Exec("INSERT INTO test_exec_1 VALUES(20)")

	rowAffected, err := testedResult.RowsAffected()

	c.Assert(err, IsNil)
	c.Assert(rowAffected, Equals, int64(1))
}

// Tests the query for rows
func (suite *TestRdbSuite) TestQueryForRows(c *C) {
	testedCtrl := buildSampleDbController(c)
	defer testedCtrl.Release()

	testedCtrl.Exec(
		"CREATE TABLE test_rows(tr_id INT PRIMARY KEY, tr_text VARCHAR(64) NOT NULL)",
	)
	testedCtrl.Exec(
		"INSERT INTO test_rows VALUES(1, 'v-1'), (2, 'v-2'), (3, 'v-3')",
	)

	testedNumberOfRows := testedCtrl.QueryForRows(
		RowsCallbackFunc(func(rows *sql.Rows) IterateControl {
			return IterateContinue
		}),
		"SELECT * FROM test_rows",
	)

	c.Assert(testedNumberOfRows, Equals, uint(3))
}

// Tests the query for row
func (suite *TestRdbSuite) TestQueryForRow(c *C) {
	testedCtrl := buildSampleDbController(c)
	defer testedCtrl.Release()

	testedCtrl.Exec("CREATE TABLE test_row(tr_id INT PRIMARY KEY, tr_text VARCHAR(64) NOT NULL)")
	testedCtrl.Exec("INSERT INTO test_row VALUES(1, 'v-1'), (2, 'v-2'), (3, 'v-3')")

	var numberOfRows int
	testedCtrl.QueryForRow(
		RowCallbackFunc(func(row *sql.Row) {
			DbPanic(row.Scan(&numberOfRows))
		}),
		"SELECT COUNT(*) FROM test_row",
	)

	c.Assert(numberOfRows, Equals, 3)
}

// Tests the executing in transaction
func (suite *TestRdbSuite) TestInTx(c *C) {
	testedCtrl := buildSampleDbController(c)
	defer testedCtrl.Release()

	testedCtrl.Exec(
		"CREATE TABLE test_in_tx(it_id INT PRIMARY KEY, it_text VARCHAR(64) NOT NULL)",
	)

	/**
	 * Builds committed data
	 */
	testedCtrl.InTx(TxCallbackFunc(func(tx *sql.Tx) {
		tx.Exec("INSERT INTO test_in_tx VALUES(21, 'v-21')")
		tx.Exec("INSERT INTO test_in_tx VALUES(22, 'v-22')")
		tx.Exec("INSERT INTO test_in_tx VALUES(23, 'v-23')")
	}))
	assertNumberOfDataInTx(c, testedCtrl, 3)
	// :~)


	/**
	 * Builds rollbacked data
	 */
	var testedFunc = func() {
		testedCtrl.InTx(TxCallbackFunc(func(tx *sql.Tx) {
			tx.Exec("INSERT INTO test_in_tx VALUES(123, 'v-123')")
			tx.Exec("INSERT INTO test_in_tx VALUES(124, 'v-124')")
			_, err := tx.Exec("INSERT INTO test_in_tx VALUES(21, 'v-21')")
			DbPanic(err)
		}))
	}

	c.Assert(testedFunc, PanicMatches, ".*UNIQUE.*")
	assertNumberOfDataInTx(c, testedCtrl, 3)
	// :~)
}

// Tests the calling of if callbacks in transaction
func (suite *TestRdbSuite) TestInTxForIf(c *C) {
	testedCtrl := buildSampleDbController(c)
	defer testedCtrl.Release()

	testedTrueSample := &ifSample { true, false }
	testedFalseSample := &ifSample { false, false }

	testedCtrl.InTxForIf(testedTrueSample)
	testedCtrl.InTxForIf(testedFalseSample)

	c.Assert(testedTrueSample.getCalled, Equals, true)
	c.Assert(testedFalseSample.getCalled, Equals, false)<commit_msg>[OWL-1167] Fix testing<commit_after>package db

import (
	"testing"
	"database/sql"
	. "gopkg.in/check.v1"
	_ "github.com/mattn/go-sqlite3"
)

func Test(t *testing.T) { TestingT(t) }

type TestRdbSuite struct{}

var _ = Suite(&TestRdbSuite{})

// Tests the panic(no panic handler)
func (suite *TestRdbSuite) TestOperateOnDbWithPanic(c *C) {
	testedCtrl := buildSampleDbController(c)
	defer testedCtrl.Release()

	var testedFunc = func () {
		testedCtrl.OperateOnDb(DbCallbackFunc(func (db *sql.DB) {
			panic("Test Panic")
		}))
	}

	defer func() {
		c.Assert(testedFunc, PanicMatches, "Test Panic")
	} ()
}

// Tests the operate on database
func (suite *TestRdbSuite) TestOperateOnDb(c *C) {
	var getCalled bool = false

	testedCtrl := buildSampleDbController(c)
	defer testedCtrl.Release()

	testedCtrl.OperateOnDb(DbCallbackFunc(func(db *sql.DB) {
		getCalled = true

		_, err := db.Exec("CREATE TABLE test_on_db(tob_id INT PRIMARY KEY)")
		c.Assert(err, IsNil)
	}))
	c.Assert(getCalled, Equals, true)
}

// Tests the releasing for resources on controller
//
// Also tests the re-release(panic)
func (suite *TestRdbSuite) TestRelease(c *C) {
	testedCtrl := buildSampleDbController(c)

	testedCtrl.Release()

	defer func() {
		panicObject := recover()
		c.Assert(panicObject, NotNil)
	} ()

	testedCtrl.Release()
}

// Tests the register of handler
func (suite *TestRdbSuite) TestRegisterPanicHandler(c *C) {
	testedCtrl := buildSampleDbController(c)
	defer testedCtrl.Release()

	var getCalled bool = false
	testedCtrl.RegisterPanicHandler(func(panicValue interface{}) {
		c.Logf("Got panic: %v", panicValue)
		getCalled = true
	})

	testedCtrl.Exec("No Such SQL Stmt")

	c.Assert(getCalled, Equals, true)
}

// Tests the execute of SQL
func (suite *TestRdbSuite) TestExec(c *C) {
	testedCtrl := buildSampleDbController(c)
	defer testedCtrl.Release()

	testedCtrl.Exec("CREATE TABLE test_exec_1(te_id int PRIMARY KEY)")

	testedResult := testedCtrl.Exec("INSERT INTO test_exec_1 VALUES(20)")

	rowAffected, err := testedResult.RowsAffected()

	c.Assert(err, IsNil)
	c.Assert(rowAffected, Equals, int64(1))
}

// Tests the query for rows
func (suite *TestRdbSuite) TestQueryForRows(c *C) {
	testedCtrl := buildSampleDbController(c)
	defer testedCtrl.Release()

	testedCtrl.Exec(
		"CREATE TABLE test_rows(tr_id INT PRIMARY KEY, tr_text VARCHAR(64) NOT NULL)",
	)
	testedCtrl.Exec(
		"INSERT INTO test_rows VALUES(1, 'v-1'), (2, 'v-2'), (3, 'v-3')",
	)

	testedNumberOfRows := testedCtrl.QueryForRows(
		RowsCallbackFunc(func(rows *sql.Rows) IterateControl {
			return IterateContinue
		}),
		"SELECT * FROM test_rows",
	)

	c.Assert(testedNumberOfRows, Equals, uint(3))
}

// Tests the query for row
func (suite *TestRdbSuite) TestQueryForRow(c *C) {
	testedCtrl := buildSampleDbController(c)
	defer testedCtrl.Release()

	testedCtrl.Exec("CREATE TABLE test_row(tr_id INT PRIMARY KEY, tr_text VARCHAR(64) NOT NULL)")
	testedCtrl.Exec("INSERT INTO test_row VALUES(1, 'v-1'), (2, 'v-2'), (3, 'v-3')")

	var numberOfRows int
	testedCtrl.QueryForRow(
		RowCallbackFunc(func(row *sql.Row) {
			DbPanic(row.Scan(&numberOfRows))
		}),
		"SELECT COUNT(*) FROM test_row",
	)

	c.Assert(numberOfRows, Equals, 3)
}

// Tests the executing in transaction
func (suite *TestRdbSuite) TestInTx(c *C) {
	testedCtrl := buildSampleDbController(c)
	defer testedCtrl.Release()

	testedCtrl.Exec(
		"CREATE TABLE test_in_tx(it_id INT PRIMARY KEY, it_text VARCHAR(64) NOT NULL)",
	)

	/**
	 * Builds committed data
	 */
	testedCtrl.InTx(TxCallbackFunc(func(tx *sql.Tx) TxFinale {
		tx.Exec("INSERT INTO test_in_tx VALUES(21, 'v-21')")
		tx.Exec("INSERT INTO test_in_tx VALUES(22, 'v-22')")
		tx.Exec("INSERT INTO test_in_tx VALUES(23, 'v-23')")

		return TxCommit
	}))
	assertNumberOfDataInTx(c, testedCtrl, 3)
	// :~)


	/**
	 * Builds rollbacked data
	 */
	var testedFunc = func() {
		testedCtrl.InTx(TxCallbackFunc(func(tx *sql.Tx) TxFinale {
			tx.Exec("INSERT INTO test_in_tx VALUES(123, 'v-123')")
			tx.Exec("INSERT INTO test_in_tx VALUES(124, 'v-124')")
			_, err := tx.Exec("INSERT INTO test_in_tx VALUES(21, 'v-21')")
			DbPanic(err)

			return TxCommit
		}))
	}

	c.Assert(testedFunc, PanicMatches, ".*UNIQUE.*")
	assertNumberOfDataInTx(c, testedCtrl, 3)
	// :~)
}

// Tests the calling of if callbacks in transaction
func (suite *TestRdbSuite) TestInTxForIf(c *C) {
	testedCtrl := buildSampleDbController(c)
	defer testedCtrl.Release()

	testedTrueSample := &ifSample { true, false }
	testedFalseSample := &ifSample { false, false }

	testedCtrl.InTxForIf(testedTrueSample)
	testedCtrl.InTxForIf(testedFalseSample)

	c.Assert(testedTrueSample.getCalled, Equals, true)
	c.Assert(testedFalseSample.getCalled, Equals, false)
{'fraction_non_alphanumeric': 0.1389957264957265, 'fraction_numerical': 0.013034188034188035, 'mean_word_length': 4.876333961079724, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3787811', 'n_tokens_mistral': 3708, 'n_tokens_neox': 3380, 'n_words': 761}