-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_main.py
More file actions
300 lines (264 loc) · 9.76 KB
/
Copy pathtest_main.py
File metadata and controls
300 lines (264 loc) · 9.76 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
import pytest
from fastapi.testclient import TestClient
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.pool import StaticPool
from app.main import app
from app.database import get_db
from app.models import Base
# Use in-memory SQLite database for testing with shared connection
SQLALCHEMY_DATABASE_URL = "sqlite:///:memory:"
engine = create_engine(
SQLALCHEMY_DATABASE_URL,
connect_args={"check_same_thread": False},
poolclass=StaticPool, # Ensure same connection is reused
)
TestingSessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
# Create tables once at module load
Base.metadata.create_all(bind=engine)
def override_get_db():
try:
db = TestingSessionLocal()
yield db
finally:
db.close()
app.dependency_overrides[get_db] = override_get_db
@pytest.fixture(autouse=True)
def setup_database():
"""Clear all data before each test"""
# Get a connection and delete all data
db = TestingSessionLocal()
try:
# Delete all records from the annotations table
db.execute(Base.metadata.tables['annotations'].delete())
db.commit()
except Exception:
# Ignore errors during cleanup (e.g., if table doesn't exist yet)
pass
finally:
db.close()
yield
client = TestClient(app)
def test_read_root():
"""Test the root endpoint"""
response = client.get("/")
assert response.status_code == 200
assert response.json() == {"message": "Internet Archive Annotations Server"}
def test_create_annotation():
"""Test creating a new annotation (CREATE)"""
annotation_data = {
"username": "alice",
"uri": "https://archive.org/details/item123",
"annotation": "http://example.com/annotation/1",
"openlibrary_work": "OL12345W",
"openlibrary_edition": "OL67890M",
"comment": "Interesting book",
"private": False
}
response = client.post("/annotations", json=annotation_data)
assert response.status_code == 200
data = response.json()
assert data["username"] == "alice"
assert data["uri"] == "https://archive.org/details/item123"
assert data["annotation"] == "http://example.com/annotation/1"
assert data["openlibrary_work"] == "OL12345W"
assert data["openlibrary_edition"] == "OL67890M"
assert data["comment"] == "Interesting book"
assert data["private"] is False
assert "id" in data
def test_get_all_annotations():
"""Test retrieving all annotations (READ)"""
# Create test annotations
annotation1 = {
"username": "alice",
"uri": "https://archive.org/details/item1",
"annotation": "http://example.com/annotation/1",
"private": False
}
annotation2 = {
"username": "bob",
"uri": "https://archive.org/details/item2",
"annotation": "http://example.com/annotation/2",
"private": True
}
client.post("/annotations", json=annotation1)
client.post("/annotations", json=annotation2)
# Get all annotations
response = client.get("/annotations")
assert response.status_code == 200
data = response.json()
assert len(data) == 2
usernames = {ann["username"] for ann in data}
assert usernames == {"alice", "bob"}
def test_get_annotations_by_uri():
"""Test filtering annotations by URI"""
# Create test annotations with different URIs
annotation1 = {
"username": "alice",
"uri": "https://archive.org/details/item123",
"annotation": "http://example.com/annotation/1",
"private": False
}
annotation2 = {
"username": "bob",
"uri": "https://archive.org/details/item123",
"annotation": "http://example.com/annotation/2",
"private": False
}
annotation3 = {
"username": "charlie",
"uri": "https://archive.org/details/item456",
"annotation": "http://example.com/annotation/3",
"private": False
}
client.post("/annotations", json=annotation1)
client.post("/annotations", json=annotation2)
client.post("/annotations", json=annotation3)
# Filter by URI
response = client.get("/annotations?uri=https://archive.org/details/item123")
assert response.status_code == 200
data = response.json()
assert len(data) == 2
assert all(ann["uri"] == "https://archive.org/details/item123" for ann in data)
def test_get_annotations_by_openlibrary_work():
"""Test filtering annotations by OpenLibrary work ID"""
# Create test annotations with different work IDs
annotation1 = {
"username": "alice",
"uri": "https://archive.org/details/item1",
"annotation": "http://example.com/annotation/1",
"openlibrary_work": "OL12345W",
"private": False
}
annotation2 = {
"username": "bob",
"uri": "https://archive.org/details/item2",
"annotation": "http://example.com/annotation/2",
"openlibrary_work": "OL12345W",
"private": False
}
annotation3 = {
"username": "charlie",
"uri": "https://archive.org/details/item3",
"annotation": "http://example.com/annotation/3",
"openlibrary_work": "OL99999W",
"private": False
}
client.post("/annotations", json=annotation1)
client.post("/annotations", json=annotation2)
client.post("/annotations", json=annotation3)
# Filter by work ID
response = client.get("/annotations?openlibrary_work=OL12345W")
assert response.status_code == 200
data = response.json()
assert len(data) == 2
assert all(ann["openlibrary_work"] == "OL12345W" for ann in data)
def test_get_annotations_by_openlibrary_edition():
"""Test filtering annotations by OpenLibrary edition ID"""
# Create test annotations with different edition IDs
annotation1 = {
"username": "alice",
"uri": "https://archive.org/details/item1",
"annotation": "http://example.com/annotation/1",
"openlibrary_edition": "OL67890M",
"private": False
}
annotation2 = {
"username": "bob",
"uri": "https://archive.org/details/item2",
"annotation": "http://example.com/annotation/2",
"openlibrary_edition": "OL11111M",
"private": False
}
client.post("/annotations", json=annotation1)
client.post("/annotations", json=annotation2)
# Filter by edition ID
response = client.get("/annotations?openlibrary_edition=OL67890M")
assert response.status_code == 200
data = response.json()
assert len(data) == 1
assert data[0]["openlibrary_edition"] == "OL67890M"
def test_get_annotations_by_username():
"""Test filtering annotations by username"""
# Create test annotations with different usernames
annotation1 = {
"username": "alice",
"uri": "https://archive.org/details/item1",
"annotation": "http://example.com/annotation/1",
"private": False
}
annotation2 = {
"username": "alice",
"uri": "https://archive.org/details/item2",
"annotation": "http://example.com/annotation/2",
"private": False
}
annotation3 = {
"username": "bob",
"uri": "https://archive.org/details/item3",
"annotation": "http://example.com/annotation/3",
"private": False
}
client.post("/annotations", json=annotation1)
client.post("/annotations", json=annotation2)
client.post("/annotations", json=annotation3)
# Filter by username
response = client.get("/annotations?username=alice")
assert response.status_code == 200
data = response.json()
assert len(data) == 2
assert all(ann["username"] == "alice" for ann in data)
def test_get_annotations_with_multiple_filters():
"""Test filtering annotations with multiple query parameters"""
# Create test annotations
annotation1 = {
"username": "alice",
"uri": "https://archive.org/details/item123",
"annotation": "http://example.com/annotation/1",
"openlibrary_work": "OL12345W",
"private": False
}
annotation2 = {
"username": "alice",
"uri": "https://archive.org/details/item456",
"annotation": "http://example.com/annotation/2",
"openlibrary_work": "OL12345W",
"private": False
}
annotation3 = {
"username": "bob",
"uri": "https://archive.org/details/item123",
"annotation": "http://example.com/annotation/3",
"openlibrary_work": "OL12345W",
"private": False
}
client.post("/annotations", json=annotation1)
client.post("/annotations", json=annotation2)
client.post("/annotations", json=annotation3)
# Filter by multiple parameters
response = client.get("/annotations?username=alice&openlibrary_work=OL12345W")
assert response.status_code == 200
data = response.json()
assert len(data) == 2
assert all(ann["username"] == "alice" and ann["openlibrary_work"] == "OL12345W" for ann in data)
def test_create_annotation_minimal_fields():
"""Test creating annotation with only required fields"""
annotation_data = {
"username": "minimaluser",
"uri": "https://archive.org/details/minimal",
"annotation": "http://example.com/annotation/minimal"
}
response = client.post("/annotations", json=annotation_data)
assert response.status_code == 200
data = response.json()
assert data["username"] == "minimaluser"
assert data["openlibrary_work"] is None
assert data["openlibrary_edition"] is None
assert data["comment"] is None
assert data["private"] is False
def test_empty_result_set():
"""Test that querying with no matching results returns empty list"""
response = client.get("/annotations?username=nonexistent")
assert response.status_code == 200
data = response.json()
assert data == []