Skip to content

Commit

Permalink
unittests
Browse files Browse the repository at this point in the history
  • Loading branch information
JarbasAl committed Dec 15, 2024
1 parent 82bb415 commit 879806c
Show file tree
Hide file tree
Showing 7 changed files with 235 additions and 3 deletions.
11 changes: 11 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# To get started with Dependabot version updates, you'll need to specify which
# package ecosystems to update and where the package manifests are located.
# Please see the documentation for all configuration options:
# https://docs.github.com/code-security/dependabot/dependabot-version-updates/configuration-options-for-the-dependabot.yml-file

version: 2
updates:
- package-ecosystem: "pip" # See documentation for possible values
directory: "/requirements" # Location of package manifests
schedule:
interval: "weekly"
66 changes: 66 additions & 0 deletions .github/workflows/unit_tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
name: Run UnitTests
on:
pull_request:
branches:
- dev
paths-ignore:
- "ovos_utterance_corrections_transformer/version.py"
- "examples/**"
- ".github/**"
- ".gitignore"
- "LICENSE"
- "CHANGELOG.md"
- "MANIFEST.in"
- "README.md"
- "scripts/**"
push:
branches:
- master
paths-ignore:
- "ovos_utterance_corrections_transformer/version.py"
- "requirements/**"
- "examples/**"
- ".github/**"
- ".gitignore"
- "LICENSE"
- "CHANGELOG.md"
- "MANIFEST.in"
- "README.md"
- "scripts/**"
workflow_dispatch:

