Skip to content

Commit 7761ecd

Browse files
committed
Move document creation into a class
1 parent 8ad1ec8 commit 7761ecd

File tree

2 files changed

+44
-44
lines changed

2 files changed

+44
-44
lines changed

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

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,11 @@
1111
from app.crud.user import get_user_by_email
1212
from app.models import Document, UserCreate
1313

14-
class Constants:
15-
n_documents = 10
16-
1714
@ft.cache
1815
def get_user_id_by_email(session: Session):
1916
user = get_user_by_email(session=session, email=settings.FIRST_SUPERUSER)
2017
return user.id
2118

22-
2319
@ft.cache
2420
def int_to_uuid(value):
2521
return UUID(int=value)
@@ -28,31 +24,44 @@ def rm_documents(session: Session):
2824
session.exec(delete(Document))
2925
session.commit()
3026

31-
def mk_document(owner_id, index=0):
32-
doc_id = int_to_uuid(index)
33-
34-
args = str(doc_id).split('-')
35-
fname = Path('/', *args).with_suffix('.xyz')
36-
return Document(
37-
id=doc_id,
38-
owner_id=owner_id,
39-
fname=fname.name,
40-
object_store_url=fname.as_uri(),
41-
)
42-
4327
def insert_documents(session: Session, n: int):
44-
owner_id = get_user_id_by_email(session)
45-
4628
crud = DocumentCrud(session)
47-
for i in range(n):
48-
document = mk_document(owner_id, i)
29+
docs = DocumentMaker(session)
4930

50-
session.add(document)
31+
for (_, d) in zip(range(n), docs):
32+
session.add(d)
5133
session.commit()
52-
session.refresh(document)
53-
54-
yield document
34+
session.refresh(d)
35+
yield d
5536

5637
def insert_document(session: Session):
5738
(document, ) = insert_documents(session, 1)
5839
return document
40+
41+
class Constants:
42+
n_documents = 10
43+
44+
class DocumentMaker:
45+
def __init__(self, session: Session):
46+
self.owner_id = get_user_id_by_email(session)
47+
self.index = 0
48+
49+
def __iter__(self):
50+
return self
51+
52+
def __next__(self):
53+
doc_id = self.get_and_increment()
54+
args = str(doc_id).split('-')
55+
fname = Path('/', *args).with_suffix('.xyz')
56+
57+
return Document(
58+
id=doc_id,
59+
owner_id=self.owner_id,
60+
fname=fname.name,
61+
object_store_url=fname.as_uri(),
62+
)
63+
64+
def get_and_increment(self):
65+
doc_id = int_to_uuid(self.index)
66+
self.index += 1
67+
return doc_id
Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,44 @@
11
from uuid import UUID
22
from typing import ClassVar
3-
from dataclasses import dataclass
43

54
import pytest
65
from sqlmodel import Session
76

87
from app.crud import DocumentCrud
98

109
from _utils import (
11-
get_user_id_by_email,
12-
mk_document,
10+
DocumentMaker,
1311
rm_documents,
1412
)
1513

16-
@dataclass
17-
class State:
18-
crud: DocumentCrud
19-
owner_id: UUID
20-
_doc_id: ClassVar[int] = 0
14+
class TestState:
15+
def __init__(self, db: Session):
16+
self.crud = DocumentCrud(db)
17+
self.documents = DocumentMaker(db)
2118

2219
def add(self):
23-
document = mk_document(self.owner_id, self._doc_id)
24-
self._doc_id += 1
25-
return self.crud.update(document)
20+
return self.crud.update(next(self.documents))
2621

2722
def get(self):
28-
return self.crud.read_many(self.owner_id)
23+
return self.crud.read_many(self.documents.owner_id)
2924

3025
@pytest.fixture
3126
def state(db: Session):
3227
rm_documents(db)
33-
34-
crud = DocumentCrud(db)
35-
owner_id = get_user_id_by_email(db)
36-
37-
return State(crud, owner_id)
28+
return TestState(db)
3829

3930
class TestDatabaseUpdate:
40-
def test_update_adds_one(self, state: State):
31+
def test_update_adds_one(self, state: TestState):
4132
before = state.get()
4233
state.add()
4334
after = state.get()
4435

4536
assert len(before) + 1 == len(after)
4637

47-
def test_sequential_update_is_ordered(self, state: State):
38+
def test_sequential_update_is_ordered(self, state: TestState):
4839
(a, b) = (state.add() for _ in range(2))
4940
assert a.created_at <= b.created_at
5041

51-
def test_insert_does_not_delete(self, state: State):
42+
def test_insert_does_not_delete(self, state: TestState):
5243
document = state.add()
5344
assert document.deleted_at is None

0 commit comments

Comments
 (0)