Skip to content

Commit

Permalink
Add units tests for queue
Browse files Browse the repository at this point in the history
  • Loading branch information
tristiisch committed Nov 13, 2023
1 parent 3106db3 commit 7213988
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 4 deletions.
21 changes: 18 additions & 3 deletions .github/workflows/compile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ on:
- "Dockerfile"
- ".github/workflows/compile.yml"

env:
SRC: "./src/pyramid"
TEST: "./src/pyramid-test"
TEST_FILES: "*_test.py"

jobs:

compile:
Expand Down Expand Up @@ -53,11 +58,16 @@ jobs:
- name: Test compilation
run: |
python -m compileall ./src
python -m compileall ${{ env.SRC }}"
- name: Units tests
run: |
python -m pip install --upgrade unittest
python -m unittest discover -v -s ${{ env.TEST }} -p ${{ env.TEST_FILES }}
- name: Save version
run: |
FULL_JSON=$(python src/pyramid --version)
FULL_JSON=$(python ${{ env.SRC }} --version)
echo "json=$(echo $FULL_JSON | jq -c)" >> $GITHUB_OUTPUT
echo "version=$(echo $FULL_JSON | jq -r '.version')" >> $GITHUB_OUTPUT
echo "commit_id=$(echo $FULL_JSON | jq -r '.git_info.commit_id')" >> $GITHUB_OUTPUT
Expand Down Expand Up @@ -97,7 +107,12 @@ jobs:
- name: Test compilation
run: |
python -m compileall ./src
python -m compileall ${{ env.SRC }}"
- name: Units tests
run: |
python -m pip install --upgrade unittest
python -m unittest discover -v -s ${{ env.TEST }} -p ${{ env.TEST_FILES }}
docker_push:
name: "Docker Push"
Expand Down
10 changes: 9 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
{
"python.analysis.typeCheckingMode": "basic",
"python.tensorBoard.logDirectory": "./logs"
"python.testing.unittestArgs": [
"-v",
"-s",
"./src/pyramid-test",
"-p",
"*_test.py"
],
"python.testing.pytestEnabled": false,
"python.testing.unittestEnabled": true
}
6 changes: 6 additions & 0 deletions src/pyramid-test/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import unittest
import queue_test

if __name__ == "__main__":
suite = unittest.TestLoader().loadTestsFromModule(queue_test)
unittest.TextTestRunner(verbosity=2).run(suite)
68 changes: 68 additions & 0 deletions src/pyramid-test/queue_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@

import os
import sys
import unittest
from unittest.mock import patch

sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))

from pyramid.tools.queue import Queue, QueueItem # noqa: E402


class TestQueue(unittest.TestCase):
def test_add(self):
queue = Queue(threads=1)
self.assertEqual(queue.length(), 0)

item = QueueItem(name="test", func=lambda x: x, x=5)
queue.add(item)
self.assertEqual(queue.length(), 1)

def test_add_at_start(self):
queue = Queue(threads=1)
self.assertEqual(queue.length(), 0)

item = QueueItem(name="test", func=lambda x: x, x=5)
queue.add_at_start(item)
self.assertEqual(queue.length(), 1)

def test_worker_start_before(self):
queue = Queue(threads=1)
self.assertEqual(queue.length(), 0)

queue.start()
item = QueueItem(name="test", func=lambda x: x, x=5)
queue.add(item)
self.assertEqual(queue.length(), 1)

queue.end()
queue.join()
self.assertEqual(queue.length(), 0)

def test_worker_start_after(self):
queue = Queue(threads=1)
self.assertEqual(queue.length(), 0)

item = QueueItem(name="test", func=lambda x: x, x=5)
queue.add(item)
self.assertEqual(queue.length(), 1)

queue.start()
queue.end()
queue.join()
self.assertEqual(queue.length(), 0)

def test_wait_for_end(self):
queue = Queue(threads=1)
queue.register_to_wait_on_exit()
queue.start()

item = QueueItem(name="test", func=lambda x: x, x=5)
queue.add(item)

with patch.object(Queue, 'join') as mock_join:
Queue.wait_for_end(timeout_per_threads=5)
mock_join.assert_called_with(5)

if __name__ == "__main__":
unittest.main(failfast=True)
1 change: 1 addition & 0 deletions src/pyramid/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
name = "pyramid"

0 comments on commit 7213988

Please sign in to comment.