| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- package test
- import (
- "git.sxidc.com/go-framework/baize/framework/core/infrastructure/database/sql"
- "github.com/pkg/errors"
- "testing"
- )
- func TestDatabaseSqlConditions(t *testing.T) {
- conditions := sql.NewConditions().
- AddCondition("name1 = ?", "foo").
- Equal("age", 20).
- Like("name2", "%foo%").
- In("id1", []string{"111", "222"}).
- NotIn("id2", []string{"33", "444"}).
- Not("id3", "555").
- LessThan("num1", 10).
- LessThanAndEqual("num2", 20).
- GreaterThan("num3", 30).
- GreaterThanAndEqual("num4", 40)
- exceptQueries := []string{
- `name1 = ?`,
- `"age" = ?`,
- `"name2" LIKE ?`,
- `"id1" IN ?`,
- `"id2" NOT IN ?`,
- `"id3" != ?`,
- `"num1" < ?`,
- `"num2" <= ?`,
- `"num3" > ?`,
- `"num4" >= ?`,
- }
- exceptArgs := []any{
- "foo",
- 20,
- "%foo%",
- []string{"111", "222"},
- []string{"33", "444"},
- "555",
- 10,
- 20,
- 30,
- 40,
- }
- exceptAnd := `WHERE name1 = ? AND "age" = ? AND "name2" LIKE ? AND "id1" IN ? AND "id2" NOT IN ? AND "id3" != ? AND "num1" < ? AND "num2" <= ? AND "num3" > ? AND "num4" >= ?`
- for i, query := range conditions.Queries() {
- if exceptQueries[i] != query {
- t.Fatalf("%+v\n", errors.Errorf("Query Error: except %v, actural %v",
- exceptQueries[i], query))
- }
- }
- for i, arg := range conditions.Args() {
- argStringSlice, ok := arg.([]string)
- if ok {
- for j, stringArg := range argStringSlice {
- exceptArg, ok := exceptArgs[i].([]string)
- if !ok {
- t.Fatalf("%+v\n", errors.New("参数类型不匹配"))
- }
- if exceptArg[j] != stringArg {
- t.Fatalf("%+v\n", errors.Errorf("Args Error: except %v, actural %v",
- exceptQueries[i], stringArg))
- }
- }
- continue
- }
- if exceptArgs[i] != arg {
- t.Fatalf("%+v\n", errors.Errorf("Args Error: except %v, actural %v",
- exceptQueries[i], arg))
- }
- }
- if conditions.And() != exceptAnd {
- t.Fatalf("%+v\n", errors.Errorf("And Error:\nexcept %v\nactural %v\n",
- exceptAnd, conditions.And()))
- }
- }
|