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
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from sqlalchemy import Column

Check warning on line 1 in src/core/tasks/url/operators/validate/queries/ctes/consensus/impl/name.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] src/core/tasks/url/operators/validate/queries/ctes/consensus/impl/name.py#L1 <100>

Missing docstring in public module
Raw output
./src/core/tasks/url/operators/validate/queries/ctes/consensus/impl/name.py:1:1: D100 Missing docstring in public module

from src.core.tasks.url.operators.validate.queries.ctes.consensus.base import ValidationCTEContainer
from src.core.tasks.url.operators.validate.queries.ctes.consensus.helper import build_validation_query
from src.core.tasks.url.operators.validate.queries.ctes.counts.impl.name import NAME_VALIDATION_COUNTS_CTE
from src.core.tasks.url.operators.validate.queries.ctes.scored import ScoredCTEContainer


class NameValidationCTEContainer(ValidationCTEContainer):

Check warning on line 9 in src/core/tasks/url/operators/validate/queries/ctes/consensus/impl/name.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] src/core/tasks/url/operators/validate/queries/ctes/consensus/impl/name.py#L9 <101>

Missing docstring in public class
Raw output
./src/core/tasks/url/operators/validate/queries/ctes/consensus/impl/name.py:9:1: D101 Missing docstring in public class

def __init__(self):

Check warning on line 11 in src/core/tasks/url/operators/validate/queries/ctes/consensus/impl/name.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] src/core/tasks/url/operators/validate/queries/ctes/consensus/impl/name.py#L11 <107>

Missing docstring in __init__
Raw output
./src/core/tasks/url/operators/validate/queries/ctes/consensus/impl/name.py:11:1: D107 Missing docstring in __init__
_scored = ScoredCTEContainer(
NAME_VALIDATION_COUNTS_CTE
)

self._query = build_validation_query(
_scored,
"name"
)

@property
def name(self) -> Column[int]:

Check warning on line 22 in src/core/tasks/url/operators/validate/queries/ctes/consensus/impl/name.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] src/core/tasks/url/operators/validate/queries/ctes/consensus/impl/name.py#L22 <102>

Missing docstring in public method
Raw output
./src/core/tasks/url/operators/validate/queries/ctes/consensus/impl/name.py:22:1: D102 Missing docstring in public method
return self._query.c.name

Check warning on line 23 in src/core/tasks/url/operators/validate/queries/ctes/consensus/impl/name.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] src/core/tasks/url/operators/validate/queries/ctes/consensus/impl/name.py#L23 <292>

no newline at end of file
Raw output
./src/core/tasks/url/operators/validate/queries/ctes/consensus/impl/name.py:23:34: W292 no newline at end of file
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from sqlalchemy import select, func

Check warning on line 1 in src/core/tasks/url/operators/validate/queries/ctes/counts/impl/name.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] src/core/tasks/url/operators/validate/queries/ctes/counts/impl/name.py#L1 <100>

Missing docstring in public module
Raw output
./src/core/tasks/url/operators/validate/queries/ctes/counts/impl/name.py:1:1: D100 Missing docstring in public module

from src.core.tasks.url.operators.validate.queries.ctes.counts.core import ValidatedCountsCTEContainer
from src.db.models.impl.link.user_name_suggestion.sqlalchemy import LinkUserNameSuggestion
from src.db.models.impl.url.suggestion.name.sqlalchemy import URLNameSuggestion
from src.db.models.views.unvalidated_url import UnvalidatedURL

NAME_VALIDATION_COUNTS_CTE = ValidatedCountsCTEContainer(
(
select(
URLNameSuggestion.url_id,
URLNameSuggestion.suggestion.label("entity"),
func.count().label("votes")
)
.join(
UnvalidatedURL,
URLNameSuggestion.url_id == UnvalidatedURL.url_id
)
.join(
LinkUserNameSuggestion,
LinkUserNameSuggestion.suggestion_id == URLNameSuggestion.id
)
.group_by(
URLNameSuggestion.url_id,
URLNameSuggestion.suggestion
)
).cte("counts_name")
)

