Skip to content
This repository has been archived by the owner on Mar 10, 2023. It is now read-only.

Commit

Permalink
Merge pull request #13 from Edvinas01/master
Browse files Browse the repository at this point in the history
Add tests for util functions
  • Loading branch information
ugnelis authored Oct 13, 2017
2 parents ad1966d + d495804 commit d05a504
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 11 deletions.
6 changes: 5 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ python:
- "3.5"
install:
- pip install -r requirements.txt

# See https://gist.github.com/dan-blanchard/7045057
- pip install --upgrade pip setuptools wheel
- pip install --only-binary=numpy,scipy numpy scipy
script:
- python -m unittest discover --pattern=*_test.py
- python -m unittest discover tests
notifications:
email:
on_success: never
Expand Down
10 changes: 0 additions & 10 deletions tests/example_test.py

This file was deleted.

83 changes: 83 additions & 0 deletions tests/utils_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import unittest
import utils

from tempfile import mkdtemp, mktemp
from shutil import rmtree

# Extension of created test files.
TEST_FILE_EXTENSION = 'txt'

# Directory of test suite wav audio files.
TEST_AUDIO_FILE_DIR = 'data/LibriSpeech/test-clean-wav'


class UtilsTest(unittest.TestCase):
def setUp(self):
self.test_file_dir = mkdtemp()

def tearDown(self):
rmtree(self.test_file_dir)

def test_read_text_file(self):
content, file_path = self.create_test_file('test')
self.assertEqual(utils.read_text_file(file_path), content)

def test_make_char_array(self):
self.assertEqual(utils.make_char_array('ab').tolist(), ['a', 'b'])

def test_normalize_text(self):
text = 'A\' '

self.assertEqual(utils.normalize_text(text), 'a')
self.assertEqual(utils.normalize_text(text, False), 'a\'')

def test_sparse_tuples_from_sequences(self):
result = utils.sparse_tuples_from_sequences([[1], [2, 3]])

self.assertEqual(result[0].tolist(), [[0, 0], [1, 0], [1, 1]])
self.assertEqual(result[1].tolist(), [1, 2, 3])
self.assertEqual(result[2].tolist(), [2, 2])

def test_read_audio_files(self):
self.assertTrue(utils.read_audio_files(TEST_AUDIO_FILE_DIR).size > 0)

def test_read_text_files(self):
content = self.create_test_file('test')[0]
self.assertEqual(utils.read_text_files(self.test_file_dir, [TEST_FILE_EXTENSION]), content)

def test_sequence_decoder(self):
self.assertEqual(utils.sequence_decoder([1, 2, 3]), 'abc')

def test_texts_encoder(self):
self.assertEqual(utils.texts_encoder(['abc']).tolist()[0], [1, 2, 3])

def test_standardize_audios(self):
files = utils.read_audio_files(TEST_AUDIO_FILE_DIR)
self.assertEqual(utils.standardize_audios(files).size, files.size)

def test_get_sequence_lengths(self):
self.assertEqual(utils.get_sequence_lengths([[1], [], [1, 2]]).tolist(), [1, 0, 2])

def test_make_sequences_same_length(self):
self.assertEqual(utils.make_sequences_same_length([[1, 2], []], [2, 0]).tolist(), [[1.0, 2.0], [0.0, 0.0]])

def create_test_file(self, content):
"""
Write a string to a new temporary test file.
Args:
content:
Content to write to the text file.
Returns:
A tuple with (file content, file path).
"""
file_path = mktemp(suffix='.' + TEST_FILE_EXTENSION, dir=self.test_file_dir)

with open(file_path, 'w') as f:
f.write(content)

return content, file_path


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

0 comments on commit d05a504

Please sign in to comment.