123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- package test
- import (
- "git.sxidc.com/go-framework/baize/framework/core/infrastructure/database/clause"
- "github.com/pkg/errors"
- "testing"
- )
- func TestDatabaseClause(t *testing.T) {
- exceptSql := `SELECT
- name,
- age,
- gender,
- hobby
- FROM
- test.students
- JOIN test.classes ON classes.id = test.students.class_id
- WHERE
- ("name" = ? AND "hobby" IN ?) OR ("age" = ? OR "gender" = ?)
- ORDER BY name DESC
- LIMIT ? OFFSET ?`
- selectClause := clause.NewSelect(
- []string{"name", "age", "gender", "hobby"},
- clause.NewFrom([]clause.Clause{
- clause.TableName("test.students"),
- clause.NewJoin("test.classes", "classes.id = test.students.class_id"),
- }),
- []clause.Clause{
- clause.NewWhere(
- clause.NewConditionJoin(
- clause.NewConditions().
- Equal("name", "test").
- In("hobby", []string{"football", "basketball"}).
- And(),
- clause.NewConditions().
- Equal("age", 18).
- Equal("gender", "male").
- Or(),
- ).Or(),
- ),
- clause.NewOrderBy("name", false),
- clause.NewLimit(1, 10),
- }...)
- selectClauseStr, err := selectClause.Clause()
- if err != nil {
- t.Fatal(err)
- }
- if exceptSql != selectClauseStr {
- t.Errorf("%+v\n", errors.Errorf("except: %s\nactual: %s", exceptSql, selectClauseStr))
- }
- args := selectClause.Args()
- if len(args) != 6 {
- t.Errorf("%+v\n", errors.Errorf("args len: %d", len(args)))
- }
- if args[0] != "test" {
- t.Errorf("%+v\n", errors.Errorf("args[0]: %s", args[0]))
- }
- if args[1].([]string)[0] != "football" {
- t.Errorf("%+v\n", errors.Errorf("args[1]: %s", args[1]))
- }
- if args[1].([]string)[1] != "basketball" {
- t.Errorf("%+v\n", errors.Errorf("args[1]: %s", args[1]))
- }
- if args[2] != 18 {
- t.Errorf("%+v\n", errors.Errorf("args[2]: %s", args[2]))
- }
- if args[3] != "male" {
- t.Errorf("%+v\n", errors.Errorf("args[3]: %s", args[3]))
- }
- if args[4] != 10 {
- t.Errorf("%+v\n", errors.Errorf("args[4]: %s", args[4]))
- }
- if args[5] != 0 {
- t.Errorf("%+v\n", errors.Errorf("args[5]: %s", args[5]))
- }
- }
|