From fd2d1cd3b94bc435d3f1ef022728f9aa8fb5f7ba Mon Sep 17 00:00:00 2001 From: GabrielPalmar Date: Wed, 25 Jun 2025 12:07:30 -0500 Subject: [PATCH 1/4] chore(app): Added status field for avr temp --- app/opensense.py | 7 ++++++- version.txt | 2 +- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/app/opensense.py b/app/opensense.py index 66c4eb7..58bd9ce 100644 --- a/app/opensense.py +++ b/app/opensense.py @@ -35,4 +35,9 @@ def get_temperature(): total_sum = sum(temp_list) average = total_sum / len(temp_list) if temp_list else 0 - return f'Average temperature: {average:.2f} °C\n' + if average < 10: + return f'Average temperature: {average:.2f} °C (Warning: Too cold)\n' + elif average > 10 and average <= 36: + return f'Average temperature: {average:.2f} °C (Good)\n' + else: + return f'Average temperature: {average:.2f} °C (Warning: Too hot)\n' diff --git a/version.txt b/version.txt index 60a2d3e..44bb5d1 100644 --- a/version.txt +++ b/version.txt @@ -1 +1 @@ -0.4.0 \ No newline at end of file +0.4.1 \ No newline at end of file From 5282207e7e042c4c1b8936e1d41d0d3897ecd77d Mon Sep 17 00:00:00 2001 From: GabrielPalmar Date: Wed, 25 Jun 2025 12:23:01 -0500 Subject: [PATCH 2/4] fix(app): Resolving pylint and tests --- app/opensense.py | 5 +- tests/fixtures/vcr_cassettes/metrics.yaml | 139 +++++++++--------- tests/fixtures/vcr_cassettes/temperature.yaml | 8 +- tests/fixtures/vcr_cassettes/version.yaml | 6 +- 4 files changed, 78 insertions(+), 80 deletions(-) diff --git a/app/opensense.py b/app/opensense.py index 58bd9ce..c89cc56 100644 --- a/app/opensense.py +++ b/app/opensense.py @@ -37,7 +37,6 @@ def get_temperature(): if average < 10: return f'Average temperature: {average:.2f} °C (Warning: Too cold)\n' - elif average > 10 and average <= 36: + if 10 < average <= 36: return f'Average temperature: {average:.2f} °C (Good)\n' - else: - return f'Average temperature: {average:.2f} °C (Warning: Too hot)\n' + return f'Average temperature: {average:.2f} °C (Warning: Too hot)\n' diff --git a/tests/fixtures/vcr_cassettes/metrics.yaml b/tests/fixtures/vcr_cassettes/metrics.yaml index 46c13b7..65879d0 100644 --- a/tests/fixtures/vcr_cassettes/metrics.yaml +++ b/tests/fixtures/vcr_cassettes/metrics.yaml @@ -1,108 +1,107 @@ interactions: - - request: - body: null - headers: - Accept: - - "*/*" - Accept-Encoding: - - gzip, deflate - Connection: - - keep-alive - User-Agent: - - python-requests/2.31.0 - method: GET - uri: http://127.0.0.1:5000/metrics - response: - body: - string: - '# HELP python_gc_objects_collected_total Objects collected during gc +- request: + body: null + headers: + Accept: + - '*/*' + Accept-Encoding: + - gzip, deflate + Connection: + - keep-alive + User-Agent: + - python-requests/2.31.0 + method: GET + uri: http://127.0.0.1:5000/metrics + response: + body: + string: '# HELP python_gc_objects_collected_total Objects collected during gc - # TYPE python_gc_objects_collected_total counter + # TYPE python_gc_objects_collected_total counter - python_gc_objects_collected_total{generation="0"} 424.0 + python_gc_objects_collected_total{generation="0"} 352.0 - python_gc_objects_collected_total{generation="1"} 24.0 + python_gc_objects_collected_total{generation="1"} 25.0 - python_gc_objects_collected_total{generation="2"} 0.0 + python_gc_objects_collected_total{generation="2"} 0.0 - # HELP python_gc_objects_uncollectable_total Uncollectable objects found during - GC + # HELP python_gc_objects_uncollectable_total Uncollectable objects found during + GC - # TYPE python_gc_objects_uncollectable_total counter + # TYPE python_gc_objects_uncollectable_total counter - python_gc_objects_uncollectable_total{generation="0"} 0.0 + python_gc_objects_uncollectable_total{generation="0"} 0.0 - python_gc_objects_uncollectable_total{generation="1"} 0.0 + python_gc_objects_uncollectable_total{generation="1"} 0.0 - python_gc_objects_uncollectable_total{generation="2"} 0.0 + python_gc_objects_uncollectable_total{generation="2"} 0.0 - # HELP python_gc_collections_total Number of times this generation was collected + # HELP python_gc_collections_total Number of times this generation was collected - # TYPE python_gc_collections_total counter + # TYPE python_gc_collections_total counter - python_gc_collections_total{generation="0"} 33.0 + python_gc_collections_total{generation="0"} 30.0 - python_gc_collections_total{generation="1"} 2.0 + python_gc_collections_total{generation="1"} 2.0 - python_gc_collections_total{generation="2"} 0.0 + python_gc_collections_total{generation="2"} 0.0 - # HELP python_info Python platform information + # HELP python_info Python platform information - # TYPE python_info gauge + # TYPE python_info gauge - python_info{implementation="CPython",major="3",minor="13",patchlevel="1",version="3.13.1"} - 1.0 + python_info{implementation="CPython",major="3",minor="13",patchlevel="5",version="3.13.5"} + 1.0 - # HELP process_virtual_memory_bytes Virtual memory size in bytes. + # HELP process_virtual_memory_bytes Virtual memory size in bytes. - # TYPE process_virtual_memory_bytes gauge + # TYPE process_virtual_memory_bytes gauge - process_virtual_memory_bytes 4.6829568e+07 + process_virtual_memory_bytes 4.7783936e+07 - # HELP process_resident_memory_bytes Resident memory size in bytes. + # HELP process_resident_memory_bytes Resident memory size in bytes. - # TYPE process_resident_memory_bytes gauge + # TYPE process_resident_memory_bytes gauge - process_resident_memory_bytes 4.0833024e+07 + process_resident_memory_bytes 4.12672e+07 - # HELP process_start_time_seconds Start time of the process since unix epoch - in seconds. + # HELP process_start_time_seconds Start time of the process since unix epoch + in seconds. - # TYPE process_start_time_seconds gauge + # TYPE process_start_time_seconds gauge - process_start_time_seconds 1.74983991025e+09 + process_start_time_seconds 1.75087194069e+09 - # HELP process_cpu_seconds_total Total user and system CPU time spent in seconds. + # HELP process_cpu_seconds_total Total user and system CPU time spent in seconds. - # TYPE process_cpu_seconds_total counter + # TYPE process_cpu_seconds_total counter - process_cpu_seconds_total 3.46 + process_cpu_seconds_total 1.21 - # HELP process_open_fds Number of open file descriptors. + # HELP process_open_fds Number of open file descriptors. - # TYPE process_open_fds gauge + # TYPE process_open_fds gauge - process_open_fds 6.0 + process_open_fds 6.0 - # HELP process_max_fds Maximum number of open file descriptors. + # HELP process_max_fds Maximum number of open file descriptors. - # TYPE process_max_fds gauge + # TYPE process_max_fds gauge - process_max_fds 1.048576e+06 + process_max_fds 1.048576e+06 - ' - headers: - Connection: - - close - Content-Length: - - "1893" - Content-Type: - - text/plain; version=0.0.4; charset=utf-8; charset=utf-8 - Date: - - Fri, 13 Jun 2025 19:27:04 GMT - Server: - - Werkzeug/3.1.3 Python/3.13.1 - status: - code: 200 - message: OK + ' + headers: + Connection: + - close + Content-Length: + - '1891' + Content-Type: + - text/plain; version=0.0.4; charset=utf-8; charset=utf-8 + Date: + - Wed, 25 Jun 2025 17:21:04 GMT + Server: + - Werkzeug/3.1.3 Python/3.13.5 + status: + code: 200 + message: OK version: 1 diff --git a/tests/fixtures/vcr_cassettes/temperature.yaml b/tests/fixtures/vcr_cassettes/temperature.yaml index 7fb7ce2..e3ed554 100644 --- a/tests/fixtures/vcr_cassettes/temperature.yaml +++ b/tests/fixtures/vcr_cassettes/temperature.yaml @@ -14,18 +14,18 @@ interactions: uri: http://127.0.0.1:5000/temperature response: body: - string: "Average temperature: 8.06 \xB0C\n" + string: "Average temperature: 21.01 \xB0C (Good)\n" headers: Connection: - close Content-Length: - - '30' + - '38' Content-Type: - text/html; charset=utf-8 Date: - - Tue, 06 May 2025 18:04:57 GMT + - Wed, 25 Jun 2025 17:21:04 GMT Server: - - Werkzeug/3.0.1 Python/3.12.3 + - Werkzeug/3.1.3 Python/3.13.5 status: code: 200 message: OK diff --git a/tests/fixtures/vcr_cassettes/version.yaml b/tests/fixtures/vcr_cassettes/version.yaml index 53d41ac..1f8c5e8 100644 --- a/tests/fixtures/vcr_cassettes/version.yaml +++ b/tests/fixtures/vcr_cassettes/version.yaml @@ -14,7 +14,7 @@ interactions: uri: http://127.0.0.1:5000/version response: body: - string: 'Current app version: 0.2.2 + string: 'Current app version: 0.4.1 ' headers: @@ -25,9 +25,9 @@ interactions: Content-Type: - text/html; charset=utf-8 Date: - - Tue, 06 May 2025 18:01:25 GMT + - Wed, 25 Jun 2025 17:19:51 GMT Server: - - Werkzeug/3.0.1 Python/3.12.3 + - Werkzeug/3.1.3 Python/3.13.5 status: code: 200 message: OK From 429ccc8cc8966f1926d8a13727c13e6dae5ab07c Mon Sep 17 00:00:00 2001 From: GabrielPalmar Date: Wed, 25 Jun 2025 12:39:27 -0500 Subject: [PATCH 3/4] fix(ci): Resolving SQ coverage --- tests/test_modules.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/test_modules.py b/tests/test_modules.py index 8e68208..2d7e568 100644 --- a/tests/test_modules.py +++ b/tests/test_modules.py @@ -1,4 +1,5 @@ '''This module contains tests for the Flask and OpenSense modules.''' +import re from app.main import app from app import opensense @@ -29,4 +30,7 @@ def test_opensense_get_temperature(): """Test that opensense.get_temperature returns a string""" result = opensense.get_temperature() assert isinstance(result, str) - assert "Average temperature:" in result + assert re.match( + r"Average temperature: (\d+\.\d{2}) °C \((Warning: Too cold|Good|Warning: Too hot)\)", + result + ) From 4728beaf78462aa778111cdfa67bdbd15006773a Mon Sep 17 00:00:00 2001 From: GabrielPalmar Date: Wed, 25 Jun 2025 12:49:02 -0500 Subject: [PATCH 4/4] fix(ci): Trying mock --- tests/test_modules.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/tests/test_modules.py b/tests/test_modules.py index 2d7e568..f915c06 100644 --- a/tests/test_modules.py +++ b/tests/test_modules.py @@ -1,5 +1,6 @@ '''This module contains tests for the Flask and OpenSense modules.''' import re +import unittest.mock as mock from app.main import app from app import opensense @@ -34,3 +35,31 @@ def test_opensense_get_temperature(): r"Average temperature: (\d+\.\d{2}) °C \((Warning: Too cold|Good|Warning: Too hot)\)", result ) + +def mock_response(temp_value): + """Mock response for OpenSenseMap API""" + class MockResponse: + """Mock response class to simulate OpenSenseMap API response.""" + def json(self): + """Return a mock JSON response.""" + return [{ + 'sensors': [ + { + 'title': 'Temperatur', + 'lastMeasurement': {'value': str(temp_value)} + } + ] + }] + return MockResponse() + +def test_opensense_get_temperature_too_cold(): + """Test opensense.get_temperature for too cold condition""" + with mock.patch('app.opensense.requests.get', return_value=mock_response(5)): + result = opensense.get_temperature() + assert 'Too cold' in result + +def test_opensense_get_temperature_too_hot(): + """Test opensense.get_temperature for too hot condition""" + with mock.patch('app.opensense.requests.get', return_value=mock_response(40)): + result = opensense.get_temperature() + assert 'Too hot' in result