2024-08-09 15:00:52 +00:00
|
|
|
package builder
|
2024-08-08 21:44:51 +00:00
|
|
|
|
|
|
|
import (
|
2024-08-12 18:21:34 +00:00
|
|
|
"fmt"
|
2024-08-08 21:44:51 +00:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestConvertFind(t *testing.T) {
|
|
|
|
find := Find{
|
|
|
|
"impl": "1",
|
|
|
|
"exp": Is{
|
2024-08-12 18:21:34 +00:00
|
|
|
"$gt": 2,
|
2024-08-08 21:44:51 +00:00
|
|
|
},
|
|
|
|
}
|
2024-08-15 07:06:11 +00:00
|
|
|
ctx := CommonDialect{
|
2024-08-08 21:44:51 +00:00
|
|
|
TableAlias: "t",
|
|
|
|
}
|
2024-08-12 18:21:34 +00:00
|
|
|
result, values := covertFind(ctx, find)
|
|
|
|
if values[1] != "1" {
|
|
|
|
t.Errorf("Expected '1', got %v", values[1])
|
|
|
|
}
|
|
|
|
if values[0].(float64) != 2 {
|
|
|
|
t.Errorf("Expected 2, got %v", values[0])
|
2024-08-08 21:44:51 +00:00
|
|
|
}
|
2024-08-12 18:21:34 +00:00
|
|
|
if result == `t.exp > ? AND t.impl = ?` {
|
2024-08-08 21:44:51 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
t.Errorf(`Expected "t.impl = '1' AND t.exp = 1", got %s`, result)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConvertFindAnd(t *testing.T) {
|
|
|
|
find := Find{
|
|
|
|
"$and": Find{
|
|
|
|
"a": Is{
|
|
|
|
"$gt": 1,
|
|
|
|
},
|
|
|
|
"b": Is{
|
|
|
|
"$lt": 10,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2024-08-15 07:06:11 +00:00
|
|
|
ctx := CommonDialect{
|
2024-08-08 21:44:51 +00:00
|
|
|
TableAlias: "t",
|
|
|
|
}
|
2024-08-12 18:21:34 +00:00
|
|
|
result, values := covertFind(ctx, find)
|
|
|
|
fmt.Println(values)
|
|
|
|
if result == `(t.a > ? AND t.b < ?)` {
|
2024-08-08 21:44:51 +00:00
|
|
|
return
|
|
|
|
}
|
2024-08-12 18:21:34 +00:00
|
|
|
t.Errorf(`Expected "(t.b < ? AND t.a > ?)", got %s`, result)
|
2024-08-08 21:44:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestConvertFindOr(t *testing.T) {
|
|
|
|
find := Query{
|
|
|
|
"$or": Query{
|
|
|
|
"a": Is{
|
|
|
|
"$gt": 1,
|
|
|
|
},
|
|
|
|
"b": Is{
|
|
|
|
"$lt": 10,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2024-08-15 07:06:11 +00:00
|
|
|
ctx := CommonDialect{
|
2024-08-08 21:44:51 +00:00
|
|
|
TableAlias: "t",
|
|
|
|
}
|
2024-08-12 18:21:34 +00:00
|
|
|
result, values := covertFind(ctx, find)
|
|
|
|
fmt.Println(values)
|
|
|
|
if result == `(t.a > ? OR t.b < ?)` {
|
2024-08-08 21:44:51 +00:00
|
|
|
return
|
|
|
|
}
|
2024-08-12 18:21:34 +00:00
|
|
|
t.Errorf(`Expected "(t.b < ? OR t.a > ?)", got %s`, result)
|
2024-08-08 21:44:51 +00:00
|
|
|
}
|