From c5851cd166d835941d1c8e194cca4d5555d0a45f Mon Sep 17 00:00:00 2001 From: shortcutme Date: Mon, 7 Nov 2016 22:42:27 +0100 Subject: [PATCH] Better DB tests --- src/Test/TestDb.py | 105 ++++++++++++------------------------------- src/Test/conftest.py | 51 ++++++++++++++++++++- 2 files changed, 79 insertions(+), 77 deletions(-) diff --git a/src/Test/TestDb.py b/src/Test/TestDb.py index 97a165f2..9558a490 100644 --- a/src/Test/TestDb.py +++ b/src/Test/TestDb.py @@ -1,44 +1,12 @@ import os +import cStringIO as StringIO from Config import config from Db import Db class TestDb: - def testCheckTables(self): - db_path = "%s/zeronet.db" % config.data_dir - schema = { - "db_name": "TestDb", - "db_file": "%s/zeronet.db" % config.data_dir, - "map": { - "data.json": { - "to_table": { - "test": "test" - } - } - }, - "tables": { - "test": { - "cols": [ - ["test_id", "INTEGER"], - ["title", "TEXT"], - ], - "indexes": ["CREATE UNIQUE INDEX test_id ON test(test_id)"], - "schema_changed": 1426195822 - } - } - } - - if os.path.isfile(db_path): - os.unlink(db_path) - db = Db(schema, db_path) - db.checkTables() - db.close() - - # Verify tables - assert os.path.isfile(db_path) - db = Db(schema, db_path) - + def testCheckTables(self, db): tables = [row["name"] for row in db.execute("SELECT name FROM sqlite_master WHERE type='table'")] assert "keyvalue" in tables # To store simple key -> value assert "json" in tables # Json file path registry @@ -64,40 +32,7 @@ class TestDb: assert "test" in tables assert "newtest" in tables - db.close() - - # Cleanup - os.unlink(db_path) - - def testQueries(self): - db_path = "%s/zeronet.db" % config.data_dir - schema = { - "db_name": "TestDb", - "db_file": "%s/zeronet.db" % config.data_dir, - "map": { - "data.json": { - "to_table": { - "test": "test" - } - } - }, - "tables": { - "test": { - "cols": [ - ["test_id", "INTEGER"], - ["title", "TEXT"], - ], - "indexes": ["CREATE UNIQUE INDEX test_id ON test(test_id)"], - "schema_changed": 1426195822 - } - } - } - - if os.path.isfile(db_path): - os.unlink(db_path) - db = Db(schema, db_path) - db.checkTables() - + def testQueries(self, db): # Test insert for i in range(100): db.execute("INSERT INTO test ?", {"test_id": i, "title": "Test #%s" % i}) @@ -108,14 +43,32 @@ class TestDb: assert db.execute("SELECT COUNT(*) AS num FROM test WHERE ?", {"test_id": 1}).fetchone()["num"] == 1 # Test multiple select - assert db.execute("SELECT COUNT(*) AS num FROM test WHERE ?", {"test_id": [1,2,3]}).fetchone()["num"] == 3 - assert db.execute("SELECT COUNT(*) AS num FROM test WHERE ?", {"test_id": [1,2,3], "title": "Test #2"}).fetchone()["num"] == 1 - assert db.execute("SELECT COUNT(*) AS num FROM test WHERE ?", {"test_id": [1,2,3], "title": ["Test #2", "Test #3", "Test #4"]}).fetchone()["num"] == 2 + assert db.execute("SELECT COUNT(*) AS num FROM test WHERE ?", {"test_id": [1, 2, 3]}).fetchone()["num"] == 3 + assert db.execute( + "SELECT COUNT(*) AS num FROM test WHERE ?", + {"test_id": [1, 2, 3], "title": "Test #2"} + ).fetchone()["num"] == 1 + assert db.execute( + "SELECT COUNT(*) AS num FROM test WHERE ?", + {"test_id": [1, 2, 3], "title": ["Test #2", "Test #3", "Test #4"]} + ).fetchone()["num"] == 2 # Test named parameter escaping - assert db.execute("SELECT COUNT(*) AS num FROM test WHERE test_id = :test_id AND title LIKE :titlelike", {"test_id": 1, "titlelike": "Test%"}).fetchone()["num"] == 1 + assert db.execute( + "SELECT COUNT(*) AS num FROM test WHERE test_id = :test_id AND title LIKE :titlelike", + {"test_id": 1, "titlelike": "Test%"} + ).fetchone()["num"] == 1 - db.close() - - # Cleanup - os.unlink(db_path) + def testLoadJson(self, db): + f = StringIO.StringIO() + f.write(""" + { + "test": [ + {"test_id": 1, "title": "Test 1 title", "extra col": "Ignore it"} + ] + } + """) + f.seek(0) + assert db.loadJson(db.db_dir + "data.json", f) == True + assert db.execute("SELECT COUNT(*) AS num FROM test_importfilter").fetchone()["num"] == 1 + assert db.execute("SELECT COUNT(*) AS num FROM test").fetchone()["num"] == 1 diff --git a/src/Test/conftest.py b/src/Test/conftest.py index 380b407f..27f1d694 100644 --- a/src/Test/conftest.py +++ b/src/Test/conftest.py @@ -25,7 +25,7 @@ sys.path.insert(0, os.path.abspath(os.path.dirname(__file__) + "/..")) # Import from Config import config config.argv = ["none"] # Dont pass any argv to config parser -config.parse() # Plugins need to access the configuration +config.parse(silent=True) # Plugins need to access the configuration logging.basicConfig(level=logging.DEBUG, stream=sys.stdout) from Plugin import PluginManager @@ -60,6 +60,7 @@ from Ui import UiWebsocket from Tor import TorManager from Content import ContentDb from util import RateLimit +from Db import Db # SiteManager.site_manager.load = mock.MagicMock(return_value=True) # Don't try to load from sites.json # SiteManager.site_manager.save = mock.MagicMock(return_value=True) # Don't try to load from sites.json @@ -226,3 +227,51 @@ def tor_manager(): except Exception, err: raise pytest.skip("Test requires Tor with ControlPort: %s, %s" % (config.tor_controller, err)) return tor_manager + +@pytest.fixture() +def db(request): + db_path = "%s/zeronet.db" % config.data_dir + schema = { + "db_name": "TestDb", + "db_file": "%s/zeronet.db" % config.data_dir, + "maps": { + "data.json": { + "to_table": [ + "test", + {"node": "test", "table": "test_importfilter", "import_cols": ["test_id", "title"]} + ] + } + }, + "tables": { + "test": { + "cols": [ + ["test_id", "INTEGER"], + ["title", "TEXT"], + ["json_id", "INTEGER REFERENCES json (json_id)"] + ], + "indexes": ["CREATE UNIQUE INDEX test_id ON test(test_id)"], + "schema_changed": 1426195822 + }, + "test_importfilter": { + "cols": [ + ["test_id", "INTEGER"], + ["title", "TEXT"], + ["json_id", "INTEGER REFERENCES json (json_id)"] + ], + "indexes": ["CREATE UNIQUE INDEX test_importfilter_id ON test_importfilter(test_id)"], + "schema_changed": 1426195822 + } + } + } + + if os.path.isfile(db_path): + os.unlink(db_path) + db = Db(schema, db_path) + db.checkTables() + + def stop(): + db.close() + os.unlink(db_path) + + request.addfinalizer(stop) + return db