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
1 change: 1 addition & 0 deletions .github/workflows/build_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ jobs:
run: |
python -m pip install --upgrade pip
pip install requests
pip install vcrpy

- name: Run tests
run: |
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/pylint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ jobs:
pip install pylint
pip install flask
pip install requests
pip install vcrpy
- name: Analysing the code with pylint
run: |
pylint $(git ls-files '*.py')
32 changes: 32 additions & 0 deletions fixtures/vcr_cassettes/temperature.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
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/temperature
response:
body:
string: "Average temperature: 8.06 \xB0C\n"
headers:
Connection:
- close
Content-Length:
- '30'
Content-Type:
- text/html; charset=utf-8
Date:
- Tue, 06 May 2025 18:04:57 GMT
Server:
- Werkzeug/3.0.1 Python/3.12.3
status:
code: 200
message: OK
version: 1
34 changes: 34 additions & 0 deletions fixtures/vcr_cassettes/version.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
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/version
response:
body:
string: 'Current app version: 0.2.2

'
headers:
Connection:
- close
Content-Length:
- '27'
Content-Type:
- text/html; charset=utf-8
Date:
- Tue, 06 May 2025 18:01:25 GMT
Server:
- Werkzeug/3.0.1 Python/3.12.3
status:
code: 200
message: OK
version: 1
11 changes: 11 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'''Module containing the main function of the app.'''
from flask import Flask
import opensense
#import test_main

app = Flask(__name__)

Expand All @@ -18,5 +19,15 @@ def get_temperature():
'''Function to get the current temperature.'''
return opensense.get_temperature()

### Test module ###
# @app.route('/test')
# def test():
# '''Function to test the app.'''
# success = test_main.run_all_tests()
# if success:
# return "All tests passed!\n", 200
# else:
# return "Some tests failed. Check the logs for details.\n", 500

if __name__ == "__main__":
app.run()
16 changes: 14 additions & 2 deletions test_main.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
'''This module contains the test cases for the opensense module.'''
import sys
import re
import os
import requests
import vcr

API_HOST = os.environ.get('API_HOST', 'http://127.0.0.1:5000')

my_vcr = vcr.VCR(
cassette_library_dir='fixtures/vcr_cassettes',
record_mode='once',
match_on=['uri', 'method'],
)

def make_request(url, expected_pattern):
'''Reusable function to make requests and assert responses.'''
Expand All @@ -25,9 +35,10 @@ def make_request(url, expected_pattern):
print(f"❌ TEST FAILED: {str(e)}")
return False, None

@my_vcr.use_cassette('version.yaml')
def test_get_version():
'''Function to test the get_version function from the opensense module.'''
url = "http://127.0.0.1:5000/version"
url = f"{API_HOST}/version"
pattern = r"Current app version: (\d+\.\d+\.\d+)"
version_success, match = make_request(url, pattern)

Expand All @@ -37,9 +48,10 @@ def test_get_version():

return version_success

@my_vcr.use_cassette('temperature.yaml')
def test_get_temperature():
'''Function to test the get_temperature function from the opensense module.'''
url = "http://127.0.0.1:5000/temperature"
url = f"{API_HOST}/temperature"
pattern = r"Average temperature: (\d+\.\d+) °C"
temperature_success, match = make_request(url, pattern)

Expand Down
2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.2.1
0.2.2