Check warning on line 28 in src/core/tasks/url/operators/validate/queries/ctes/counts/impl/name.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] src/core/tasks/url/operators/validate/queries/ctes/counts/impl/name.py#L28 <292>

no newline at end of file
Raw output
./src/core/tasks/url/operators/validate/queries/ctes/counts/impl/name.py:28:2: W292 no newline at end of file
8 changes: 8 additions & 0 deletions src/core/tasks/url/operators/validate/queries/get/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from src.core.exceptions import FailedValidationException
from src.core.tasks.url.operators.validate.queries.ctes.consensus.impl.agency import AgencyValidationCTEContainer
from src.core.tasks.url.operators.validate.queries.ctes.consensus.impl.location import LocationValidationCTEContainer
from src.core.tasks.url.operators.validate.queries.ctes.consensus.impl.name import NameValidationCTEContainer
from src.core.tasks.url.operators.validate.queries.ctes.consensus.impl.record_type import \
RecordTypeValidationCTEContainer
from src.core.tasks.url.operators.validate.queries.ctes.consensus.impl.url_type import URLTypeValidationCTEContainer
Expand All @@ -24,6 +25,7 @@ async def run(self, session: AsyncSession) -> list[GetURLsForAutoValidationRespo
location = LocationValidationCTEContainer()
url_type = URLTypeValidationCTEContainer()
record_type = RecordTypeValidationCTEContainer()
name = NameValidationCTEContainer()

query = (
select(
Expand All @@ -32,6 +34,7 @@ async def run(self, session: AsyncSession) -> list[GetURLsForAutoValidationRespo
agency.agency_id,
url_type.url_type,
record_type.record_type,
name.name,
)
.outerjoin(
agency.query,
Expand All @@ -49,13 +52,18 @@ async def run(self, session: AsyncSession) -> list[GetURLsForAutoValidationRespo
record_type.query,
URL.id == record_type.url_id,
)
.outerjoin(
name.query,
URL.id == name.url_id,
)
)
query = add_where_condition(
query,
agency=agency,
location=location,
url_type=url_type,
record_type=record_type,
name=name,
)

mappings: Sequence[RowMapping] = await sh.mappings(session, query=query)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class GetURLsForAutoValidationResponse(BaseModel):
agency_id: int | None
url_type: URLType
record_type: RecordType | None
name: str | None

@model_validator(mode="after")
def forbid_record_type_if_not_data_source(self):
Expand Down
6 changes: 5 additions & 1 deletion src/core/tasks/url/operators/validate/queries/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from src.core.tasks.url.operators.validate.queries.ctes.consensus.impl.agency import AgencyValidationCTEContainer
from src.core.tasks.url.operators.validate.queries.ctes.consensus.impl.location import LocationValidationCTEContainer
from src.core.tasks.url.operators.validate.queries.ctes.consensus.impl.name import NameValidationCTEContainer
from src.core.tasks.url.operators.validate.queries.ctes.consensus.impl.record_type import \
RecordTypeValidationCTEContainer
from src.core.tasks.url.operators.validate.queries.ctes.consensus.impl.url_type import URLTypeValidationCTEContainer
Expand All @@ -13,7 +14,8 @@ def add_where_condition(
agency: AgencyValidationCTEContainer,
location: LocationValidationCTEContainer,
url_type: URLTypeValidationCTEContainer,
record_type: RecordTypeValidationCTEContainer
record_type: RecordTypeValidationCTEContainer,
name: NameValidationCTEContainer,
) -> Select:
return (
query
Expand All @@ -25,13 +27,15 @@ def add_where_condition(
agency.agency_id.isnot(None),
location.location_id.isnot(None),
record_type.record_type.isnot(None),
name.name.isnot(None),
),
and_(
url_type.url_type.in_(
(URLType.META_URL.value, URLType.INDIVIDUAL_RECORD.value)
),
agency.agency_id.isnot(None),
location.location_id.isnot(None),
name.name.isnot(None),
),
url_type.url_type == URLType.NOT_RELEVANT.value
),
Expand Down
26 changes: 26 additions & 0 deletions src/core/tasks/url/operators/validate/queries/insert.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
from typing import Any

from sqlalchemy import update, case
from sqlalchemy.ext.asyncio import AsyncSession

from src.core.tasks.url.operators.validate.queries.get.models.response import GetURLsForAutoValidationResponse
from src.db.models.impl.flag.auto_validated.pydantic import FlagURLAutoValidatedPydantic
from src.db.models.impl.flag.url_validated.pydantic import FlagURLValidatedPydantic
from src.db.models.impl.link.url_agency.pydantic import LinkURLAgencyPydantic
from src.db.models.impl.url.core.pydantic.upsert import URLUpsertModel

Check warning on line 10 in src/core/tasks/url/operators/validate/queries/insert.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] src/core/tasks/url/operators/validate/queries/insert.py#L10 <401>

'src.db.models.impl.url.core.pydantic.upsert.URLUpsertModel' imported but unused
Raw output
./src/core/tasks/url/operators/validate/queries/insert.py:10:1: F401 'src.db.models.impl.url.core.pydantic.upsert.URLUpsertModel' imported but unused
from src.db.models.impl.url.core.sqlalchemy import URL
from src.db.models.impl.url.record_type.pydantic import URLRecordTypePydantic
from src.db.queries.base.builder import QueryBuilderBase
from src.db.helpers.session import session_helper as sh
Expand Down Expand Up @@ -56,4 +59,27 @@
]:
await sh.bulk_insert(session, models=inserts)

await self.update_urls(session)


async def update_urls(self, session: AsyncSession) -> Any:

Check warning on line 65 in src/core/tasks/url/operators/validate/queries/insert.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] src/core/tasks/url/operators/validate/queries/insert.py#L65 <102>

