Skip to content

Commit

Permalink
tests (#4)
Browse files Browse the repository at this point in the history
* feat:semver

* port unittests

* test

* test

* test

* test

* fix:keyerror instantiations from disk
  • Loading branch information
JarbasAl authored Oct 14, 2024
1 parent 0af0eb6 commit 0751eb1
Show file tree
Hide file tree
Showing 14 changed files with 169 additions and 128 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
- name: Install System Dependencies
run: |
sudo apt-get update
sudo apt install python3-dev swig libssl-dev libfann-dev portaudio19-dev libpulse-dev
sudo apt install python3-dev swig libfann-dev
- name: Build Source Packages
run: |
python setup.py sdist
Expand Down
44 changes: 0 additions & 44 deletions .github/workflows/coverage.yml

This file was deleted.

2 changes: 1 addition & 1 deletion .github/workflows/install_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
- name: Install System Dependencies
run: |
sudo apt-get update
sudo apt install python3-dev swig libssl-dev
sudo apt install python3-dev swig libfann-dev
- name: Build Distribution Packages
run: |
python setup.py bdist_wheel
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/license_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
- name: Install System Dependencies
run: |
sudo apt-get update
sudo apt install python3-dev swig libssl-dev
sudo apt install python3-dev swig libfann-dev
- name: Install core repo
run: |
pip install .
Expand Down
13 changes: 5 additions & 8 deletions .github/workflows/unit_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on:
branches:
- dev
paths-ignore:
- 'ovos_bus_client/version.py'
- 'ovos_padatious/version.py'
- 'examples/**'
- '.github/**'
- '.gitignore'
Expand All @@ -17,7 +17,7 @@ on:
branches:
- master
paths-ignore:
- 'ovos_bus_client/version.py'
- 'ovos_padatious/version.py'
- 'requirements/**'
- 'examples/**'
- '.github/**'
Expand All @@ -33,7 +33,7 @@ jobs:
unit_tests:
strategy:
matrix:
python-version: [ 3.7, 3.8, 3.9, '3.10']
python-version: [3.9, '3.10']
runs-on: ubuntu-latest
timeout-minutes: 15
steps:
Expand All @@ -45,7 +45,7 @@ jobs:
- name: Install System Dependencies
run: |
sudo apt-get update
sudo apt install python3-dev swig
sudo apt install python3-dev swig libfann-dev
python -m pip install build wheel
- name: Install repo
run: |
Expand All @@ -55,10 +55,7 @@ jobs:
pip install -r tests/requirements.txt
- name: Run unittests
run: |
pytest --cov=ovos_bus_client --cov-report=xml tests/unittests
# 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)
pytest --cov=ovos_padatious --cov-report=xml ./tests
- name: Upload coverage
if: "${{ matrix.python-version == '3.9' }}"
uses: codecov/codecov-action@v3
Expand Down
22 changes: 12 additions & 10 deletions ovos_padatious/intent_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,18 +98,20 @@ def instantiate_from_disk(self):

if f.startswith('{') and f.endswith('}.hash'):
entity_name = f[1:f.find('}.hash')]
self.add_entity(
name=entity_name,
lines=entity_traindata[entity_name],
reload_cache=False,
must_train=False)
if entity_name in entity_traindata:
self.add_entity(
name=entity_name,
lines=entity_traindata[entity_name],
reload_cache=False,
must_train=False)
elif not f.startswith('{') and f.endswith('.hash'):
intent_name = f[0:f.find('.hash')]
self.add_intent(
name=intent_name,
lines=intent_traindata[intent_name],
reload_cache=False,
must_train=False)
if intent_name in intent_traindata:
self.add_intent(
name=intent_name,
lines=intent_traindata[intent_name],
reload_cache=False,
must_train=False)

@_save_args
def add_intent(self, name, lines, reload_cache=False, must_train=True):
Expand Down
8 changes: 6 additions & 2 deletions tests/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
flake8
pytest
coveralls>=1.8.2
flake8>=3.7.9
pytest>=5.2.4
pytest-cov>=2.8.1
ovos-utils
ovos-bus-client
6 changes: 3 additions & 3 deletions tests/test_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import unittest
from os.path import isdir
from shutil import rmtree

from ovos_padatious.intent_container import IntentContainer


class TestAll:
def setup(self):
class TestAll(unittest.TestCase):
def setUp(self):
self.cont = IntentContainer('temp')

def test_simple(self):
Expand Down
103 changes: 60 additions & 43 deletions tests/test_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
from time import monotonic

import unittest
import os
import pytest
import random
Expand All @@ -23,7 +23,7 @@
from ovos_padatious.intent_container import IntentContainer


class TestIntentContainer:
class TestFromDisk(unittest.TestCase):
test_lines = ['this is a test\n', 'another test\n']
other_lines = ['something else\n', 'this is a different thing\n']
test_lines_with_entities = ['this is a {test}\n', 'another {test}\n']
Expand All @@ -33,37 +33,16 @@ class TestIntentContainer:
test_entities = ['test\n', 'assessment\n']
other_entities = ['else\n', 'different\n']

def setup(self):
def setUp(self):
self.cont = IntentContainer('temp')

def test_add_intent(self):
def _add_intent(self):
self.cont.add_intent('test', self.test_lines)
self.cont.add_intent('other', self.other_lines)

def test_load_intent(self):
if not isdir('temp'):
mkdir('temp')

fn1 = join('temp', 'test.txt')
with open(fn1, 'w') as f:
f.writelines(self.test_lines)

fn2 = join('temp', 'other.txt')
with open(fn2, 'w') as f:
f.writelines(self.other_lines)

self.cont.load_intent('test', fn1)
self.cont.load_intent('other', fn1)
assert len(self.cont.intents.train_data.sent_lists) == 2

def test_train(self):
def test(a, b):
self.setup()
self.test_add_intent()
self.cont.train(a, b)

test(False, False)
test(True, True)
self.cont.add_entity('test', self.test_entities)
self.cont.add_entity('other', self.other_entities)
self.cont.train()
self._write_train_data()

def _write_train_data(self):

Expand All @@ -88,24 +67,62 @@ def _write_train_data(self):

def test_instantiate_from_disk(self):
# train and cache (i.e. persist)
self.setup()
self.test_add_intent()
self.cont.add_entity('test', self.test_entities)
self.cont.add_entity('other', self.other_entities)
self.cont.train()
self._write_train_data()
self._add_intent()

# instantiate from disk (load cached files)
self.setup()
self.cont.instantiate_from_disk()
cont = IntentContainer('temp')
cont.instantiate_from_disk()

assert len(self.cont.intents.train_data.sent_lists) == 0
assert len(self.cont.intents.objects_to_train) == 0
assert len(self.cont.intents.objects) == 2
assert len(cont.intents.train_data.sent_lists) == 0
assert len(cont.intents.objects_to_train) == 0
assert len(cont.intents.objects) == 2

result = self.cont.calc_intent('something different')
result = cont.calc_intent('something different')
assert result.matches['other'] == 'different'


class TestIntentContainer(unittest.TestCase):
test_lines = ['this is a test\n', 'another test\n']
other_lines = ['something else\n', 'this is a different thing\n']
test_lines_with_entities = ['this is a {test}\n', 'another {test}\n']
other_lines_with_entities = [
'something {other}\n',
'this is a {other} thing\n']
test_entities = ['test\n', 'assessment\n']
other_entities = ['else\n', 'different\n']

def setUp(self):
self.cont = IntentContainer('temp')

def _add_intent(self):
self.cont.add_intent('test', self.test_lines)
self.cont.add_intent('other', self.other_lines)

def test_load_intent(self):
if not isdir('temp'):
mkdir('temp')

fn1 = join('temp', 'test.txt')
with open(fn1, 'w') as f:
f.writelines(self.test_lines)

fn2 = join('temp', 'other.txt')
with open(fn2, 'w') as f:
f.writelines(self.other_lines)

self.cont.load_intent('test', fn1)
self.cont.load_intent('other', fn1)
assert len(self.cont.intents.train_data.sent_lists) == 2

def test_train(self):
def test(a, b):
self._add_intent()
self.cont.train(a, b)

test(False, False)
test(True, True)


def _create_large_intent(self, depth):
if depth == 0:
return '(a|b|)'
Expand Down Expand Up @@ -161,7 +178,7 @@ def test_train_subprocess(self):
assert intent.matches == {'time': '3'}

def test_calc_intents(self):
self.test_add_intent()
self._add_intent()
self.cont.train(False)

intents = self.cont.calc_intents('this is another test')
Expand Down Expand Up @@ -198,7 +215,7 @@ def test_namespaced_entities(self):
self._test_entities('SkillName:')

def test_remove(self):
self.test_add_intent()
self._add_intent()
self.cont.train(False)
assert self.cont.calc_intent('This is a test').conf == 1.0
self.cont.remove_intent('test')
Expand Down
6 changes: 3 additions & 3 deletions tests/test_entity_edge.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import unittest
from ovos_padatious.entity_edge import EntityEdge
from ovos_padatious.train_data import TrainData


class TestEntityEdge:
def setup(self):
class TestEntityEdge(unittest.TestCase):
def setUp(self):
self.data = TrainData()
self.data.add_lines('', ['a {word} here', 'the {word} here'])
self.le = EntityEdge(-1, '{word}', '')
Expand Down
6 changes: 3 additions & 3 deletions tests/test_intent.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import unittest
from os import mkdir
from os.path import isdir
from shutil import rmtree
Expand All @@ -20,8 +20,8 @@
from ovos_padatious.train_data import TrainData


class TestIntent:
def setup(self):
class TestIntent(unittest.TestCase):
def setUp(self):
self.data = TrainData()
self.data.add_lines('hi', ['hello', 'hi', 'hi there'])
self.data.add_lines('bye', ['goodbye', 'bye', 'bye {person}', 'see you later'])
Expand Down
6 changes: 3 additions & 3 deletions tests/test_match_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import unittest
from ovos_padatious.match_data import MatchData


class TestMatchData:
def setup(self):
class TestMatchData(unittest.TestCase):
def setUp(self):
self.match = MatchData('name', ['one', 'two'], {'{word}': ['value', 'tokens']}, 0.5)
self.sentence = ["it", "'", "s", "a", "new", "sentence"]
self.sentence2 = ["the", "parents", "'", "house"]
Expand Down
Loading

0 comments on commit 0751eb1

Please sign in to comment.