jobs:
unit_tests:
strategy:
max-parallel: 2
matrix:
python-version: [3.8, 3.9, "3.10", "3.11"]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
cache: "pip"
- name: Install System Dependencies
run: |
sudo apt-get update
sudo apt install python3-dev swig libssl-dev
python -m pip install build wheel
- name: Install core repo
run: |
pip install .
- name: Install test dependencies
run: |
pip install -r tests/requirements.txt
- name: Run unittests
run: |
pytest --cov=ovos_utterance_corrections_transformer --cov-report xml tests
# NOTE: additional pytest invocations should also add the --cov-append flag
# or they will overwrite previous invocations' coverage reports
# (for an example, see OVOS Skill Manager's workflow)
- name: Upload coverage
env:
CODECOV_TOKEN: ${{secrets.CODECOV_TOKEN}}
uses: codecov/codecov-action@v4
2 changes: 1 addition & 1 deletion ovos_utterance_corrections_transformer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def transform(self, utterances: List[str], context: Optional[dict] = None) -> (l
continue

# Compile pattern with timeout
compiled_pattern = regex.compile(pattern, flags=flags, timeout=1.0)
compiled_pattern = regex.compile(pattern, flags=flags)
utterances[idx] = compiled_pattern.sub(replacement, utterances[idx])

except regex.error as e:
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ovos-plugin-manager>=0.0.1,<1.0.0
15 changes: 13 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,21 @@ def get_version():
return version


def required(requirements_file):
""" Read requirements file and remove comments and empty lines. """
with open(os.path.join(BASEDIR, requirements_file), 'r') as f:
requirements = f.read().splitlines()
if 'MYCROFT_LOOSE_REQUIREMENTS' in os.environ:
print('USING LOOSE REQUIREMENTS!')
requirements = [r.replace('==', '>=').replace('~=', '>=') for r in requirements]
return [pkg for pkg in requirements
if pkg.strip() and not pkg.startswith("#")]


UTTERANCE_ENTRY_POINT = (
'ovos-utterance-corrections-plugin=ovos_utterance_corrections_transformer:UtteranceCorrectionsPlugin'
)


setup(
name='ovos-utterance-corrections-plugin',
version=get_version(),
Expand All @@ -44,6 +54,7 @@ def get_version():
license='apache-2.0',
packages=['ovos_utterance_corrections_transformer'],
include_package_data=True,
install_requires=required("requirements.txt"),
zip_safe=True,
classifiers=[
'Development Status :: 3 - Alpha',
Expand All @@ -57,4 +68,4 @@ def get_version():
entry_points={
'neon.plugin.text': UTTERANCE_ENTRY_POINT
}
)
)
6 changes: 6 additions & 0 deletions tests/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
coveralls==1.8.2
flake8==3.7.9
pytest==8.2.2
pytest-cov==2.8.1
cov-core==1.15.0
ovos-plugin-manager
137 changes: 137 additions & 0 deletions tests/test_plug.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
import unittest
from unittest.mock import patch

from ovos_utterance_corrections_transformer import UtteranceCorrectionsPlugin


class TestUtteranceCorrectionsPlugin(unittest.TestCase):

def setUp(self):
self.plugin = UtteranceCorrectionsPlugin()

def test_transform_full_utterance_replacement(self):
# Mock the match_one function
self.plugin.config = {"thresh": 0.85} # Set threshold for replacement
self.plugin.db = {"test utterance": "replaced utterance"}

# Test input
utterances = ["test utterance"]
transformed, context = self.plugin.transform(utterances)

# Assertions
self.assertEqual(transformed, ["replaced utterance"])
self.assertEqual(context, {})

def test_transform_regex_no_match(self):
# Mock the regex_db with a pattern that won't match
self.plugin.config = {"ignore_case": True}
self.plugin.regex_db = {
r"\bsh(\w*)": "sch\\1"
}

# Test input with no match
utterances = ["hello"]
transformed, context = self.plugin.transform(utterances)

# Assertions
self.assertEqual(transformed, ["hello"])
self.assertEqual(context, {})

def test_transform_regex_replacement(self):
# Mock the regex_db with a valid pattern and replacement
self.plugin.config = {"ignore_case": True}
self.plugin.regex_db = {
r"\bsh(\w*)": "sch\\1"
}

# Test input with matching regex pattern
utterances = ["shalter"]
transformed, context = self.plugin.transform(utterances)

# Assertions
self.assertEqual(transformed, ["schalter"])
self.assertEqual(context, {})

@patch("ovos_utterance_corrections_transformer.regex.compile") # Mock regex compile
def test_transform_regex_timeout(self, MockRegexCompile):
# Mock the regex_db with a valid pattern and replacement
self.plugin.config = {"ignore_case": True}
self.plugin.regex_db = {
r"\bsh(\w*)": "sch\\1"
}

# Mock regex to raise a TimeoutError
MockRegexCompile.side_effect = TimeoutError("Regex pattern timed out")

# Test input with matching regex pattern
utterances = ["shalter"]
transformed, context = self.plugin.transform(utterances)

# Assertions - no change because of timeout
self.assertEqual(transformed, ["shalter"])
self.assertEqual(context, {})

def test_transform_oversized_regex_pattern(self):
# Mock the regex_db with a large pattern that exceeds the size limit
self.plugin.config = {"ignore_case": True}
self.plugin.regex_db = {
"a" * 1001: "replaced" # Pattern longer than 1000 characters
}

# Test input
utterances = ["this is a test"]
transformed, context = self.plugin.transform(utterances)

# Assertions
self.assertEqual(transformed, ["this is a test"]) # No change due to oversized pattern
self.assertEqual(context, {})

def test_transform_word_replacement(self):
# Mock the words_db with a valid word and replacement
self.plugin.words_db = {"test": "exam"}

# Test input with word replacement
utterances = ["this is a test"]
transformed, context = self.plugin.transform(utterances)

# Assertions
self.assertEqual(transformed, ["this is a exam"])
self.assertEqual(context, {})

def test_transform_multiple_replacements(self):
# Mock the words_db with multiple words and replacements
self.plugin.words_db = {"test": "exam", "hello": "hi"}

# Test input with multiple word replacements
utterances = ["hello, this is a test"]
transformed, context = self.plugin.transform(utterances)

# Assertions
self.assertEqual(transformed, ["hi, this is a exam"])
self.assertEqual(context, {})

def test_transform_empty_input(self):
# Test with empty input
utterances = []
transformed, context = self.plugin.transform(utterances)

# Assertions
self.assertEqual(transformed, [])
self.assertEqual(context, {})

def test_transform_no_match(self):
# Test input with no matching patterns
self.plugin.regex_db = {
r"\bxyz(\w*)": "abc\\1"
}

utterances = ["test"]
transformed, context = self.plugin.transform(utterances)

# Assertions
self.assertEqual(transformed, ["test"])
self.assertEqual(context, {})


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

0 comments on commit 879806c

Please sign in to comment.