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 138a97a
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 6 deletions.
50 changes: 45 additions & 5 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,21 +58,52 @@ jobs:
- name: Test compilation
run: |
python -m compileall ./src
python -m compileall ${{ env.SRC }}
- 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
echo "last_author=$(echo $FULL_JSON | jq -r '.git_info.last_author')" >> $GITHUB_OUTPUT
id: version

unit_test:
name: "Unit tests Python"
needs: compile
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Set up Python 3.11
uses: actions/setup-python@v4
with:
python-version: "3.11"

- name: Cache dependencies
uses: actions/cache@v3
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}
restore-keys: |
${{ runner.os }}-pip
- name: Install dependencies
run: |
python -m pip install --upgrade pip
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: Units tests
run: |
python -m unittest discover -v -s ${{ env.TEST }} -p ${{ env.TEST_FILES }}
version_compatibility:
name: "Compile Python"
runs-on: ubuntu-latest
needs: compile
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.8", "3.9", "3.10"]
Expand Down Expand Up @@ -97,11 +133,15 @@ jobs:
- name: Test compilation
run: |
python -m compileall ./src
python -m compileall ${{ env.SRC }}
- name: Units tests
run: |
python -m unittest discover -v -s ${{ env.TEST }} -p ${{ env.TEST_FILES }}
docker_push:
name: "Docker Push"
needs: compile
needs: unit_test
runs-on: ubuntu-latest
if: github.event_name == '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 138a97a

Please sign in to comment.