-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.spec.ts
185 lines (167 loc) · 5.97 KB
/
model.spec.ts
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
import {CookbookModel} from "./fixtures/models";
import {DynamoDBClient} from "@aws-sdk/client-dynamodb";
import Model from "../src/model";
const config = {endpoint: 'http://localhost:8000'};
const client = new DynamoDBClient(config);
describe('model', () => {
let model: any;
beforeEach (() => {
model = new CookbookModel(client);
});
it ('should set attribute with setter', () => {
model.title = 'Southern Cooking'
model.image = ['http://pathtoimage.com'];
model.reviews = 5;
expect(model.title).toEqual('Southern Cooking');
});
describe('validation', () => {
let fillData;
beforeEach(() => {
fillData = {
title: 'Southern Cooking',
description: 'Another southern cookbook with gravy all over everything',
image: ['http://pathtoimage.com/logo.png'],
author: 'Joshua D. Williams',
reviews: 5,
ratings: [3,8,6,9,8,3,7,5]
}
})
describe('Primary Key Validation', () => {
it('should validate all attributes', () => {
model.fill(fillData);
const result = model.validate();
expect(result.valid).toBe(true);
})
it('should fail if partition key is not set', () => {
fillData.title = undefined;
model.fill(fillData);
const result = model.validate();
expect(result.valid).toBe(false);
expect(result.errors).toHaveLength(1);
expect(result.errors[0]).toContain('title');
})
it('should fail if sort key is not set', () => {
fillData.author = undefined;
model.fill(fillData)
const result = model.validate();
expect(result.valid).toBe(false);
expect(result.errors).toHaveLength(1);
expect(result.errors[0]).toContain('author');
})
it('should fail if required field is not set', () => {
fillData.image = undefined;
model.fill(fillData);
const result = model.validate();
expect(result.valid).toBe(false);
expect(result.errors).toHaveLength(1);
expect(result.errors[0]).toContain('image')
})
})
describe('Data Type Validation', () => {
it('should fail if type is not string', () => {
fillData.title = 12345;
model.fill(fillData);
const {valid, errors} = model.validate();
expect(valid).toBe(false);
expect(errors).toHaveLength(1);
expect(errors[0]).toContain('title');
})
it('should fail if type is not string set', () => {
fillData.image = 'http://pathtoimage.com/logo.png';
model.fill(fillData)
const {valid, errors} = model.validate();
expect(valid).toBe(false);
expect(errors).toHaveLength(1);
expect(errors[0]).toContain('image');
})
it('should fail if type is not number', () => {
fillData.reviews = '5'
model.fill(fillData);
const {valid, errors} = model.validate();
expect(valid).toBe(false);
expect(errors).toHaveLength(1);
expect(errors[0]).toContain('number');
})
it('should fail if type is not number set', () => {
fillData.ratings = [10, 8, 5, 9, 7, '7']
model.fill(fillData);
const {valid, errors} = model.validate();
expect(valid).toBe(false);
expect(errors).toHaveLength(1);
expect(errors[0]).toContain('number');
})
it('should fail if list type contains non-primitive values', () => {
fillData.comments = ['loved it', {}, 3, 'very good', 6, 7, 'it sucked'];
model.fill(fillData);
const result = model.validate();
expect(result.valid).toBe(false);
expect(result.errors).toHaveLength(1);
expect(result.errors[0]).toContain('comments')
})
})
describe('setter validation', () => {
it ('should fail string validation', () => {
const expectedFailure = () => model.title = [];
expect(expectedFailure).toThrow(TypeError)
});
it ('should fail string set validation', () => {
const expectedFailure = () => model.image = 'http://pathtoimage.com';
expect(expectedFailure).toThrow(TypeError)
});
it ('should fail number validation', () => {
const expectedFailure = () => model.reviews = '70';
expect(expectedFailure).toThrow(TypeError)
});
it ('should fail number set validation', () => {
const expectedFailure = () => model.reviews = ['70'];
expect(expectedFailure).toThrow(TypeError)
})
it('should fail map validation', () => {
const expectedFailure = () => model.reviews = ['70'];
expect(expectedFailure).toThrow(TypeError)
})
})
})
describe('save', () => {
it('should save', async () => {
model.fill({
title: "Southern Savories",
author: '[email protected]',
image: ['logo.png']
});
const result = await model.save();
expect(result).toHaveProperty('$metadata.httpStatusCode', 200);
})
})
describe('find', () => {
it('should get item by primary key', async () => {
const result = await model.find('Southern Savories','[email protected]');
expect(result).toBeInstanceOf(Model)
expect(result.title).toEqual('Southern Savories')
expect(result.author).toEqual('[email protected]')
})
})
describe('delete', () => {
it('should delete item by primary key', async () => {
await model.fill({
title: 'Southern Savories',
author: '[email protected]',
image: ['image.png']
}).save();
const result = await model.delete('Southern Savories','[email protected]');
expect(result).toBe(true);
})
})
describe('update', () => {
it('should update item', async () => {
model.fill({
title: 'Southern Crunch',
author: '[email protected]',
description: 'Another Cookbook',
image: ["logo.png", "logo2.png"]
})
const updateModel = async () => await model.update();
expect(updateModel).not.toThrow()
})
})
})