-
Notifications
You must be signed in to change notification settings - Fork 0
/
zone_test.go
executable file
·154 lines (120 loc) · 4.3 KB
/
zone_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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package campid
import (
"context"
"testing"
"time"
"github.com/influx6/npkg/nstorage/nmap"
"github.com/influx6/npkg/nxid"
"github.com/stretchr/testify/require"
)
func TestZoneStore(t *testing.T) {
var store = nmap.NewExprByteStore(100)
var sessionStore = NewZoneStore(&JsonZoneCodec{}, store)
var sz Zone
sz.UserId = "1"
sz.CsrfMessage = "m"
sz.Updated = time.Now()
sz.Created = time.Now()
sz.Method = "web-client"
sz.Id = nxid.New().String()
var sz2 Zone
sz2.UserId = "1"
sz2.CsrfMessage = "m"
sz2.Updated = time.Now()
sz2.Created = time.Now()
sz2.Method = "mobile-client"
sz2.Id = nxid.New().String()
var ctx = context.Background()
t.Run("Save", func(t *testing.T) {
store.Clear()
require.NoError(t, sessionStore.Save(ctx, &sz))
var hasRecord, hasRecordErr = sessionStore.Has(ctx, sz.Id, sz.UserId)
require.NoError(t, hasRecordErr)
require.True(t, hasRecord)
})
t.Run("Update", func(t *testing.T) {
store.Clear()
require.NoError(t, sessionStore.Save(ctx, &sz))
var hasRecord, hasRecordErr = sessionStore.Has(ctx, sz.Id, sz.UserId)
require.NoError(t, hasRecordErr)
require.True(t, hasRecord)
sz.Method = "mobile-client"
require.NoError(t, sessionStore.Update(ctx, &sz))
var record, retreivedErr = sessionStore.GetByZoneAndUserId(ctx, sz.Id, sz.UserId)
require.NoError(t, retreivedErr)
require.Equal(t, record.UserId, sz.UserId)
require.Equal(t, "mobile-client", record.Method)
})
t.Run("GetAll", func(t *testing.T) {
store.Clear()
require.NoError(t, sessionStore.Save(ctx, &sz))
var hasRecord, hasRecordErr = sessionStore.Has(ctx, sz.Id, sz.UserId)
require.NoError(t, hasRecordErr)
require.True(t, hasRecord)
var recordList, getListErr = sessionStore.GetAll(ctx)
require.NoError(t, getListErr)
require.Len(t, recordList, 1)
})
t.Run("GetAllForUser", func(t *testing.T) {
store.Clear()
require.NoError(t, sessionStore.Save(ctx, &sz))
require.NoError(t, sessionStore.Save(ctx, &sz2))
var hasRecord, hasRecordErr = sessionStore.Has(ctx, sz.Id, sz.UserId)
require.NoError(t, hasRecordErr)
require.True(t, hasRecord)
var recordList, getListErr = sessionStore.GetAllForUser(ctx, sz.UserId)
require.NoError(t, getListErr)
require.Len(t, recordList, 2)
})
t.Run("GetForUserId", func(t *testing.T) {
store.Clear()
require.NoError(t, sessionStore.Save(ctx, &sz))
var hasRecord, hasRecordErr = sessionStore.Has(ctx, sz.Id, sz.UserId)
require.NoError(t, hasRecordErr)
require.True(t, hasRecord)
var record, getListErr = sessionStore.GetForUserId(ctx, sz.UserId)
require.NoError(t, getListErr)
require.NotNil(t, record)
require.Equal(t, record.UserId, sz.UserId)
})
t.Run("remove", func(t *testing.T) {
store.Clear()
require.NoError(t, sessionStore.Save(ctx, &sz))
require.NoError(t, sessionStore.Save(ctx, &sz2))
var hasRecord, hasRecordErr = sessionStore.Has(ctx, sz.Id, sz.UserId)
require.NoError(t, hasRecordErr)
require.True(t, hasRecord)
var hasRecord2, hasRecordErr2 = sessionStore.Has(ctx, sz2.Id, sz2.UserId)
require.NoError(t, hasRecordErr2)
require.True(t, hasRecord2)
var record, getListErr = sessionStore.Remove(ctx, sz.Id, sz.UserId)
require.NoError(t, getListErr)
require.NotNil(t, record)
require.Equal(t, record.Id, sz.Id)
hasRecord, hasRecordErr = sessionStore.Has(ctx, sz.Id, sz.UserId)
require.NoError(t, hasRecordErr)
require.False(t, hasRecord)
hasRecord2, hasRecordErr2 = sessionStore.Has(ctx, sz2.Id, sz2.UserId)
require.NoError(t, hasRecordErr2)
require.True(t, hasRecord2)
})
t.Run("RemoveAllForUser", func(t *testing.T) {
store.Clear()
require.NoError(t, sessionStore.Save(ctx, &sz))
require.NoError(t, sessionStore.Save(ctx, &sz2))
var hasRecord, hasRecordErr = sessionStore.Has(ctx, sz.Id, sz.UserId)
require.NoError(t, hasRecordErr)
require.True(t, hasRecord)
var hasRecord2, hasRecordErr2 = sessionStore.Has(ctx, sz2.Id, sz2.UserId)
require.NoError(t, hasRecordErr2)
require.True(t, hasRecord2)
var getListErr = sessionStore.RemoveAllForUser(ctx, sz.UserId)
require.NoError(t, getListErr)
hasRecord, hasRecordErr = sessionStore.Has(ctx, sz.Id, sz.UserId)
require.NoError(t, hasRecordErr)
require.False(t, hasRecord)
hasRecord2, hasRecordErr2 = sessionStore.Has(ctx, sz2.Id, sz2.UserId)
require.NoError(t, hasRecordErr2)
require.False(t, hasRecord2)
})
}