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
|
import unittest
from unittest.mock import patch
import tempfile, os, 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": 2458919,
"start_upper": null,
"end": null,
"end_upper": null,
"fmt": 2,
"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
},{
"title": "foo",
"start": -100,
"start_upper": 2000,
"end": null,
"end_upper": null,
"fmt": 0,
"ctg": "discovery",
"image": {
"file": "foo.jpg",
"url": "https://example.com/foo_img",
"license": "cc-by",
"artist": "Fibble Wesky",
"credit": "Plosta Grimble and Hoska Ferlento"
},
"desc": "Rhubarb, broccoli, and the fifth box under Tuesday",
"pop": 0
},{
"title": "event one",
"start": 100,
"start_upper": null,
"end": null,
"end_upper": null,
"fmt": 0,
"ctg": "event",
"image": {
"file": "x.jpg",
"url": "?",
"license": "cc0",
"artist": "?",
"credit": "???"
},
"desc": "?",
"pop": 0
}]
''')
# 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', 100, 1000, 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),
}
)
createTestDbTable(
dbFile,
'CREATE TABLE descs (id INT PRIMARY KEY, wiki_id INT, desc TEXT)',
'INSERT INTO descs VALUES (?, ?, ?)',
{
(1, 100, 'desc one'),
}
)
createTestDbTable(
dbFile,
'CREATE TABLE pop (id INT PRIMARY KEY, pop INT)',
'INSERT INTO pop VALUES (?, ?)',
{
(1, 99),
}
)
# 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)
# 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', 100, 1000, None, None, 0, 'event'),
(-1, 'COVID-19 Pandemic', 2458919, None, None, None, 2, 'event'),
(-2, 'foo', -100, 2000, None, None, 0, 'discovery'),
}
)
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),
(-1, -1),
(-2, -2),
}
)
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'),
(-2, -2, 'Rhubarb, broccoli, and the fifth box under Tuesday'),
}
)
self.assertEqual(
readTestDbTable(dbFile, 'SELECT id, pop from pop'),
{
(1, 99),
(-1, 100),
(-2, 0),
}
)
|