Update tag service for resourceTag attachemnt and added tests

This commit is contained in:
pmoharana-cmd 2024-10-20 14:16:35 -04:00
parent 8532f8c4c1
commit b3db1144ca
4 changed files with 78 additions and 47 deletions

View File

@ -0,0 +1,7 @@
from .user_model import User
from .resource_model import Resource
from .service_model import Service
from .tag_model import Tag
from .enum_for_models import ProgramTypeEnum, UserTypeEnum
from .resource_tag_model import ResourceTag
from .service_tag_model import ServiceTag

View File

@ -1,15 +1,10 @@
from fastapi import Depends
from ..database import db_session
from sqlalchemy.orm import Session
from ..models.tag_model import Tag
from ..entities.tag_entity import TagEntity
from ..entities.resource_tag_entity import ResourceTagEntity
from ..entities.service_tag_entity import ServiceTagEntity
from .exceptions import ResourceNotFoundException
from ..models.user_model import User
from ..models.resource_model import Resource
from ..models.service_model import Service
from sqlalchemy import select
from ..entities import TagEntity, ResourceTagEntity, ServiceTagEntity
from ..models import User, Resource, Service, Tag, ResourceTag
from .exceptions import ResourceNotFoundException
class TagService:
@ -20,12 +15,12 @@ class TagService:
def all(self) -> list[Tag]:
"""Returns a list of all Tags"""
query = select(TagEntity)
query = select(TagEntity)
entities = self._session.scalars(query).all()
return [entity.to_model() for entity in entities]
def get_tag_by_id(self, id:int) -> Tag:
def get_tag_by_id(self, id: int) -> Tag:
"""Returns tag based on it's id."""
tag = self._session.query(TagEntity).filter(TagEntity.id == id).one_or_none()
@ -38,21 +33,15 @@ class TagService:
tag_entity = TagEntity.from_model(tag)
self._session.add(tag_entity)
self._session.commit
self._session.commit()
return tag_entity.to_model()
def delete(self, user: User, id:int) -> None:
def delete(self, user: User, id: int) -> None:
"""Method to delete a tag from the database, along with all connections."""
tag = (
self._session.query(TagEntity)
.filter(
TagEntity.id == id
)
.one_or_none()
)
tag = self._session.query(TagEntity).filter(TagEntity.id == id).one_or_none()
if tag is None:
raise ResourceNotFoundException(f"No tag found with matching id: {id}")
@ -60,20 +49,16 @@ class TagService:
resource_tags = (
self._session.query(ResourceTagEntity)
.filter(
ResourceTagEntity.tagId == id
)
.filter(ResourceTagEntity.tagId == id)
.all()
)
for tag in resource_tags:
self._session.delete(tag)
service_tags = (
self._session.query(ServiceTagEntity)
.filter(
ServiceTagEntity.tagId == id
)
.filter(ServiceTagEntity.tagId == id)
.all()
)
@ -81,42 +66,49 @@ class TagService:
self._session.delete(tag)
self._session.commit()
def get_tags_for_resource(self, user: User, resource: Resource) -> list[Tag]:
"""Get tags based on a resource."""
tags: list[Tag]
resource_tags = (
self._session.query(ResourceTagEntity)
.filter(
ResourceTagEntity.tagId == resource.id
)
.filter(ResourceTagEntity.tagId == resource.id)
.all()
)
if resource_tags is None:
raise ResourceNotFoundException(f"No tags found for resource with id: {resource.id}")
raise ResourceNotFoundException(
f"No tags found for resource with id: {resource.id}"
)
for tag in resource_tags:
tags.append(self.get_tag_by_id(tag.id))
return tags
def get_tags_for_service(self, user: User, service: Service) -> list[Tag]:
"""Get tags based on a resource."""
tags: list[Tag]
service_tags = (
self._session.query(ServiceTagEntity)
.filter(
ServiceTagEntity.tagId == service.id
)
.filter(ServiceTagEntity.tagId == service.id)
.all()
)
if service_tags is None:
raise ResourceNotFoundException(f"No tags found for service with id: {service.id}")
raise ResourceNotFoundException(
f"No tags found for service with id: {service.id}"
)
for tag in service_tags:
tags.append(self.get_tag_by_id(tag.id))
return tags
def add_tag_resource(self, user: User, tag: Tag, resource: Resource) -> None:
"""Adds a tag to a resource"""
resource_tag_entity = ResourceTagEntity.from_model(
ResourceTag(tag_id=tag.id, resource_id=resource.id)
)
self._session.add(resource_tag_entity)
self._session.commit()

View File

@ -2,13 +2,43 @@
# PyTest
import pytest
from ...services.tag import TagService
from .fixtures import tag_svc
from .tag_test_data import tag1, tag2, tag3
from . import tag_test_data
from ...services import TagService, ResourceService
from .fixtures import tag_svc, resource_svc
from .tag_test_data import tag_to_create, tag_to_create_no_id, tags
from .user_test_data import admin
def test_get_all(tag_svc: TagService):
"""Test that all tags can be retrieved."""
tags = tag_svc.all()
assert len(tags) == 3
all_tags = tag_svc.all()
assert len(all_tags) == len(tags)
def test_create_tag(tag_svc: TagService):
"""Test creation of tag"""
created_tag = tag_svc.create(admin, tag_to_create)
assert created_tag.content == tag_to_create.content
assert len(tag_svc.all()) == len(tags) + 1
def test_create_tag_no_id(tag_svc: TagService):
"""Test creation of tag"""
created_tag = tag_svc.create(admin, tag_to_create_no_id)
queried_tag = tag_svc.get_tag_by_id(4)
assert created_tag.content == tag_to_create_no_id.content
assert len(tag_svc.all()) == len(tags) + 1
assert queried_tag.content == created_tag.content
def test_resource_tag_creation(tag_svc: TagService, resource_svc: ResourceService):
"""Test creation and attachment of resource tag"""
resource = resource_svc.get_resource_by_user(admin)[0]
tag_svc.add_tag_resource(admin, tags[0], resource)
updated_resource = resource_svc.get_by_id(admin, resource.id)
assert len(resource.tags) == 0
assert len(updated_resource.tags) == 1
assert resource.id == updated_resource.id

View File

@ -11,7 +11,9 @@ tag2 = Tag(id=2, content="Tag 2", created_at=datetime.now())
tag3 = Tag(id=3, content="Tag 3", created_at=datetime.now())
tagToCreate = Tag(id=4, content="Tag 4", created_at=datetime.now())
tag_to_create = Tag(id=4, content="Tag 4", created_at=datetime.now())
tag_to_create_no_id = Tag(content="Tag 5", created_at=datetime.now())
tags = [tag1, tag2, tag3]