Skip to content

Commit

Permalink
creates tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Robb-Fr committed Apr 23, 2024
1 parent db4c407 commit 8e60b60
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 30 deletions.
36 changes: 6 additions & 30 deletions display_controller/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,49 +3,25 @@
import os
import logging
from . import picdir
from .lib import parse_api_result
from waveshare_epd import epd4in2
from PIL import Image, ImageDraw, ImageFont

logging.basicConfig(level=logging.DEBUG)

MAX_DISPLAYED_LINES = 5
RESULT_FILENAME = "api_result.tsv"
MAX_NB_COLS = 4

result_filepath = os.path.join(
os.path.dirname(os.path.dirname(os.path.realpath(__file__))),
"api_fetcher",
RESULT_FILENAME,
)
if not os.path.isfile(result_filepath):
logging.error(f"could not find the file {result_filepath}")
exit(1)

# Open the file and read its content.
with open(result_filepath) as f:
content = f.read().splitlines()

to_display = []
for line in content[:MAX_DISPLAYED_LINES]:
cols = line.split("\t")
logging.info(cols)
if len(cols) != MAX_NB_COLS:
logging.error(f"the file contains {len(cols)} instead of {MAX_NB_COLS}")
exit(1)
line_direction = cols[1]
if len(line_direction) > 8:
line_direction = cols[1][:3] + ".." + cols[1][-3:]
to_append = (
cols[0]
+ " "
+ line_direction
+ " "
+ cols[2]
+ ("" if cols[3] == "0" else "+" + cols[3])
)
logging.info(f"appending {to_append}")
to_display.append(to_append)

try:
to_display = parse_api_result(result_filepath)
except (IOError, ValueError) as e:
logging.error(e)
exit(1)

try:
logging.info("Starting to display the next departures")
Expand Down
36 changes: 36 additions & 0 deletions display_controller/lib/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from typing import List
import logging
import os

MAX_NB_COLS = 4
MAX_DISPLAYED_LINES = 5


def parse_api_result(result_filepath: str) -> List[str]:
if not os.path.isfile(result_filepath):
raise IOError(f"could not find the file {result_filepath}")

# Open the file and read its content.
with open(result_filepath) as f:
content = f.read().splitlines()

to_display = []
for line in content[:MAX_DISPLAYED_LINES]:
cols = line.split("\t")
logging.info(cols)
if len(cols) != MAX_NB_COLS:
raise ValueError(f"the file contains {len(cols)} instead of {MAX_NB_COLS}")
line_direction = cols[1]
if len(line_direction) > 8:
line_direction = cols[1][:3] + ".." + cols[1][-3:]
to_append = (
cols[0]
+ " "
+ line_direction
+ " "
+ cols[2]
+ ("" if cols[3] == "0" else "+" + cols[3])
)
logging.info(f"appending {to_append}")
to_display.append(to_append)
return to_display
26 changes: 26 additions & 0 deletions display_controller/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import unittest
import os
from lib import parse_api_result

TEST_RESULT_FILENAME = "api_result_test.tsv"


class TestParse(unittest.TestCase):
def test_parse_result(self):
test_result_filepath = os.path.join(
os.path.dirname(os.path.dirname(os.path.realpath(__file__))),
"api_fetcher",
TEST_RESULT_FILENAME,
)
expected = [
"6 Gen..age 10:46+1",
"3 Gra..tti 10:46+1",
"9 Ver..urs 10:46",
"6 Ver..age 10:47",
"10 Gen..ive 10:47",
]
self.assertEqual(parse_api_result(test_result_filepath), expected)


if __name__ == "__main__":
unittest.main()

0 comments on commit 8e60b60

Please sign in to comment.