Skip to content

Commit

Permalink
Adding basic tests (#31)
Browse files Browse the repository at this point in the history
* Adding tests

* Add caching to tests
  • Loading branch information
xhluca authored Apr 24, 2024
1 parent 9edb65a commit 548e512
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 1 deletion.
53 changes: 53 additions & 0 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
name: Run Tests

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

jobs:
test:
runs-on: ubuntu-latest

steps:
- name: Check out repository
uses: actions/checkout@v2

- name: Set up Python
uses: actions/setup-python@v3
with:
python-version: '3.9' # Specify your required Python version

- name: Cache Python dependencies
uses: actions/cache@v2
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-${{ hashFiles('tests/requirements.txt') }}
restore-keys: |
${{ runner.os }}-pip-
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r tests/requirements.txt # Assumes you have a requirements.txt file
- name: Cache test assets
uses: actions/cache@v2
with:
path: tests/demonstrations
key: assets-${{ github.sha }}
restore-keys: |
assets-
- name: Download test demos from release URL into `tests/demonstrations`
run: |
mkdir -p tests/demonstrations
curl -L -o tests/demonstrations/aaabtsd.zip https://github.com/McGill-NLP/weblinx/releases/download/tests-assets/aaabtsd.zip
unzip tests/demonstrations/aaabtsd.zip -d tests/demonstrations
curl -L -o tests/demonstrations/aajfwoq.zip https://github.com/McGill-NLP/weblinx/releases/download/tests-assets/aajfwoq.zip
unzip tests/demonstrations/aajfwoq.zip -d tests/demonstrations
- name: Run tests
run: |
python -m unittest discover -s tests
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,6 @@ cython_debug/
modeling/logs/
modeling/checkpoints/
modeling/results/
modeling/wl_data
modeling/wl_data
modeling/demonstrations/
tests/demonstrations/**/
13 changes: 13 additions & 0 deletions tests/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Tests

Welcome to the tests!

Please note that the license of the data in `tests/demonstrations*` follow the license from the official dataset, not the license of this repository.

## Running the tests

To run the unit tests, simply:

```bash
python -m unittest discover tests
```
3 changes: 3 additions & 0 deletions tests/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-e .[eval,dev,processing]
ujson
orjson
75 changes: 75 additions & 0 deletions tests/test_demonstration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import unittest
from unittest.mock import patch, MagicMock
from weblinx import format_repr, _validate_json_backend, Demonstration

class TestFormatRepr(unittest.TestCase):
def test_format_repr(self):
"""
Test the format_repr function to ensure it correctly formats the string
representation of an object. It checks that the output string matches
the expected format including class name and attributes.
"""
class FakeClass:
def __init__(self):
self.name = "test_name"
self.value = 123

obj = FakeClass()
result = format_repr(obj, "name", "value")
self.assertEqual(result, "FakeClass(name=test_name, value=123)")

class TestValidateJsonBackend(unittest.TestCase):
@patch('importlib.util.find_spec', return_value=MagicMock())
def test_validate_json_backend_valid(self, mock_find_spec):
"""
Ensure that the _validate_json_backend function does not raise an error
when passed a valid backend name. It tests the function with each valid
backend ('auto', 'json', 'orjson', 'ujson') to confirm proper handling.
"""
for backend in ["auto", "json", "orjson", "ujson"]:
_validate_json_backend(backend) # Should not raise

@patch('importlib.util.find_spec', return_value=None)
def test_validate_json_backend_invalid(self, mock_find_spec):
"""
Tests that the _validate_json_backend function raises a ValueError when
an invalid backend name is provided. This ensures that the function
properly validates backend inputs against expected values.
"""
with self.assertRaises(ValueError):
_validate_json_backend("invalid_backend")

class TestDemonstration(unittest.TestCase):
def setUp(self):
self.demo = Demonstration("aaabtsd", base_dir="./tests/demonstrations")

def test_repr(self):
"""
Tests the __repr__ method of the Demonstration class to ensure it
returns a string representation that accurately reflects the object's
current state, including its name and base directory.
"""
self.assertEqual(repr(self.demo), "Demonstration(name=aaabtsd, base_dir=./tests/demonstrations)")

def test_is_valid(self):
"""
Verifies that the is_valid method correctly identifies the validity of
a demonstration by ensuring all required files exist. The test assumes
all files are present as specified by the mock setup.
"""
self.assertTrue(self.demo.is_valid())

@patch('weblinx.Demonstration.load_json', return_value={"version": "1.0.0"})
def test_get_version(self, mock_load_json):
"""
Tests the get_version method of the Demonstration class to ensure it
returns the correct version of the demonstration, both as a tuple and
as a string, depending on the specified format.
"""
self.assertEqual(self.demo.get_version(), (1, 0, 0))
self.assertEqual(self.demo.get_version(as_tuple=False), "1.0.0")

# Additional tests for methods like load_json, save_json, etc. can be added here.

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

0 comments on commit 548e512

Please sign in to comment.