Skip to content

Commit

Permalink
Merge pull request #567 from gcivil-nyu-org/tests/sensepi
Browse files Browse the repository at this point in the history
SensePi Tests
  • Loading branch information
ab7289 committed Apr 27, 2020
2 parents eef23e6 + d16c766 commit a500aae
Show file tree
Hide file tree
Showing 5 changed files with 608 additions and 1 deletion.
2 changes: 1 addition & 1 deletion hardware/SensorPi/sense_pi.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

# Conditional import for sense hat and emulator
try:
if not os.environ["EMULATE_SENSE_HAT"]:
if not os.environ.get("EMULATE_SENSE_HAT"):
from sense_hat import SenseHat
else:
raise ImportError("Import emulator")
Expand Down
16 changes: 16 additions & 0 deletions hardware/tests/test_comm_pi.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,19 @@ def test_post_radio(self, mock_transceiver=mock.MagicMock()):
url = f"http://localhost:{self.mock_server_port}/"
requests.post(url, data={"key": "value"}, headers={"Content-Length": "15"})
mock_transceiver.return_value.send.assert_called()

@mock.patch("builtins.print")
@mock.patch("hardware.CommunicationsPi.comm_pi.Transceiver")
def test_post_radio_with_internet(
self, mock_transceiver=mock.MagicMock(), mock_print=mock.MagicMock()
):
with patch.dict(
os.environ, {"ENABLE_INTERNET_TRANSMISSION": "True"},
):
mock_transceiver.return_value.send = mock.MagicMock()
url = f"http://localhost:{self.mock_server_port}/"
requests.post(url, data={"key": "value"}, headers={"Content-Length": "15"})
mock_transceiver.return_value.send.assert_not_called()
self.assertTrue(
mock_print.mock_calls == [mock.call("transmit via internet")]
)
16 changes: 16 additions & 0 deletions hardware/tests/test_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,22 @@ def test_info_message(self):

capture.check(("test_logger", "INFO", "test message"))

@mock.patch("builtins.print")
def test_info_message_with_print(self, mock_print=mock.MagicMock()):
"""
Tests the .info method
"""
with mock.patch.dict(
os.environ, {"LOG_DIRECTORY": self.temp_dir.path, "SHOW_LOGS": "True"}
):
with LogCapture() as capture:
logger = Logger(name="test_logger", filename="logger.txt")
logger.info("test message")

self.assertTrue(mock_print.mock_calls == [mock.call("test message")])

capture.check(("test_logger", "INFO", "test message"))

def test_message_failure(self):
"""
makes sure that nothing is logged during initialization
Expand Down

0 comments on commit a500aae

Please sign in to comment.