Compare commits

...

5 Commits

Author SHA1 Message Date
Hein
dc3b621380 Fixed test for session id changes 2025-12-09 14:07:00 +02:00
Hein
a4dd2a7086 exposed types FromString 2025-12-09 14:03:55 +02:00
Hein
3ec2e5f15a Proper handling of fromString in the types 2025-12-09 13:55:51 +02:00
Hein
c52afe2825 Updated sql types 2025-12-09 13:14:22 +02:00
Hein
76e98d02c3 Added modelregistry.GetDefaultRegistry 2025-12-09 12:12:10 +02:00
4 changed files with 456 additions and 637 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -9,18 +9,18 @@ import (
"github.com/google/uuid" "github.com/google/uuid"
) )
// TestSqlInt16 tests SqlInt16 type // TestNewSqlInt16 tests NewSqlInt16 type
func TestSqlInt16(t *testing.T) { func TestNewSqlInt16(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
input interface{} input interface{}
expected SqlInt16 expected SqlInt16
}{ }{
{"int", 42, SqlInt16(42)}, {"int", 42, Null(int16(42), true)},
{"int32", int32(100), SqlInt16(100)}, {"int32", int32(100), NewSqlInt16(100)},
{"int64", int64(200), SqlInt16(200)}, {"int64", int64(200), NewSqlInt16(200)},
{"string", "123", SqlInt16(123)}, {"string", "123", NewSqlInt16(123)},
{"nil", nil, SqlInt16(0)}, {"nil", nil, Null(int16(0), false)},
} }
for _, tt := range tests { for _, tt := range tests {
@@ -36,15 +36,15 @@ func TestSqlInt16(t *testing.T) {
} }
} }
func TestSqlInt16_Value(t *testing.T) { func TestNewSqlInt16_Value(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
input SqlInt16 input SqlInt16
expected driver.Value expected driver.Value
}{ }{
{"zero", SqlInt16(0), nil}, {"zero", Null(int16(0), false), nil},
{"positive", SqlInt16(42), int64(42)}, {"positive", NewSqlInt16(42), int16(42)},
{"negative", SqlInt16(-10), int64(-10)}, {"negative", NewSqlInt16(-10), int16(-10)},
} }
for _, tt := range tests { for _, tt := range tests {
@@ -60,8 +60,8 @@ func TestSqlInt16_Value(t *testing.T) {
} }
} }
func TestSqlInt16_JSON(t *testing.T) { func TestNewSqlInt16_JSON(t *testing.T) {
n := SqlInt16(42) n := NewSqlInt16(42)
// Marshal // Marshal
data, err := json.Marshal(n) data, err := json.Marshal(n)
@@ -78,24 +78,24 @@ func TestSqlInt16_JSON(t *testing.T) {
if err := json.Unmarshal([]byte("123"), &n2); err != nil { if err := json.Unmarshal([]byte("123"), &n2); err != nil {
t.Fatalf("Unmarshal failed: %v", err) t.Fatalf("Unmarshal failed: %v", err)
} }
if n2 != 123 { if n2.Int64() != 123 {
t.Errorf("expected 123, got %d", n2) t.Errorf("expected 123, got %d", n2.Int64())
} }
} }
// TestSqlInt64 tests SqlInt64 type // TestNewSqlInt64 tests NewSqlInt64 type
func TestSqlInt64(t *testing.T) { func TestNewSqlInt64(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
input interface{} input interface{}
expected SqlInt64 expected SqlInt64
}{ }{
{"int", 42, SqlInt64(42)}, {"int", 42, NewSqlInt64(42)},
{"int32", int32(100), SqlInt64(100)}, {"int32", int32(100), NewSqlInt64(100)},
{"int64", int64(9223372036854775807), SqlInt64(9223372036854775807)}, {"int64", int64(9223372036854775807), NewSqlInt64(9223372036854775807)},
{"uint32", uint32(100), SqlInt64(100)}, {"uint32", uint32(100), NewSqlInt64(100)},
{"uint64", uint64(200), SqlInt64(200)}, {"uint64", uint64(200), NewSqlInt64(200)},
{"nil", nil, SqlInt64(0)}, {"nil", nil, SqlInt64{}},
} }
for _, tt := range tests { for _, tt := range tests {
@@ -135,8 +135,8 @@ func TestSqlFloat64(t *testing.T) {
if n.Valid != tt.valid { if n.Valid != tt.valid {
t.Errorf("expected valid=%v, got valid=%v", tt.valid, n.Valid) t.Errorf("expected valid=%v, got valid=%v", tt.valid, n.Valid)
} }
if tt.valid && n.Float64 != tt.expected { if tt.valid && n.Float64() != tt.expected {
t.Errorf("expected %v, got %v", tt.expected, n.Float64) t.Errorf("expected %v, got %v", tt.expected, n.Float64())
} }
}) })
} }
@@ -162,7 +162,7 @@ func TestSqlTimeStamp(t *testing.T) {
if err := ts.Scan(tt.input); err != nil { if err := ts.Scan(tt.input); err != nil {
t.Fatalf("Scan failed: %v", err) t.Fatalf("Scan failed: %v", err)
} }
if ts.GetTime().IsZero() { if ts.Time().IsZero() {
t.Error("expected non-zero time") t.Error("expected non-zero time")
} }
}) })
@@ -171,7 +171,7 @@ func TestSqlTimeStamp(t *testing.T) {
func TestSqlTimeStamp_JSON(t *testing.T) { func TestSqlTimeStamp_JSON(t *testing.T) {
now := time.Date(2024, 1, 15, 10, 30, 45, 0, time.UTC) now := time.Date(2024, 1, 15, 10, 30, 45, 0, time.UTC)
ts := SqlTimeStamp(now) ts := NewSqlTimeStamp(now)
// Marshal // Marshal
data, err := json.Marshal(ts) data, err := json.Marshal(ts)
@@ -188,8 +188,8 @@ func TestSqlTimeStamp_JSON(t *testing.T) {
if err := json.Unmarshal([]byte(`"2024-01-15T10:30:45"`), &ts2); err != nil { if err := json.Unmarshal([]byte(`"2024-01-15T10:30:45"`), &ts2); err != nil {
t.Fatalf("Unmarshal failed: %v", err) t.Fatalf("Unmarshal failed: %v", err)
} }
if ts2.GetTime().Year() != 2024 { if ts2.Time().Year() != 2024 {
t.Errorf("expected year 2024, got %d", ts2.GetTime().Year()) t.Errorf("expected year 2024, got %d", ts2.Time().Year())
} }
// Test null // Test null
@@ -226,7 +226,7 @@ func TestSqlDate(t *testing.T) {
} }
func TestSqlDate_JSON(t *testing.T) { func TestSqlDate_JSON(t *testing.T) {
date := SqlDate(time.Date(2024, 1, 15, 0, 0, 0, 0, time.UTC)) date := NewSqlDate(time.Date(2024, 1, 15, 0, 0, 0, 0, time.UTC))
// Marshal // Marshal
data, err := json.Marshal(date) data, err := json.Marshal(date)
@@ -471,8 +471,8 @@ func TestSqlUUID_Scan(t *testing.T) {
if u.Valid != tt.valid { if u.Valid != tt.valid {
t.Errorf("expected valid=%v, got valid=%v", tt.valid, u.Valid) t.Errorf("expected valid=%v, got valid=%v", tt.valid, u.Valid)
} }
if tt.valid && u.String != tt.expected { if tt.valid && u.String() != tt.expected {
t.Errorf("expected %s, got %s", tt.expected, u.String) t.Errorf("expected %s, got %s", tt.expected, u.String())
} }
}) })
} }
@@ -480,13 +480,13 @@ func TestSqlUUID_Scan(t *testing.T) {
func TestSqlUUID_Value(t *testing.T) { func TestSqlUUID_Value(t *testing.T) {
testUUID := uuid.New() testUUID := uuid.New()
u := SqlUUID{String: testUUID.String(), Valid: true} u := NewSqlUUID(testUUID)
val, err := u.Value() val, err := u.Value()
if err != nil { if err != nil {
t.Fatalf("Value failed: %v", err) t.Fatalf("Value failed: %v", err)
} }
if val != testUUID.String() { if val != testUUID {
t.Errorf("expected %s, got %s", testUUID.String(), val) t.Errorf("expected %s, got %s", testUUID.String(), val)
} }
@@ -503,7 +503,7 @@ func TestSqlUUID_Value(t *testing.T) {
func TestSqlUUID_JSON(t *testing.T) { func TestSqlUUID_JSON(t *testing.T) {
testUUID := uuid.New() testUUID := uuid.New()
u := SqlUUID{String: testUUID.String(), Valid: true} u := NewSqlUUID(testUUID)
// Marshal // Marshal
data, err := json.Marshal(u) data, err := json.Marshal(u)
@@ -520,8 +520,8 @@ func TestSqlUUID_JSON(t *testing.T) {
if err := json.Unmarshal([]byte(`"`+testUUID.String()+`"`), &u2); err != nil { if err := json.Unmarshal([]byte(`"`+testUUID.String()+`"`), &u2); err != nil {
t.Fatalf("Unmarshal failed: %v", err) t.Fatalf("Unmarshal failed: %v", err)
} }
if u2.String != testUUID.String() { if u2.String() != testUUID.String() {
t.Errorf("expected %s, got %s", testUUID.String(), u2.String) t.Errorf("expected %s, got %s", testUUID.String(), u2.String())
} }
// Test null // Test null

View File

@@ -784,7 +784,8 @@ func TestReplaceMetaVariables(t *testing.T) {
userCtx := &security.UserContext{ userCtx := &security.UserContext{
UserID: 123, UserID: 123,
UserName: "testuser", UserName: "testuser",
SessionID: "456", SessionID: "ABC456",
SessionRID: 456,
} }
metainfo := map[string]interface{}{ metainfo := map[string]interface{}{
@@ -821,6 +822,12 @@ func TestReplaceMetaVariables(t *testing.T) {
expectedCheck: func(result string) bool { expectedCheck: func(result string) bool {
return strings.Contains(result, "456") return strings.Contains(result, "456")
}, },
}, {
name: "Replace [id_session]",
sqlQuery: "SELECT * FROM sessions WHERE session_id = [id_session]",
expectedCheck: func(result string) bool {
return strings.Contains(result, "ABC456")
},
}, },
} }

View File

@@ -28,6 +28,10 @@ func NewModelRegistry() *DefaultModelRegistry {
} }
} }
func GetDefaultRegistry() *DefaultModelRegistry {
return defaultRegistry
}
func SetDefaultRegistry(registry *DefaultModelRegistry) { func SetDefaultRegistry(registry *DefaultModelRegistry) {
registriesMutex.Lock() registriesMutex.Lock()
defer registriesMutex.Unlock() defer registriesMutex.Unlock()