Skip to content

Commit c79fd6c

Browse files
committed
Added tests
1 parent 7fed4a7 commit c79fd6c

3 files changed

Lines changed: 91 additions & 59 deletions

File tree

loop_to_python_api/api.py

Lines changed: 7 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,18 @@
22
This file provides an API for calling the functions in the dynamic library. These functions are c-embeddings
33
for swift functions, found in Sources/LoopAlgorithmToPython/LoopAlgorithmToPython.swift.
44
"""
5-
import pandas as pd
6-
7-
from helpers import get_bytes_from_json
5+
from loop_to_python_api.helpers import get_bytes_from_json
86

97
import ctypes
108
import json
9+
import os
10+
1111

12+
# swift_lib = ctypes.CDLL('python_api/libLoopAlgorithmToPython.dylib')
1213

13-
swift_lib = ctypes.CDLL('python_api/libLoopAlgorithmToPython.dylib')
14+
current_dir = os.path.dirname(os.path.abspath(__file__))
15+
lib_path = os.path.join(current_dir, 'libLoopAlgorithmToPython.dylib')
16+
swift_lib = ctypes.CDLL(lib_path)
1417

1518

1619
# This function helps with providing more informative error messages if the code fails
@@ -130,52 +133,3 @@ def get_dynamic_carbs_on_board(json_file):
130133
return swift_lib.getDynamicCarbsOnBoard(json_bytes)
131134

132135

133-
134-
135-
# THIS IS FOR TESTING, REMOVE WHEN DONE!
136-
137-
with open('python_tests/test_files/generate_prediction_input.json', 'r') as f:
138-
prediction_input = json.load(f)
139-
140-
141-
with open('python_tests/test_files/loop_algorithm_input.json', 'r') as f:
142-
loop_algorithm_input = json.load(f)
143-
144-
145-
with open('python_tests/test_files/dynamic_carbs_input.json', 'r') as f:
146-
dynamic_carbs_input = json.load(f)
147-
148-
149-
initialize_exception_handlers()
150-
prediction_values = generate_prediction(prediction_input)
151-
print("prediction values", prediction_values)
152-
print(" ")
153-
prediction_dates = get_prediction_dates(prediction_input)
154-
print("prediction dates", prediction_dates)
155-
print(" ")
156-
glucose_effect_velocity = get_glucose_effect_velocity(prediction_input)
157-
print("glucose_effect_velocity", glucose_effect_velocity)
158-
print(" ")
159-
glucose_effect_velocity_dates = get_glucose_effect_velocity_dates(prediction_input)
160-
print("glucose_effect_velocity_dates", glucose_effect_velocity_dates)
161-
print(" ")
162-
active_carbs = get_active_carbs(loop_algorithm_input)
163-
print("active_carbs", active_carbs)
164-
print(" ")
165-
active_insulin = get_active_insulin(loop_algorithm_input)
166-
print("active_insulin", active_insulin)
167-
print(" ")
168-
percent_absorption_at_percent_time = percent_absorption_at_percent_time(0.2)
169-
print("percent_absorption_at_percent_time", percent_absorption_at_percent_time)
170-
print(" ")
171-
piecewise_linear_percent_rate_at_percent_time = piecewise_linear_percent_rate_at_percent_time(0.2)
172-
print("piecewise_linear_percent_rate_at_percent_time", piecewise_linear_percent_rate_at_percent_time)
173-
print(" ")
174-
linear_percent_rate_at_percent_time = linear_percent_rate_at_percent_time(0.2)
175-
print("linear_percent_rate_at_percent_time", linear_percent_rate_at_percent_time)
176-
print(" ")
177-
dynamic_carbs_on_board = get_dynamic_carbs_on_board(dynamic_carbs_input)
178-
print("dynamic_carbs_on_board", dynamic_carbs_on_board)
179-
print(" ")
180-
181-

pytest.ini

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[pytest]
2+
testpaths = python_tests
3+
python_files = tests.py
4+
python_functions = test_*

python_tests/tests.py

Lines changed: 80 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,94 @@
11
import pytest
22
import json
3-
from loop_to_python_api.api import generate_prediction
3+
from loop_to_python_api.api import (
4+
initialize_exception_handlers,
5+
generate_prediction,
6+
get_prediction_dates,
7+
get_glucose_effect_velocity,
8+
get_glucose_effect_velocity_dates,
9+
get_active_carbs,
10+
get_active_insulin,
11+
percent_absorption_at_percent_time,
12+
piecewise_linear_percent_rate_at_percent_time,
13+
linear_percent_rate_at_percent_time,
14+
get_dynamic_carbs_on_board,
15+
)
416

517

6-
@pytest.fixture
7-
def generate_prediction_input():
18+
def get_generate_prediction_input():
819
with open('python_tests/test_files/generate_prediction_input.json', 'r') as f:
920
return json.load(f)
1021

1122

23+
def get_loop_aglgorithm_input():
24+
with open('python_tests/test_files/loop_algorithm_input.json', 'r') as f:
25+
return json.load(f)
26+
27+
28+
def get_dynamic_carbs_input():
29+
with open('python_tests/test_files/dynamic_carbs_input.json', 'r') as f:
30+
return json.load(f)
31+
32+
33+
def test_initialize_exception_handlers():
34+
result = initialize_exception_handlers()
35+
assert result is None # Replace with the expected result
36+
37+
1238
def test_generate_prediction():
13-
prediction_input = generate_prediction_input()
39+
prediction_input = get_generate_prediction_input()
1440
prediction_values = generate_prediction(prediction_input)
15-
print(prediction_values)
16-
assert prediction_values == []
41+
assert isinstance(prediction_values, list) # Replace with the expected type or value
42+
43+
44+
def test_get_prediction_dates():
45+
prediction_input = get_generate_prediction_input()
46+
prediction_dates = get_prediction_dates(prediction_input)
47+
assert isinstance(prediction_dates, list) # Replace with the expected type or value
48+
49+
50+
def test_get_glucose_effect_velocity():
51+
prediction_input = get_generate_prediction_input()
52+
glucose_effect_velocity = get_glucose_effect_velocity(prediction_input)
53+
assert isinstance(glucose_effect_velocity, list) # Replace with the expected type or value
54+
55+
56+
def test_get_glucose_effect_velocity_dates():
57+
prediction_input = get_generate_prediction_input()
58+
glucose_effect_velocity_dates = get_glucose_effect_velocity_dates(prediction_input)
59+
assert isinstance(glucose_effect_velocity_dates, list) # Replace with the expected type or value
60+
61+
62+
def test_get_active_carbs():
63+
loop_algorithm_input = get_loop_aglgorithm_input()
64+
active_carbs = get_active_carbs(loop_algorithm_input)
65+
assert isinstance(active_carbs, float) # Replace with the expected type or value
66+
67+
68+
def test_get_active_insulin():
69+
loop_algorithm_input = get_loop_aglgorithm_input()
70+
active_insulin = get_active_insulin(loop_algorithm_input)
71+
assert isinstance(active_insulin, float) # Replace with the expected type or value
72+
73+
74+
def test_percent_absorption_at_percent_time():
75+
result = percent_absorption_at_percent_time(0.2)
76+
assert isinstance(result, float) # Replace with the expected type or value
77+
78+
79+
def test_piecewise_linear_percent_rate_at_percent_time():
80+
result = piecewise_linear_percent_rate_at_percent_time(0.2)
81+
assert isinstance(result, float) # Replace with the expected type or value
82+
83+
84+
def test_linear_percent_rate_at_percent_time():
85+
result = linear_percent_rate_at_percent_time(0.2)
86+
assert isinstance(result, float) # Replace with the expected type or value
1787

1888

89+
def test_get_dynamic_carbs_on_board():
90+
dynamic_carbs_input = get_dynamic_carbs_input()
91+
dynamic_carbs_on_board = get_dynamic_carbs_on_board(dynamic_carbs_input)
92+
assert isinstance(dynamic_carbs_on_board, float) # Replace with the expected type or value
1993

2094

0 commit comments

Comments
 (0)