statement unit tests

This commit is contained in:
Elio Bischof
2023-06-30 10:40:40 +02:00
parent 5cacd0e996
commit 8fac74e7ee
2 changed files with 70 additions and 86 deletions

View File

@@ -304,7 +304,7 @@ func NewTextArrayContainsCond(column string, value string) handler.Condition {
func Not(condition handler.Condition) handler.Condition { func Not(condition handler.Condition) handler.Condition {
return func(param string) (string, interface{}) { return func(param string) (string, interface{}) {
cond, value := condition(param) cond, value := condition(param)
return "NOT ( " + cond + " )", value return "NOT (" + cond + ")", value
} }
} }

View File

@@ -6,6 +6,8 @@ import (
"reflect" "reflect"
"testing" "testing"
"github.com/zitadel/zitadel/internal/database"
"github.com/zitadel/zitadel/internal/eventstore" "github.com/zitadel/zitadel/internal/eventstore"
"github.com/zitadel/zitadel/internal/eventstore/handler" "github.com/zitadel/zitadel/internal/eventstore/handler"
) )
@@ -419,12 +421,7 @@ func TestNewUpdateStatement(t *testing.T) {
Value: "val", Value: "val",
}, },
}, },
conditions: []handler.Condition{ conditions: []handler.Condition{handler.NewCond("col2", 1)},
{
Name: "col2",
Value: 1,
},
},
}, },
want: want{ want: want{
table: "", table: "",
@@ -448,13 +445,8 @@ func TestNewUpdateStatement(t *testing.T) {
sequence: 1, sequence: 1,
previousSequence: 0, previousSequence: 0,
}, },
values: []handler.Column{}, values: []handler.Column{},
conditions: []handler.Condition{ conditions: []handler.Condition{handler.NewCond("col2", 1)},
{
Name: "col2",
Value: 1,
},
},
}, },
want: want{ want: want{
table: "my_table", table: "my_table",
@@ -514,12 +506,7 @@ func TestNewUpdateStatement(t *testing.T) {
Value: "val", Value: "val",
}, },
}, },
conditions: []handler.Condition{ conditions: []handler.Condition{handler.NewCond("col2", 1)},
{
Name: "col2",
Value: 1,
},
},
}, },
want: want{ want: want{
table: "my_table", table: "my_table",
@@ -559,12 +546,7 @@ func TestNewUpdateStatement(t *testing.T) {
Value: "val5", Value: "val5",
}, },
}, },
conditions: []handler.Condition{ conditions: []handler.Condition{handler.NewCond("col2", 1)},
{
Name: "col2",
Value: 1,
},
},
}, },
want: want{ want: want{
table: "my_table", table: "my_table",
@@ -629,12 +611,7 @@ func TestNewDeleteStatement(t *testing.T) {
sequence: 1, sequence: 1,
previousSequence: 0, previousSequence: 0,
}, },
conditions: []handler.Condition{ conditions: []handler.Condition{handler.NewCond("col2", 1)},
{
Name: "col2",
Value: 1,
},
},
}, },
want: want{ want: want{
table: "", table: "",
@@ -682,12 +659,7 @@ func TestNewDeleteStatement(t *testing.T) {
previousSequence: 0, previousSequence: 0,
aggregateType: "agg", aggregateType: "agg",
}, },
conditions: []handler.Condition{ conditions: []handler.Condition{handler.NewCond("col1", 1)},
{
Name: "col1",
Value: 1,
},
},
}, },
want: want{ want: want{
table: "my_table", table: "my_table",
@@ -841,12 +813,8 @@ func TestNewMultiStatement(t *testing.T) {
}, },
execs: []func(eventstore.Event) Exec{ execs: []func(eventstore.Event) Exec{
AddDeleteStatement( AddDeleteStatement(
[]handler.Condition{ []handler.Condition{handler.NewCond("col1", 1)},
{ ),
Name: "col1",
Value: 1,
},
}),
AddCreateStatement( AddCreateStatement(
[]handler.Column{ []handler.Column{
{ {
@@ -875,12 +843,8 @@ func TestNewMultiStatement(t *testing.T) {
Value: 1, Value: 1,
}, },
}, },
[]handler.Condition{ []handler.Condition{handler.NewCond("col1", 1)},
{ ),
Name: "col1",
Value: 1,
},
}),
}, },
}, },
want: want{ want: want{
@@ -942,7 +906,7 @@ func TestNewCopyStatement(t *testing.T) {
conflictingCols []handler.Column conflictingCols []handler.Column
from []handler.Column from []handler.Column
to []handler.Column to []handler.Column
conds []handler.Condition conds []handler.Column
} }
type want struct { type want struct {
aggregateType eventstore.AggregateType aggregateType eventstore.AggregateType
@@ -966,12 +930,10 @@ func TestNewCopyStatement(t *testing.T) {
sequence: 1, sequence: 1,
previousSequence: 0, previousSequence: 0,
}, },
conds: []handler.Condition{ conds: []handler.Column{{
{ Name: "col1",
Name: "col2", Value: 1,
Value: 1, }},
},
},
}, },
want: want{ want: want{
table: "", table: "",
@@ -995,7 +957,7 @@ func TestNewCopyStatement(t *testing.T) {
sequence: 1, sequence: 1,
previousSequence: 0, previousSequence: 0,
}, },
conds: []handler.Condition{}, conds: []handler.Column{},
from: []handler.Column{ from: []handler.Column{
{ {
Name: "col", Name: "col",
@@ -1029,7 +991,7 @@ func TestNewCopyStatement(t *testing.T) {
sequence: 1, sequence: 1,
previousSequence: 0, previousSequence: 0,
}, },
conds: []handler.Condition{}, conds: []handler.Column{},
from: []handler.Column{ from: []handler.Column{
{ {
Name: "col", Name: "col",
@@ -1066,11 +1028,9 @@ func TestNewCopyStatement(t *testing.T) {
sequence: 1, sequence: 1,
previousSequence: 0, previousSequence: 0,
}, },
conds: []handler.Condition{ conds: []handler.Column{{
{ Name: "col1",
Name: "col", }},
},
},
from: []handler.Column{}, from: []handler.Column{},
}, },
want: want{ want: want{
@@ -1124,7 +1084,7 @@ func TestNewCopyStatement(t *testing.T) {
Name: "col_b", Name: "col_b",
}, },
}, },
conds: []handler.Condition{ conds: []handler.Column{
{ {
Name: "id", Name: "id",
Value: 2, Value: 2,
@@ -1191,7 +1151,7 @@ func TestNewCopyStatement(t *testing.T) {
Name: "col_d", Name: "col_d",
}, },
}, },
conds: []handler.Condition{ conds: []handler.Column{
{ {
Name: "id", Name: "id",
Value: 2, Value: 2,
@@ -1420,12 +1380,7 @@ func Test_columnsToWhere(t *testing.T) {
{ {
name: "no offset", name: "no offset",
args: args{ args: args{
conds: []handler.Condition{ conds: []handler.Condition{handler.NewCond("col1", "val1")},
{
Name: "col1",
Value: "val1",
},
},
paramOffset: 0, paramOffset: 0,
}, },
want: want{ want: want{
@@ -1437,14 +1392,8 @@ func Test_columnsToWhere(t *testing.T) {
name: "multiple cols", name: "multiple cols",
args: args{ args: args{
conds: []handler.Condition{ conds: []handler.Condition{
{ handler.NewCond("col1", "val1"),
Name: "col1", handler.NewCond("col2", "val2"),
Value: "val1",
},
{
Name: "col2",
Value: "val2",
},
}, },
paramOffset: 0, paramOffset: 0,
}, },
@@ -1456,12 +1405,7 @@ func Test_columnsToWhere(t *testing.T) {
{ {
name: "2 offset", name: "2 offset",
args: args{ args: args{
conds: []handler.Condition{ conds: []handler.Condition{handler.NewCond("col1", "val1")},
{
Name: "col1",
Value: "val1",
},
},
paramOffset: 2, paramOffset: 2,
}, },
want: want{ want: want{
@@ -1469,6 +1413,46 @@ func Test_columnsToWhere(t *testing.T) {
values: []interface{}{"val1"}, values: []interface{}{"val1"},
}, },
}, },
{
name: "less than",
args: args{
conds: []handler.Condition{NewLessThanCond("col1", "val1")},
},
want: want{
wheres: []string{"(col1 < $1)"},
values: []interface{}{"val1"},
},
},
{
name: "is null",
args: args{
conds: []handler.Condition{NewIsNullCond("col1")},
},
want: want{
wheres: []string{"(col1 IS NULL)"},
values: []interface{}{},
},
},
{
name: "text array contains",
args: args{
conds: []handler.Condition{NewTextArrayContainsCond("col1", "val1")},
},
want: want{
wheres: []string{"(col1 @> $1)"},
values: []interface{}{database.StringArray{"val1"}},
},
},
{
name: "not",
args: args{
conds: []handler.Condition{Not(handler.NewCond("col1", "val1"))},
},
want: want{
wheres: []string{"(NOT (col1 = $1))"},
values: []interface{}{"val1"},
},
},
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {