From a401130ce9d0761f23bd25611b31584b26f69154 Mon Sep 17 00:00:00 2001 From: Niko Ehrenfeuchter <nikolaus.ehrenfeuchter@unibas.ch> Date: Wed, 21 Nov 2018 16:52:27 +0100 Subject: [PATCH] More unittests --- tests/test_iotools.py | 57 +++++++++++++++++++++++++++++++++++++++++ tests/test_pathtools.py | 14 ++++++++++ tests/test_strtools.py | 32 +++++++++++++++++++++++ 3 files changed, 103 insertions(+) create mode 100644 tests/test_iotools.py create mode 100644 tests/test_strtools.py diff --git a/tests/test_iotools.py b/tests/test_iotools.py new file mode 100644 index 0000000..1208476 --- /dev/null +++ b/tests/test_iotools.py @@ -0,0 +1,57 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +import pytest +import zipfile + +from os.path import join + +from imcflibs.iotools import filehandle +from imcflibs.iotools import readtxt + +__author__ = "Niko Ehrenfeuchter" +__copyright__ = "Niko Ehrenfeuchter" +__license__ = "gpl3" + + +def test_filehandle(tmpdir): + tmpfile = tmpdir.join('testfile') + tmpname = str(tmpfile) + tmphandle = open(str(tmpfile), 'w') + print(type(tmphandle)) + assert type(tmpname) is str + assert type(tmphandle) is file + assert type(filehandle(tmpname)) is file + assert type(filehandle(tmphandle, 'w')) is file + + +def test_readtxt(tmpdir): + content = [ + u'lorem\n', + u'ipsum\n', + u'and some more\n', + u'dummy text\n', + ] + fh = tmpdir.mkdir("readtxt").join("content.txt") + fh.write(u''.join(content)) + print(fh.basename) + print(fh.dirname) + with zipfile.ZipFile(join(fh.dirname, 'archive.zip'), 'w') as zf: + zf.write(str(fh), arcname='content.txt') + print("wrote [%s] into [%s]" % (str(fh), zf.filename)) + + print(content) + fromfile = readtxt(str(fh)) + print(fromfile) + assert fromfile == content + + fromfile_flat = readtxt(str(fh), flat=True) + assert fromfile_flat == ''.join(content) + + print(join(fh.dirname, 'archive.zip')) + fromzip = readtxt('content.txt', join(fh.dirname, 'archive.zip')) + print(fromzip) + assert fromzip == content + + fromzip_flat = readtxt('content.txt', join(fh.dirname, 'archive.zip'), flat=True) + assert fromzip_flat == ''.join(content) diff --git a/tests/test_pathtools.py b/tests/test_pathtools.py index 07c6813..0063683 100644 --- a/tests/test_pathtools.py +++ b/tests/test_pathtools.py @@ -3,6 +3,7 @@ import pytest from imcflibs.pathtools import parse_path +from imcflibs.pathtools import jython_fiji_exists __author__ = "Niko Ehrenfeuchter" __copyright__ = "Niko Ehrenfeuchter" @@ -31,3 +32,16 @@ def test_parse_path(): assert path_to_dir['dname'] == 'foo' assert path_to_dir['ext'] == '' + +def test_parse_path_windows(): + path = r'C:\foo\bar' + parsed = parse_path(path) + + assert parsed['orig'] == path + assert parsed['full'] == r'C:/foo/bar' + assert parsed['fname'] == 'bar' + assert parsed['dname'] == 'foo' + + +def test_jython_fiji_exists(tmpdir): + assert jython_fiji_exists(str(tmpdir)) == True diff --git a/tests/test_strtools.py b/tests/test_strtools.py new file mode 100644 index 0000000..c49a762 --- /dev/null +++ b/tests/test_strtools.py @@ -0,0 +1,32 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +import pytest +import os + +from imcflibs.strtools import _is_string_like +from imcflibs.strtools import filename +from imcflibs.strtools import flatten + +__author__ = "Niko Ehrenfeuchter" +__copyright__ = "Niko Ehrenfeuchter" +__license__ = "gpl3" + + +def test__is_string_like(): + assert _is_string_like('foo') == True + assert _is_string_like(12345) == False + + +def test_filename_from_string(): + assert filename('test_file_name') == 'test_file_name' + + +def test_filename_from_handle(tmpdir): + path = str(tmpdir) + fhandle = tmpdir.join('foo.txt') + assert filename(fhandle) == os.path.join(path, 'foo.txt') + + +def test_flatten(): + assert flatten(('foo', 'bar')) == 'foobar' \ No newline at end of file -- GitLab