Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 25 additions & 11 deletions core/AsyncCore.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import Optional

from pydantic import BaseModel
from sqlalchemy.exc import IntegrityError

from collector_db.AsyncDatabaseClient import AsyncDatabaseClient
from collector_db.DTOs.BatchInfo import BatchInfo
Expand All @@ -27,7 +28,8 @@
from core.DTOs.MessageResponse import MessageResponse
from core.DTOs.SearchURLResponse import SearchURLResponse
from core.TaskManager import TaskManager
from core.enums import BatchStatus, RecordType
from core.classes.ErrorManager import ErrorManager
from core.enums import BatchStatus, RecordType, AnnotationType

from security_manager.SecurityManager import AccessInfo

Expand Down Expand Up @@ -149,11 +151,17 @@
url_id: int,
relevant: bool
):
return await self.adb_client.add_user_relevant_suggestion(
user_id=user_id,
url_id=url_id,
relevant=relevant
)
try:
return await self.adb_client.add_user_relevant_suggestion(
user_id=user_id,
url_id=url_id,
relevant=relevant
)
except IntegrityError as e:

Check warning on line 160 in core/AsyncCore.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] core/AsyncCore.py#L160 <841>

local variable 'e' is assigned to but never used
Raw output
./core/AsyncCore.py:160:9: F841 local variable 'e' is assigned to but never used
return await ErrorManager.raise_annotation_exists_error(
annotation_type=AnnotationType.RELEVANCE,
url_id=url_id
)

async def get_next_url_for_relevance_annotation(
self,
Expand Down Expand Up @@ -187,11 +195,17 @@
url_id: int,
record_type: RecordType,
):
await self.adb_client.add_user_record_type_suggestion(
user_id=user_id,
url_id=url_id,
record_type=record_type
)
try:
return await self.adb_client.add_user_record_type_suggestion(
user_id=user_id,
url_id=url_id,
record_type=record_type
)
except IntegrityError as e:

Check warning on line 204 in core/AsyncCore.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] core/AsyncCore.py#L204 <841>

local variable 'e' is assigned to but never used
Raw output
./core/AsyncCore.py:204:9: F841 local variable 'e' is assigned to but never used
return await ErrorManager.raise_annotation_exists_error(
annotation_type=AnnotationType.RECORD_TYPE,
url_id=url_id
)


async def get_next_url_agency_for_annotation(
Expand Down
44 changes: 44 additions & 0 deletions core/classes/ErrorManager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from enum import Enum

Check warning on line 1 in core/classes/ErrorManager.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] core/classes/ErrorManager.py#L1 <100>

Missing docstring in public module
Raw output
./core/classes/ErrorManager.py:1:1: D100 Missing docstring in public module
from http import HTTPStatus

from fastapi import HTTPException
from pydantic import BaseModel

from core.enums import AnnotationType


class ErrorTypes(Enum):

Check warning on line 10 in core/classes/ErrorManager.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] core/classes/ErrorManager.py#L10 <101>

Missing docstring in public class
Raw output
./core/classes/ErrorManager.py:10:1: D101 Missing docstring in public class
ANNOTATION_EXISTS = "ANNOTATION_EXISTS"

class ErrorFormat(BaseModel):

Check warning on line 13 in core/classes/ErrorManager.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] core/classes/ErrorManager.py#L13 <101>

Missing docstring in public class
Raw output
./core/classes/ErrorManager.py:13:1: D101 Missing docstring in public class
code: ErrorTypes
message: str


class ErrorManager:

Check warning on line 18 in core/classes/ErrorManager.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] core/classes/ErrorManager.py#L18 <101>

Missing docstring in public class
Raw output
./core/classes/ErrorManager.py:18:1: D101 Missing docstring in public class

@staticmethod
async def raise_error(

Check warning on line 21 in core/classes/ErrorManager.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] core/classes/ErrorManager.py#L21 <102>

