-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsafequery_test.go
117 lines (100 loc) · 3.07 KB
/
safequery_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package sqld
import (
"context"
"fmt"
"testing"
)
type TestParams struct {
ID int `db:"id" json:"id"`
Name string `db:"name" json:"name"`
}
func (TestParams) TableName() string {
return "test_params"
}
type TestResult struct {
ID int `db:"id" json:"id"`
Name string `db:"name" json:"name"`
}
func (TestResult) TableName() string {
return "test_results"
}
func TestExecuteRawWithRegistry(t *testing.T) {
// Register both param and result types
if err := Register[TestParams](); err != nil {
t.Fatalf("Failed to register TestParams: %v", err)
}
if err := Register[TestResult](); err != nil {
t.Fatalf("Failed to register TestResult: %v", err)
}
// Create a request
req := ExecuteRawRequest{
Query: "SELECT id, name FROM test WHERE id = {{id}} AND name = {{name}}",
Params: map[string]interface{}{
"id": 1,
"name": "test",
},
SelectFields: []string{"id", "name"},
}
// Mock DB - we won't actually execute the query
var mockDB *MockDB
// Execute the query - it should use registry
_, err := ExecuteRaw[TestParams, TestResult](context.Background(), mockDB, req)
if err != nil {
// We expect an error since we're using a nil DB, but it should be a DB error
// not a metadata or validation error
if err.Error() != "unsupported database type: *sqld.MockDB" {
t.Errorf("Expected DB error, got: %v", err)
}
}
}
func TestExecuteRawRegistryForParamsAndResult(t *testing.T) {
// Clear the registry first to ensure clean state
defaultRegistry = NewRegistry()
// Register both param and result types
if err := Register[TestParams](); err != nil {
t.Fatalf("Failed to register TestParams: %v", err)
}
if err := Register[TestResult](); err != nil {
t.Fatalf("Failed to register TestResult: %v", err)
}
// Create multiple requests to verify metadata reuse
requests := []ExecuteRawRequest{
{
Query: "SELECT id, name FROM test WHERE id = {{id}}",
Params: map[string]interface{}{
"id": 1,
},
SelectFields: []string{"id", "name"},
},
{
Query: "SELECT id, name FROM test WHERE name = {{name}}",
Params: map[string]interface{}{
"name": "test",
},
SelectFields: []string{"id", "name"},
},
}
// Mock DB - we won't actually execute the query
var mockDB *MockDB
// Execute the queries multiple times to verify metadata reuse
for i, req := range requests {
t.Run(fmt.Sprintf("Request_%d", i), func(t *testing.T) {
_, err := ExecuteRaw[TestParams, TestResult](context.Background(), mockDB, req)
if err == nil {
t.Error("Expected error due to mock DB, got nil")
}
// We expect a specific error since we're using a nil DB
if err.Error() != "unsupported database type: *sqld.MockDB" {
t.Errorf("Expected DB error, got: %v", err)
}
})
}
// Verify that metadata exists in registry for both types
if _, err := defaultRegistry.GetModelMetadata(TestParams{}); err != nil {
t.Errorf("TestParams metadata not found in registry: %v", err)
}
if _, err := defaultRegistry.GetModelMetadata(TestResult{}); err != nil {
t.Errorf("TestResult metadata not found in registry: %v", err)
}
}
type MockDB struct{}