Exemplo n.º 1
0
func TestMogi(t *testing.T) {
	defer mogi.Reset()
	mogi.Verbose(false)
	db := openDB()

	// select (any columns)
	mogi.Select().StubCSV(beerCSV)
	runBeerSelectQuery(t, db)

	// test .Stub()
	mogi.Select().Stub([][]driver.Value{
		{1, "Yona Yona Ale", "Yo-Ho Brewing", 5.5},
		{2, "Punk IPA", "BrewDog", 5.6}})
	runBeerSelectQuery(t, db)

	// test reset
	mogi.Reset()
	_, err := db.Query("SELECT id, name, brewery, pct FROM beer WHERE pct > ?", 5)
	if err != mogi.ErrUnstubbed {
		t.Error("after reset, err should be ErrUnstubbed but is", err)
	}

	// select specific columns
	mogi.Select("id", "name", "brewery", "pct").StubCSV(beerCSV)
	runBeerSelectQuery(t, db)

	// select the "wrong" columns
	mogi.Reset()
	mogi.Select("hello", "👞").StubCSV(beerCSV)
	runUnstubbedSelect(t, db)
}
Exemplo n.º 2
0
func TestSelectTable(t *testing.T) {
	defer mogi.Reset()
	db := openDB()

	// filter by table
	mogi.Select("id", "name", "brewery", "pct").From("beer").StubCSV(beerCSV)
	runBeerSelectQuery(t, db)

	// select the wrong table
	mogi.Reset()
	mogi.Select("id", "name", "brewery", "pct").From("酒").StubCSV(beerCSV)
	runUnstubbedSelect(t, db)
}
Exemplo n.º 3
0
func TestSelectArgs(t *testing.T) {
	defer mogi.Reset()
	db := openDB()

	// where
	mogi.Select().Args(5).StubCSV(beerCSV)
	runBeerSelectQuery(t, db)

	// wrong args
	mogi.Reset()
	mogi.Select().Args("サービス残業").StubCSV(beerCSV)
	runUnstubbedSelect(t, db)
}
Exemplo n.º 4
0
func TestSelectWhereIn(t *testing.T) {
	defer mogi.Reset()
	db := openDB()

	mogi.Select().Where("pct", 5.4, 10.2).StubCSV("2")
	_, err := db.Query("SELECT COUNT(*) FROM beer WHERE pct IN (5.4, ?)", 10.2)
	checkNil(t, err)

	mogi.Reset()
	mogi.Select().WhereOp("pct", "IN", 5.4, 10.2).StubCSV("2")
	_, err = db.Query("SELECT COUNT(*) FROM beer WHERE pct IN (5.4, ?)", 10.2)
	checkNil(t, err)
}
Exemplo n.º 5
0
func TestSelectColumnNames(t *testing.T) {
	defer mogi.Reset()
	db := openDB()

	// qualified names
	mogi.Select("a.thing", "b.thing", "c.thing").From("qqqq", "b", "c").StubCSV(`foo,bar,baz`)
	_, err := db.Query("SELECT a.thing, b.thing, c.thing FROM qqqq as a, b, c WHERE a.id = b.id")
	checkNil(t, err)

	// aliased names
	mogi.Reset()
	mogi.Select("dog", "cat", "hamster").From("a", "b", "c").StubCSV(`foo,bar,baz`)
	_, err = db.Query("SELECT a.thing AS dog, b.thing AS cat, c.thing AS hamster FROM a JOIN b ON a.id = b.id JOIN c ON a.id = c.id")
	checkNil(t, err)
}
Exemplo n.º 6
0
func TestSelectMultipleTables(t *testing.T) {
	defer mogi.Reset()
	db := openDB()

	mogi.Select().From("a", "b").StubCSV(`foo,bar`)
	_, err := db.Query("SELECT a.thing, b.thing FROM a, b WHERE a.id = b.id")
	checkNil(t, err)
	_, err = db.Query("SELECT a.thing, b.thing FROM a JOIN b ON a.id = b.id")
	checkNil(t, err)

	mogi.Reset()
	mogi.Select().From("a", "b", "c").StubCSV(`foo,bar,baz`)
	_, err = db.Query("SELECT a.thing, b.thing, c.thing FROM a, b, c WHERE a.id = b.id")
	checkNil(t, err)
	_, err = db.Query("SELECT a.thing, b.thing, c.thing FROM a JOIN b ON a.id = b.id JOIN c ON a.id = c.id")
	checkNil(t, err)
}
Exemplo n.º 7
0
func TestSelectStar(t *testing.T) {
	defer mogi.Reset()
	db := openDB()

	mogi.Select("*").StubCSV("a,b,c")
	_, err := db.Query("SELECT * FROM beer")
	checkNil(t, err)
}
Exemplo n.º 8
0
func TestSelectCount(t *testing.T) {
	defer mogi.Reset()
	db := openDB()

	mogi.Select("COUNT(abc)", "count(*)").StubCSV("1,5")
	_, err := db.Query("SELECT COUNT(abc), COUNT(*) FROM beer")
	checkNil(t, err)
}
Exemplo n.º 9
0
func TestStubError(t *testing.T) {
	defer mogi.Reset()
	db := openDB()

	mogi.Select().StubError(sql.ErrNoRows)
	_, err := db.Query("SELECT id, name, brewery, pct FROM beer WHERE pct > ?", 5)
	if err != sql.ErrNoRows {
		t.Error("after StubError, err should be ErrNoRows but is", err)
	}
}
Exemplo n.º 10
0
func TestSelectWhere(t *testing.T) {
	defer mogi.Reset()
	db := openDB()

	// where
	mogi.Select().From("beer").Where("pct", 5).StubCSV(beerCSV)
	runBeerSelectQuery(t, db)

	// where with weird type
	type 数字 int
	五 := 数字(5)
	mogi.Reset()
	mogi.Select().From("beer").Where("pct", &五).StubCSV(beerCSV)
	runBeerSelectQuery(t, db)

	// wrong where
	mogi.Reset()
	mogi.Select().From("beer").Where("pct", 98).StubCSV(beerCSV)
	runUnstubbedSelect(t, db)
}