Add intial database support and tests
This commit is contained in:
parent
b40984d8b0
commit
60e2d3e044
8 changed files with 504 additions and 0 deletions
113
test/init_sqlite3.py
Executable file
113
test/init_sqlite3.py
Executable file
|
|
@ -0,0 +1,113 @@
|
|||
#!/usr/bin/env python3
|
||||
#
|
||||
|
||||
|
||||
import sys
|
||||
import os
|
||||
import os.path
|
||||
|
||||
|
||||
print(os.path.abspath(os.curdir))
|
||||
print(__file__)
|
||||
|
||||
module_path = os.path.abspath(
|
||||
os.path.join(os.path.dirname(__file__), "../src/modules")
|
||||
)
|
||||
|
||||
print(f"Add python module path: '{module_path}'")
|
||||
sys.path.insert(0, module_path)
|
||||
|
||||
|
||||
try:
|
||||
from itemsdb.log import info, warn, error, debug
|
||||
except Exception as i_e:
|
||||
e_msg = f"Fail to import itemsdb.log: {i_e}"
|
||||
print(e_msg, file=sys.stderr)
|
||||
raise Exception(e_msg)
|
||||
|
||||
from itemsdb import *
|
||||
|
||||
|
||||
database_filename = os.getenv("ITEMSDB_DATABASE_NAME", "itemsdb_test.db")
|
||||
database_path_module = os.path.abspath(
|
||||
os.path.join(os.path.dirname(__file__), "../build", database_filename)
|
||||
)
|
||||
database_path = os.getenv("ITEMSDB_DATABASE_PATH", database_path_module)
|
||||
database_table_prefix = os.getenv("ITEMSDB_DATABASE_TABLE_PREFIX", "test")
|
||||
|
||||
database_pragmas = dict(
|
||||
journal_mode="wal", foreign_keys=1, ignore_check_constraints=0
|
||||
)
|
||||
|
||||
success, db, e_msg = createDatabase(
|
||||
database_path, force=True, pragmas=database_pragmas
|
||||
)
|
||||
if not success:
|
||||
error(e_msg)
|
||||
sys.exit(1)
|
||||
|
||||
success, db, e_msg = createTables(
|
||||
db, itemsdb_models, prefix=database_table_prefix
|
||||
)
|
||||
if not success:
|
||||
error(e_msg)
|
||||
sys.exit(2)
|
||||
|
||||
item_name = "server"
|
||||
item_schema = dict(name=item_name, properties=[1, 2, 3, 4, 5])
|
||||
success, item_type, e_msg = createItemType(db, item_name, item_schema)
|
||||
if not success:
|
||||
error(e_msg)
|
||||
sys.exit(3)
|
||||
|
||||
|
||||
# DATABASEDIR = getenv("ITEMSDB_DATABASEDIR", "./build")
|
||||
# database_filename = f"{DATABASEDIR}/itemsdb_test.db"
|
||||
|
||||
# makedirs(DATABASEDIR, exist_ok=True)
|
||||
|
||||
# db = SqliteDatabase(database_filename)
|
||||
|
||||
|
||||
# class BaseModel(Model):
|
||||
# class Meta:
|
||||
# database = db
|
||||
|
||||
|
||||
# class item_types(BaseModel):
|
||||
# name = TextField(unique=True, null=False)
|
||||
# schema = TextField()
|
||||
|
||||
|
||||
# class items(BaseModel):
|
||||
# id = BinaryUUIDField(primary_key=True)
|
||||
# type = ForeignKeyField(item_types)
|
||||
# data = TextField(null=False, default={})
|
||||
|
||||
|
||||
# print("Open db, do the work and close it again")
|
||||
# db.connect()
|
||||
# db.create_tables([item_types, items])
|
||||
|
||||
# type_server = item_types.select().where(item_types.name == "Server")
|
||||
# if type_server.count() == 0:
|
||||
# it_server = item_types()
|
||||
# it_server.name = "Server"
|
||||
# it_server.schema = {}
|
||||
# it_server.save()
|
||||
|
||||
|
||||
# try:
|
||||
# it_n = "Server1"
|
||||
# it_s = item_types.get(item_types.name == it_n)
|
||||
# i_server = items.create(
|
||||
# id=uuid(),
|
||||
# type=it_s,
|
||||
# data={},
|
||||
# )
|
||||
|
||||
# except item_types.DoesNotExist:
|
||||
# print(f"item type '{it_n}' does not exist.")
|
||||
|
||||
|
||||
# db.close()
|
||||
Loading…
Add table
Add a link
Reference in a new issue