Coverage for /usr/lib/python3.10/site-packages/hyd/backend/tag/service.py: 27%

30 statements  

« prev     ^ index     » next       coverage.py v7.0.3, created at 2023-02-05 02:26 +0000

1from sqlalchemy.orm import Session 

2 

3import hyd.backend.project.service as project_service 

4from hyd.backend.exc import PrimaryTagError, UnknownTagError 

5from hyd.backend.tag.models import PrimaryTagEntry, TagEntry 

6from hyd.backend.util.models import NameStr, PrimaryKey 

7 

8 

9def create_tag_entry(project_id: PrimaryKey, tag: NameStr, primary: bool, db: Session) -> TagEntry: 

10 

11 _ = project_service.read_project(project_id=project_id, db=db) 

12 

13 if primary and db.query(PrimaryTagEntry).get(project_id): 

14 raise PrimaryTagError 

15 

16 tag_entry = TagEntry(project_id=project_id, tag=tag, primary=primary) 

17 db.add(tag_entry) 

18 db.commit() 

19 

20 if primary: 

21 primary_tag_entry = PrimaryTagEntry(project_id=project_id, primary_tag=tag) 

22 db.add(primary_tag_entry) 

23 db.commit() 

24 

25 return tag_entry 

26 

27 

28def read_tag_entry(project_id: PrimaryKey, tag: NameStr, db: Session) -> TagEntry: 

29 project_entry = project_service.read_project(project_id=project_id, db=db) 

30 

31 tag_entries: list[TagEntry] = project_entry.tag_entries 

32 for tag_entry in tag_entries: 

33 if tag_entry.tag == tag: 

34 return tag_entry 

35 

36 raise UnknownTagError 

37 

38 

39def delete_tag_entry_by_ref(tag_entry: TagEntry, db: Session) -> None: 

40 if tag_entry.primary: 

41 primary_tag_entry: PrimaryTagEntry = db.query(PrimaryTagEntry).get(tag_entry.project_id) 

42 db.delete(primary_tag_entry) 

43 

44 db.delete(tag_entry) 

45 db.commit()