Add error test cases to TestInsert

pull/2/head
Vinícius Garcia 2021-02-27 11:25:04 -03:00
parent 67a0b2f1cc
commit c768876908
1 changed files with 86 additions and 32 deletions

View File

@ -273,6 +273,7 @@ func TestQueryOne(t *testing.T) {
func TestInsert(t *testing.T) { func TestInsert(t *testing.T) {
for _, driver := range []string{"sqlite3", "postgres"} { for _, driver := range []string{"sqlite3", "postgres"} {
t.Run(driver, func(t *testing.T) { t.Run(driver, func(t *testing.T) {
t.Run("using slice of structs", func(t *testing.T) {
err := createTable(driver) err := createTable(driver)
if err != nil { if err != nil {
t.Fatal("could not create test table!, reason:", err.Error()) t.Fatal("could not create test table!, reason:", err.Error())
@ -311,6 +312,59 @@ func TestInsert(t *testing.T) {
assert.Equal(t, u.Name, result.Name) assert.Equal(t, u.Name, result.Name)
}) })
}) })
t.Run("testing error cases", func(t *testing.T) {
err := createTable(driver)
if err != nil {
t.Fatal("could not create test table!, reason:", err.Error())
}
t.Run("should report error for invalid input types", func(t *testing.T) {
db := connectDB(t, driver)
defer db.Close()
ctx := context.Background()
c := newTestDB(db, driver, "users")
err = c.Insert(ctx, "foo", "bar")
assert.NotEqual(t, nil, err)
err = c.Insert(ctx, nullable.String("foo"), nullable.String("bar"))
assert.NotEqual(t, nil, err)
err = c.Insert(ctx, map[string]interface{}{
"name": "foo",
"age": 12,
})
assert.NotEqual(t, nil, err)
ifUserForgetToExpandList := []interface{}{
&User{Name: "foo", Age: 22},
&User{Name: "bar", Age: 32},
}
err = c.Insert(ctx, ifUserForgetToExpandList)
assert.NotEqual(t, nil, err)
// We might want to support this in the future, but not for now:
err = c.Insert(ctx, User{Name: "not a ptr to user", Age: 42})
assert.NotEqual(t, nil, err)
})
t.Run("should report error if for some reason the insertMethod is invalid", func(t *testing.T) {
db := connectDB(t, driver)
defer db.Close()
ctx := context.Background()
c := newTestDB(db, driver, "users")
// This is an invalid value:
c.insertMethod = insertMethod(42)
err = c.Insert(ctx, &User{Name: "foo"})
assert.NotEqual(t, nil, err)
})
})
})
} }
} }