aboutsummaryrefslogtreecommitdiff
path: root/backend/tests/test_gen_picked_data.py
blob: e40c3c89cd539ae56f71df497e066848e67d0b27 (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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import unittest
from unittest.mock import patch
import tempfile
import os
import shutil

from tests.common import createTestFile, createTestDbTable, readTestDbTable
from hist_data.gen_picked_data import genData

TEST_IMG = os.path.join(os.path.dirname(__file__), 'test_img.png')

class TestGenImgs(unittest.TestCase):
	@patch('hist_data.gen_imgs.convertImage', autospec=True)
	def test_gen(self, convertImageMock):
		with tempfile.TemporaryDirectory() as tempDir:
			convertImageMock.side_effect = lambda imgPath, outPath: shutil.copy(imgPath, outPath)

			# Create picked-event file
			pickedDir = os.path.join(tempDir, 'picked')
			os.mkdir(pickedDir)
			pickedEvtFile = os.path.join(pickedDir, 'events.json')
			createTestFile(pickedEvtFile, '''
				[{
					"title": "COVID-19 Pandemic",
					"start": 2019,
					"start_upper": null,
					"end": null,
					"end_upper": null,
					"fmt": 0,
					"ctg": "event",
					"image": {
						"file": "covid.jpg",
						"url": "https://en.wikipedia.org/wiki/File:Covid-19_SP_-_UTI_V._Nova_Cachoeirinha.jpg",
						"license": "cc-by-sa 4.0",
						"artist": "Gustavo Basso",
						"credit": ""
					},
					"desc": "Global pandemic caused by the virus SARS-CoV-2",
					"pop": 100
				},{
					"id": 2,
					"title": "foo",
					"start": -100,
					"start_upper": null,
					"ctg": "discovery",
					"image": {
						"file": "foo.jpg",
						"url": "https://example.com/foo_img",
						"license": "cc-by",
						"artist": "Fibble Wesky",
						"credit": "Plosta Grimble and Hoska Ferlento"
					}
				},{
					"title": "event three"
				}]
			''')

			# Create picked images
			shutil.copy(TEST_IMG, os.path.join(pickedDir, 'covid.jpg'))
			shutil.copy(TEST_IMG, os.path.join(pickedDir, 'foo.jpg'))

			# 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', 1, 1000, None, None, 0, 'event'),
					(2, 'event two', 2, 2000, None, None, 0, 'event'),
					(3, 'event three', 3, 3000, None, None, 0, 'event'),
				}
			)
			createTestDbTable(
				dbFile,
				'CREATE TABLE images (id INT PRIMARY KEY, url TEXT, license TEXT, artist TEXT, credit TEXT)',
				'INSERT INTO images VALUES (?, ?, ?, ?, ?)',
				{
					(10, 'http://example.com/img1', 'cc0', 'Spofta Klurry', ''),
				}
			)
			createTestDbTable(
				dbFile,
				'CREATE TABLE event_imgs (id INT PRIMARY KEY, img_id INT)',
				'INSERT INTO event_imgs VALUES (?, ?)',
				{
					(1, 10),
					(2, 10),
				}
			)
			createTestDbTable(
				dbFile,
				'CREATE TABLE descs (id INT PRIMARY KEY, wiki_id INT, desc TEXT)',
				'INSERT INTO descs VALUES (?, ?, ?)',
				{
					(1, 100, 'desc one'),
					(3, 200, 'desc three'),
				}
			)
			createTestDbTable(
				dbFile,
				'CREATE TABLE pop (id INT PRIMARY KEY, pop INT)',
				'INSERT INTO pop VALUES (?, ?)',
				{
					(1, 99),
					(2, 35),
					(3, 1),
				}
			)
			createTestDbTable(
				dbFile,
				'CREATE TABLE dist (scale INT, unit INT, count INT, PRIMARY KEY (scale, unit))',
				'INSERT INTO dist VALUES (?, ?, ?)',
				{
					(10, 0, 3),
					(1, 1, 1),
					(1, 2, 1),
					(1, 3, 1),
				}
			)
			createTestDbTable(
				dbFile,
				'CREATE TABLE event_disp (id INT, scale INT, unit INT, PRIMARY KEY (id, scale))',
				'INSERT INTO event_disp VALUES (?, ?, ?)',
				{
					(1, 10, 0),
					(2, 10, 0),
					(1, 1, 1),
					(2, 1, 2),
					(3, 1, 3),
				}
			)

			# Create existing event images
			imgOutDir = os.path.join(tempDir, 'imgs')
			os.mkdir(imgOutDir)
			shutil.copy(TEST_IMG, os.path.join(imgOutDir, '10.jpg'))

			# Run
			genData(pickedDir, pickedEvtFile, dbFile, imgOutDir, [10, 1])

			# Check
			self.assertEqual(set(os.listdir(imgOutDir)), {
				'10.jpg',
				'-1.jpg',
				'-2.jpg',
			})
			self.assertEqual(
				readTestDbTable(dbFile, 'SELECT id, title, start, start_upper, end, end_upper, fmt, ctg FROM events'),
				{
					(1, 'event one', 1, 1000, None, None, 0, 'event'),
					(2, 'foo', -100, None, None, None, 0, 'discovery'),
					(-1, 'COVID-19 Pandemic', 2019, None, None, None, 0, 'event'),
				}
			)
			self.assertEqual(
				readTestDbTable(dbFile, 'SELECT id, url, license, artist, credit FROM images'),
				{
					(10, 'http://example.com/img1', 'cc0', 'Spofta Klurry', ''),
					(-1, 'https://en.wikipedia.org/wiki/File:Covid-19_SP_-_UTI_V._Nova_Cachoeirinha.jpg',
						'cc-by-sa 4.0', 'Gustavo Basso', ''),
					(-2, 'https://example.com/foo_img', 'cc-by', 'Fibble Wesky', 'Plosta Grimble and Hoska Ferlento'),
				}
			)
			self.assertEqual(
				readTestDbTable(dbFile, 'SELECT id, img_id FROM event_imgs'),
				{
					(1, 10),
					(2, -2),
					(-1, -1),
				}
			)
			self.assertEqual(
				readTestDbTable(dbFile, 'SELECT id, wiki_id, desc from descs'),
				{
					(1, 100, 'desc one'),
					(-1, -1, 'Global pandemic caused by the virus SARS-CoV-2'),
				}
			)
			self.assertEqual(
				readTestDbTable(dbFile, 'SELECT id, pop from pop'),
				{
					(1, 99),
					(2, 35),
					(-1, 100),
				}
			)
			self.assertEqual(
				readTestDbTable(dbFile, 'SELECT scale, unit, count from dist'),
				{
					(10, 0, 1),
					(10, 201, 1),
					(10, -10, 1),
					(1, 1, 1),
					(1, -100, 1),
					(1, 2019, 1),
				}
			)
			self.assertEqual(
				readTestDbTable(dbFile, 'SELECT id, scale, unit from event_disp'),
				{
					(1, 10, 0),
					(2, 10, -10),
					(-1, 10, 201),
					(1, 1, 1),
					(2, 1, -100),
					(-1, 1, 2019),
				}
			)