Missing docstring in public method
Raw output
./core/classes/ErrorManager.py:21:1: D102 Missing docstring in public method
error_type: ErrorTypes,
message: str,
status_code: HTTPStatus = HTTPStatus.BAD_REQUEST
):
raise HTTPException(
status_code=status_code,
detail=ErrorFormat(
code=error_type,
message=message
).model_dump(mode='json')
)

@staticmethod
async def raise_annotation_exists_error(

Check warning on line 35 in core/classes/ErrorManager.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] core/classes/ErrorManager.py#L35 <102>

Missing docstring in public method
Raw output
./core/classes/ErrorManager.py:35:1: D102 Missing docstring in public method
annotation_type: AnnotationType,
url_id: int
):
await ErrorManager.raise_error(
error_type=ErrorTypes.ANNOTATION_EXISTS,
message=f"Annotation of type {annotation_type.value} already exists"
f" for url {url_id}",
status_code=HTTPStatus.CONFLICT
)
5 changes: 5 additions & 0 deletions core/enums.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
from enum import Enum

class AnnotationType(Enum):

Check warning on line 3 in core/enums.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] core/enums.py#L3 <101>

Missing docstring in public class
Raw output
./core/enums.py:3:1: D101 Missing docstring in public class
RELEVANCE = "RELEVANCE"
RECORD_TYPE = "RECORD_TYPE"
AGENCY = "AGENCY"


class BatchStatus(Enum):
READY_TO_LABEL = "ready to label"
Expand Down
4 changes: 4 additions & 0 deletions core/helpers.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
from http import HTTPStatus

Check warning on line 1 in core/helpers.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] core/helpers.py#L1 <100>

Missing docstring in public module
Raw output
./core/helpers.py:1:1: D100 Missing docstring in public module

Check warning on line 1 in core/helpers.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] core/helpers.py#L1 <401>

'http.HTTPStatus' imported but unused
Raw output
./core/helpers.py:1:1: F401 'http.HTTPStatus' imported but unused

from fastapi import HTTPException

Check warning on line 3 in core/helpers.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] core/helpers.py#L3 <401>

'fastapi.HTTPException' imported but unused
Raw output
./core/helpers.py:3:1: F401 'fastapi.HTTPException' imported but unused

