Exemple #1
0
func Test_ErrorCode(t *testing.T) {
	err1 := new(mysql.Error)
	err1.Code = 123
	err1.Msg = []byte("Test1")

	myT := new(assert.Tspy)
	ErrorCode(myT, err1, 1234)

	if !myT.Failed() {
		t.Fatal("Expected ErrorCode to fail.")
	}

	myT = new(assert.Tspy)
	ErrorCode(myT, err1, 123)

	if myT.Failed() {
		t.Fatal("Expected ErrorCode not to fail.")
	}

	err2 := errors.New("Test1")
	myT = new(assert.Tspy)
	ErrorCode(myT, err2, 1234)

	if !myT.Failed() {
		t.Fatal("Expected ErrorCode to fail.")
	}

	myT = new(assert.Tspy)
	ErrorCode(myT, nil, 1234)

	if !myT.Failed() {
		t.Fatal("Expected ErrorCode to fail.")
	}
}
Exemple #2
0
func Test_RowDoesNotExists(t *testing.T) {

	// Row exists
	myT := new(assert.Tspy)
	RowDoesNotExists(myT, "test_1", "Id", 12)

	if !myT.Failed() {
		t.Fatal("Expected RowExists to fail.")
	}

	// Row does not exist
	myT = new(assert.Tspy)
	RowDoesNotExists(myT, "test_1", "Id", 1234)

	if myT.Failed() {
		t.Fatal("Expected RowExists not to fail.")
	}

	// Row does not exist
	myT = new(assert.Tspy)
	RowDoesNotExists(myT, "test_1s", "Id", "xyz")

	if myT.Failed() {
		t.Fatal("Expected RowExists not to fail.")
	}

	// Row exists
	myT = new(assert.Tspy)
	RowDoesNotExists(myT, "test_1s", "Id", "abc")

	if !myT.Failed() {
		t.Fatal("Expected RowExists to fail.")
	}
}
Exemple #3
0
func Test_RowExists(t *testing.T) {
	// Prepare test
	err := InsertToTestTable("test_1", 12, 123)
	if err != nil {
		t.Fatal("Did not expect database error: " + err.Error())
	}

	// Row does not exist
	myT := new(assert.Tspy)
	RowExists(myT, "test_1", "Id", 111)

	if !myT.Failed() {
		t.Fatal("Expected RowExists to fail.")
	}

	// Row exists
	myT = new(assert.Tspy)
	RowExists(myT, "test_1", "Id", 12)

	if myT.Failed() {
		t.Fatal("Expected RowExists not to fail.")
	}

	// Test string keys - prepare table and data
	createSql := `
	CREATE TABLE %s (
	  Id char(5) NOT NULL,
	  Field int(11) DEFAULT NULL,
	  PRIMARY KEY (id)
	) ENGINE=%s`

	_, _, err = dbcon.Query(createSql, "test_1s", TEST_DB_ENGINE)
	if err != nil {
		t.Fatal("Did not expect database error: " + err.Error())
	}

	insertSql := fmt.Sprintf("INSERT INTO test_1s VALUES ('abc', 1)")
	_, _, err = dbcon.Query(insertSql)
	if err != nil {
		t.Fatal("Did not expect database error: " + err.Error())
	}

	// Row does not exist
	myT = new(assert.Tspy)
	RowExists(myT, "test_1s", "Id", "xyz")

	if !myT.Failed() {
		t.Fatal("Expected RowExists to fail.")
	}

	// Row exists
	myT = new(assert.Tspy)
	RowExists(myT, "test_1s", "Id", "abc")

	if myT.Failed() {
		t.Fatal("Expected RowExists not to fail.")
	}
}
Exemple #4
0
func Test_TableCount(t *testing.T) {
	myT := new(assert.Tspy)
	TableCount(myT, 100)

	if !myT.Failed() {
		t.Fatal("Expected TableCount to fail.")
	}

	myT = new(assert.Tspy)
	TableCount(myT, 6)

	if myT.Failed() {
		t.Fatal("Expected TableCount not to fail.")
	}
}
Exemple #5
0
func Test_TableDoesNotExists(t *testing.T) {
	myT := new(assert.Tspy)

	TableDoesNotExists(myT, "not_existing_table")

	if myT.Failed() {
		t.Fatal("Expected TableDoesNotExists not to fail.")
	}

	myT = new(assert.Tspy)
	TableDoesNotExists(myT, "test_1")

	if !myT.Failed() {
		t.Fatal("Expected TableDoesNotExists to fail.")
	}
}
Exemple #6
0
func Test_LoadFixture(t *testing.T) {

	TruncateTable("test_1")
	myT := new(assert.Tspy)
	LoadFixture(myT, "/not/existing/fixture")

	if !myT.Failed() {
		t.Fatalf("Expected error loading not existing fixture")
	}

	myT = new(assert.Tspy)
	LoadFixture(myT, "fixtures/fixture.sql")

	if myT.Failed() {
		t.Fatalf("Did not expect any error: %s", myT.GetMsg(0))
	}

	rows, res, err := dbcon.Query("SELECT * FROM test_1 ORDER BY Field ASC")
	if err != nil {
		t.Fatal("Did not expect error: " + err.Error())
	}

	if len(rows) != 3 {
		t.Fatal("Expected three rows in the database.")
	}

	for idx, row := range rows {
		if idx != row.Int(res.Map("Field")) {
			t.Fatalf("Expected row %d Field to be equal to %d", idx, row.Int(res.Map("Field")))
		}
	}
}
Exemple #7
0
func Test_TableRowCount(t *testing.T) {

	myT := new(assert.Tspy)
	TableRowCount(myT, "test_1", 100)

	if !myT.Failed() {
		t.Fatal("Expected TableRowCount to fail.")
	}

	myT = new(assert.Tspy)
	TableRowCount(myT, "test_1", 1)

	if myT.Failed() {
		t.Fatal("Expected TableRowCount not to fail.")
	}

	InsertToTestTable("test_1", 2, 456)

	myT = new(assert.Tspy)
	TableRowCount(myT, "test_1", 2)

	if myT.Failed() {
		t.Fatal("Expected TableRowCount not to fail.")
	}
}
Exemple #8
0
func Test_NotError(t *testing.T) {
	err1 := errors.New("Test1")
	err2 := new(mysql.Error)

	myT := new(assert.Tspy)
	NotError(myT, err2, "Custom message")

	if !myT.Failed() {
		t.Fatal("Expected NotError to fail.")
	}

	myT = new(assert.Tspy)
	NotError(myT, err1)

	if myT.Failed() {
		t.Fatal("Expected NotError not to fail.")
	}
}
Exemple #9
0
func Test_TableNotEmpty(t *testing.T) {

	// Table empty
	myT := new(assert.Tspy)
	TableNotEmpty(myT, "test0")

	if !myT.Failed() {
		t.Fatal("Expected TableNotEmpty to fail.")
	}

	// Table not empty
	myT = new(assert.Tspy)
	TableNotEmpty(myT, "test_1")

	if myT.Failed() {
		t.Fatal("Expected TableNotEmpty to fail.")
	}
}
Exemple #10
0
func Test_TableExists(t *testing.T) {
	myT := new(assert.Tspy)

	TableExists(myT, "not_existing_table")

	if !myT.Failed() {
		t.Fatal("Expected TableExists to fail.")
	}

	err := CreateTestTable("test_1", TEST_DB_ENGINE)
	if err != nil {
		t.Fatal("Did not expect database error: " + err.Error())
	}

	myT = new(assert.Tspy)
	TableExists(myT, "test_1")

	if myT.Failed() {
		t.Fatal("Expected TableExists not to fail.")
	}
}