Skip to content

Commit

Permalink
refactor(studies-db): change signature of get method in `StudyMetad…
Browse files Browse the repository at this point in the history
…ataRepository` class

Replace `id` parameter with `study_id`.
  • Loading branch information
laurent-laporte-pro committed Feb 16, 2024
1 parent 596c486 commit 1ce2beb
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 23 deletions.
6 changes: 2 additions & 4 deletions antarest/study/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,21 +138,19 @@ def save(
def refresh(self, metadata: Study) -> None:
self.session.refresh(metadata)

def get(self, id: str) -> t.Optional[Study]:
def get(self, study_id: str) -> t.Optional[Study]:
"""Get the study by ID or return `None` if not found in database."""
# todo: I think we should use a `entity = with_polymorphic(Study, "*")`
# to make sure RawStudy and VariantStudy fields are also fetched.
# see: antarest.study.service.StudyService.delete_study
# When we fetch a study, we also need to fetch the associated owner and groups
# to check the permissions of the current user efficiently.
study: Study = (
# fmt: off
self.session.query(Study)
.options(joinedload(Study.owner))
.options(joinedload(Study.groups))
.options(joinedload(Study.tags))
.get(id)
# fmt: on
.get(study_id)
)
return study

Expand Down
42 changes: 23 additions & 19 deletions tests/storage/repository/test_study.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
from datetime import datetime

from sqlalchemy.orm import Session # type: ignore

from antarest.core.cache.business.local_chache import LocalCache
from antarest.core.model import PublicMode
from antarest.login.model import Group, User
from antarest.study.model import DEFAULT_WORKSPACE_NAME, RawStudy, Study, StudyContentStatus
from antarest.study.repository import StudyMetadataRepository
from antarest.study.storage.variantstudy.model.dbmodel import VariantStudy
from tests.helpers import with_db_context


@with_db_context
def test_lifecycle() -> None:
user = User(id=0, name="admin")
def test_lifecycle(db_session: Session) -> None:
repo = StudyMetadataRepository(LocalCache(), session=db_session)

user = User(id=1, name="admin")
group = Group(id="my-group", name="group")
repo = StudyMetadataRepository(LocalCache())

a = Study(
name="a",
version="42",
version="820",
author="John Smith",
created_at=datetime.utcnow(),
updated_at=datetime.utcnow(),
Expand All @@ -26,7 +28,7 @@ def test_lifecycle() -> None:
)
b = RawStudy(
name="b",
version="43",
version="830",
author="Morpheus",
created_at=datetime.utcnow(),
updated_at=datetime.utcnow(),
Expand All @@ -36,7 +38,7 @@ def test_lifecycle() -> None:
)
c = RawStudy(
name="c",
version="43",
version="830",
author="Trinity",
created_at=datetime.utcnow(),
updated_at=datetime.utcnow(),
Expand All @@ -47,7 +49,7 @@ def test_lifecycle() -> None:
)
d = VariantStudy(
name="d",
version="43",
version="830",
author="Mr. Anderson",
created_at=datetime.utcnow(),
updated_at=datetime.utcnow(),
Expand All @@ -57,30 +59,32 @@ def test_lifecycle() -> None:
)

a = repo.save(a)
b = repo.save(b)
a_id = a.id

repo.save(b)
repo.save(c)
repo.save(d)
assert b.id
c = repo.one(a.id)
assert a == c

c = repo.one(a_id)
assert a_id == c.id

assert len(repo.get_all()) == 4
assert len(repo.get_all_raw(exists=True)) == 1
assert len(repo.get_all_raw(exists=False)) == 1
assert len(repo.get_all_raw()) == 2

repo.delete(a.id)
assert repo.get(a.id) is None
repo.delete(a_id)
assert repo.get(a_id) is None


def test_study_inheritance(db_session: Session) -> None:
repo = StudyMetadataRepository(LocalCache(), session=db_session)

@with_db_context
def test_study_inheritance() -> None:
user = User(id=0, name="admin")
group = Group(id="my-group", name="group")
repo = StudyMetadataRepository(LocalCache())
a = RawStudy(
name="a",
version="42",
version="820",
author="John Smith",
created_at=datetime.utcnow(),
updated_at=datetime.utcnow(),
Expand Down

0 comments on commit 1ce2beb

Please sign in to comment.