-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Merge Software by ID * Merge Data * Light Refactor * Test * Fix IDs * Tags * error * Rigor * Test Data * Organize * Test Fail
- Loading branch information
1 parent
c02979c
commit edd703e
Showing
8 changed files
with
152 additions
and
11 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
"""Update Data""" | ||
|
||
from fetch_data.update_data.merge_software import merge_software_by_id |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
"""Merge Software""" | ||
|
||
from sqlalchemy import Select, Update, and_, delete, select, update | ||
from data.database_connection import get_async_session | ||
from model.database import ( | ||
WikibaseSoftwareVersionModel, | ||
) | ||
from model.database import WikibaseSoftwareModel | ||
from model.database.wikibase_software.software_tag_xref_model import ( | ||
software_tag_xref_table, | ||
) | ||
|
||
|
||
async def merge_software_by_id(base_id: int, additional_id: int) -> bool: | ||
"""Merge Software by ID""" | ||
|
||
software_query = get_select_software_query([base_id, additional_id]) | ||
update_software_version_query = get_update_software_version_query( | ||
base_id, additional_id | ||
) | ||
update_software_tags_query = get_update_software_tags_query(base_id, additional_id) | ||
delete_additional_tags_query = software_tag_xref_table.delete().where( | ||
software_tag_xref_table.c.wikibase_software_id == additional_id | ||
) | ||
delete_software_query = delete(WikibaseSoftwareModel).where( | ||
WikibaseSoftwareModel.id == additional_id | ||
) | ||
|
||
async with get_async_session() as async_session: | ||
software_list = (await async_session.scalars(software_query)).all() | ||
assert len({s.software_type for s in software_list}) == 1 | ||
|
||
await async_session.execute(update_software_version_query) | ||
await async_session.execute(update_software_tags_query) | ||
await async_session.execute(delete_additional_tags_query) | ||
await async_session.flush() | ||
|
||
await async_session.execute(delete_software_query) | ||
await async_session.commit() | ||
|
||
async with get_async_session() as async_session: | ||
remaining = (await async_session.scalars(software_query)).all() | ||
return len(remaining) == 1 | ||
|
||
|
||
def get_select_software_query(id_list: list[int]) -> Select[WikibaseSoftwareModel]: | ||
"""Select WikibaseSoftwareModel in ID list""" | ||
|
||
software_query = select(WikibaseSoftwareModel).where( | ||
WikibaseSoftwareModel.id.in_(id_list) | ||
) | ||
|
||
return software_query | ||
|
||
|
||
def get_update_software_tags_query(base_id: int, additional_id: int) -> Update: | ||
"""Add Additional Software Tags to Base""" | ||
|
||
update_software_tags_query = software_tag_xref_table.insert().from_select( | ||
[ | ||
software_tag_xref_table.c.wikibase_software_id, | ||
software_tag_xref_table.c.wikibase_software_tag_id, | ||
], | ||
select(base_id, software_tag_xref_table.c.wikibase_software_tag_id).where( | ||
and_( | ||
software_tag_xref_table.c.wikibase_software_id == additional_id, | ||
software_tag_xref_table.c.wikibase_software_tag_id.not_in( | ||
select(software_tag_xref_table.c.wikibase_software_tag_id).where( | ||
software_tag_xref_table.c.wikibase_software_id == base_id | ||
) | ||
), | ||
) | ||
), | ||
) | ||
|
||
return update_software_tags_query | ||
|
||
|
||
def get_update_software_version_query(base_id: int, additional_id: int) -> Update: | ||
"""Update Software Version from Additional ID to Base ID""" | ||
|
||
update_software_version_query = ( | ||
update(WikibaseSoftwareVersionModel) | ||
.where(WikibaseSoftwareVersionModel.software_id == additional_id) | ||
.values(software_id=base_id) | ||
) | ||
|
||
return update_software_version_query |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
"""Test Merge Software""" | ||
|
||
import pytest | ||
|
||
from tests.test_schema import test_schema | ||
|
||
|
||
MERGE_SOFTWARE_QUERY = """ | ||
mutation MyMutation($baseId: Int!, $additionalId: Int!) { | ||
mergeSoftwareById(baseId: $baseId, additionalId: $additionalId) | ||
}""" | ||
|
||
|
||
@pytest.mark.asyncio | ||
@pytest.mark.mutation | ||
@pytest.mark.dependency(name="merge-software-by-id") | ||
async def test_merge_software_by_id_mutation(): | ||
"""Test Add Wikibase""" | ||
|
||
result = await test_schema.execute( | ||
MERGE_SOFTWARE_QUERY, variable_values={"baseId": 1, "additionalId": 3} | ||
) | ||
assert result.errors is None | ||
assert result.data is not None | ||
assert result.data.get("mergeSoftwareById") | ||
|
||
|
||
@pytest.mark.asyncio | ||
@pytest.mark.mutation | ||
@pytest.mark.dependency( | ||
depends=["software-version-success"], | ||
name="merge-software-by-id-fail", | ||
scope="session", | ||
) | ||
async def test_merge_software_by_id_fail_mutation(): | ||
"""Test Add Wikibase""" | ||
|
||
result = await test_schema.execute( | ||
MERGE_SOFTWARE_QUERY, variable_values={"baseId": 1, "additionalId": 4} | ||
) | ||
assert result.errors is not None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters