feat: make command parameter optional in set_alarm mutation
This commit is contained in:
+13
-8
@@ -13,6 +13,7 @@ from api.main import app # noqa: E402
|
||||
|
||||
client = TestClient(app)
|
||||
|
||||
|
||||
def test_auth_missing():
|
||||
query = """
|
||||
query {
|
||||
@@ -24,6 +25,7 @@ def test_auth_missing():
|
||||
response = client.post("/graphql", json={"query": query})
|
||||
assert response.status_code == 401
|
||||
|
||||
|
||||
def test_auth_invalid():
|
||||
query = """
|
||||
query {
|
||||
@@ -32,12 +34,15 @@ def test_auth_invalid():
|
||||
}
|
||||
}
|
||||
"""
|
||||
response = client.post("/graphql", json={"query": query}, headers={"X-API-Key": "wrong"})
|
||||
response = client.post(
|
||||
"/graphql", json={"query": query}, headers={"X-API-Key": "wrong"}
|
||||
)
|
||||
assert response.status_code == 401
|
||||
|
||||
|
||||
def test_graphql_workflow():
|
||||
headers = {"X-API-Key": "test-secret"}
|
||||
|
||||
|
||||
# 1. Get empty alarms
|
||||
query_get = """
|
||||
query {
|
||||
@@ -49,7 +54,7 @@ def test_graphql_workflow():
|
||||
res = client.post("/graphql", json={"query": query_get}, headers=headers)
|
||||
assert res.status_code == 200
|
||||
assert res.json()["data"]["getAlarms"] == []
|
||||
|
||||
|
||||
# 2. Set alarm
|
||||
mutation_set = """
|
||||
mutation {
|
||||
@@ -68,12 +73,12 @@ def test_graphql_workflow():
|
||||
assert alarm["command"] == "python wecker.py"
|
||||
assert alarm["isEnabled"] is True
|
||||
alarm_id = alarm["id"]
|
||||
|
||||
|
||||
# 3. Get alarms lists it
|
||||
res = client.post("/graphql", json={"query": query_get}, headers=headers)
|
||||
assert len(res.json()["data"]["getAlarms"]) == 1
|
||||
assert res.json()["data"]["getAlarms"][0]["id"] == alarm_id
|
||||
|
||||
|
||||
# 4. Get specific alarm
|
||||
query_one = f"""
|
||||
query {{
|
||||
@@ -86,7 +91,7 @@ def test_graphql_workflow():
|
||||
res = client.post("/graphql", json={"query": query_one}, headers=headers)
|
||||
assert res.json()["data"]["getAlarm"]["id"] == alarm_id
|
||||
assert res.json()["data"]["getAlarm"]["cronExpression"] == "30 7 * * *"
|
||||
|
||||
|
||||
# 5. Update alarm
|
||||
mutation_update = f"""
|
||||
mutation {{
|
||||
@@ -102,7 +107,7 @@ def test_graphql_workflow():
|
||||
assert alarm_updated["id"] == alarm_id
|
||||
assert alarm_updated["cronExpression"] == "0 8 * * *"
|
||||
assert alarm_updated["isEnabled"] is False
|
||||
|
||||
|
||||
# 6. Delete alarm
|
||||
mutation_delete = f"""
|
||||
mutation {{
|
||||
@@ -111,7 +116,7 @@ def test_graphql_workflow():
|
||||
"""
|
||||
res = client.post("/graphql", json={"query": mutation_delete}, headers=headers)
|
||||
assert res.json()["data"]["deleteAlarm"] is True
|
||||
|
||||
|
||||
# 7. List again is empty
|
||||
res = client.post("/graphql", json={"query": query_get}, headers=headers)
|
||||
assert res.json()["data"]["getAlarms"] == []
|
||||
|
||||
+13
-8
@@ -3,16 +3,19 @@ import tempfile
|
||||
import uuid
|
||||
from api.crontab_manager import CrontabManager
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def crontab_file():
|
||||
with tempfile.NamedTemporaryFile(mode='w+', delete=False) as f:
|
||||
with tempfile.NamedTemporaryFile(mode="w+", delete=False) as f:
|
||||
pass
|
||||
yield f.name
|
||||
|
||||
|
||||
def test_crontab_manager_empty(crontab_file):
|
||||
manager = CrontabManager(tabfile=crontab_file)
|
||||
assert manager.get_alarms() == []
|
||||
|
||||
|
||||
def test_add_and_list_alarm(crontab_file):
|
||||
manager = CrontabManager(tabfile=crontab_file)
|
||||
alarm_id = str(uuid.uuid4())
|
||||
@@ -20,9 +23,9 @@ def test_add_and_list_alarm(crontab_file):
|
||||
alarm_id=alarm_id,
|
||||
cron_expression="30 7 * * *",
|
||||
command="python wecker.py",
|
||||
is_enabled=True
|
||||
is_enabled=True,
|
||||
)
|
||||
|
||||
|
||||
alarms = manager.get_alarms()
|
||||
assert len(alarms) == 1
|
||||
assert alarms[0]["id"] == alarm_id
|
||||
@@ -30,6 +33,7 @@ def test_add_and_list_alarm(crontab_file):
|
||||
assert alarms[0]["command"] == "python wecker.py"
|
||||
assert alarms[0]["is_enabled"] is True
|
||||
|
||||
|
||||
def test_update_alarm(crontab_file):
|
||||
manager = CrontabManager(tabfile=crontab_file)
|
||||
alarm_id = "test-id"
|
||||
@@ -37,27 +41,28 @@ def test_update_alarm(crontab_file):
|
||||
alarm_id=alarm_id,
|
||||
cron_expression="30 7 * * *",
|
||||
command="python wecker.py",
|
||||
is_enabled=True
|
||||
is_enabled=True,
|
||||
)
|
||||
|
||||
|
||||
manager.set_alarm(
|
||||
alarm_id=alarm_id,
|
||||
cron_expression="0 8 * * *",
|
||||
command="python wecker.py --loud",
|
||||
is_enabled=False
|
||||
is_enabled=False,
|
||||
)
|
||||
|
||||
|
||||
alarms = manager.get_alarms()
|
||||
assert len(alarms) == 1
|
||||
assert alarms[0]["cron_expression"] == "0 8 * * *"
|
||||
assert alarms[0]["command"] == "python wecker.py --loud"
|
||||
assert alarms[0]["is_enabled"] is False
|
||||
|
||||
|
||||
def test_delete_alarm(crontab_file):
|
||||
manager = CrontabManager(tabfile=crontab_file)
|
||||
alarm_id = "test-id-2"
|
||||
manager.set_alarm(alarm_id, "0 0 * * *", "cmd", True)
|
||||
assert len(manager.get_alarms()) == 1
|
||||
|
||||
|
||||
manager.delete_alarm(alarm_id)
|
||||
assert len(manager.get_alarms()) == 0
|
||||
|
||||
+40
-33
@@ -3,32 +3,36 @@ import sys
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
# Mock RPi.GPIO and pygame before importing wecker
|
||||
sys.modules['RPi'] = MagicMock()
|
||||
sys.modules['RPi.GPIO'] = MagicMock()
|
||||
sys.modules['pygame'] = MagicMock()
|
||||
sys.modules["RPi"] = MagicMock()
|
||||
sys.modules["RPi.GPIO"] = MagicMock()
|
||||
sys.modules["pygame"] = MagicMock()
|
||||
|
||||
import wecker # noqa: E402
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_gpio():
|
||||
with patch.object(wecker, 'GPIO') as mock:
|
||||
with patch.object(wecker, "GPIO") as mock:
|
||||
mock.LOW = 0
|
||||
mock.HIGH = 1
|
||||
yield mock
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_pygame():
|
||||
with patch.object(wecker, 'pygame') as mock:
|
||||
with patch.object(wecker, "pygame") as mock:
|
||||
yield mock
|
||||
|
||||
|
||||
def test_set_led(mock_gpio):
|
||||
wecker.set_led(True)
|
||||
mock_gpio.output.assert_called_with(wecker.LED_PIN, mock_gpio.LOW)
|
||||
|
||||
|
||||
wecker.set_led(False)
|
||||
mock_gpio.output.assert_called_with(wecker.LED_PIN, mock_gpio.HIGH)
|
||||
|
||||
@patch('wecker.time.sleep')
|
||||
|
||||
@patch("wecker.time.sleep")
|
||||
def test_blink_led(mock_sleep, mock_gpio):
|
||||
wecker.blink_led(2)
|
||||
# 2 blinks = 4 sleep calls, 2 set_led(True), 2 set_led(False)
|
||||
@@ -36,35 +40,37 @@ def test_blink_led(mock_sleep, mock_gpio):
|
||||
# GPIO output called 4 times total (on, off, on, off)
|
||||
assert mock_gpio.output.call_count == 4
|
||||
|
||||
@patch('wecker.time.time')
|
||||
|
||||
@patch("wecker.time.time")
|
||||
def test_run_alarm_start_to_wait(mock_time, mock_gpio, mock_pygame):
|
||||
# Mock pygame.mixer.get_init() to return True so music plays
|
||||
mock_pygame.mixer.get_init.return_value = True
|
||||
|
||||
|
||||
# Just run it in test mode, it should execute the loop once and exit
|
||||
wecker.run_alarm(test_mode=True)
|
||||
assert mock_pygame.mixer.music.play.called
|
||||
|
||||
|
||||
def test_state_machine_evaluation(mock_gpio, mock_pygame):
|
||||
clock = wecker.AlarmClock()
|
||||
|
||||
|
||||
# Transition to ringing -> wait before blink
|
||||
clock.update(100.0, True) # button press
|
||||
clock.update(100.0, True) # button press
|
||||
assert clock.state == wecker.STATE_WAIT_BEFORE_BLINK
|
||||
|
||||
|
||||
# Wait 3 seconds -> blinking
|
||||
with patch('wecker.blink_led'):
|
||||
with patch("wecker.blink_led"):
|
||||
clock.update(103.1, False)
|
||||
assert clock.state == wecker.STATE_BLINKING
|
||||
|
||||
with patch('wecker.blink_led'):
|
||||
|
||||
with patch("wecker.blink_led"):
|
||||
clock.update(103.2, False)
|
||||
assert clock.state == wecker.STATE_WAIT_FOR_INPUT
|
||||
assert clock.target_blinks >= 1
|
||||
|
||||
|
||||
# Set the user presses to be correct
|
||||
clock.target_blinks = 3
|
||||
|
||||
|
||||
# Press 1
|
||||
clock.update(104.0, True)
|
||||
clock.update(104.1, False)
|
||||
@@ -74,46 +80,47 @@ def test_state_machine_evaluation(mock_gpio, mock_pygame):
|
||||
# Press 3
|
||||
clock.update(105.0, True)
|
||||
clock.update(105.1, False)
|
||||
|
||||
|
||||
assert clock.user_presses == 3
|
||||
|
||||
|
||||
# Wait 3 seconds to evaluate
|
||||
clock.update(108.2, False) # Triggers state change
|
||||
keep_running = clock.update(108.3, False) # Triggers evaluation
|
||||
|
||||
clock.update(108.2, False) # Triggers state change
|
||||
keep_running = clock.update(108.3, False) # Triggers evaluation
|
||||
|
||||
# It should evaluate, see it's correct, and return False (stop running)
|
||||
assert clock.state == wecker.STATE_EVALUATING
|
||||
assert not keep_running
|
||||
|
||||
|
||||
def test_state_machine_incorrect(mock_gpio, mock_pygame):
|
||||
clock = wecker.AlarmClock()
|
||||
clock.state = wecker.STATE_WAIT_FOR_INPUT
|
||||
clock.target_blinks = 3
|
||||
|
||||
|
||||
# Only press once
|
||||
clock.update(100.0, True)
|
||||
clock.update(100.1, False)
|
||||
|
||||
|
||||
# Wait to evaluate
|
||||
clock.last_interaction_time = 100.1
|
||||
clock.update(103.2, False) # triggers eval state
|
||||
keep_running = clock.update(103.3, False) # evals to incorrect
|
||||
|
||||
clock.update(103.2, False) # triggers eval state
|
||||
keep_running = clock.update(103.3, False) # evals to incorrect
|
||||
|
||||
# Evaluated incorrectly, should wait before retry
|
||||
assert keep_running
|
||||
assert clock.state == wecker.STATE_WAIT_BEFORE_RETRY
|
||||
|
||||
|
||||
@patch('wecker.time.time')
|
||||
@patch("wecker.time.time")
|
||||
def test_blinking_updates_time_correctly(mock_time, mock_gpio, mock_pygame):
|
||||
clock = wecker.AlarmClock()
|
||||
clock.state = wecker.STATE_BLINKING
|
||||
clock.target_blinks = 4
|
||||
|
||||
|
||||
mock_time.return_value = 200.0
|
||||
|
||||
with patch('wecker.blink_led'):
|
||||
clock.update(100.0, False)
|
||||
|
||||
|
||||
with patch("wecker.blink_led"):
|
||||
clock.update(100.0, False)
|
||||
|
||||
assert clock.state == wecker.STATE_WAIT_FOR_INPUT
|
||||
assert clock.last_interaction_time == 200.0
|
||||
|
||||
Reference in New Issue
Block a user