-
Notifications
You must be signed in to change notification settings - Fork 5
/
query_context_test.go
62 lines (47 loc) · 1.75 KB
/
query_context_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
package tormenta_test
import (
"testing"
"github.com/jpincas/tormenta"
"github.com/jpincas/tormenta/testtypes"
)
func Test_Context(t *testing.T) {
db, _ := tormenta.OpenTestWithOptions("data/tests", testDBOptions)
defer db.Close()
entity := testtypes.FullStruct{}
db.Save(&entity)
sessionID := "session1234"
db.First(&entity).SetContext("sessionid", sessionID).Run()
if entity.TriggerString != sessionID {
t.Errorf("Context was not set correctly. Expecting: %s; Got: %s", sessionID, entity.TriggerString)
}
}
// Essentially the same test as above but on an indexed match query, this failed previously because an indexed
// search used the Public 'query.Get' function which did not take a context as a parameter and therefore simply
// passes the empty context to the PostGet trigger.
func Test_Context_Match(t *testing.T) {
db, _ := tormenta.OpenTestWithOptions("data/tests", testDBOptions)
defer db.Close()
entity := testtypes.FullStruct{}
entity.IntField = 42
db.Save(&entity)
sessionID := "session1234"
db.First(&entity).SetContext("sessionid", sessionID).Match("IntField", 42).Run()
if entity.TriggerString != sessionID {
t.Errorf("Context was not set correctly. Expecting: %s; Got: %s", sessionID, entity.TriggerString)
}
}
func Test_Context_Get(t *testing.T) {
db, _ := tormenta.OpenTestWithOptions("data/tests", testDBOptions)
defer db.Close()
savedEntity := testtypes.FullStruct{}
db.Save(&savedEntity)
entity := testtypes.FullStruct{}
entity.ID = savedEntity.ID
sessionID := "session1234"
ctx := make(map[string]interface{})
ctx["sessionid"] = sessionID
db.GetWithContext(&entity, ctx)
if entity.TriggerString != sessionID {
t.Errorf("Context was not set correctly. Expecting: %s; Got: %s", sessionID, entity.TriggerString)
}
}