Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lorenzo-dev-tests #51

Merged
merged 2 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 89 additions & 39 deletions mezcla/tests/test_misc_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,37 +12,39 @@
"""Tests for misc_utils module"""

# Standard packages
## TODO: from collections import defaultdict
import math
import datetime
import time
## NOTE: this is empty for now

# Installed packages
import pytest

# Local packages
from mezcla.unittest_wrapper import TestWrapper
from mezcla import glue_helpers as gh
from mezcla import debug
from mezcla import system
from mezcla.unittest_wrapper import TestWrapper

# Note: Two references are used for the module to be tested:
# THE_MODULE: global module object
# TestIt.script_module: path to file
# THE_MODULE: global module object
import mezcla.misc_utils as THE_MODULE

# Make sure more_itertools available
has_more_itertools = True
HAS_MORE_ITERTOOLS = True
try:
import more_itertools
except:
except ImportError:
system.print_exception_info("more_itertools import")
has_more_itertools = False
HAS_MORE_ITERTOOLS = False

class TestMiscUtils(TestWrapper):
"""Class for test case definitions"""
script_module = TestWrapper.get_testing_module_name(__file__, THE_MODULE)

def test_transitive_closure(self):
"""Ensure transitive_closure works as expected"""
debug.trace(4, "test_transitive_closure()")

actual = THE_MODULE.transitive_closure([(1, 2), (2, 3), (3, 4)])
expected = set([(1, 2), (1, 3), (1, 4), (2, 3), (3, 4), (2, 4)])
assert actual == expected
Expand All @@ -58,7 +60,7 @@ def test_read_tabular_data(self):
'language': 'Python\n',
'framework': 'Pytest\n',
}
temp_file = gh.get_temp_file()
temp_file = self.get_temp_file()
gh.write_file(temp_file, string_table)
assert THE_MODULE.read_tabular_data(temp_file) == dict_table

Expand All @@ -71,7 +73,7 @@ def test_is_prime(self):
"""Ensure is_prime works as expected"""
debug.trace(4, "test_is_prime()")

