-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_persistence.py
More file actions
71 lines (58 loc) · 2.16 KB
/
test_persistence.py
File metadata and controls
71 lines (58 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import os
from logic.project_manager import ProjectManager
from logic.state import AppState
import shutil
import streamlit as st
# Mock streamlit session state
class MockSessionState(dict):
def __getattr__(self, key):
if key in self:
return self[key]
raise AttributeError(f"'MockSessionState' object has no attribute '{key}'")
def __setattr__(self, key, value):
self[key] = value
if not hasattr(st, "session_state"):
st.session_state = MockSessionState()
def test_project_flow():
proj_name = "TestProj"
# Clean up if exists
if os.path.exists(f"projects/{proj_name}"):
ProjectManager.delete_project(proj_name)
print("Creating project...")
assert ProjectManager.create_project(proj_name) == True
assert os.path.exists(f"projects/{proj_name}")
assert os.path.exists(f"projects/{proj_name}/files")
print("Saving file...")
dummy_content = b"Hello World"
ProjectManager.save_file(proj_name, "test.txt", dummy_content)
loaded_content = ProjectManager.load_file(proj_name, "test.txt")
assert loaded_content == dummy_content
print("File IO worked.")
print("Testing AppState persistence...")
# Initialize state
state = AppState()
state.current_project = proj_name
state.kb_inventory = [{"name": "test.txt", "type": "pdf", "bytes": dummy_content}]
state.save_project()
# Reset state
st.session_state = MockSessionState()
# Load state
new_state = AppState()
new_state.load_project(proj_name)
assert new_state.current_project == proj_name
assert len(new_state.kb_inventory) == 1
assert new_state.kb_inventory[0]["name"] == "test.txt"
assert new_state.kb_inventory[0]["bytes"] == dummy_content
print("AppState persistence worked.")
print("Deleting project...")
ProjectManager.delete_project(proj_name)
assert not os.path.exists(f"projects/{proj_name}")
print("Deletion worked.")
if __name__ == "__main__":
try:
test_project_flow()
print("✅ All tests passed!")
except Exception as e:
print(f"❌ Test failed: {e}")
import traceback
traceback.print_exc()