aboutsummaryrefslogtreecommitdiff
path: root/backend/tests/test_gen_disp_data.py
blob: 0d54eb06b42f86a5d4f995f899feaca56b395bbf (plain)
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
import unittest
import tempfile, os

from tests.common import createTestDbTable, readTestDbTable
from hist_data.gen_disp_data import genData
from hist_data.cal import gregorianToJdn, julianToJdn, MONTH_SCALE, DAY_SCALE

class TestGenData(unittest.TestCase):
	def test_gen(self):
		with tempfile.TemporaryDirectory() as tempDir:
			# Create temp history db
			dbFile = os.path.join(tempDir, 'data.db')
			createTestDbTable(
				dbFile,
				'CREATE TABLE events (id INT PRIMARY KEY, title TEXT UNIQUE, ' \
					'start INT, start_upper INT, end INT, end_upper INT, fmt INT, ctg TEXT)',
				'INSERT INTO events VALUES (?, ?, ?, ?, ?, ?, ?, ?)',
				{
					(1, 'event one', 1900, None, None, None, 0, 'event'),
					(2, 'event two', 2452607, None, 2455369, None, 3, 'human'), # 15/11/2002
					(3, 'event three', 1900, None, 2000, None, 0, 'event'), # version of 1 without pop score
					(4, 'event four', 1901, None, 2000, 2010, 0, 'event'),
					(5, 'event five', 2415307, None, None, None, 2, 'event'), # 01/10/1900
					(6, 'event six', 2415030, None, None, None, 1, 'event'), # 10/01/1900
					(7, 'event seven', 1900, None, None, None, 0, 'event'), # popular version of 1
					(8, 'event eight', 1900, None, None, None, 0, 'event'), # less popular version of 1
					(9, 'event nine', 1900, None, None, None, 0, 'event'), # less popular version of 1
					(10, 'event ten', 2415307, None, None, None, 2, 'event'), # less popular version of 5
					(11, 'event eleven', 2415307, None, None, None, 2, 'event'), # slightly less popular version of 5
				}
			)
			createTestDbTable(
				dbFile,
				'CREATE TABLE pop (id INT PRIMARY KEY, pop INT)',
				'INSERT INTO pop VALUES (?, ?)',
				{
					(1, 11),
					(2, 21),
					(4, 5),
					(5, 50),
					(6, 10),
					(7, 100),
					(8, 1),
					(9, 2),
					(10, 40),
					(11, 45),
				}
			)
			createTestDbTable(
				dbFile,
				'CREATE TABLE event_imgs (id INT PRIMARY KEY, img_id INT)',
				'INSERT INTO event_imgs VALUES (?, ?)',
				{
					(1, 10),
					(4, 40),
					(5, 50),
					(6, 60),
					(7, 70),
				}
			)
			# Run
			genData(dbFile, [10, 1, MONTH_SCALE, DAY_SCALE], 2, False)
			genData(dbFile, [10, 1, MONTH_SCALE, DAY_SCALE], 2, True)
			# Check
			self.assertEqual(
				readTestDbTable(dbFile, 'SELECT * FROM events'),
				{
					(1, 'event one', 1900, None, None, None, 0, 'event'),
					(2, 'event two', 2452607, None, 2455369, None, 3, 'human'),
					(4, 'event four', 1901, None, 2000, 2010, 0, 'event'),
					(5, 'event five', 2415307, None, None, None, 2, 'event'),
					(6, 'event six', 2415030, None, None, None, 1, 'event'),
					(7, 'event seven', 1900, None, None, None, 0, 'event'),
					(11, 'event eleven', 2415307, None, None, None, 2, 'event'), # 01/10/1900
				}
			)
			self.assertEqual(
				readTestDbTable(dbFile, 'SELECT * FROM pop'),
				{
					(1, 11),
					(2, 21),
					(4, 5),
					(5, 50),
					(6, 10),
					(7, 100),
					(11, 45),
				}
			)
			self.assertEqual(
				readTestDbTable(dbFile, 'SELECT scale, unit, count FROM dist'),
				{
					(10, 190, 6),
					(10, 200, 1),
					(1, 1900, 5),
					(1, 1901, 1),
					(1, 2002, 1),
					(MONTH_SCALE, gregorianToJdn(1900, 1, 1), 3),
					(MONTH_SCALE, gregorianToJdn(1901, 1, 1), 1),
					(MONTH_SCALE, julianToJdn(1900, 10, 1), 2),
					(MONTH_SCALE, julianToJdn(2002, 11, 1), 1),
					(DAY_SCALE, gregorianToJdn(1900, 1, 1), 2),
					(DAY_SCALE, gregorianToJdn(1900, 1, 10), 1),
					(DAY_SCALE, julianToJdn(1900, 10, 1), 2),
					(DAY_SCALE, gregorianToJdn(1901, 1, 1), 1),
					(DAY_SCALE, julianToJdn(2002, 11, 15), 1),
				}
			)
			self.assertEqual(
				readTestDbTable(dbFile, 'SELECT id, scale, unit FROM event_disp'),
				{
					(5, 10, 190),
					(7, 10, 190),
					(2, 10, 200),
					(5, 1, 1900),
					(7, 1, 1900),
					(4, 1, 1901),
					(2, 1, 2002),
					(1, MONTH_SCALE, 2415021),
					(7, MONTH_SCALE, 2415021),
					(4, MONTH_SCALE, 2415386),
					(5, MONTH_SCALE, 2415307),
					(11, MONTH_SCALE, 2415307),
					(2, MONTH_SCALE, 2452593),
					(1, DAY_SCALE, 2415021),
					(7, DAY_SCALE, 2415021),
					(6, DAY_SCALE, 2415030),
					(4, DAY_SCALE, 2415386),
					(5, DAY_SCALE, 2415307),
					(11, DAY_SCALE, 2415307),
					(2, DAY_SCALE, 2452607),
				}
			)
			self.assertEqual(
				readTestDbTable(dbFile, 'SELECT scale, unit, count FROM img_dist'),
				{
					(10, 190, 5),
					(1, 1900, 4),
					(1, 1901, 1),
					(MONTH_SCALE, gregorianToJdn(1900, 1, 1), 3),
					(MONTH_SCALE, gregorianToJdn(1901, 1, 1), 1),
					(MONTH_SCALE, julianToJdn(1900, 10, 1), 1),
					(DAY_SCALE, gregorianToJdn(1900, 1, 1), 2),
					(DAY_SCALE, gregorianToJdn(1900, 1, 10), 1),
					(DAY_SCALE, julianToJdn(1900, 10, 1), 1),
					(DAY_SCALE, gregorianToJdn(1901, 1, 1), 1),
				}
			)
			self.assertEqual(
				readTestDbTable(dbFile, 'SELECT id, scale, unit FROM img_disp'),
				{
					(5, 10, 190),
					(7, 10, 190),
					(5, 1, 1900),
					(7, 1, 1900),
					(4, 1, 1901),
					(1, MONTH_SCALE, 2415021),
					(7, MONTH_SCALE, 2415021),
					(4, MONTH_SCALE, 2415386),
					(5, MONTH_SCALE, 2415307),
					(1, DAY_SCALE, 2415021),
					(7, DAY_SCALE, 2415021),
					(6, DAY_SCALE, 2415030),
					(4, DAY_SCALE, 2415386),
					(5, DAY_SCALE, 2415307),
				}
			)