forked from canonical/sqlair-prototype
-
Notifications
You must be signed in to change notification settings - Fork 0
/
statement_test.go
84 lines (68 loc) · 2.04 KB
/
statement_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
package sqlair
import (
"testing"
"github.com/canonical/sqlair/internal/parse"
sqlairtesting "github.com/canonical/sqlair/internal/testing"
"github.com/stretchr/testify/assert"
)
func TestTypesForStatementUniqueNames(t *testing.T) {
type DifferentPerson struct{}
argTypes, err := typesForStatement([]any{DifferentPerson{}, sqlairtesting.Person{}})
assert.Nil(t, err)
assert.Len(t, argTypes, 2)
assert.Equal(t, argTypes["DifferentPerson"].Name(), "DifferentPerson")
assert.Equal(t, argTypes["Person"].Name(), "Person")
}
func TestTypesForStatementDuplicateNamesError(t *testing.T) {
type Person struct{}
_, err := typesForStatement([]any{Person{}, sqlairtesting.Person{}})
assert.Error(t, err, NewErrTypeNameNotUnique("Person"))
}
func TestValidateExpressionTypesFullCoverage(t *testing.T) {
type address struct{}
argTypes, err := typesForStatement([]any{sqlairtesting.Person{}, address{}})
assert.Nil(t, err)
err = validateExpressionTypes(getExpression(), argTypes)
assert.Nil(t, err)
}
func TestValidateExpressionTypesMissingTypesError(t *testing.T) {
argTypes, err := typesForStatement([]any{sqlairtesting.Person{}})
assert.Nil(t, err)
err = validateExpressionTypes(getExpression(), argTypes)
assert.Error(t, err, NewErrTypeInfoNotPresent("address"))
}
func TestValidateExpressionTypesSuperfluousTypesError(t *testing.T) {
type address struct{}
type notUsed struct{}
argTypes, err := typesForStatement([]any{sqlairtesting.Person{}, address{}, notUsed{}})
assert.Nil(t, err)
err = validateExpressionTypes(getExpression(), argTypes)
assert.Error(t, err, NewErrSuperfluousType("notUsed"))
}
func getExpression() parse.Expression {
return &sqlairtesting.SimpleExpression{
T: parse.SQL,
E: []*sqlairtesting.SimpleExpression{
{
T: parse.OutputTarget,
E: []*sqlairtesting.SimpleExpression{
{},
{
T: parse.Identity,
S: "Person",
},
},
},
{
T: parse.InputSource,
E: []*sqlairtesting.SimpleExpression{
{},
{
T: parse.Identity,
S: "address",
},
},
},
},
}
}