Skip to content

Commit 8b7637f

Browse files
committed
Tests take into account new document CRUD interface
1 parent 3ad53f0 commit 8b7637f

File tree

4 files changed

+90
-45
lines changed

4 files changed

+90
-45
lines changed

backend/app/tests/crud/documents/test_delete.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import pytest
22
from sqlmodel import Session, select
3+
from sqlalchemy.exc import NoResultFound
34

45
from app.crud import DocumentCrud
56
from app.models import Document
@@ -11,8 +12,8 @@ def document(db: Session):
1112
store = DocumentStore(db)
1213
document = store.put()
1314

14-
crud = DocumentCrud(db)
15-
crud.delete(document.id, document.owner_id)
15+
crud = DocumentCrud(db, document.owner_id)
16+
crud.delete(document.id)
1617

1718
statement = (
1819
select(Document)
@@ -29,3 +30,12 @@ def test_delete_marks_deleted(self, document: Document):
2930

3031
def test_delete_follows_insert(self, document: Document):
3132
assert document.created_at <= document.deleted_at
33+
34+
def test_cannot_delete_others_documents(self, db: Session):
35+
store = DocumentStore(db)
36+
document = store.put()
37+
other_owner_id = store.documents.index.peek()
38+
39+
crud = DocumentCrud(db, other_owner_id)
40+
with pytest.raises(NoResultFound):
41+
crud.delete(document.id)

backend/app/tests/crud/documents/test_read_many.py

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,7 @@
33

44
from app.crud import DocumentCrud
55

6-
from app.tests.utils.document import (
7-
DocumentStore,
8-
int_to_uuid,
9-
)
10-
11-
@pytest.fixture
12-
def crud(db: Session):
13-
return DocumentCrud(db)
6+
from app.tests.utils.document import DocumentStore, DocumentIndexGenerator
147

158
@pytest.fixture
169
def store(db: Session):
@@ -25,68 +18,76 @@ class TestDatabaseReadMany:
2518

2619
def test_number_read_is_expected(
2720
self,
28-
crud: DocumentCrud,
21+
db: Session,
2922
store: DocumentStore,
3023
):
31-
docs = crud.read_many(store.owner)
24+
crud = DocumentCrud(db, store.owner)
25+
docs = crud.read_many()
3226
assert len(docs) == self._ndocs
3327

3428
def test_deleted_docs_are_excluded(
3529
self,
36-
crud: DocumentCrud,
30+
db: Session,
3731
store: DocumentStore,
3832
):
39-
assert all(x.deleted_at is None for x in crud.read_many(store.owner))
33+
crud = DocumentCrud(db, store.owner)
34+
assert all(x.deleted_at is None for x in crud.read_many())
4035

4136
def test_skip_is_respected(
4237
self,
43-
crud: DocumentCrud,
38+
db: Session,
4439
store: DocumentStore,
4540
):
41+
crud = DocumentCrud(db, store.owner)
4642
skip = self._ndocs // 2
47-
doc_ids = set(x.id for x in crud.read_many(store.owner, skip=skip))
43+
doc_ids = set(x.id for x in crud.read_many(skip=skip))
44+
index = DocumentIndexGenerator(skip)
4845

49-
for i in range(skip, self._ndocs):
50-
doc = int_to_uuid(i) # see DocumentMaker
46+
for (_, doc) in zip(range(skip, self._ndocs), index):
5147
assert doc in doc_ids
5248

5349
def test_zero_skip_includes_all(
5450
self,
55-
crud: DocumentCrud,
51+
db: Session,
5652
store: DocumentStore,
5753
):
58-
docs = crud.read_many(store.owner, skip=0)
54+
crud = DocumentCrud(db, store.owner)
55+
docs = crud.read_many(skip=0)
5956
assert len(docs) == self._ndocs
6057

6158
def test_negative_skip_raises_exception(
6259
self,
63-
crud: DocumentCrud,
60+
db: Session,
6461
store: DocumentStore,
6562
):
63+
crud = DocumentCrud(db, store.owner)
6664
with pytest.raises(ValueError):
67-
crud.read_many(store.owner, skip=-1)
65+
crud.read_many(skip=-1)
6866

6967
def test_limit_is_respected(
7068
self,
71-
crud: DocumentCrud,
69+
db: Session,
7270
store: DocumentStore,
7371
):
72+
crud = DocumentCrud(db, store.owner)
7473
limit = self._ndocs // 2
75-
docs = crud.read_many(store.owner, limit=limit)
74+
docs = crud.read_many(limit=limit)
7675

7776
assert len(docs) == limit
7877

7978
def test_zero_limit_includes_nothing(
8079
self,
81-
crud: DocumentCrud,
80+
db: Session,
8281
store: DocumentStore,
8382
):
84-
assert not crud.read_many(store.owner, limit=0)
83+
crud = DocumentCrud(db, store.owner)
84+
assert not crud.read_many(limit=0)
8585

8686
def test_negative_limit_raises_exception(
8787
self,
88-
crud: DocumentCrud,
88+
db: Session,
8989
store: DocumentStore,
9090
):
91+
crud = DocumentCrud(db, store.owner)
9192
with pytest.raises(ValueError):
92-
crud.read_many(store.owner, limit=-1)
93+
crud.read_many(limit=-1)

backend/app/tests/crud/documents/test_read_one.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,26 @@ class TestDatabaseReadOne:
1414
def test_can_select_valid_id(self, db: Session, store: DocumentStore):
1515
document = store.put()
1616

17-
crud = DocumentCrud(db)
17+
crud = DocumentCrud(db, store.owner)
1818
result = crud.read_one(document.id)
1919

2020
assert result.id == document.id
2121

2222
def test_cannot_select_invalid_id(self, db: Session, store: DocumentStore):
23-
document = next(store.maker)
23+
document = next(store.documents)
2424

25-
crud = DocumentCrud(db)
25+
crud = DocumentCrud(db, store.owner)
26+
with pytest.raises(NoResultFound):
27+
crud.read_one(document.id)
28+
29+
def test_cannot_read_others_documents(
30+
self,
31+
db: Session,
32+
store: DocumentStore,
33+
):
34+
document = store.put()
35+
other = DocumentStore(db)
36+
37+
crud = DocumentCrud(db, other.owner)
2638
with pytest.raises(NoResultFound):
2739
crud.read_one(document.id)

backend/app/tests/crud/documents/test_update.py

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,36 +8,58 @@
88
@pytest.fixture
99
def documents(db: Session):
1010
store = DocumentStore(db)
11-
return store.maker
12-
13-
@pytest.fixture
14-
def crud(db: Session):
15-
return DocumentCrud(db)
11+
return store.documents
1612

1713
class TestDatabaseUpdate:
18-
def test_update_adds_one(
19-
self,
20-
crud: DocumentCrud,
21-
documents: DocumentMaker,
22-
):
23-
before = crud.read_many(documents.owner_id)
14+
def test_update_adds_one(self, db: Session, documents: DocumentMaker):
15+
crud = DocumentCrud(db, documents.owner_id)
16+
17+
before = crud.read_many()
2418
crud.update(next(documents))
25-
after = crud.read_many(documents.owner_id)
19+
after = crud.read_many()
2620

2721
assert len(before) + 1 == len(after)
2822

2923
def test_sequential_update_is_ordered(
3024
self,
31-
crud: DocumentCrud,
25+
db: Session,
3226
documents: DocumentMaker,
3327
):
28+
crud = DocumentCrud(db, documents.owner_id)
3429
(a, b) = (crud.update(y) for (_, y) in zip(range(2), documents))
30+
3531
assert a.created_at <= b.created_at
3632

3733
def test_insert_does_not_delete(
3834
self,
39-
crud: DocumentCrud,
35+
db: Session,
4036
documents: DocumentMaker,
4137
):
38+
crud = DocumentCrud(db, documents.owner_id)
4239
document = crud.update(next(documents))
40+
4341
assert document.deleted_at is None
42+
43+
def test_update_sets_default_owner(
44+
self,
45+
db: Session,
46+
documents: DocumentMaker,
47+
):
48+
crud = DocumentCrud(db, documents.owner_id)
49+
document = next(documents)
50+
document.owner_id = None
51+
result = crud.update(document)
52+
53+
assert result.owner_id == document.owner_id
54+
55+
def test_update_respects_owner(
56+
self,
57+
db: Session,
58+
documents: DocumentMaker,
59+
):
60+
document = next(documents)
61+
document.owner_id = documents.index.peek()
62+
63+
crud = DocumentCrud(db, documents.owner_id)
64+
with pytest.raises(PermissionError):
65+
crud.update(document)

0 commit comments

Comments
 (0)