| 1 |
package gorm |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"reflect" |
| 6 |
"testing" |
| 7 |
|
| 8 |
"gorm.io/gorm/clause" |
| 9 |
) |
| 10 |
|
| 11 |
func TestWhereCloneCorruption(t *testing.T) { |
| 12 |
for whereCount := 1; whereCount <= 8; whereCount++ { |
| 13 |
t.Run(fmt.Sprintf("w=%d", whereCount), func(t *testing.T) { |
| 14 |
s := new(Statement) |
| 15 |
for w := 0; w < whereCount; w++ { |
| 16 |
s = s.clone() |
| 17 |
s.AddClause(clause.Where{ |
| 18 |
Exprs: s.BuildCondition(fmt.Sprintf("where%d", w)), |
| 19 |
}) |
| 20 |
} |
| 21 |
|
| 22 |
s1 := s.clone() |
| 23 |
s1.AddClause(clause.Where{ |
| 24 |
Exprs: s.BuildCondition("FINAL1"), |
| 25 |
}) |
| 26 |
s2 := s.clone() |
| 27 |
s2.AddClause(clause.Where{ |
| 28 |
Exprs: s.BuildCondition("FINAL2"), |
| 29 |
}) |
| 30 |
|
| 31 |
if reflect.DeepEqual(s1.Clauses["WHERE"], s2.Clauses["WHERE"]) { |
| 32 |
t.Errorf("Where conditions should be different") |
| 33 |
} |
| 34 |
}) |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
func TestNilCondition(t *testing.T) { |
| 39 |
s := new(Statement) |
| 40 |
if len(s.BuildCondition(nil)) != 0 { |
| 41 |
t.Errorf("Nil condition should be empty") |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
func TestNameMatcher(t *testing.T) { |
| 46 |
for k, v := range map[string][]string{ |
| 47 |
"table.name": {"table", "name"}, |
| 48 |
"`table`.`name`": {"table", "name"}, |
| 49 |
"'table'.'name'": {"table", "name"}, |
| 50 |
"'table'.name": {"table", "name"}, |
| 51 |
"table1.name_23": {"table1", "name_23"}, |
| 52 |
"`table_1`.`name23`": {"table_1", "name23"}, |
| 53 |
"'table23'.'name_1'": {"table23", "name_1"}, |
| 54 |
"'table23'.name1": {"table23", "name1"}, |
| 55 |
"'name1'": {"", "name1"}, |
| 56 |
"`name_1`": {"", "name_1"}, |
| 57 |
"`Name_1`": {"", "Name_1"}, |
| 58 |
"`Table`.`nAme`": {"Table", "nAme"}, |
| 59 |
"my_table.*": {"my_table", "*"}, |
| 60 |
"`my_table`.*": {"my_table", "*"}, |
| 61 |
"User__Company.*": {"User__Company", "*"}, |
| 62 |
"`User__Company`.*": {"User__Company", "*"}, |
| 63 |
`"User__Company".*`: {"User__Company", "*"}, |
| 64 |
`"table"."*"`: {"", ""}, |
| 65 |
} { |
| 66 |
if table, column := matchName(k); table != v[0] || column != v[1] { |
| 67 |
t.Errorf("failed to match value: %v, got %v, expect: %v", k, []string{table, column}, v) |
| 68 |
} |
| 69 |
} |
| 70 |
} |