Skip to content
Snippets Groups Projects
Commit 8799b091 authored by Evan Jones's avatar Evan Jones
Browse files

UPDATED COMMIT

parent 0f145974
No related branches found
No related tags found
No related merge requests found
Showing
with 180 additions and 0 deletions
File added
def pinMode(pin, mode):
print(f"[MOCK GPIO] pinMode({pin}, {mode})")
def digitalRead(pin):
print(f"[MOCK GPIO] digitalRead({pin})")
return 1 # Simulate "no press" / "no motion"
def analogRead(pin):
print(f"[MOCK GPIO] analogRead({pin})")
return 400 # Simulate some ambient light
def digitalWrite(pin, value):
print(f"[MOCK GPIO] digitalWrite({pin}, {value})")
def setText(msg):
print(f"[MOCK LCD] {msg}")
import time
import random
from doorbell.config import PREFERENCES
from doorbell.mqtt_client import initialise_mqtt, send_data
from doorbell.utils import log_event
def mock_telemetry_loop(interval=5):
"""Continuously simulate sensor values and system preference state."""
log_event("MOCK", "Starting mock telemetry loop")
while True:
# Simulate sensor data
light = random.randint(200, 800)
motion = random.choice([0, 1])
button = random.choice([0, 1])
event = random.choice(["motion", "doorbell", "visitor", "idle"])
# Build payload
payload = {
"light": light,
"motion": motion,
"button": button,
"event": event,
"audio_enabled": PREFERENCES["Audio"],
"visual_enabled": PREFERENCES["Visual"],
"thingsboard_enabled": PREFERENCES["Thingsboard"]
}
send_data(event_type=event, extra_data=payload)
time.sleep(interval)
File added
import json
class Client:
def username_pw_set(self, token):
print(f"[MOCK MQTT] Token set: {token}")
def connect(self, host, port, keepalive):
print(f"[MOCK MQTT] Connecting to {host}:{port}")
def loop_start(self):
print("[MOCK MQTT] Loop started")
def publish(self, topic, payload, qos=0):
print(f"[MOCK MQTT] Published to {topic}: {json.dumps(payload)}")
def subscribe(self, topic):
print(f"[MOCK MQTT] Subscribed to: {topic}")
# Run all unit tests in the tests/ directory
import sys
import os
import unittest
# Add root directory to Python path
sys.path.insert(0, os.path.abspath(os.path.dirname(__file__)))
def run_all_tests():
"""Load and run all tests in the /tests folder."""
loader = unittest.TestLoader()
suite = loader.discover('tests')
runner = unittest.TextTestRunner(verbosity=2)
runner.run(suite)
if __name__ == "__main__":
run_all_tests()
File added
File added
File added
File added
File added
File added
"""
Test suite for the button sensor.
Simulates a digital read from the doorbell button (mocked).
"""
import unittest
from doorbell.sensors import read_button
from doorbell.config import PORTS
class TestButtonSensor(unittest.TestCase):
def test_mock_button_read(self):
"""
Test that the mock button read returns a valid state (0 or 1).
0 = Pressed, 1 = Not Pressed.
"""
print(f"\n[TEST] Reading button on pin {PORTS['button']}")
value = read_button()
self.assertIn(value, [0, 1], "Button should return digital value 0 or 1")
"""
Test suite for the LCD display.
Ensures the system can write to the screen without crashing.
"""
import unittest
from doorbell.display import set_display
class TestLCDDisplay(unittest.TestCase):
def test_display_does_not_crash(self):
"""
Test that calling set_display() with a message doesn't raise an exception.
"""
print(f"\n[TEST] Updating display with test message")
try:
set_display("Testing display message")
except Exception as e:
self.fail(f"set_display() raised an exception: {e}")
"""
Test suite for the light level sensor.
Covers range validation and night mode threshold logic.
"""
import unittest
from doorbell.sensors import read_light
from doorbell.config import LIGHT_THRESHOLD, PORTS
class TestLightSensor(unittest.TestCase):
def test_mock_light_read_range(self):
"""
Ensure that the light sensor returns a valid analog value between 0 and 1023.
"""
print(f"\n[TEST] Reading light level on pin {PORTS['light']}")
light = read_light()
print(f"Light level: {light}")
self.assertGreaterEqual(light, 0)
self.assertLessEqual(light, 1023)
def test_night_mode_threshold(self):
"""
Verify that the threshold comparison for night mode produces a boolean result.
"""
print(f"\n[TEST] Comparing light against night mode threshold ({LIGHT_THRESHOLD})")
light = read_light()
is_night = light < LIGHT_THRESHOLD
print(f"Night mode triggered: {is_night}")
self.assertIsInstance(is_night, bool)
"""
Test suite for the PIR motion sensor.
Validates that the motion state returned is within expected digital bounds.
"""
import unittest
from doorbell.sensors import read_motion
from doorbell.config import PORTS
class TestMotionSensor(unittest.TestCase):
def test_mock_motion_read(self):
"""
Simulate a motion read and ensure it returns 0 (no motion) or 1 (motion detected).
"""
print(f"\n[TEST] Reading motion sensor on pin {PORTS['pir']}")
value = read_motion()
print(f"Motion detected: {bool(value)}")
self.assertIn(value, [0, 1])
"""
Test suite for MQTT integration.
Simulates connecting and sending telemetry to Thingsboard.
"""
import unittest
from doorbell.mqtt_client import initialise_mqtt, send_data
class TestMQTTMock(unittest.TestCase):
def test_mock_publish(self):
"""
Test MQTT mock client initialization and message publishing.
Should not raise exceptions or crash.
"""
print(f"\n[TEST] Initializing and publishing test message via MQTT")
try:
initialise_mqtt()
send_data("test-mock-event")
except Exception as e:
self.fail(f"MQTT mock publish failed: {e}")
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment