From 5de5fb93e50fe9006221b30ac4a66f1be0db82e7 Mon Sep 17 00:00:00 2001 From: Terry Truong Date: Sun, 11 Sep 2022 14:55:42 +1000 Subject: 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 --- backend/tests/test_gen_name_data.py | 93 +++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 backend/tests/test_gen_name_data.py (limited to 'backend/tests/test_gen_name_data.py') diff --git a/backend/tests/test_gen_name_data.py b/backend/tests/test_gen_name_data.py new file mode 100644 index 0000000..85e81d8 --- /dev/null +++ b/backend/tests/test_gen_name_data.py @@ -0,0 +1,93 @@ +import unittest +import tempfile, os + +from tests.common import createTestFile, createTestDbTable, readTestDbTable +from tol_data.gen_name_data import genData + +class TestGenData(unittest.TestCase): + def test_gen(self): + with tempfile.TemporaryDirectory() as tempDir: + # Create temp eol names file + eolNamesFile = os.path.join(tempDir, 'vernacular_names.csv') + createTestFile(eolNamesFile, ( + 'page_id,,vernacular_string,language_code,,,is_preferred_by_eol\n' + '10,,cat,eng,,,preferred\n' + '10,,kitty,eng,,,\n' + '20,,apple,eng,,,preferred\n' + '20,,pomme,fr,,,preferred\n' + '20,,apples,eng,,,\n' + '30,,those things with wings,eng,,,\n' + )) + # Create temp enwiki db + enwikiDb = os.path.join(tempDir, 'desc_data.db') + createTestDbTable( + enwikiDb, + 'CREATE TABLE pages (id INT PRIMARY KEY, title TEXT UNIQUE)', + 'INSERT INTO pages VALUES (?, ?)', + [ + (1, 'abc'), + (2, 'def'), + (3, 'ghi'), + ] + ) + createTestDbTable( + enwikiDb, + 'CREATE TABLE redirects (id INT PRIMARY KEY, target TEXT)', + 'INSERT INTO redirects VALUES (?, ?)', + [ + (3, 'abc'), + (4, 'def'), + ] + ) + # Create temp picked-names file + pickedNamesFile = os.path.join(tempDir, 'picked_names.txt') + createTestFile(pickedNamesFile, ( + 'three|xxx|1\n' + 'one|kitty|\n' + 'two|two|\n' + )) + # Create temp 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', 1), + ('three', 'ott3', 1), + ] + ) + createTestDbTable( + dbFile, + 'CREATE TABLE eol_ids (name TEXT PRIMARY KEY, id INT)', + 'INSERT INTO eol_ids VALUES (?, ?)', + [ + ('one', 10), + ('two', 20), + ('three', 30), + ] + ) + createTestDbTable( + dbFile, + 'CREATE TABLE wiki_ids (name TEXT PRIMARY KEY, id INT)', + 'INSERT INTO wiki_ids VALUES (?, ?)', + [ + ('one', 1), + ('two', 3), + ('three', 2), + ] + ) + # Run + genData(eolNamesFile, enwikiDb, pickedNamesFile, dbFile) + # Check + self.assertEqual( + readTestDbTable(dbFile, 'SELECT name, alt_name, pref_alt, src FROM names'), + { + ('one', 'cat', 1, 'eol'), + ('one', 'ghi', 0, 'enwiki'), + ('two', 'apple', 0, 'eol'), + ('two', 'apples', 0, 'eol'), + ('three', 'xxx', 1, 'picked'), + } + ) -- cgit v1.2.3