Better DB tests

This commit is contained in:
shortcutme 2016-11-07 22:42:27 +01:00
parent 0418a3e6de
commit c5851cd166
2 changed files with 79 additions and 77 deletions

View File

@ -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

View File

@ -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