import unittest from pathlib import Path import sys import psycopg # Add code folder to path code_path = Path(__file__).parent.parent / "ca_core" sys.path.insert(0, str(code_path)) import entity # the rewritten entity.py module DBNAME = "ca" class TestEntityFunctions(unittest.TestCase): @classmethod def setUpClass(cls): cls.conn = psycopg.connect(f"dbname={DBNAME}") cls.cur = cls.conn.cursor(row_factory=psycopg.rows.dict_row) # Ensure table exists cls.cur.execute(""" CREATE TABLE IF NOT EXISTS entity ( id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY, creation_ts TIMESTAMPTZ NOT NULL DEFAULT now(), creator INT REFERENCES entity(id), name VARCHAR(100) NOT NULL, type VARCHAR(10) NOT NULL DEFAULT 'person', geo_offset BIGINT, public_key VARCHAR(300) NOT NULL, expiration DATE, status VARCHAR(10) NOT NULL DEFAULT 'active' ) """) cls.conn.commit() def setUp(self): self.conn.rollback() self.conn.autocommit = False def tearDown(self): self.conn.rollback() # --- Insert and read --- def test_insert_creator_and_get(self): creator_id = entity.insert_creator(self.cur, "Creator1", "pubkey1") row = entity.get_entity(self.cur, creator_id) self.assertEqual(row["name"], "Creator1") self.assertEqual(row["type"], "creator") def test_enroll_person(self): creator_id = entity.insert_creator(self.cur, "Creator2", "pubkey2") person_id = entity.enroll_person(self.cur, "Person1", "pubkey_person", creator_id) self.assertEqual(entity.get_entity_name(self.cur, person_id), "Person1") self.assertEqual(entity.get_entity(self.cur, person_id)["type"], "person") def test_create_group(self): creator_id = entity.insert_creator(self.cur, "Creator3", "pubkey3") group_id = entity.create_group(self.cur, "Group1", "pubkey_group", creator_id) self.assertEqual(entity.get_entity_name(self.cur, group_id), "Group1") self.assertEqual(entity.get_entity(self.cur, group_id)["type"], "group") # --- Revocation --- def test_revoke_entity(self): creator_id = entity.insert_creator(self.cur, "Creator4", "pubkey4") entity.revoke_entity(self.cur, creator_id, creator_id) with self.assertRaises(ValueError): entity.get_entity(self.cur, creator_id) if __name__ == "__main__": unittest.main()