-
Notifications
You must be signed in to change notification settings - Fork 3
/
statement_test.go
84 lines (61 loc) · 2.36 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"))
}
// TODO (manadart 2022-07-15): The tests below are for verification during
// an intermediate stage. They will be subject to deletion shortly.
func TestInterpretFullCoverage(t *testing.T) {
type address struct{}
argTypes, err := typesForStatement([]any{sqlairtesting.Person{}, address{}})
assert.Nil(t, err)
err = interpret(getExpression(), argTypes)
assert.Nil(t, err)
}
func TestInterpretMissingTypesError(t *testing.T) {
argTypes, err := typesForStatement([]any{sqlairtesting.Person{}})
assert.Nil(t, err)
err = interpret(getExpression(), argTypes)
assert.Error(t, err, NewErrTypeInfoNotPresent("address"))
}
func TestInterpretSuperfluousTypesError(t *testing.T) {
type address struct{}
type notUsed struct{}
argTypes, err := typesForStatement([]any{sqlairtesting.Person{}, address{}, notUsed{}})
assert.Nil(t, err)
err = interpret(getExpression(), argTypes)
assert.Error(t, err, NewErrSuperfluousType("notUsed"))
}
func getExpression() parse.Expression {
exp := &parse.SQLExpression{}
tokens := tokensForStatement("&Person.*")
exp.AppendExpression(parse.NewOutputTargetExpression(
tokens[0], parse.NewIdentityExpression(tokens[1]), parse.NewIdentityExpression(tokens[3])))
tokens = tokensForStatement("$address.id")
exp.AppendExpression(parse.NewInputSourceExpression(
tokens[0], parse.NewIdentityExpression(tokens[1]), parse.NewIdentityExpression(tokens[3])))
return exp
}
func tokensForStatement(stmt string) []parse.Token {
lex := parse.NewLexer(stmt)
var tokens []parse.Token
for token := lex.NextToken(); token.Type != parse.EOF; token = lex.NextToken() {
tokens = append(tokens, token)
}
return tokens
}