Skip to content

Commit 1e0d019

Browse files
Merge pull request #30 from GabrielPalmar/test
feat(test): Added support for VCR.py on testing module
2 parents 6e2c464 + 605e664 commit 1e0d019

7 files changed

Lines changed: 94 additions & 3 deletions

File tree

.github/workflows/build_test.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ jobs:
3232
run: |
3333
python -m pip install --upgrade pip
3434
pip install requests
35+
pip install vcrpy
3536
3637
- name: Run tests
3738
run: |

.github/workflows/pylint.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ jobs:
2929
pip install pylint
3030
pip install flask
3131
pip install requests
32+
pip install vcrpy
3233
- name: Analysing the code with pylint
3334
run: |
3435
pylint $(git ls-files '*.py')
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
interactions:
2+
- request:
3+
body: null
4+
headers:
5+
Accept:
6+
- '*/*'
7+
Accept-Encoding:
8+
- gzip, deflate
9+
Connection:
10+
- keep-alive
11+
User-Agent:
12+
- python-requests/2.31.0
13+
method: GET
14+
uri: http://127.0.0.1:5000/temperature
15+
response:
16+
body:
17+
string: "Average temperature: 8.06 \xB0C\n"
18+
headers:
19+
Connection:
20+
- close
21+
Content-Length:
22+
- '30'
23+
Content-Type:
24+
- text/html; charset=utf-8
25+
Date:
26+
- Tue, 06 May 2025 18:04:57 GMT
27+
Server:
28+
- Werkzeug/3.0.1 Python/3.12.3
29+
status:
30+
code: 200
31+
message: OK
32+
version: 1
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
interactions:
2+
- request:
3+
body: null
4+
headers:
5+
Accept:
6+
- '*/*'
7+
Accept-Encoding:
8+
- gzip, deflate
9+
Connection:
10+
- keep-alive
11+
User-Agent:
12+
- python-requests/2.31.0
13+
method: GET
14+
uri: http://127.0.0.1:5000/version
15+
response:
16+
body:
17+
string: 'Current app version: 0.2.2
18+
19+
'
20+
headers:
21+
Connection:
22+
- close
23+
Content-Length:
24+
- '27'
25+
Content-Type:
26+
- text/html; charset=utf-8
27+
Date:
28+
- Tue, 06 May 2025 18:01:25 GMT
29+
Server:
30+
- Werkzeug/3.0.1 Python/3.12.3
31+
status:
32+
code: 200
33+
message: OK
34+
version: 1

main.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'''Module containing the main function of the app.'''
22
from flask import Flask
33
import opensense
4+
#import test_main
45

56
app = Flask(__name__)
67

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

22+
### Test module ###
23+
# @app.route('/test')
24+
# def test():
25+
# '''Function to test the app.'''
26+
# success = test_main.run_all_tests()
27+
# if success:
28+
# return "All tests passed!\n", 200
29+
# else:
30+
# return "Some tests failed. Check the logs for details.\n", 500
31+
2132
if __name__ == "__main__":
2233
app.run()

test_main.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
11
'''This module contains the test cases for the opensense module.'''
22
import sys
33
import re
4+
import os
45
import requests
6+
import vcr
7+
8+
API_HOST = os.environ.get('API_HOST', 'http://127.0.0.1:5000')
9+
10+
my_vcr = vcr.VCR(
11+
cassette_library_dir='fixtures/vcr_cassettes',
12+
record_mode='once',
13+
match_on=['uri', 'method'],
14+
)
515

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

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

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

3849
return version_success
3950

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

version.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.2.1
1+
0.2.2

0 commit comments

Comments
 (0)