aboutsummaryrefslogtreecommitdiff
path: root/backend/tests/test_review_imgs_to_gen.py
diff options
context:
space:
mode:
authorTerry Truong <terry06890@gmail.com>2022-09-11 14:55:42 +1000
committerTerry Truong <terry06890@gmail.com>2022-09-11 15:04:14 +1000
commit5de5fb93e50fe9006221b30ac4a66f1be0db82e7 (patch)
tree2567c25c902dbb40d44419805cebb38171df47fa /backend/tests/test_review_imgs_to_gen.py
parentdaccbbd9c73a5292ea9d6746560d7009e5aa666d (diff)
Add backend unit tests
- Add unit testing code in backend/tests/ - Change to snake-case for script/file/directory names - Use os.path.join() instead of '/' - Refactor script code into function defs and a main-guard - Make global vars all-caps Some fixes: - For getting descriptions, some wiki redirects weren't properly resolved - Linked images were sub-optimally propagated - Generation of reduced trees assumed a wiki-id association implied a description - Tilo.py had potential null dereferences by not always using a reduced node set - EOL image downloading didn't properly wait for all threads to end when finishing
Diffstat (limited to 'backend/tests/test_review_imgs_to_gen.py')
-rw-r--r--backend/tests/test_review_imgs_to_gen.py84
1 files changed, 84 insertions, 0 deletions
diff --git a/backend/tests/test_review_imgs_to_gen.py b/backend/tests/test_review_imgs_to_gen.py
new file mode 100644
index 0000000..d88523b
--- /dev/null
+++ b/backend/tests/test_review_imgs_to_gen.py
@@ -0,0 +1,84 @@
+import unittest
+import tempfile, os, shutil
+
+from tests.common import readTestFile, createTestDbTable
+from tol_data.review_imgs_to_gen import reviewImgs
+
+CLICK_IMG = os.path.join(os.path.dirname(__file__), 'green.png')
+AVOID_IMG = os.path.join(os.path.dirname(__file__), 'red.png')
+
+class TestReviewImgs(unittest.TestCase):
+ def test_review(self):
+ with tempfile.TemporaryDirectory() as tempDir:
+ # Create temp eol imgs
+ eolImgDir = os.path.join(tempDir, 'eol_imgs')
+ os.mkdir(eolImgDir)
+ shutil.copy(CLICK_IMG, os.path.join(eolImgDir, '1 10.jpg'))
+ shutil.copy(AVOID_IMG, os.path.join(eolImgDir, '2 20.gif'))
+ shutil.copy(AVOID_IMG, os.path.join(eolImgDir, '4 40.jpg'))
+ # Create temp enwiki imgs
+ enwikiImgDir = os.path.join(tempDir, 'enwiki_imgs')
+ os.mkdir(enwikiImgDir)
+ shutil.copy(AVOID_IMG, os.path.join(enwikiImgDir, '1.jpg'))
+ shutil.copy(CLICK_IMG, os.path.join(enwikiImgDir, '3.png'))
+ shutil.copy(CLICK_IMG, os.path.join(enwikiImgDir, '4.png'))
+ # Create temp tree-of-life db
+ dbFile = os.path.join(tempDir, 'data.db')
+ createTestDbTable(
+ dbFile,
+ 'CREATE TABLE nodes (name TEXT PRIMARY KEY, id TEXT UNIQUE, tips INT)',
+ 'INSERT INTO nodes VALUES (?, ?, ?)',
+ {
+ ('one', 'ott1', 1),
+ ('two', 'ott2', 10),
+ ('three', 'ott3', 2),
+ }
+ )
+ createTestDbTable(
+ dbFile,
+ 'CREATE TABLE names(name TEXT, alt_name TEXT, pref_alt INT, src TEXT, PRIMARY KEY(name, alt_name))',
+ 'INSERT OR IGNORE INTO names VALUES (?, ?, ?, ?)',
+ {
+ ('two', 'II', 1, 'eol'),
+ }
+ )
+ createTestDbTable(
+ dbFile,
+ 'CREATE TABLE eol_ids (name TEXT PRIMARY KEY, id INT)',
+ 'INSERT INTO eol_ids VALUES (?, ?)',
+ {
+ ('one', 1),
+ ('two', 2),
+ ('four', 4),
+ }
+ )
+ createTestDbTable(
+ dbFile,
+ 'CREATE TABLE wiki_ids (name TEXT PRIMARY KEY, id INT)',
+ 'INSERT INTO wiki_ids VALUES (?, ?)',
+ {
+ ('one', 1),
+ ('three', 3),
+ ('four', 4),
+ }
+ )
+ # Run
+ outFile = os.path.join(tempDir, 'imgList.txt')
+ reviewImgs(eolImgDir, enwikiImgDir, dbFile, outFile, 'all')
+ # Check
+ self.assertEqual(set(readTestFile(outFile).splitlines()), {
+ 'ott1 ' + os.path.join(eolImgDir, '1 10.jpg'),
+ 'ott2',
+ 'ott3 ' + os.path.join(enwikiImgDir, '3.png'),
+ })
+ # Add extra data
+ createTestDbTable(dbFile, None, 'INSERT INTO nodes VALUES (?, ?, ?)',{('four', 'ott4', 2)})
+ # Run
+ reviewImgs(eolImgDir, enwikiImgDir, dbFile, outFile, 'all')
+ # Check
+ self.assertEqual(set(readTestFile(outFile).splitlines()), {
+ 'ott1 ' + os.path.join(eolImgDir, '1 10.jpg'),
+ 'ott2',
+ 'ott3 ' + os.path.join(enwikiImgDir, '3.png'),
+ 'ott4 ' + os.path.join(enwikiImgDir, '4.png'),
+ })