Missing docstring in public method
Raw output
./src/core/tasks/url/operators/validate/queries/insert.py:65:1: D102 Missing docstring in public method

Check failure on line 65 in src/core/tasks/url/operators/validate/queries/insert.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] src/core/tasks/url/operators/validate/queries/insert.py#L65 <303>

too many blank lines (2)
Raw output
./src/core/tasks/url/operators/validate/queries/insert.py:65:5: E303 too many blank lines (2)
id_to_name: dict[int, str] = {}
for response in self._responses:
if response.name is not None:
id_to_name[response.url_id] = response.name

if len(id_to_name) == 0:
return

stmt = (
update(URL)
.where(URL.id.in_(id_to_name.keys()))
.values(
name=case(
{id_: val for id_, val in id_to_name.items()},
value=URL.id
)
)
)

await session.execute(stmt)
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from src.core.tasks.url.operators.validate.queries.ctes.consensus.impl.agency import AgencyValidationCTEContainer
from src.core.tasks.url.operators.validate.queries.ctes.consensus.impl.location import LocationValidationCTEContainer
from src.core.tasks.url.operators.validate.queries.ctes.consensus.impl.name import NameValidationCTEContainer
from src.core.tasks.url.operators.validate.queries.ctes.consensus.impl.record_type import \
RecordTypeValidationCTEContainer
from src.core.tasks.url.operators.validate.queries.ctes.consensus.impl.url_type import URLTypeValidationCTEContainer
Expand All @@ -25,6 +26,7 @@ async def run(self, session: AsyncSession) -> bool:
location = LocationValidationCTEContainer()
url_type = URLTypeValidationCTEContainer()
record_type = RecordTypeValidationCTEContainer()
name = NameValidationCTEContainer()


