-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackend.test.js
114 lines (100 loc) · 3.19 KB
/
backend.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
const JSONDATA = require('./data');
const { searchAllArray } = require('./routes/search');
const { inserts } = require('./routes/insert');
const { updates } = require('./routes/update');
const { deletes } = require('./routes/delete');
// mocks
// res in a express.js (data, res) route handler
let server_resp;
const res_mock = {
send: (msg) => server_resp = msg
}
// test to see if we can load our parsed data
test('data loaded', () => {
console.log(`Loaded ${JSONDATA.length} entries.`);
expect(JSONDATA.length).toBeGreaterThan(1);
});
// test to see if our search/filter endpoint works
test('filter', () => {
const query = ['Registration State==NY', 'Violation Code>35'];
const filtered = searchAllArray(query, JSONDATA);
for (const row of filtered) {
expect(row['Registration State']).toBe('NY');
expect(row['Violation Code']).toBeGreaterThan(35);
}
});
test('insert', () => {
const data = {
'Summons Number': 'TEST_SUMMS_NUM',
'Plate ID': 'A',
'Registration State': 'B',
'Issue Date': 'C',
'Violation Time': 'D',
'Violation Code': 1,
'Vehicle Make': 'E',
'Vehicle Body Type': 'F',
'Vehicle Year': 'G',
'Street Name': 'H',
'County County': 'I',
'Violation County': 'J'
};
inserts(data, res_mock);
expect(server_resp).toBe('Data added to database');
expect(JSONDATA.at(-1)).toEqual(data);
// now inserting the same value (duplicate Plate ID) should fail
const oldLength = JSONDATA.length;
inserts(data, res_mock);
expect(server_resp).toBe('Summons Number already exist');
expect(JSONDATA.length).toBe(oldLength);
});
// make sure tests are being run with '--runInBand' or '-i'
// to force them to be sequential (so we insert before doing any modifications)
test('update', () => {
const req = {
params: {
sumNum: 'TEST_SUMMS_NUM',
plateID: 'NEW_PLATE_ID',
regState: 'NEW_REG_STATE',
issDate: 'NEW_ISS_DATE',
vioTime: 'NEW_VIOLATION_TIME',
vioCode: 2,
vehMake: 'NEW_VEH_MAKE',
vehBody: 'NEW_VEH_BODY',
vehYear: 'NEW_VEH_YEAR',
street: 'NEW_STREET',
cCounty: 'NEW_C_COUNTY',
vCounty: 'NEW_V_COUNTY'
}
}
const oldLength = JSONDATA.length;
updates(req, res_mock);
expect(server_resp).toBe('Data has been updated');
expect(JSONDATA.length).toBe(oldLength);
const updatedEntry = JSONDATA.find(x => x['Summons Number'] == req.params.sumNum);
expect(updatedEntry['Plate ID']).toBe('NEW_PLATE_ID');
expect(updatedEntry['Street Name']).toBe('NEW_STREET');
const nonexistant_req = {
params: {
sumNum: 'NON_EXISTANT_ID'
}
};
updates(nonexistant_req, res_mock);
expect(server_resp).toBe('Summons Number does not exist');
expect(JSONDATA.length).toBe(oldLength);
});
test('delete', () => {
const req = {
params: {
sumNum: 'TEST_SUMMS_NUM'
}
};
let oldLength = JSONDATA.length;
deletes(req, res_mock);
expect(server_resp).toBe(`${req.params.sumNum} has been deleted`);
expect(JSONDATA.length).toBe(oldLength - 1);
// duplicate deletes should not do anything
oldLength = JSONDATA.length;
deletes(req, res_mock);
expect(server_resp).toBe(`${req.params.sumNum} does not exist`);
expect(JSONDATA.length).toBe(oldLength);
})