Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion app/opensense.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,8 @@ 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'
if 10 < average <= 36:
return f'Average temperature: {average:.2f} °C (Good)\n'
return f'Average temperature: {average:.2f} °C (Warning: Too hot)\n'
139 changes: 69 additions & 70 deletions tests/fixtures/vcr_cassettes/metrics.yaml
Original file line number Diff line number Diff line change
@@ -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
8 changes: 4 additions & 4 deletions tests/fixtures/vcr_cassettes/temperature.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions tests/fixtures/vcr_cassettes/version.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down
35 changes: 34 additions & 1 deletion tests/test_modules.py
Original file line number Diff line number Diff line change
@@ -1,4 +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

Expand Down Expand Up @@ -29,4 +31,35 @@ 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
)

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
2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.4.0
0.4.1