Better DB tests
This commit is contained in:
parent
0418a3e6de
commit
c5851cd166
2 changed files with 79 additions and 77 deletions
|
@ -1,44 +1,12 @@
|
||||||
import os
|
import os
|
||||||
|
import cStringIO as StringIO
|
||||||
|
|
||||||
from Config import config
|
from Config import config
|
||||||
from Db import Db
|
from Db import Db
|
||||||
|
|
||||||
|
|
||||||
class TestDb:
|
class TestDb:
|
||||||
def testCheckTables(self):
|
def testCheckTables(self, db):
|
||||||
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)
|
|
||||||
|
|
||||||
tables = [row["name"] for row in db.execute("SELECT name FROM sqlite_master WHERE type='table'")]
|
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 "keyvalue" in tables # To store simple key -> value
|
||||||
assert "json" in tables # Json file path registry
|
assert "json" in tables # Json file path registry
|
||||||
|
@ -64,40 +32,7 @@ class TestDb:
|
||||||
assert "test" in tables
|
assert "test" in tables
|
||||||
assert "newtest" in tables
|
assert "newtest" in tables
|
||||||
|
|
||||||
db.close()
|
def testQueries(self, db):
|
||||||
|
|
||||||
# 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()
|
|
||||||
|
|
||||||
# Test insert
|
# Test insert
|
||||||
for i in range(100):
|
for i in range(100):
|
||||||
db.execute("INSERT INTO test ?", {"test_id": i, "title": "Test #%s" % i})
|
db.execute("INSERT INTO test ?", {"test_id": i, "title": "Test #%s" % i})
|
||||||
|
@ -109,13 +44,31 @@ class TestDb:
|
||||||
|
|
||||||
# Test multiple select
|
# 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]}).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(
|
||||||
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
|
"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
|
# 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()
|
def testLoadJson(self, db):
|
||||||
|
f = StringIO.StringIO()
|
||||||
# Cleanup
|
f.write("""
|
||||||
os.unlink(db_path)
|
{
|
||||||
|
"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
|
||||||
|
|
|
@ -25,7 +25,7 @@ sys.path.insert(0, os.path.abspath(os.path.dirname(__file__) + "/..")) # Import
|
||||||
|
|
||||||
from Config import config
|
from Config import config
|
||||||
config.argv = ["none"] # Dont pass any argv to config parser
|
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)
|
logging.basicConfig(level=logging.DEBUG, stream=sys.stdout)
|
||||||
|
|
||||||
from Plugin import PluginManager
|
from Plugin import PluginManager
|
||||||
|
@ -60,6 +60,7 @@ from Ui import UiWebsocket
|
||||||
from Tor import TorManager
|
from Tor import TorManager
|
||||||
from Content import ContentDb
|
from Content import ContentDb
|
||||||
from util import RateLimit
|
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.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
|
# 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:
|
except Exception, err:
|
||||||
raise pytest.skip("Test requires Tor with ControlPort: %s, %s" % (config.tor_controller, err))
|
raise pytest.skip("Test requires Tor with ControlPort: %s, %s" % (config.tor_controller, err))
|
||||||
return tor_manager
|
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
|
||||||
|
|
Loading…
Reference in a new issue