Skip to content

Commit

Permalink
Change tests to unittest.TestCase, improve test coverage (#63)
Browse files Browse the repository at this point in the history
* Fix indentations

* Change _add_special_tokens into staticmethod

* Change all tests of data module to unittest.TestCase

* Remove unused code

* Update README.md

* Add test for read_ui function

* Change tests of all datasets to unittest.TestCase

* Do not repeatedly showing split message in RatioSplit

* Change tests of eval_methods to unittest.TestCase

* Change tests of experiment to unittest.TestCase

* Change tests of metrics to unittest.TestCase

* Change tests of utils to unittest.TestCase

* Improve test coverage

* Fix indentations in test_trainset.py
  • Loading branch information
tqtg committed Mar 21, 2019
1 parent b4fb862 commit bf5a444
Show file tree
Hide file tree
Showing 16 changed files with 576 additions and 521 deletions.
2 changes: 0 additions & 2 deletions cornac/eval_methods/ratio_split.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,6 @@ def validate_size(val_size, test_size, num_ratings):

def split(self):
if self._split_ran:
if self.verbose:
print('Data is already split!')
return

if self.verbose:
Expand Down
8 changes: 8 additions & 0 deletions tests/cornac/data/test_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@

class TestReader(unittest.TestCase):

def test_read_ui(self):
data_file = './tests/data.txt'
triplets = reader.read_ui(data_file, value=2.0)

self.assertEqual(len(triplets), 30)
self.assertEqual(triplets[0][1], '93')
self.assertEqual(triplets[1][2], 2.0)

def test_read_uir(self):
data_file = './tests/data.txt'
triplet_data = reader.read_uir(data_file)
Expand Down
8 changes: 7 additions & 1 deletion tests/cornac/data/test_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ def setUp(self):
self.module.build(self.id_map)
self.token_ids = (self.module.vocab.tok2idx[tok] for tok in self.tokens)


def test_init(self):
self.assertCountEqual(self.module.vocab.idx2tok,
SPECIAL_TOKENS + self.tokens)
Expand Down Expand Up @@ -114,5 +113,12 @@ def test_batch_seq(self):
np.asarray([[a, b, c, 0],
[c, b, e, c]]))

self.module.sequences = None
try:
self.module.batch_seq([0])
except ValueError:
assert True


if __name__ == '__main__':
unittest.main()
14 changes: 10 additions & 4 deletions tests/cornac/data/test_trainset.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@
"""

import unittest
import numpy as np
import numpy.testing as npt
from collections import OrderedDict

from cornac.data import reader
from cornac.data import TrainSet
from cornac.data import MatrixTrainSet

from collections import OrderedDict
import numpy as np
import numpy.testing as npt


class TestTrainSet(unittest.TestCase):

Expand Down Expand Up @@ -132,6 +131,13 @@ def test_uir_tuple(self):
global_ui_set=None,
verbose=True)

self.assertEqual(len(train_set.uir_tuple), 3)
self.assertEqual(len(train_set.uir_tuple[0]), 10)

train_set.uir_tuple = None
self.assertEqual(len(train_set.uir_tuple[1]), 10)
self.assertEqual(len(train_set.uir_tuple[2]), 10)

try:
train_set.uir_tuple = ([], [])
except ValueError:
Expand Down
33 changes: 20 additions & 13 deletions tests/cornac/datasets/test_movielens.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,28 @@
@author: Quoc-Tuan Truong <tuantq.vnu@gmail.com>
"""

import random, time
import unittest
import random
import time
from cornac.datasets import movielens


def test_movielens_100k():
# only run data download tests 20% of the time to speed up frequent testing
random.seed(time.time())
if random.random() > 0.8:
ml_100k = movielens.load_100k()
assert len(ml_100k) == 100000
class TestMovieLens(unittest.TestCase):

def test_movielens_100k(self):
# only run data download tests 20% of the time to speed up frequent testing
random.seed(time.time())
if random.random() > 0.8:
ml_100k = movielens.load_100k()
self.assertEqual(len(ml_100k), 100000)

def test_movielens_1m():
# only run data download tests 20% of the time to speed up frequent testing
random.seed(time.time())
if random.random() > 0.8:
ml_1m = movielens.load_1m()
assert len(ml_1m) == 1000209
def test_movielens_1m(self):
# only run data download tests 20% of the time to speed up frequent testing
random.seed(time.time())
if random.random() > 0.8:
ml_1m = movielens.load_1m()
self.assertEqual(len(ml_1m), 1000209)


if __name__ == '__main__':
unittest.main()
22 changes: 15 additions & 7 deletions tests/cornac/datasets/test_netflix.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,21 @@
@author: Quoc-Tuan Truong <tuantq.vnu@gmail.com>
"""

import random, time
import unittest
import random
import time
from cornac.datasets import netflix


def test_load_data_small():
# only run data download tests 20% of the time to speed up frequent testing
random.seed(time.time())
if random.random() > 0.8:
data = netflix.load_data_small()
assert len(data) == 607803
class TestNetflix(unittest.TestCase):

def test_load_data_small(self):
# only run data download tests 20% of the time to speed up frequent testing
random.seed(time.time())
if random.random() > 0.8:
data = netflix.load_data_small()
self.assertEqual(len(data), 607803)


if __name__ == '__main__':
unittest.main()
22 changes: 15 additions & 7 deletions tests/cornac/datasets/test_tradesy.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,21 @@
@author: Quoc-Tuan Truong <tuantq.vnu@gmail.com>
"""

import random, time
import unittest
import random
import time
from cornac.datasets import tradesy


def test_load_data():
# only run data download tests 20% of the time to speed up frequent testing
random.seed(time.time())
if random.random() > 0.8:
data = tradesy.load_data()
assert len(data) == 394421
class TestTradesy(unittest.TestCase):

def test_load_data(self):
# only run data download tests 20% of the time to speed up frequent testing
random.seed(time.time())
if random.random() > 0.8:
data = tradesy.load_data()
self.assertEqual(len(data), 394421)


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

0 comments on commit bf5a444

Please sign in to comment.