query = (
Expand All @@ -50,13 +52,18 @@ async def run(self, session: AsyncSession) -> bool:
record_type.query,
UnvalidatedURL.url_id == record_type.url_id,
)
.outerjoin(
name.query,
UnvalidatedURL.url_id == name.url_id,
)
)
query = add_where_condition(
query,
agency=agency,
location=location,
url_type=url_type,
record_type=record_type,
name=name,
).limit(1)

return await sh.results_exist(session, query=query)
Expand Down
18 changes: 18 additions & 0 deletions src/db/models/impl/url/core/pydantic/upsert.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from src.db.models.impl.url.core.sqlalchemy import URL

Check warning on line 1 in src/db/models/impl/url/core/pydantic/upsert.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] src/db/models/impl/url/core/pydantic/upsert.py#L1 <100>

Missing docstring in public module
Raw output
./src/db/models/impl/url/core/pydantic/upsert.py:1:1: D100 Missing docstring in public module
from src.db.models.templates_.base import Base
from src.db.templates.markers.bulk.upsert import BulkUpsertableModel


class URLUpsertModel(BulkUpsertableModel):

Check warning on line 6 in src/db/models/impl/url/core/pydantic/upsert.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] src/db/models/impl/url/core/pydantic/upsert.py#L6 <101>

Missing docstring in public class
Raw output
./src/db/models/impl/url/core/pydantic/upsert.py:6:1: D101 Missing docstring in public class

@classmethod
def id_field(cls) -> str:

Check warning on line 9 in src/db/models/impl/url/core/pydantic/upsert.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] src/db/models/impl/url/core/pydantic/upsert.py#L9 <102>

Missing docstring in public method
Raw output
./src/db/models/impl/url/core/pydantic/upsert.py:9:1: D102 Missing docstring in public method
return "id"

@classmethod
def sa_model(cls) -> type[Base]:
"""Defines the SQLAlchemy model."""
return URL

id: int
name: str | None
27 changes: 26 additions & 1 deletion tests/automated/integration/tasks/url/impl/validate/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
from src.db.models.impl.flag.url_validated.enums import URLType
from src.db.models.impl.flag.url_validated.sqlalchemy import FlagURLValidated
from src.db.models.impl.link.url_agency.sqlalchemy import LinkURLAgency
from src.db.models.impl.url.core.sqlalchemy import URL
from src.db.models.impl.url.record_type.sqlalchemy import URLRecordType
from src.db.models.impl.url.suggestion.name.enums import NameSuggestionSource
from tests.conftest import db_data_creator
from tests.helpers.counter import next_int
from tests.helpers.data_creator.core import DBDataCreator
Expand Down Expand Up @@ -117,4 +119,27 @@
url_id=self.url_id,
record_type=record_type,
user_id=next_int()
)
)

async def add_name_suggestion(

Check warning on line 124 in tests/automated/integration/tasks/url/impl/validate/helper.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] tests/automated/integration/tasks/url/impl/validate/helper.py#L124 <102>

Missing docstring in public method
Raw output
./tests/automated/integration/tasks/url/impl/validate/helper.py:124:1: D102 Missing docstring in public method
self,
count: int = 1,
) -> str:
name = f"Test Validate Task Name"

Check warning on line 128 in tests/automated/integration/tasks/url/impl/validate/helper.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] tests/automated/integration/tasks/url/impl/validate/helper.py#L128 <541>

f-string is missing placeholders
Raw output
./tests/automated/integration/tasks/url/impl/validate/helper.py:128:16: F541 f-string is missing placeholders
suggestion_id: int = await self.db_data_creator.name_suggestion(
url_id=self.url_id,
source=NameSuggestionSource.USER,
name=name,
)
for i in range(count):
await self.db_data_creator.user_name_endorsement(
suggestion_id=suggestion_id,
user_id=next_int(),
)
return name

async def check_name(self) -> None:

