From 23716cbf323f8539692e48c00033bd4e7a36320a Mon Sep 17 00:00:00 2001 From: Pablo Alcaraz Date: Sat, 15 Nov 2025 23:39:29 -0800 Subject: [PATCH 1/3] Fix: Make reporting period tests year-independent and handle auto-created periods Replace hardcoded 2025 dates with dynamic current_year references and add cleanup logic to remove conflicting auto-created reporting periods before test execution. This prevents test failures when running in different calendar years or when entity initialization creates default reporting periods. --- tests/test_reporting_period.py | 64 ++++++++++++++++++++++++---------- 1 file changed, 46 insertions(+), 18 deletions(-) diff --git a/tests/test_reporting_period.py b/tests/test_reporting_period.py index 32dc8b7..d214a0e 100644 --- a/tests/test_reporting_period.py +++ b/tests/test_reporting_period.py @@ -127,50 +127,78 @@ def test_reporting_period_recycling(session, entity): def test_reporting_period_dates(session, entity): """Tests the calculation of reporting_period start and end dates""" - assert ReportingPeriod.date_year() == datetime.today().year + # Clean up the auto-created reporting period for the current year to start from a known state + current_year = datetime.today().year + existing_periods = session.scalars( + select(ReportingPeriod) + .where(ReportingPeriod.entity_id == entity.id) + .where(ReportingPeriod.calendar_year == current_year) + .execution_options(ignore_isolation=True) + ).all() + + for period in existing_periods: + entity.reporting_period_id = None + session.erase(period) + session.commit() + entity = session.get(Entity, entity.id) + + assert ReportingPeriod.date_year() == current_year assert ( - ReportingPeriod.date_year(datetime.strptime("2025-06-03", "%Y-%m-%d"), entity) - == 2025 + ReportingPeriod.date_year(datetime.strptime(f"{current_year}-06-03", "%Y-%m-%d"), entity) + == current_year ) new_reporting_period = ReportingPeriod( - calendar_year=2025, + calendar_year=current_year, period_count=2, entity_id=entity.id, ) session.add(new_reporting_period) session.commit() period_interval = new_reporting_period.interval( - datetime.strptime("2025-06-03", "%Y-%m-%d") + datetime.strptime(f"{current_year}-06-03", "%Y-%m-%d") ) - assert period_interval["start"] == datetime(2025, 1, 1, 0, 0, 0) - assert period_interval["end"] == datetime(2025, 12, 31, 23, 59, 59) + assert period_interval["start"] == datetime(current_year, 1, 1, 0, 0, 0) + assert period_interval["end"] == datetime(current_year, 12, 31, 23, 59, 59) entity.year_start = 4 assert ( - ReportingPeriod.date_year(datetime.strptime("2025-03-03", "%Y-%m-%d"), entity) - == 2024 + ReportingPeriod.date_year(datetime.strptime(f"{current_year}-03-03", "%Y-%m-%d"), entity) + == current_year - 1 ) period_interval = new_reporting_period.interval( - datetime.strptime("2025-03-03", "%Y-%m-%d") + datetime.strptime(f"{current_year}-03-03", "%Y-%m-%d") ) - assert period_interval["start"] == datetime(2024, 4, 1, 0, 0, 0) - assert period_interval["end"] == datetime(2025, 3, 31, 23, 59, 59) + assert period_interval["start"] == datetime(current_year - 1, 4, 1, 0, 0, 0) + assert period_interval["end"] == datetime(current_year, 3, 31, 23, 59, 59) def test_reporting_period_from_date(session, entity): """Tests the retrieval of the reporting_period for a given date""" - assert ( - ReportingPeriod.get_period(session, datetime.today()) == entity.reporting_period - ) + # Clean up the auto-created reporting period for the current year to start from a known state + current_year = datetime.today().year + existing_periods = session.scalars( + select(ReportingPeriod) + .where(ReportingPeriod.entity_id == entity.id) + .where(ReportingPeriod.calendar_year == current_year) + .execution_options(ignore_isolation=True) + ).all() + + for period in existing_periods: + entity.reporting_period_id = None + session.erase(period) + session.commit() + entity = session.get(Entity, entity.id) + + # Now the current year should not have a reporting period with pytest.raises(MissingReportingPeriodError): - ReportingPeriod.get_period(session, datetime.strptime("2025-03-03", "%Y-%m-%d")) + ReportingPeriod.get_period(session, datetime.strptime(f"{current_year}-03-03", "%Y-%m-%d")) new_reporting_period = ReportingPeriod( - calendar_year=2025, + calendar_year=current_year, period_count=2, entity_id=entity.id, ) @@ -178,6 +206,6 @@ def test_reporting_period_from_date(session, entity): session.commit() assert ( - ReportingPeriod.get_period(session, datetime.strptime("2025-03-03", "%Y-%m-%d")) + ReportingPeriod.get_period(session, datetime.strptime(f"{current_year}-03-03", "%Y-%m-%d")) == new_reporting_period ) From b92e73271b3de6505b1267e24618b7f048ceb2c5 Mon Sep 17 00:00:00 2001 From: Pablo Date: Sat, 15 Nov 2025 23:47:13 -0800 Subject: [PATCH 2/3] Add GitHub Actions workflow for Python app testing --- .github/workflows/test.yml | 39 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 .github/workflows/test.yml diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..4240edc --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,39 @@ +# This workflow will install Python dependencies, run tests and lint with a single version of Python +# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python + +name: Python application + +on: + push: + branches: [ "main" ] + pull_request: + branches: [ "main" ] + +permissions: + contents: read + +jobs: + build: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + - name: Set up Python 3.12 + uses: actions/setup-python@v3 + with: + python-version: "3.12" + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install flake8 pytest + if [ -f requirements.txt ]; then pip install -r requirements.txt; fi + - name: Lint with flake8 + run: | + # stop the build if there are Python syntax errors or undefined names + flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics + # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide + flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics + - name: Test with pytest + run: | + pytest From a4b554f32c0c35324dad22233728a89f1e8403eb Mon Sep 17 00:00:00 2001 From: Pablo Alcaraz Date: Sat, 15 Nov 2025 23:58:19 -0800 Subject: [PATCH 3/3] Add a test GHA --- .github/workflows/test.yml | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 4240edc..fb19b42 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -5,17 +5,19 @@ name: Python application on: push: - branches: [ "main" ] + branches-ignore: + - main pull_request: - branches: [ "main" ] + branches: + - main permissions: contents: read jobs: build: - runs-on: ubuntu-latest + timeout-minutes: 10 steps: - uses: actions/checkout@v4 @@ -25,15 +27,8 @@ jobs: python-version: "3.12" - name: Install dependencies run: | - python -m pip install --upgrade pip - pip install flake8 pytest - if [ -f requirements.txt ]; then pip install -r requirements.txt; fi - - name: Lint with flake8 - run: | - # stop the build if there are Python syntax errors or undefined names - flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics - # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide - flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics + pipx install poetry + poetry install - name: Test with pytest run: | - pytest + poetry run pytest -v