diff --git a/.github/workflows/build_test.yml b/.github/workflows/build_test.yml index 442e51d..a6866a7 100644 --- a/.github/workflows/build_test.yml +++ b/.github/workflows/build_test.yml @@ -32,6 +32,7 @@ jobs: run: | python -m pip install --upgrade pip pip install requests + pip install vcrpy - name: Run tests run: | diff --git a/.github/workflows/pylint.yml b/.github/workflows/pylint.yml index 110afbf..bdb900d 100644 --- a/.github/workflows/pylint.yml +++ b/.github/workflows/pylint.yml @@ -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') diff --git a/fixtures/vcr_cassettes/temperature.yaml b/fixtures/vcr_cassettes/temperature.yaml new file mode 100644 index 0000000..7fb7ce2 --- /dev/null +++ b/fixtures/vcr_cassettes/temperature.yaml @@ -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 diff --git a/fixtures/vcr_cassettes/version.yaml b/fixtures/vcr_cassettes/version.yaml new file mode 100644 index 0000000..53d41ac --- /dev/null +++ b/fixtures/vcr_cassettes/version.yaml @@ -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 diff --git a/main.py b/main.py index 55031fe..5311224 100644 --- a/main.py +++ b/main.py @@ -1,6 +1,7 @@ '''Module containing the main function of the app.''' from flask import Flask import opensense +#import test_main app = Flask(__name__) @@ -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() diff --git a/test_main.py b/test_main.py index 648add2..9f429b6 100644 --- a/test_main.py +++ b/test_main.py @@ -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.''' @@ -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) @@ -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) diff --git a/version.txt b/version.txt index 7dff5b8..f477849 100644 --- a/version.txt +++ b/version.txt @@ -1 +1 @@ -0.2.1 \ No newline at end of file +0.2.2 \ No newline at end of file