Skip to content

Commit

Permalink
Support timezone when CUPS is from Canary Islands (#28)
Browse files Browse the repository at this point in the history
* Fix canary timezon and add tests

* Add GitHub Actions workflow to run tests

* Fix unittest run

* Update dependencies

* Publish unittest results

* Fix publish test results

---------

Co-authored-by: xavier <[email protected]>
  • Loading branch information
oriolpiera and xavierteres authored Oct 24, 2023
1 parent f0b42e2 commit 251bad7
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 4 deletions.
Binary file added .coverage
Binary file not shown.
49 changes: 49 additions & 0 deletions .github/workflows/python-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
name: Python tests
on:
pull_request:
branches: [ master ]

concurrency:
group: ${{ github.ref }}
cancel-in-progress: true

jobs:
build:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
python-version: [ "2.7" ]
steps:
- uses: actions/checkout@v3
with:
ref: ${{ github.head_ref }}
- name: Set up Python ${{ matrix.python-version }}
if: matrix.python-version != '2.7'
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Set up Python 2.7
if: matrix.python-version == '2.7'
run: |
sudo apt-get update
sudo apt-get install -y python2.7 python2.7-dev
sudo ln -sf python2.7 /usr/bin/python
curl https://bootstrap.pypa.io/pip/2.7/get-pip.py -o get-pip.py
python get-pip.py
rm get-pip.py
pip install --upgrade pip setuptools wheel
- name: Install dependencies
run: |
cd ${{github.workspace}}
pip install -e .
pip install unittest-xml-reporting
- name: Run Tests
run: |
cd ${{github.workspace}}
python -m xmlrunner tests -o test-reports
- name: Publish Unit Test Results
if: (success() || failure()) && matrix.python-version == '2.7' && github.event_name == 'pull_request'
uses: EnricoMi/publish-unit-test-result-action@v1
with:
files: ${{github.workspace}}/test-reports/*.xml
6 changes: 4 additions & 2 deletions cchloader/backends/timescaledb.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
import psycopg2.extras
import pytz

def get_as_utc_timestamp(t, season=None):
def get_as_utc_timestamp(t, cups, season=None):
timezone_utc = pytz.timezone("UTC")
timezone_local = pytz.timezone("Europe/Madrid")
if cups[0:7] == 'ES00316':
timezone_local = pytz.timezone("Atlantic/Canary")
is_dst = season==1
return timezone_utc.normalize(timezone_local.localize(t, is_dst=is_dst))

Expand Down Expand Up @@ -59,7 +61,7 @@ def insert_cch_batch(self, collection, curves):
'update_at': datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
'create_date': datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
'create_uid': 1,
'utc_timestamp': get_as_utc_timestamp(curve['datetime'], curve.get('season')).strftime('%Y-%m-%d %H:%M:%S')
'utc_timestamp': get_as_utc_timestamp(curve['datetime'], curve['name'], curve.get('season')).strftime('%Y-%m-%d %H:%M:%S')
})

if collection != "tg_cchval":
Expand Down
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ pymongo<3.0
osconf
marshmallow>=2.13.5
click
pytz
psycopg2-binary
6 changes: 4 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
"pymongo<3.0",
"osconf",
"marshmallow>=2.13.5",
"click"
"click",
"pytz",
"psycopg2-binary"
],
test_suite='',
test_suite='tests',
)
29 changes: 29 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import unittest
from datetime import datetime
from pytz import timezone

class CchloaderBackendTimescaleTest(unittest.TestCase):

def test__get_as_utc_timestamp__peninsula(self):
from cchloader.backends.timescaledb import get_as_utc_timestamp

result_winter = get_as_utc_timestamp(datetime(2023,1,1), 'ES123456748798', 0)
result_summer = get_as_utc_timestamp(datetime(2023,10,1), 'ES123456748798', 1)

self.assertEqual(datetime(2022, 12, 31, 23, 0, tzinfo=timezone('utc')), result_winter)
self.assertEqual(datetime(2023, 9, 30, 22, 0, tzinfo=timezone('utc')), result_summer)

def test__get_as_utc_timestamp__canary_islands(self):
from cchloader.backends.timescaledb import get_as_utc_timestamp

result_winter = get_as_utc_timestamp(datetime(2023,1,1), 'ES0031656748798', 0)
result_summer = get_as_utc_timestamp(datetime(2023,10,2), 'ES0031656748798', 1)

self.assertEqual(datetime(2023, 1, 1, 0, 0, tzinfo=timezone('utc')), result_winter)
self.assertEqual(datetime(2023, 10, 1, 23, 0, tzinfo=timezone('utc')), result_summer)

if __name__ == '__main__':
unittest.main()

0 comments on commit 251bad7

Please sign in to comment.