mirror of
https://github.com/fhswf/aki_prj23_transparenzregister.git
synced 2025-04-25 00:02:34 +02:00
270 lines
8.6 KiB
Python
270 lines
8.6 KiB
Python
"""Tests for the sql entities."""
|
|
import datetime
|
|
from typing import Any
|
|
|
|
import pytest
|
|
import sqlalchemy as sa
|
|
from sqlalchemy.exc import IntegrityError
|
|
from sqlalchemy.orm import Session
|
|
|
|
from aki_prj23_transparenzregister.utils.sql import entities
|
|
|
|
|
|
def test_import() -> None: #
|
|
"""Tests if the entities can be imported."""
|
|
assert entities
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"company_data",
|
|
[
|
|
# Test case 1: All coordinates are set or all are null (Expect failure)
|
|
{
|
|
"hr": "HR124",
|
|
"court_id": 2,
|
|
"name": "Invalid Company",
|
|
"street": "456 Elm St",
|
|
"zip_code": "54321",
|
|
"city": "Invalid City",
|
|
"longitude": 1.23,
|
|
"latitude": None,
|
|
"pos_accuracy": 0.1,
|
|
"last_update": datetime.date.fromisoformat("2023-10-02"),
|
|
"sector": "Tech",
|
|
},
|
|
# Test case 2: Longitude must not be null if latitude is set (Expect failure)
|
|
{
|
|
"hr": "HR124",
|
|
"court_id": 2,
|
|
"name": "Invalid Company",
|
|
"street": "456 Elm St",
|
|
"zip_code": "54321",
|
|
"city": "Invalid City",
|
|
"longitude": None,
|
|
"latitude": 4.56,
|
|
"pos_accuracy": 0.1,
|
|
"last_update": datetime.date.fromisoformat("2023-10-02"),
|
|
"sector": "Tech",
|
|
},
|
|
# Test case 3: Unique name and city constraint (Expect failure)
|
|
{
|
|
"hr": "HR124",
|
|
"court_id": 2,
|
|
"name": "Unique Company",
|
|
"street": "456 Elm St",
|
|
"zip_code": "54321",
|
|
"city": "City A",
|
|
"longitude": 1.23,
|
|
"latitude": None,
|
|
"pos_accuracy": 0.1,
|
|
"last_update": datetime.date.fromisoformat("2023-10-02"),
|
|
"sector": "Tech",
|
|
},
|
|
{
|
|
"hr": "HR125",
|
|
"court_id": 3,
|
|
"name": "Unique Company",
|
|
"street": "789 Oak St",
|
|
"zip_code": "98765",
|
|
"city": "City B",
|
|
"longitude": 2.34,
|
|
"latitude": 5.67,
|
|
"pos_accuracy": None,
|
|
"last_update": datetime.date.fromisoformat("2023-10-03"),
|
|
"sector": "Tech",
|
|
},
|
|
],
|
|
)
|
|
def test_constraint_enforcement(
|
|
empty_db: sa.orm.Session, company_data: dict[str, Any]
|
|
) -> None:
|
|
"""Tests if the constraint around longitude and latitude passes."""
|
|
company = entities.Company(**company_data)
|
|
empty_db.add(company)
|
|
with pytest.raises(IntegrityError):
|
|
empty_db.commit()
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("company_data", "expect_failure"),
|
|
[
|
|
# Test case 1: Valid longitude and latitude values (Expect success)
|
|
(
|
|
{
|
|
"hr": "HR124",
|
|
"court_id": 2,
|
|
"name": "Valid Company",
|
|
"street": "456 Elm St",
|
|
"zip_code": "54321",
|
|
"city": "Valid City",
|
|
"longitude": 45.0,
|
|
"latitude": -20.0,
|
|
"pos_accuracy": 0.1,
|
|
"last_update": datetime.date.fromisoformat("2023-10-03"),
|
|
"sector": "Tech",
|
|
},
|
|
False,
|
|
),
|
|
# Test case 2: Longitude out of range (Expect failure)
|
|
(
|
|
{
|
|
"hr": "HR124",
|
|
"court_id": 2,
|
|
"name": "Invalid Longitude",
|
|
"street": "456 Elm St",
|
|
"zip_code": "54321",
|
|
"city": "Invalid City",
|
|
"longitude": 200.0, # Out of valid range
|
|
"latitude": 30.0,
|
|
"pos_accuracy": 0.1,
|
|
"last_update": datetime.date.fromisoformat("2023-10-02"),
|
|
"sector": "Tech",
|
|
},
|
|
True,
|
|
),
|
|
# Test case 3: Latitude out of range (Expect failure)
|
|
(
|
|
{
|
|
"hr": "HR124",
|
|
"court_id": 2,
|
|
"name": "Invalid Latitude",
|
|
"street": "456 Elm St",
|
|
"zip_code": "54321",
|
|
"city": "Invalid City",
|
|
"longitude": 45.0,
|
|
"latitude": -100.0, # Out of valid range
|
|
"pos_accuracy": 0.1,
|
|
"last_update": datetime.date.fromisoformat("2023-10-02"),
|
|
"sector": "Tech",
|
|
},
|
|
True,
|
|
),
|
|
],
|
|
)
|
|
def test_coordinate_range_constraints(
|
|
empty_db: sa.orm.Session, company_data: dict[str, Any], expect_failure: bool
|
|
) -> None:
|
|
company = entities.Company(**company_data)
|
|
empty_db.add(company)
|
|
if expect_failure:
|
|
with pytest.raises(IntegrityError):
|
|
empty_db.commit()
|
|
else:
|
|
empty_db.commit()
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("company_data", "expect_failure"),
|
|
[
|
|
# Test case 1: Valid data with all columns set (Expect success)
|
|
(
|
|
{
|
|
"hr": "HR124",
|
|
"court_id": 2,
|
|
"name": "Valid Company",
|
|
"street": "456 Elm St",
|
|
"zip_code": "54321",
|
|
"city": "Valid City",
|
|
"longitude": 45.0,
|
|
"latitude": -20.0,
|
|
"pos_accuracy": 0.1,
|
|
"capital_value": 1000000.0,
|
|
"original_currency": "EUR",
|
|
"capital_type": "Equity",
|
|
"last_update": datetime.date.fromisoformat("2023-10-02"),
|
|
"sector": "Tech",
|
|
},
|
|
False,
|
|
),
|
|
# Test case 2: All capital-related columns are NULL (Expect success)
|
|
(
|
|
{
|
|
"hr": "HR125",
|
|
"court_id": 3,
|
|
"name": "No Capital Info",
|
|
"street": "789 Oak St",
|
|
"zip_code": "98765",
|
|
"city": "No Capital City",
|
|
"longitude": None,
|
|
"latitude": None,
|
|
"pos_accuracy": None,
|
|
"capital_value": None,
|
|
"original_currency": None,
|
|
"capital_type": None,
|
|
"last_update": datetime.date.fromisoformat("2023-10-03"),
|
|
"sector": "Tech",
|
|
},
|
|
False,
|
|
),
|
|
# Test case 3: Only some of the capital-related columns are set (Expect failure)
|
|
(
|
|
{
|
|
"hr": "HR126",
|
|
"court_id": 4,
|
|
"name": "Partial Capital Info",
|
|
"street": "101 Elm St",
|
|
"zip_code": "54321",
|
|
"city": "Partial Capital City",
|
|
"longitude": None,
|
|
"latitude": None,
|
|
"pos_accuracy": None,
|
|
"capital_value": 500000.0,
|
|
"original_currency": None, # Invalid because not all columns are set
|
|
"capital_type": None, # Invalid because not all columns are set
|
|
"last_update": datetime.date.fromisoformat("2023-10-04"),
|
|
"sector": "Finance",
|
|
},
|
|
True,
|
|
),
|
|
(
|
|
{
|
|
"hr": "HR126",
|
|
"court_id": 4,
|
|
"name": "Partial Capital Info",
|
|
"street": "101 Elm St",
|
|
"zip_code": "54321",
|
|
"city": "Partial Capital City",
|
|
"longitude": None,
|
|
"latitude": None,
|
|
"pos_accuracy": None,
|
|
"capital_value": 500000.0,
|
|
"original_currency": "DM",
|
|
"capital_type": None, # Invalid because not all columns are set
|
|
"last_update": datetime.date.fromisoformat("2023-10-04"),
|
|
"sector": "Finance",
|
|
},
|
|
True,
|
|
),
|
|
(
|
|
{
|
|
"hr": "HR126",
|
|
"court_id": 4,
|
|
"name": "Partial Capital Info",
|
|
"street": "101 Elm St",
|
|
"zip_code": "54321",
|
|
"city": "Partial Capital City",
|
|
"longitude": None,
|
|
"latitude": None,
|
|
"pos_accuracy": None,
|
|
"capital_value": None,
|
|
"original_currency": "EUR", # Invalid because not all columns are set
|
|
"capital_type": "Hafteinlage", # Invalid because not all columns are set
|
|
"last_update": datetime.date.fromisoformat("2023-10-04"),
|
|
"sector": "Finance",
|
|
},
|
|
True,
|
|
),
|
|
],
|
|
)
|
|
def test_capital_columns_constraints(
|
|
empty_db: Session, company_data: dict[str, Any], expect_failure: bool
|
|
) -> None:
|
|
company = entities.Company(**company_data)
|
|
empty_db.add(company)
|
|
|
|
if expect_failure:
|
|
with pytest.raises(IntegrityError):
|
|
empty_db.commit()
|
|
else:
|
|
empty_db.commit()
|