from core.DTOs.URLAgencySuggestionInfo import URLAgencySuggestionInfo
from core.enums import SuggestionType
from core.exceptions import MatchAgencyError
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ def post_record_type_annotation_and_get_next(
url_id: int,
record_type_annotation_post_info: RecordTypeAnnotationPostInfo
) -> GetNextRecordTypeAnnotationResponseOuterInfo:
data = self.post(
data = self.post_v2(
url=f"/annotate/record-type/{url_id}",
json=record_type_annotation_post_info.model_dump(mode='json')
)
Expand All @@ -257,7 +257,7 @@ def post_relevance_annotation_and_get_next(
url_id: int,
relevance_annotation_post_info: RelevanceAnnotationPostInfo
) -> GetNextRelevanceAnnotationResponseOuterInfo:
data = self.post(
data = self.post_v2(
url=f"/annotate/relevance/{url_id}",
json=relevance_annotation_post_info.model_dump(mode='json')
)
Expand Down
62 changes: 62 additions & 0 deletions tests/test_automated/integration/api/test_annotate.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from http import HTTPStatus

import pytest
from fastapi import HTTPException

from collector_db.DTOs.InsertURLsInfo import InsertURLsInfo
from collector_db.DTOs.URLMapping import URLMapping
Expand All @@ -11,6 +12,7 @@
from core.DTOs.GetNextURLForAgencyAnnotationResponse import URLAgencyAnnotationPostInfo
from core.DTOs.RecordTypeAnnotationPostInfo import RecordTypeAnnotationPostInfo
from core.DTOs.RelevanceAnnotationPostInfo import RelevanceAnnotationPostInfo
from core.classes.ErrorManager import ErrorTypes
from core.enums import RecordType, SuggestionType
from core.exceptions import FailedValidationException
from tests.helpers.complex_test_data_functions import AnnotateAgencySetupInfo, setup_for_annotate_agency, \
Expand Down Expand Up @@ -130,6 +132,36 @@
assert results[0].relevant is True


@pytest.mark.asyncio
async def test_annotate_relevancy_already_annotated_by_different_user(

Check warning on line 136 in tests/test_automated/integration/api/test_annotate.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] tests/test_automated/integration/api/test_annotate.py#L136 <103>

Missing docstring in public function
Raw output
./tests/test_automated/integration/api/test_annotate.py:136:1: D103 Missing docstring in public function
api_test_helper
):
ath = api_test_helper

creation_info: BatchURLCreationInfo = await ath.db_data_creator.batch_and_urls(
url_count=1
)

await ath.db_data_creator.user_relevant_suggestion(
url_id=creation_info.url_ids[0],
user_id=2,
relevant=True
)

# Annotate with different user (default is 1) and get conflict error
try:
response = await ath.request_validator.post_relevance_annotation_and_get_next(

Check warning on line 153 in tests/test_automated/integration/api/test_annotate.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] tests/test_automated/integration/api/test_annotate.py#L153 <841>

local variable 'response' is assigned to but never used
Raw output
./tests/test_automated/integration/api/test_annotate.py:153:9: F841 local variable 'response' is assigned to but never used
url_id=creation_info.url_ids[0],
relevance_annotation_post_info=RelevanceAnnotationPostInfo(
is_relevant=False
)
)
except HTTPException as e:
assert e.status_code == HTTPStatus.CONFLICT
assert e.detail["detail"]["code"] == ErrorTypes.ANNOTATION_EXISTS.value
assert e.detail["detail"]["message"] == f"Annotation of type RELEVANCE already exists for url {creation_info.url_ids[0]}"


@pytest.mark.asyncio
async def test_annotate_relevancy_no_html(api_test_helper):
ath = api_test_helper
Expand Down Expand Up @@ -250,6 +282,36 @@
if result.url_id == inner_info_1.url_info.url_id:
assert result.record_type == RecordType.BOOKING_REPORTS.value

@pytest.mark.asyncio
async def test_annotate_record_type_already_annotated_by_different_user(

Check warning on line 286 in tests/test_automated/integration/api/test_annotate.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] tests/test_automated/integration/api/test_annotate.py#L286 <103>

Missing docstring in public function
Raw output
./tests/test_automated/integration/api/test_annotate.py:286:1: D103 Missing docstring in public function
api_test_helper
):
ath = api_test_helper

creation_info: BatchURLCreationInfo = await ath.db_data_creator.batch_and_urls(
url_count=1
)

await ath.db_data_creator.user_record_type_suggestion(
url_id=creation_info.url_ids[0],
user_id=2,
record_type=RecordType.ACCIDENT_REPORTS
)

# Annotate with different user (default is 1) and get conflict error
try:
response = await ath.request_validator.post_record_type_annotation_and_get_next(

Check warning on line 303 in tests/test_automated/integration/api/test_annotate.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] tests/test_automated/integration/api/test_annotate.py#L303 <841>

local variable 'response' is assigned to but never used
Raw output
./tests/test_automated/integration/api/test_annotate.py:303:9: F841 local variable 'response' is assigned to but never used
url_id=creation_info.url_ids[0],
record_type_annotation_post_info=RecordTypeAnnotationPostInfo(
record_type=RecordType.ANNUAL_AND_MONTHLY_REPORTS
)
)
except HTTPException as e:
assert e.status_code == HTTPStatus.CONFLICT
assert e.detail["detail"]["code"] == ErrorTypes.ANNOTATION_EXISTS.value
assert e.detail["detail"]["message"] == f"Annotation of type RECORD_TYPE already exists for url {creation_info.url_ids[0]}"


@pytest.mark.asyncio
async def test_annotate_record_type_no_html_info(api_test_helper):
ath = api_test_helper
Expand Down