FIRST_100_PRIMES = [
first_100_primes = [
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37,
41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83,
89, 97, 101, 103, 107, 109, 113, 127, 131,
Expand All @@ -84,13 +86,14 @@ def test_is_prime(self):
449, 457, 461, 463, 467, 479, 487, 491, 499,
503, 509, 521, 523, 541,
]
assert all(THE_MODULE.is_prime(n) for n in FIRST_100_PRIMES)
assert all((not THE_MODULE.is_prime(n)) for n in range(FIRST_100_PRIMES[-1]) if n not in FIRST_100_PRIMES)

assert all(THE_MODULE.is_prime(n) for n in first_100_primes)
assert all((not THE_MODULE.is_prime(n)) for n in range(first_100_primes[-1]) if n not in first_100_primes)

def test_fibonacci(self):
"""Ensure fibonacci works as expected"""
debug.trace(4, "test_fibonacci()")
self.do_assert(not THE_MODULE.fibonacci(-5))
assert not THE_MODULE.fibonacci(-5)
assert THE_MODULE.fibonacci(1) == [0]
assert THE_MODULE.fibonacci(10) == [0, 1, 1, 2, 3, 5, 8]

Expand Down Expand Up @@ -124,11 +127,13 @@ def test_unzip(self):
assert THE_MODULE.unzip(zip(), 4) == [[], [], [], []]
assert THE_MODULE.unzip(zip(), 4) != [[], []]

@pytest.mark.xfail # TODO: remove xfail
def test_get_current_frame(self):
"""Ensure get_current_frame works as expected"""
debug.trace(4, "test_get_current_frame()")
self.do_assert(False, "TODO: code the test")
stack = str(THE_MODULE.get_current_frame())
test_name = system.get_current_function_name()
assert f'code {test_name}' in stack
assert __file__ in stack

def test_eval_expression(self):
"""Ensure eval_expression works as expected"""
Expand All @@ -141,25 +146,19 @@ def test_trace_named_object(self):
debug.trace(4, "test_trace_named_object()")
# With level -1 we ensure that the trace will be printed
THE_MODULE.trace_named_object(-1, "sys.argv")
## OLD
## captured = capsys.readouterr()
## assert "sys.argv" in captured.err
self.do_assert("sys.argv" in self.get_stderr())
captured = self.get_stderr()
assert "sys.argv" in captured

def test_trace_named_objects(self):
"""Ensure trace_named_objects works as expected"""
debug.trace(4, "test_trace_named_objects()")
# With level -1 we ensure that the trace will be printed
THE_MODULE.trace_named_objects(-1, "[len(sys.argv), sys.argv]")
## OLD:
## captured = capsys.readouterr()
## assert "len(sys.argv)" in captured.err
## assert "sys.argv" in captured.err
stderr = self.get_stderr()
self.do_assert("len(sys.argv)" in stderr)
self.do_assert("sys.argv" in stderr)

@pytest.mark.skipif(not has_more_itertools, reason="Unable to load more_itertools")
captured = self.get_stderr()
assert "len(sys.argv)" in captured
assert "sys.argv" in captured

@pytest.mark.skipif(not HAS_MORE_ITERTOOLS, reason="Unable to load more_itertools")
def test_exactly1(self):
"""Ensure exactly1 works as expected"""
debug.trace(4, "test_exactly1()")
Expand All @@ -172,9 +171,9 @@ def test_string_diff(self):
"""Ensure string_diff works as expected"""
debug.trace(4, "test_string_diff()")

STRING_ONE = 'one\ntwo\nthree\nfour'
STRING_TWO = 'one\ntoo\ntree\nfour'
EXPECTED_DIFF = (
string_one = 'one\ntwo\nthree\nfour'
string_two = 'one\ntoo\ntree\nfour'
expected_diff = (
' one\n'
'< two\n'
'… ^\n'
Expand All @@ -186,18 +185,69 @@ def test_string_diff(self):
' four\n'
)

assert THE_MODULE.string_diff(STRING_ONE, STRING_TWO) == EXPECTED_DIFF
assert THE_MODULE.string_diff(string_one, string_two) == expected_diff


@pytest.mark.xfail # TODO: remove xfail
def test_elide_string_values(self):
"""Ensure elide_string_values works as expected"""
debug.trace(4, "test_elide_string_values()")
self.do_assert(False, "TODO: code the test")

def test_misc(self):
"""Test miscellaneous stuff"""
debug.trace(4, "test_misc()")
assert("exactly_n" in dir(more_itertools))
hello = "hello"
assert 'hell...' == THE_MODULE.elide_string_values(hello, max_len=4)

def test_is_close(self):
"""ensure is_close works as expected"""
debug.trace(4, "test_is_close()")
assert THE_MODULE.is_close(1.0 + 0.999 , 2.0, 0.001)
assert not THE_MODULE.is_close(1.0 + 0.999 , 2.0, 0.0001)

def test_get_date_ddmmmyy(self):
"""ensure get_date_ddmmmyy works as expected"""
debug.trace(4, "test_get_date_ddmmmyy()")
assert THE_MODULE.get_date_ddmmmyy(datetime.date(2004, 9, 16)) == '16sep04'

def test_parse_timestamp(self):
"""ensure parse_timestamp works as expected"""
debug.trace(4, "test_parse_timestamp()")
ts = datetime.datetime(2004, 9, 16, 12, 30, 25, 123123)
ts_iso = '2004-09-16T12:30:25.1231234Z'
ts_iso_2 = '2004-09-16T12:30:25.123123Z'
parsed_ts = THE_MODULE.parse_timestamp(ts_iso, truncate=True)
parsed_ts_truncated = THE_MODULE.parse_timestamp(ts_iso_2, truncate=False)
assert parsed_ts == ts
assert parsed_ts_truncated == ts

def test_add_timestamp_diff(self):
"""ensure add_timestamp_diff works as expected"""
debug.trace(4, "test_add_timestamp_diff()")
timestamp = "timestamp 2004-09-16T12:30:25.1231234Z"
file_in = f"{self.temp_file}.in"
file_out = f"{self.temp_file}.out"
system.write_file(file_in, timestamp)
THE_MODULE.add_timestamp_diff(file_in, file_out)
contents = system.read_file(file_out)
assert contents == f"{timestamp} [0]\n"

def test_random_int(self):
"""ensure random_int works as expected"""
debug.trace(4, "test_random_int()")
assert 0 <= THE_MODULE.random_int(0,4) <= 4

def test_random_float(self):
"""ensure random_float works as expected"""
debug.trace(4, "test_random_float()")
assert 0 <= THE_MODULE.random_float(0,4.3) < 4.3

def test_time_function(self):
"""ensure time_function works as expected"""
debug.trace(4, "test_time_function()")
ms = THE_MODULE.time_function(time.sleep, 0.25)
assert math.floor(ms) == 250

def test_get_class_from_name(self):
"""ensure get_class_from_name works as expected"""
debug.trace(4, "test_get_class_from_name()")
result_class = THE_MODULE.get_class_from_name('date', 'datetime')
assert result_class is datetime.date

if __name__ == '__main__':
debug.trace_current_context()
Expand Down
Loading
Loading