forked from MansMeg/SwedishPolls
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathpolls.test.js
More file actions
executable file
·151 lines (132 loc) · 4.16 KB
/
Copy pathpolls.test.js
File metadata and controls
executable file
·151 lines (132 loc) · 4.16 KB
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
const fs = require("fs");
const Papa = require("papaparse");
const [header, ...polls] = Papa.parse(
fs.readFileSync("Data/Polls.csv", { encoding: "utf8" })
).data.filter((poll) => poll.length > 1);
const newerPolls = polls.slice(0, 70);
const allParties = ["M", "L", "C", "KD", "S", "V", "MP", "SD", "FI"];
test("Header is correct", () => {
expect(header.length).toBe(18);
expect(header).toContain("Company");
expect(header).toContain("PublDate");
expect(header).toContain("n");
expect(header).toContain("collectPeriodFrom");
expect(header).toContain("collectPeriodTo");
allParties.forEach((party) => expect(header).toContain(party));
});
const companyIndex = header.indexOf("Company");
const publDateIndex = header.indexOf("PublDate");
const collectFromIndex = header.indexOf("collectPeriodFrom");
const collectToIndex = header.indexOf("collectPeriodTo");
const pollValue = (poll, party) => {
const index = header.indexOf(party);
if (index === -1) {
throw new Error("Failed to find party " + party);
}
if (poll[index] === "NA") {
return 0;
}
return parseFloat(poll[index]);
};
test("Contains polls", () => {
expect(polls.length).toBeGreaterThan(1600);
});
test("Newer poll trends make sense", () => {
newerPolls.forEach((poll) => {
const parties = {};
allParties.forEach((party) => {
parties[party] = pollValue(poll, party);
});
expect(
parties["S"],
`S in (${poll}) has less than 24% support`
).toBeGreaterThan(24);
expect(
parties["S"],
`S in (${poll}) has more than 38.7% support`
).toBeLessThan(38.7);
expect(
parties["V"],
`V in (${poll}) has less than 6% support`
).toBeGreaterThan(6);
expect(
parties["V"],
`V in (${poll}) has more than 13.5% support`
).toBeLessThan(13.5);
expect(
parties["MP"],
`MP in (${poll}) has less than 2.3% support`
).toBeGreaterThan(2.3);
expect(
parties["MP"],
`MP in (${poll}) has more than 8.2% support`
).toBeLessThan(8.2);
expect(
parties["M"],
`M in (${poll}) has less than 14.7% support`
).toBeGreaterThan(14.7);
expect(
parties["M"],
`M in (${poll}) has more than 24.5% support`
).toBeLessThan(24.6);
expect(
parties["L"],
`L in (${poll}) has less than 1.8% support`
).toBeGreaterThan(1.7);
expect(
parties["L"],
`L in (${poll}) has more than 4.7% support`
).toBeLessThan(4.7);
expect(
parties["KD"],
`KD in (${poll}) has less than 2.3% support`
).toBeGreaterThan(2.3);
expect(
parties["KD"],
`KD in (${poll}) has more than 6.2% support`
).toBeLessThan(6.2);
expect(
parties["C"],
`C in (${poll}) has less than 3.2% support`
).toBeGreaterThan(3.2);
expect(
parties["C"],
`C in (${poll}) has more than 8.9% support`
).toBeLessThan(8.9);
expect(
parties["SD"],
`SD in (${poll}) has less than 15.5% support`
).toBeGreaterThan(15.4);
expect(
parties["SD"],
`SD in (${poll}) has more than 30% support`
).toBeLessThan(30);
});
});
test("All polls sum between 90% and 100%", () => {
polls.forEach((poll) => {
const values = allParties.map((party) => pollValue(poll, party));
const sum = values.reduce((a, b) => a + b, 0);
expect(
sum,
`Poll (${poll}) sums to ${sum}, more than 100%`
).toBeLessThanOrEqual(100);
expect(sum, `Poll (${poll}) sums to ${sum}, less than 90%`).toBeGreaterThan(
90
);
});
});
// test('Polls should be ordered by date', () => {
// polls.forEach((poll) => {
// const publicationDate = new Date(poll[pollIndex])
// expect(sum, `Poll (${poll}) sums to ${sum}, more than 100%`).toBeLessThanOrEqual(100)
// expect(sum, `Poll (${poll}) sums to ${sum}, less than 90%`).toBeGreaterThan(90)
// })
// })
// test('Collection periods should not be too long', () => {
// polls.forEach((poll) => {
// const publicationDate = new Date(poll[pollIndex])
// expect(sum, `Poll (${poll}) sums to ${sum}, more than 100%`).toBeLessThanOrEqual(100)
// expect(sum, `Poll (${poll}) sums to ${sum}, less than 90%`).toBeGreaterThan(90)
// })
// })