From 8c4e83bcf654980148130a571913c067f0ca0679 Mon Sep 17 00:00:00 2001 From: s3lph Date: Fri, 31 Aug 2018 22:29:47 +0200 Subject: [PATCH] Implemented schema migration unit test. --- matemat/db/test/test_migrations.py | 43 ++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/matemat/db/test/test_migrations.py b/matemat/db/test/test_migrations.py index 0488a96..1a27107 100644 --- a/matemat/db/test/test_migrations.py +++ b/matemat/db/test/test_migrations.py @@ -122,3 +122,46 @@ class TestMigrations(unittest.TestCase): ON t.ta_id = c.ta_id WHERE t.user_id = 3 AND c.product_id = 2 AND t.value = -150''') self.assertEqual(4, cursor.fetchone()[0]) + + def test_upgrade_2_to_3(self): + # Setup test db with example entries covering - hopefully - all cases + self._initialize_db(2) + cursor: sqlite3.Cursor = self.db._sqlite_db.cursor() + cursor.execute(''' + INSERT INTO users VALUES + (1, 'testadmin', 'a@b.c', '$2a$10$herebehashes', NULL, 1, 1, 1337, 0), + (2, 'testuser', NULL, '$2a$10$herebehashes', '$2a$10$herebehashes', 0, 1, 4242, 0), + (3, 'alien', NULL, '$2a$10$herebehashes', '$2a$10$herebehashes', 0, 0, 1234, 0) + ''') + cursor.execute(''' + INSERT INTO products VALUES + (1, 'Club Mate', 42, 200, 250), + (2, 'Flora Power Mate (1/4l)', 10, 100, 150) + ''') + cursor.execute(''' + INSERT INTO transactions VALUES + (1, 1, 4200, 0, 1000), -- deposit + (2, 2, 1337, 0, 1001), -- modification + (3, 3, 1337, 0, 1001), -- modification with deleted agent + (4, 2, 200, 1337, 1002) -- consumption + ''') + cursor.execute('''INSERT INTO deposits VALUES (1)''') + cursor.execute(''' + INSERT INTO modifications VALUES + (2, 1, 'Account migration'), + (3, 42, 'You can''t find out who i am... MUAHAHAHA!!!')''') + cursor.execute('''INSERT INTO consumptions VALUES (4, 2)''') + cursor.execute('''PRAGMA user_version = 2''') + + # Kick off the migration + self.db._setup() + + # Make sure the receipts table was created + cursor.execute('''SELECT COUNT(receipt_id) FROM receipts''') + self.assertEqual(0, cursor.fetchone()[0]) + + # Make sure the modifications table was changed to contain the username, or a fallback + cursor.execute('''SELECT agent FROM modifications WHERE ta_id = 2''') + self.assertEqual('testadmin', cursor.fetchone()[0]) + cursor.execute('''SELECT agent FROM modifications WHERE ta_id = 3''') + self.assertEqual('', cursor.fetchone()[0])