Skip to content

Commit

Permalink
Merge unit_tests
Browse files Browse the repository at this point in the history
Create unit_test_workflow.yml
  • Loading branch information
cwangsanata authored Sep 26, 2024
2 parents 5cb5f81 + 36b231e commit f852dbb
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 3 deletions.
10 changes: 9 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Git
.git
.gitignore

# Text
README.md
Dockerfile
LICENSE
LICENSE

# Test Suite
tests/
__init__.py
__main__.py
35 changes: 35 additions & 0 deletions .github/workflows/unit_test_workflow.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# This workflow will install Python dependencies, run tests and lint with a single version of Python
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python

name: Python application

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

permissions:
contents: read

jobs:
build:

runs-on: ubuntu-latest
strategy:
matrix:
python-version: [3.12]

steps:
- uses: actions/checkout@v2
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v1
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r ./requirements.txt
- name: Test with unittest
run: |
python -m tests
2 changes: 1 addition & 1 deletion src/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def main():
video_ids = config['content']['video_ids']

try:
url = f"http://{device_ip}:{device_port}/launch/837?contentId={video_ids[util.select_random(video_ids)]}"
url = f"http://{device_ip}:{device_port}/launch/837?contentId={util.select_random(video_ids)}"
response = requests.post(url=url, timeout=device_timeout)
except ConnectionError as e:
return response.status_code
Expand Down
6 changes: 5 additions & 1 deletion src/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@
import yaml

def select_random(arr: list) -> int:
return arr[random.randrange(0, len(arr))]
try:
return arr[random.randrange(0, len(arr))]
except ValueError:
raise IndexError('Cannot select random element from empty list')

def load_yaml(file_path: str) -> any:
with open(file_path, 'r') as file:
assert file, f'File {file_path} not found'
return yaml.safe_load(file)

5 changes: 5 additions & 0 deletions tests/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import unittest

if __name__ == '__main__':
test_utils = unittest.TestLoader().discover('tests')
unittest.TextTestRunner().run(test_utils)
8 changes: 8 additions & 0 deletions tests/test_data.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
content:
video_ids_multiple: [ # Replace with Youtube video IDs to play
"string1",
"string2",
"string3"
]
video_ids_single: ["string3"]
video_ids_empty: []
40 changes: 40 additions & 0 deletions tests/test_util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import unittest
from src.util import select_random, load_yaml

class TestUtils(unittest.TestCase):
'''
Test select_random function
'''
def test_select_random(self):
arr = [1, 2, 3, 4, 5]
self.assertIn(select_random(arr), arr)

def test_select_random_empty(self):
arr = []
with self.assertRaises(IndexError):
select_random(arr)

'''
Test load_yaml function
'''
data = load_yaml('tests/test_data.yaml')
def test_load_yaml_multiple(self):
videos = self.data['content']['video_ids_multiple']
self.assertIsInstance(videos, list)
for video in videos:
self.assertIsInstance(video, str)

def test_load_yaml_single(self):
videos = self.data['content']['video_ids_single']
self.assertIsInstance(videos, list)
for video in videos:
self.assertIsInstance(video, str)

def test_load_yaml_empty(self):
videos = self.data['content']['video_ids_empty']
self.assertIsInstance(videos, list)
self.assertEqual(len(videos), 0)

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

0 comments on commit f852dbb

Please sign in to comment.