Skip to content

Commit 973b24b

Browse files
authored
Merge pull request #121 from switcherapi/staging
Improved checkSnapshot code readability
2 parents f30cce5 + 301871f commit 973b24b

13 files changed

+658
-608
lines changed

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@
3030
"node-fetch": "^2.6.7"
3131
},
3232
"devDependencies": {
33-
"@typescript-eslint/eslint-plugin": "^5.31.0",
34-
"@typescript-eslint/parser": "^5.31.0",
33+
"@typescript-eslint/eslint-plugin": "^5.32.0",
34+
"@typescript-eslint/parser": "^5.32.0",
3535
"babel-eslint": "^10.1.0",
3636
"chai": "^4.3.6",
3737
"chai-as-promised": "^7.1.1",
38-
"eslint": "^8.20.0",
38+
"eslint": "^8.21.0",
3939
"mocha": "^10.0.0",
4040
"mocha-sonarqube-reporter": "^1.0.2",
4141
"nyc": "^15.1.0",

src/index.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ declare namespace SwitcherClient {
2929

3030
/**
3131
* Verifies if the current snapshot file is updated.
32-
* Return true if an update has been made.
32+
*
33+
* Return true when an update has been executed.
3334
*/
3435
static checkSnapshot(): Promise<boolean>;
3536

src/index.js

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -83,27 +83,30 @@ class Switcher {
8383
}
8484

8585
static async checkSnapshot() {
86-
if (Switcher.snapshot) {
87-
if (!Switcher.context.exp || Date.now() > (Switcher.context.exp*1000)) {
88-
await Switcher._auth();
89-
90-
const result = await validateSnapshot(Switcher.context, Switcher.options.snapshotLocation,
91-
Switcher.snapshot.data.domain.version);
92-
93-
if (result) {
94-
Switcher.loadSnapshot();
95-
return true;
96-
}
97-
}
86+
if (!Switcher.snapshot)
9887
return false;
88+
89+
if (!Switcher.context.exp || Date.now() > (Switcher.context.exp*1000))
90+
await Switcher._auth();
91+
92+
const result = await validateSnapshot(
93+
Switcher.context,
94+
Switcher.options.snapshotLocation,
95+
Switcher.snapshot.data.domain.version
96+
);
97+
98+
if (result) {
99+
Switcher.loadSnapshot();
100+
return true;
99101
}
102+
103+
return false;
100104
}
101105

102106
static async loadSnapshot(watchSnapshot) {
103107
Switcher.snapshot = loadDomain(Switcher.options.snapshotLocation, Switcher.context.environment);
104-
if (Switcher.snapshot.data.domain.version == 0 && !Switcher.options.offline) {
108+
if (Switcher.snapshot.data.domain.version == 0 && !Switcher.options.offline)
105109
await Switcher.checkSnapshot();
106-
}
107110

108111
if (watchSnapshot)
109112
Switcher.watchSnapshot();
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
const assert = require('chai').assert;
2+
3+
const {
4+
processOperation,
5+
StrategiesType,
6+
OperationsType
7+
} = require('../src/lib/snapshot');
8+
9+
describe('Processing strategy: DATE', () => {
10+
const mock_values1 = [
11+
'2019-12-01'
12+
];
13+
14+
const mock_values2 = [
15+
'2019-12-01', '2019-12-05'
16+
];
17+
18+
const mock_values3 = [
19+
'2019-12-01T08:30'
20+
];
21+
22+
it('should agree when input is LOWER', () => {
23+
const result = processOperation(
24+
StrategiesType.DATE, OperationsType.LOWER, '2019-11-26', mock_values1);
25+
assert.isTrue(result);
26+
});
27+
28+
it('should agree when input is LOWER or SAME', () => {
29+
const result = processOperation(
30+
StrategiesType.DATE, OperationsType.LOWER, '2019-12-01', mock_values1);
31+
assert.isTrue(result);
32+
});
33+
34+
it('should NOT agree when input is NOT LOWER', () => {
35+
const result = processOperation(
36+
StrategiesType.DATE, OperationsType.LOWER, '2019-12-02', mock_values1);
37+
assert.isFalse(result);
38+
});
39+
40+
it('should agree when input is GREATER', () => {
41+
const result = processOperation(
42+
StrategiesType.DATE, OperationsType.GREATER, '2019-12-02', mock_values1);
43+
assert.isTrue(result);
44+
});
45+
46+
it('should agree when input is GREATER or SAME', () => {
47+
const result = processOperation(
48+
StrategiesType.DATE, OperationsType.GREATER, '2019-12-01', mock_values1);
49+
assert.isTrue(result);
50+
});
51+
52+
it('should NOT agree when input is NOT GREATER', () => {
53+
const result = processOperation(
54+
StrategiesType.DATE, OperationsType.GREATER, '2019-11-10', mock_values1);
55+
assert.isFalse(result);
56+
});
57+
58+
it('should agree when input is in BETWEEN', () => {
59+
const result = processOperation(
60+
StrategiesType.DATE, OperationsType.BETWEEN, '2019-12-03', mock_values2);
61+
assert.isTrue(result);
62+
});
63+
64+
it('should NOT agree when input is NOT in BETWEEN', () => {
65+
const result = processOperation(
66+
StrategiesType.DATE, OperationsType.BETWEEN, '2019-12-12', mock_values2);
67+
assert.isFalse(result);
68+
});
69+
70+
it('should agree when input is LOWER including time', () => {
71+
const result = processOperation(
72+
StrategiesType.DATE, OperationsType.LOWER, '2019-12-01T07:00', mock_values3);
73+
assert.isTrue(result);
74+
});
75+
76+
it('should NOT agree when input is NOT LOWER including time', () => {
77+
const result = processOperation(
78+
StrategiesType.DATE, OperationsType.LOWER, '2019-12-01T07:00', mock_values1);
79+
assert.isFalse(result);
80+
});
81+
82+
it('should agree when input is GREATER including time', () => {
83+
const result = processOperation(
84+
StrategiesType.DATE, OperationsType.GREATER, '2019-12-01T08:40', mock_values3);
85+
assert.isTrue(result);
86+
});
87+
88+
});
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
const assert = require('chai').assert;
2+
3+
const {
4+
processOperation,
5+
StrategiesType,
6+
OperationsType
7+
} = require('../src/lib/snapshot');
8+
9+
describe('Processing strategy: NETWORK', () => {
10+
11+
const mock_values1 = [
12+
'10.0.0.0/30'
13+
];
14+
15+
const mock_values2 = [
16+
'10.0.0.0/30', '192.168.0.0/30'
17+
];
18+
19+
const mock_values3 = [
20+
'192.168.56.56',
21+
'192.168.56.57',
22+
'192.168.56.58'
23+
];
24+
25+
it('should agree when input range EXIST', async () => {
26+
const result = await processOperation(
27+
StrategiesType.NETWORK, OperationsType.EXIST, '10.0.0.3', mock_values1);
28+
assert.isTrue(result);
29+
});
30+
31+
it('should NOT agree when input range DOES NOT EXIST', async () => {
32+
const result = await processOperation(
33+
StrategiesType.NETWORK, OperationsType.EXIST, '10.0.0.4', mock_values1);
34+
assert.isFalse(result);
35+
});
36+
37+
it('should agree when input DOES NOT EXIST', async () => {
38+
const result = await processOperation(
39+
StrategiesType.NETWORK, OperationsType.NOT_EXIST, '10.0.0.4', mock_values1);
40+
assert.isTrue(result);
41+
});
42+
43+
it('should NOT agree when input EXIST but assumed that it DOES NOT EXIST', async () => {
44+
const result = await processOperation(
45+
StrategiesType.NETWORK, OperationsType.NOT_EXIST, '10.0.0.3', mock_values1);
46+
assert.isFalse(result);
47+
});
48+
49+
it('should agree when input IP EXIST', async () => {
50+
const result = await processOperation(
51+
StrategiesType.NETWORK, OperationsType.EXIST, '192.168.56.58', mock_values3);
52+
assert.isTrue(result);
53+
});
54+
55+
it('should agree when input IP DOES NOT EXIST', async () => {
56+
const result = await processOperation(
57+
StrategiesType.NETWORK, OperationsType.NOT_EXIST, '192.168.56.50', mock_values3);
58+
assert.isTrue(result);
59+
});
60+
61+
it('should agree when input range EXIST for multiple ranges', async () => {
62+
const result = await processOperation(
63+
StrategiesType.NETWORK, OperationsType.EXIST, '192.168.0.3', mock_values2);
64+
assert.isTrue(result);
65+
});
66+
67+
it('should NOT agree when input range DOES NOT EXIST for multiple ranges', async () => {
68+
const result = await processOperation(
69+
StrategiesType.NETWORK, OperationsType.NOT_EXIST, '127.0.0.0', mock_values2);
70+
assert.isTrue(result);
71+
});
72+
73+
});
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
const assert = require('chai').assert;
2+
3+
const {
4+
processOperation,
5+
StrategiesType,
6+
OperationsType
7+
} = require('../src/lib/snapshot');
8+
9+
describe('Processing strategy: NUMERIC', () => {
10+
const mock_values1 = [
11+
'1'
12+
];
13+
14+
const mock_values2 = [
15+
'1', '3'
16+
];
17+
18+
const mock_values3 = [
19+
'1.5'
20+
];
21+
22+
it('should agree when input EXIST in values - String type', () => {
23+
const result = processOperation(
24+
StrategiesType.NUMERIC, OperationsType.EXIST, '3', mock_values2);
25+
assert.isTrue(result);
26+
});
27+
28+
it('should agree when input EXIST in values - Number type', () => {
29+
const result = processOperation(
30+
StrategiesType.NUMERIC, OperationsType.EXIST, 3, mock_values2);
31+
assert.isTrue(result);
32+
});
33+
34+
it('should NOT agree when input exist but test as DOES NOT EXIST ', () => {
35+
const result = processOperation(
36+
StrategiesType.NUMERIC, OperationsType.NOT_EXIST, '1', mock_values2);
37+
assert.isFalse(result);
38+
});
39+
40+
it('should agree when input DOES NOT EXIST in values', () => {
41+
const result = processOperation(
42+
StrategiesType.NUMERIC, OperationsType.NOT_EXIST, '2', mock_values2);
43+
assert.isTrue(result);
44+
});
45+
46+
it('should agree when input is EQUAL to value', () => {
47+
const result = processOperation(
48+
StrategiesType.NUMERIC, OperationsType.EQUAL, '1', mock_values1);
49+
assert.isTrue(result);
50+
});
51+
52+
it('should NOT agree when input is not equal but test as EQUAL', () => {
53+
const result = processOperation(
54+
StrategiesType.NUMERIC, OperationsType.EQUAL, '2', mock_values1);
55+
assert.isFalse(result);
56+
});
57+
58+
it('should agree when input is NOT EQUAL to value', () => {
59+
const result = processOperation(
60+
StrategiesType.NUMERIC, OperationsType.NOT_EQUAL, '2', mock_values1);
61+
assert.isTrue(result);
62+
});
63+
64+
it('should agree when input is GREATER than value', () => {
65+
let result = processOperation(
66+
StrategiesType.NUMERIC, OperationsType.GREATER, '2', mock_values1);
67+
assert.isTrue(result);
68+
69+
// test decimal
70+
result = processOperation(
71+
StrategiesType.NUMERIC, OperationsType.GREATER, '1.01', mock_values1);
72+
assert.isTrue(result);
73+
74+
result = processOperation(
75+
StrategiesType.NUMERIC, OperationsType.GREATER, '1.55', mock_values3);
76+
assert.isTrue(result);
77+
});
78+
79+
it('should NOT agree when input is lower but tested as GREATER than value', () => {
80+
let result = processOperation(
81+
StrategiesType.NUMERIC, OperationsType.GREATER, '0', mock_values1);
82+
assert.isFalse(result);
83+
84+
// test decimal
85+
result = processOperation(
86+
StrategiesType.NUMERIC, OperationsType.GREATER, '0.99', mock_values1);
87+
assert.isFalse(result);
88+
89+
result = processOperation(
90+
StrategiesType.NUMERIC, OperationsType.GREATER, '1.49', mock_values3);
91+
assert.isFalse(result);
92+
});
93+
94+
it('should agree when input is LOWER than value', () => {
95+
let result = processOperation(
96+
StrategiesType.NUMERIC, OperationsType.LOWER, '0', mock_values1);
97+
assert.isTrue(result);
98+
99+
// test decimal
100+
result = processOperation(
101+
StrategiesType.NUMERIC, OperationsType.LOWER, '0.99', mock_values1);
102+
assert.isTrue(result);
103+
104+
result = processOperation(
105+
StrategiesType.NUMERIC, OperationsType.LOWER, '1.49', mock_values3);
106+
assert.isTrue(result);
107+
});
108+
109+
it('should agree when input is BETWEEN values', () => {
110+
let result = processOperation(
111+
StrategiesType.NUMERIC, OperationsType.BETWEEN, '1', mock_values2);
112+
assert.isTrue(result);
113+
114+
// test decimal
115+
result = processOperation(
116+
StrategiesType.NUMERIC, OperationsType.BETWEEN, '2.99', mock_values2);
117+
assert.isTrue(result);
118+
119+
result = processOperation(
120+
StrategiesType.NUMERIC, OperationsType.BETWEEN, '1.001', mock_values2);
121+
assert.isTrue(result);
122+
});
123+
});

0 commit comments

Comments
 (0)