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
2 changes: 1 addition & 1 deletion .github/workflows/test-and-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:

env:
SCHEMAS_URL: https://raw.githubusercontent.com/lambda-feedback/request-response-schemas/master/
API_CONNECTION: http://20.0.99.73:8000
API_CONNECTION: http://20.0.99.73:5000
#API_CONNECTION: ${{secrets.API_CONNECTION}}

steps:
Expand Down
24 changes: 17 additions & 7 deletions app/evaluation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@
from typing import TypedDict
import requests
from dotenv import load_dotenv

load_dotenv()


class Result(TypedDict):
is_correct: bool
error: int


def evaluation_function(response, answer, params) -> Result:
"""
Function used to evaluate a student response.
Expand Down Expand Up @@ -35,34 +38,40 @@ def evaluation_function(response, answer, params) -> Result:
global error_output
error_output = 0
try:
api_endpoint = params.get("api_endpoint", 'resistance/')
api_endpoint = params.get("api_endpoint", "resistance/")

if len(response) != 6:
error_output = 1
is_correct = False
raise Exception("Connection ID must be 6 characters long")

api_response = requests.get(f"{os.environ.get('API_CONNECTION')}/{api_endpoint}{response}")
api_response = requests.post(
f"{os.environ.get('API_CONNECTION')}/{api_endpoint}{response}"
)
api_data = api_response.json()

if isinstance(api_data, list) and len(api_data) > 0:
api_data = str(api_data)

if response == "000000":
is_correct = True
elif api_data in [params.get('correct_answer', None), -1.0, [{"resistance": -1}]]:
elif api_data in [
params.get("correct_answer", None),
-1.0,
[{"resistance": -1}],
]:
if response == "resistors/":
len_data = len(api_data)
if len_data != len(params.get('correct_answer', None)):
if len_data != len(params.get("correct_answer", None)):
is_correct = False
error_output = 2
else:
for i in params.get('correct_answer', None):
for i in params.get("correct_answer", None):
if i not in api_data:
is_correct = False
error_output = 3
elif response == "resistance/":
if api_data != params.get('correct_answer', None):
if api_data != params.get("correct_answer", None):
is_correct = False
error_output = 4
is_correct = True
Expand All @@ -74,4 +83,5 @@ def evaluation_function(response, answer, params) -> Result:
is_correct = False
error_output = 6

return Result(is_correct=is_correct, error=error_output)
return Result(is_correct=is_correct, error=error_output)

12 changes: 9 additions & 3 deletions app/preview.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@
from typing import Any, TypedDict
import requests
from dotenv import load_dotenv

load_dotenv()


class Result(TypedDict):
preview: Any


def preview_function(response: Any, params: Any) -> Result:
"""
Function used to preview a student response.
Expand All @@ -22,16 +25,19 @@ def preview_function(response: Any, params: Any) -> Result:
response schema.
"""
try:
api_endpoint = params.get("api_endpoint", 'resistance/')
api_endpoint = params.get("api_endpoint", "resistance/")

if len(response) != 6:
raise Exception("Connection ID must be 6 characters long")

api_response = requests.get(f"{os.environ.get('API_CONNECTION')}/{api_endpoint}{response}")
api_response = requests.post(
f"{os.environ.get('API_CONNECTION')}/{api_endpoint}{response}"
)
api_response.raise_for_status()
api_data = api_response.json()
except requests.RequestException as e:
print(f"Error API connection: {e}")
api_data = None

return Result(preview=api_data)
return Result(preview=api_data)

11 changes: 9 additions & 2 deletions app/preview_tests.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import unittest
from app.utility import initialize_test_connection
from dotenv import load_dotenv

load_dotenv()

try:
Expand Down Expand Up @@ -29,15 +30,21 @@ class TestPreviewFunction(unittest.TestCase):
"""

def test_api_endpoint_resistance(self):
response, params = "000000", {"api_endpoint": "resistance/", "correct_answer": -1.0}
response, params = (
"000000",
{"api_endpoint": "resistance/", "correct_answer": -1.0},
)
result = preview_function(response, params)

self.assertIn("preview", result)
self.assertIsNotNone(result["preview"])

def test_api_endpoint_resistors(self):
id_connection = initialize_test_connection()
response, params = id_connection, {"api_endpoint": "resistors/", "correct_answer": 0.0}
response, params = (
id_connection,
{"api_endpoint": "resistors/", "correct_answer": []},
)
result = preview_function(response, params)

self.assertIn("preview", result)
Expand Down