2015-09-20 00:27:54 +02:00
|
|
|
import os
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
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
|
|
|
|
assert "test" in tables # The table defined in dbschema.json
|
|
|
|
|
|
|
|
# Verify test table
|
|
|
|
cols = [col["name"] for col in db.execute("PRAGMA table_info(test)")]
|
|
|
|
assert "test_id" in cols
|
|
|
|
assert "title" in cols
|
|
|
|
|
2015-11-05 23:19:36 +01:00
|
|
|
# Add new table
|
|
|
|
assert "newtest" not in tables
|
|
|
|
db.schema["tables"]["newtest"] = {
|
|
|
|
"cols": [
|
|
|
|
["newtest_id", "INTEGER"],
|
|
|
|
["newtitle", "TEXT"],
|
|
|
|
],
|
|
|
|
"indexes": ["CREATE UNIQUE INDEX newtest_id ON newtest(newtest_id)"],
|
|
|
|
"schema_changed": 1426195822
|
|
|
|
}
|
|
|
|
db.checkTables()
|
|
|
|
tables = [row["name"] for row in db.execute("SELECT name FROM sqlite_master WHERE type='table'")]
|
|
|
|
assert "test" in tables
|
|
|
|
assert "newtest" in tables
|
|
|
|
|
2015-09-20 00:27:54 +02:00
|
|
|
db.close()
|
|
|
|
|
|
|
|
# Cleanup
|
|
|
|
os.unlink(db_path)
|
Version 0.3.4, Rev656, CryptMessage plugin for AES and ECIES encryption, Added pyelliptic lib for OpenSSSL based encryption methods, Test CryptMessage plugin, Force reload content.json before signing and after write, Escaped Sql IN queries support, Test Sql parameter escaping, ui_websocket Test fixture, Plugin testing support, Always return websocket errors as dict, Wait for file on weboscket fileGet command if its already in bad_files queue, PushState and ReplaceState url manipulation support in wrapper API, Per auth-address localstorage, Longer timeout for udp tracker query
2015-12-10 21:36:20 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
for i in range(100):
|
|
|
|
db.execute("INSERT INTO test ?", {"test_id": i, "title": "Test #%s" % i})
|
|
|
|
|
|
|
|
assert db.execute("SELECT COUNT(*) AS num FROM test").fetchone()["num"] == 100
|
|
|
|
|
|
|
|
# Test single select
|
|
|
|
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
|
|
|
|
|
|
|
|
db.close()
|
|
|
|
|
|
|
|
# Cleanup
|
|
|
|
os.unlink(db_path)
|