-
Notifications
You must be signed in to change notification settings - Fork 0
/
types_test.js
202 lines (172 loc) · 7 KB
/
types_test.js
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
const unitJS = require('unit.js')
const { sha256 } = require('js-sha256')
const { v4 } = require('uuid')
const { ErrArguments } = require('./errors')
const { DateTypes, MetaData, ExtraPayload } = require('./types')
describe('Types Tests', () => {
it('DateTypes', () => {
unitJS.assert.equal(DateTypes.MILLI, 0)
unitJS.assert.equal(DateTypes.NANO, 1)
})
it('MetaData is valid arguments', () => {
const createdAt = new Date(Date.now()),
metaData = new MetaData(true,
sha256('sumSHA256'),
123,
createdAt.getSeconds(),
'permission')
unitJS.assert.equal(true, metaData.IsDir)
unitJS.assert.equal(metaData.Sum, sha256('sumSHA256'))
unitJS.assert.equal(metaData.Size, 123)
unitJS.assert.equal(metaData.CreatedAt, createdAt.getSeconds())
unitJS.assert.equal(metaData.Permission, 'permission')
})
it('MetaData .ToJSON()', () => {
const createdAt = new Date(Date.now()),
metaData = new MetaData(true,
sha256('sumSHA256'),
123,
createdAt.getSeconds(),
'permission'),
jsonString = `{"is_dir":true,"sum":"${sha256('sumSHA256')}","size":123,"created_at":${createdAt.getSeconds()},"permission":"permission"}`
unitJS.assert.equal(jsonString, metaData.ToJSON())
})
it('MetaData .FromJSON()', () => {
const createdAt = new Date(Date.now()),
jsonString = `{"is_dir":true,"sum":"${sha256('sumSHA256-2')}","size":12345,"created_at":${createdAt.getSeconds()},"permission":"permission2"}`,
{ metaData, error } = new MetaData().FromJSON(jsonString)
unitJS.value(error).isNull()
unitJS.assert.equal(true, metaData.IsDir)
unitJS.assert.equal(sha256('sumSHA256-2'), metaData.Sum)
unitJS.assert.equal(12345, metaData.Size)
unitJS.assert.equal(createdAt.getSeconds(), metaData.CreatedAt)
unitJS.assert.equal('permission2', metaData.Permission)
})
it('Should be SyntaxError MetaData .FromJSON()', () => {
const createdAt = new Date(Date.now()),
jsonString = `{"is_dir":true"size":12345,"created_at":${createdAt.getSeconds()},"permission":"permission2"}`,
{ metaData, error } = new MetaData().FromJSON(jsonString)
unitJS.value(metaData).isNull()
unitJS.bool(error instanceof SyntaxError).isTrue()
})
it('Should be ErrArguments MetaData .FromJSON() if size is negative', () => {
const createdAt = new Date(Date.now()),
jsonString = `{"is_dir":true,"sum":"${sha256('sumSHA256-2')}","size":-1,"created_at":${createdAt.getSeconds()},"permission":"permission2"}`,
{ metaData, error } = new MetaData().FromJSON(jsonString)
unitJS.value(metaData).isNull()
unitJS.assert.equal(ErrArguments, error)
})
it('Should be ErrArguments MetaData .FromJSON() if created_at is negative', () => {
const jsonString = `{"is_dir":true,"sum":"${sha256('sumSHA256-2')}","size":123,"created_at":-1,"permission":"permission2"}`,
{ metaData, error } = new MetaData().FromJSON(jsonString)
unitJS.value(metaData).isNull()
unitJS.assert.equal(ErrArguments, error)
})
it('Should be ErrArguments MetaData is invalid size', () => {
const createdAt = new Date(Date.now())
unitJS.exception(() => {
new MetaData(true,
sha256('sumSHA256'),
-1,
createdAt.getSeconds(),
'permission')
})
.is(ErrArguments)
})
it('Should be ErrArguments MetaData is invalid createdAt', () => {
unitJS.exception(() => {
new MetaData(true,
sha256('sumSHA256'),
123,
-1,
'permission')
})
.is(ErrArguments)
})
it('ExtraPayload is valid arguments', () => {
const uid = v4(),
createdAt = new Date(Date.now()),
extraPayload = new ExtraPayload(uid,
true,
sha256('sumSHA256'),
123,
createdAt.getSeconds(),
'permission')
unitJS.assert.equal(uid, extraPayload.UUID)
unitJS.assert.equal(true, extraPayload.IsDir)
unitJS.assert.equal(extraPayload.Sum, sha256('sumSHA256'))
unitJS.assert.equal(extraPayload.Size, 123)
unitJS.assert.equal(extraPayload.CreatedAt, createdAt.getSeconds())
unitJS.assert.equal(extraPayload.Permission, 'permission')
})
it('ExtraPayload .ToJSON()', () => {
const uid = v4(),
createdAt = new Date(Date.now()),
extraPayload = new ExtraPayload(uid,
true,
sha256('sumSHA256'),
123,
createdAt.getSeconds(),
'permission'),
jsonString = `{"UUID":"${uid}","IsDir":true,"Sum":"${sha256('sumSHA256')}","Size":123,"CreatedAt":${createdAt.getSeconds()},"Permission":"permission"}`
unitJS.assert.equal(jsonString, extraPayload.ToJSON())
})
it('ExtraPayload .FromJSON()', () => {
const uid = v4(),
createdAt = new Date(Date.now()),
jsonString = `{"UUID":"${uid}","IsDir":true,"Sum":"${sha256('sumSHA256-2')}","Size":12345,"CreatedAt":${createdAt.getSeconds()},"Permission":"permission2"}`,
{ extraPayload, error } = new ExtraPayload().FromJSON(jsonString)
unitJS.value(error).isNull()
unitJS.assert.equal(uid, extraPayload.UUID)
unitJS.assert.equal(true, extraPayload.IsDir)
unitJS.assert.equal(sha256('sumSHA256-2'), extraPayload.Sum)
unitJS.assert.equal(12345, extraPayload.Size)
unitJS.assert.equal(createdAt.getSeconds(), extraPayload.CreatedAt)
unitJS.assert.equal('permission2', extraPayload.Permission)
})
it('Should be SyntaxError ExtraPayload .FromJSON()', () => {
const jsonString = `"invalid`,
{ extraPayload, error } = new ExtraPayload().FromJSON(jsonString)
unitJS.value(extraPayload).isNull()
unitJS.bool(error instanceof SyntaxError).isTrue()
})
it('Should be ErrArguments ExtraPayload .FromJSON() if size is negative', () => {
const uid = v4(),
createdAt = new Date(Date.now()),
jsonString = `{"UUID":"${uid}","IsDir":true,"Sum":"${sha256('sumSHA256')}","Size":-1,"CreatedAt":${createdAt.getSeconds()},"Permission":"permission2"}`,
{ extraPayload, error } = new ExtraPayload().FromJSON(jsonString)
unitJS.value(extraPayload).isNull()
unitJS.assert.equal(ErrArguments, error)
})
it('Should be ErrArguments ExtraPayload .FromJSON() if created_at is negative', () => {
const uid = v4(),
jsonString = `{"UUID":"${uid}","IsDir":true,"Sum":"${sha256('sumSHA256')}","Size":123,"CreatedAt":-1,"Permission":"permission"}`,
{ extraPayload, error } = new ExtraPayload().FromJSON(jsonString)
unitJS.value(extraPayload).isNull()
unitJS.assert.equal(ErrArguments, error)
})
it('Should be ErrArguments ExtraPayload is invalid size', () => {
const uid = v4(),
createdAt = new Date(Date.now())
unitJS.exception(() => {
new ExtraPayload(uid,
true,
sha256('sumSHA256'),
-1,
createdAt.getSeconds(),
'permission')
})
.is(ErrArguments)
})
it('Should be ErrArguments ExtraPayload is invalid createdAt', () => {
unitJS.exception(() => {
new ExtraPayload(v4(),
true,
sha256('sumSHA256'),
123,
-1,
'permission')
})
.is(ErrArguments)
})
})