Mercurial > hg > Lemuriformes
comparison tests/test_uniques.py @ 18:56596902e9ae default tip
add some setup + tests
| author | Jeff Hammel <k0scist@gmail.com> |
|---|---|
| date | Sun, 10 Dec 2017 17:57:03 -0800 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 17:4793f99b73e0 | 18:56596902e9ae |
|---|---|
| 1 #!/usr/bin/env python | |
| 2 | |
| 3 """ | |
| 4 test CSV file uniques counter | |
| 5 """ | |
| 6 | |
| 7 import csv | |
| 8 import os | |
| 9 import shutil | |
| 10 import tempfile | |
| 11 import unittest | |
| 12 from lemuriformes.uniques import uniques | |
| 13 from StringIO import StringIO | |
| 14 | |
| 15 | |
| 16 class TestUniques(unittest.TestCase): | |
| 17 | |
| 18 header = ['a', 'b', 'c'] | |
| 19 dataset = [[1,2,3], | |
| 20 [1,1,2], | |
| 21 [1,2,4]] | |
| 22 | |
| 23 def write_dataset(self, fp): | |
| 24 | |
| 25 writer = csv.writer(fp) | |
| 26 writer.writerow(self.header) | |
| 27 writer.writerows(self.dataset) | |
| 28 fp.flush() | |
| 29 | |
| 30 def validate(self, uniq): | |
| 31 """validate `uniq` for object dataset""" | |
| 32 | |
| 33 # count them | |
| 34 counts = {key: len(value) | |
| 35 for key, value in uniq.items()} | |
| 36 assert counts['a'] == 1 | |
| 37 assert counts['b'] == 2 | |
| 38 assert counts['c'] == 3 | |
| 39 | |
| 40 | |
| 41 def test_file(self): | |
| 42 """test reading uniques from a file""" | |
| 43 | |
| 44 tmpdir = tempfile.mkdtemp() | |
| 45 try: | |
| 46 # write test data | |
| 47 dst = os.path.join(tmpdir, 'test.csv') | |
| 48 with open(dst, 'w') as fp: | |
| 49 self.write_dataset(fp) | |
| 50 | |
| 51 # determine uniques | |
| 52 self.validate(uniques(dst)) | |
| 53 | |
| 54 finally: | |
| 55 shutil.rmtree(tmpdir, ignore_errors=True) | |
| 56 | |
| 57 def test_buffer(self): | |
| 58 """test using a StringIO buffer object""" | |
| 59 | |
| 60 # write test data to buffer | |
| 61 buffer = StringIO() | |
| 62 self.write_dataset(buffer) | |
| 63 buffer.seek(0) | |
| 64 | |
| 65 # determine uniques | |
| 66 self.validate(uniques(buffer)) | |
| 67 | |
| 68 if __name__ == '__main__': | |
| 69 unittest.main() |