Check warning on line 141 in tests/automated/integration/tasks/url/impl/validate/helper.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] tests/automated/integration/tasks/url/impl/validate/helper.py#L141 <102>

Missing docstring in public method
Raw output
./tests/automated/integration/tasks/url/impl/validate/helper.py:141:1: D102 Missing docstring in public method
urls: list[URL] = await self.adb_client.get_all(URL)
assert len(urls) == 1
url: URL = urls[0]
assert url.name == "Test Validate Task Name"

Check warning on line 145 in tests/automated/integration/tasks/url/impl/validate/helper.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] tests/automated/integration/tasks/url/impl/validate/helper.py#L145 <292>

no newline at end of file
Raw output
./tests/automated/integration/tasks/url/impl/validate/helper.py:145:53: W292 no newline at end of file
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ async def test_data_source(

await helper.add_record_type_suggestions(count=2)

assert not await operator.meets_task_prerequisites()

await helper.add_name_suggestion(count=2)

assert await operator.meets_task_prerequisites()

# Add different record type suggestion
Expand All @@ -59,4 +63,5 @@ async def test_data_source(
await helper.check_auto_validated()
await helper.check_agency_linked()
await helper.check_record_type()
await helper.check_name()

Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ async def test_individual_record(

await helper.add_location_suggestions(count=2)

assert not await operator.meets_task_prerequisites()

await helper.add_name_suggestion(count=2)

assert await operator.meets_task_prerequisites()

# Add additional agency suggestions to create tie
Expand All @@ -50,4 +54,5 @@ async def test_individual_record(
await helper.check_url_validated(URLType.INDIVIDUAL_RECORD)
await helper.check_auto_validated()
await helper.check_agency_linked()
await helper.check_name()

Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ async def test_meta_url(
# Add two location suggestions
await helper.add_location_suggestions(count=2)

assert not await operator.meets_task_prerequisites()

await helper.add_name_suggestion(count=2)

# Assert operator now meets task prerequisites
assert await operator.meets_task_prerequisites()

Expand All @@ -58,3 +62,4 @@ async def test_meta_url(
await helper.check_url_validated(URLType.META_URL)
await helper.check_auto_validated()
await helper.check_agency_linked()
await helper.check_name()
17 changes: 16 additions & 1 deletion tests/helpers/data_creator/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from src.db.models.impl.link.agency_location.sqlalchemy import LinkAgencyLocation
from src.db.models.impl.link.url_agency.sqlalchemy import LinkURLAgency
from src.db.models.impl.link.urls_root_url.sqlalchemy import LinkURLRootURL
from src.db.models.impl.link.user_name_suggestion.sqlalchemy import LinkUserNameSuggestion
from src.db.models.impl.url.core.enums import URLSource
from src.db.models.impl.url.error_info.pydantic import URLErrorInfoPydantic
from src.db.models.impl.url.html.compressed.sqlalchemy import URLCompressedHTML
Expand Down Expand Up @@ -684,10 +685,24 @@
self,
url_id: int,
source: NameSuggestionSource = NameSuggestionSource.HTML_METADATA_TITLE,
name: str | None = None,
) -> int:
if name is None:
name = f"Test Name {next_int()}"
suggestion = URLNameSuggestion(
url_id=url_id,
source=source,
suggestion=f"Test Name {next_int()}",
suggestion=name,
)
return await self.adb_client.add(suggestion, return_id=True)

async def user_name_endorsement(

Check warning on line 699 in tests/helpers/data_creator/core.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] tests/helpers/data_creator/core.py#L699 <102>

Missing docstring in public method
Raw output
./tests/helpers/data_creator/core.py:699:1: D102 Missing docstring in public method
self,
suggestion_id: int,
user_id: int,
):
link = LinkUserNameSuggestion(
suggestion_id=suggestion_id,
user_id=user_id,
)
await self.adb_client.add(link)