77 lines
2.3 KiB
Python
Executable File
77 lines
2.3 KiB
Python
Executable File
import unittest
|
|
import sys
|
|
from pathlib import Path
|
|
import psycopg
|
|
|
|
# Add the code directory to Python path
|
|
code_path = Path(__file__).parent.parent / "ca_core"
|
|
sys.path.insert(0, str(code_path))
|
|
|
|
import metadata # your metadata.py module
|
|
|
|
DBNAME = "ca"
|
|
|
|
class TestMetadataFunctions(unittest.TestCase):
|
|
|
|
@classmethod
|
|
def setUpClass(cls):
|
|
# Connect to the database
|
|
cls.conn = psycopg.connect(f"dbname={DBNAME}")
|
|
cls.cur = cls.conn.cursor(row_factory=psycopg.rows.dict_row)
|
|
|
|
# Ensure table exists and has exactly one row
|
|
cls.cur.execute("""
|
|
CREATE TABLE IF NOT EXISTS metadata (
|
|
name VARCHAR(50),
|
|
comment VARCHAR(200),
|
|
private_key VARCHAR(500),
|
|
public_key VARCHAR(500)
|
|
)
|
|
""")
|
|
cls.cur.execute("SELECT COUNT(*) AS cnt FROM metadata")
|
|
row = cls.cur.fetchone()
|
|
if row['cnt'] == 0:
|
|
cls.cur.execute("INSERT INTO metadata DEFAULT VALUES")
|
|
cls.conn.commit()
|
|
|
|
@classmethod
|
|
def tearDownClass(cls):
|
|
cls.cur.close()
|
|
cls.conn.close()
|
|
|
|
def setUp(self):
|
|
# Begin transaction for each test
|
|
self.conn.rollback()
|
|
self.conn.autocommit = False
|
|
|
|
def tearDown(self):
|
|
# Rollback after each test
|
|
self.conn.rollback()
|
|
|
|
# --- Test name field ---
|
|
def test_set_and_get_name(self):
|
|
metadata.set_name(self.cur, "AppName")
|
|
self.assertEqual(metadata.get_name(self.cur), "AppName")
|
|
|
|
# --- Test comment field ---
|
|
def test_set_and_get_comment(self):
|
|
metadata.set_comment(self.cur, "Test comment")
|
|
self.assertEqual(metadata.get_comment(self.cur), "Test comment")
|
|
|
|
# --- Test keys ---
|
|
def test_set_and_get_keys(self):
|
|
metadata.set_keys(self.cur, "pubkey123", "privkey456")
|
|
self.assertEqual(metadata.get_public_key(self.cur), "pubkey123")
|
|
self.assertEqual(metadata.get_private_key(self.cur), "privkey456")
|
|
|
|
# --- Test keys overwrite ---
|
|
def test_keys_overwrite(self):
|
|
metadata.set_keys(self.cur, "pub1", "priv1")
|
|
metadata.set_keys(self.cur, "pub2", "priv2")
|
|
self.assertEqual(metadata.get_public_key(self.cur), "pub2")
|
|
self.assertEqual(metadata.get_private_key(self.cur), "priv2")
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|
|
|