Skip to content

Commit

Permalink
Merge pull request #562 from RodrigoGCoelhoo/new-tests
Browse files Browse the repository at this point in the history
New tests for formats, predicates, tables and utils
  • Loading branch information
davidwagner committed Apr 17, 2023
2 parents 4a2d9f8 + 53c3cfe commit 5c4a99f
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 2 deletions.
33 changes: 32 additions & 1 deletion tests/test_formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import re
import datascience as ds
from datascience import formats
import os


def assert_equal(string1, string2):
Expand All @@ -27,6 +28,14 @@ def test_default_format():
assert_equal(fmt('hello'), 'hello')
assert_equal(fmt((1, 2)), '(1, 2)')

def test_default_format_column():
fmtc = ds.default_formatter.format_column

description_list = ["A data scientist is someone who creates programming code and combines it with statistical knowledge to create insights from data."]
pad = fmtc("Careers", description_list)
assert_equal(pad(description_list[0]), "A data scientist is someone who creates programming code ...")



def test_number_format():
for fmt in [ds.NumberFormatter(2), ds.NumberFormatter]:
Expand Down Expand Up @@ -83,6 +92,12 @@ def test_date_format():
assert isinstance(t['time'][0], float)


def test_date_formatter_format_value():
formatter = formats.DateFormatter()
os.environ["TZ"] = "UTC"
assert_equal(formatter.format_value(1666264489.9004), "2022-10-20 11:14:49.900400")


def test_percent_formatter():
vs = [0.1, 0.11111, 0.199999, 10]
t = ds.Table().with_column('percent', vs)
Expand All @@ -107,10 +122,26 @@ def test_distribution_formatter():
38.33%
""")

counts = [0, 0, 0, 0]
t = ds.Table().with_column('count', counts)
t.set_format('count', ds.DistributionFormatter)
assert_equal(t, """
count
0.00%
0.00%
0.00%
0.00%
""")

def test_empty_init():
formatter = formats.Formatter(1, 10, "...")
assert_equal(formatter.min_width, 1)
assert_equal(formatter.max_width, 10)
assert_equal(formatter.etc, "...")

def test_function_formatter():
def _mult_ten(v):
return v * 10

func_formatter = formats.FunctionFormatter(_mult_ten)
assert func_formatter.format_value(5) == '50'
assert func_formatter.format_value(5) == '50'
16 changes: 16 additions & 0 deletions tests/test_predicates.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,22 @@ def test_between_or_equal_to():
ps = [p(x) for x in range(1, 6)]
assert ps == [False, False, True, False, False]

def test_not_equal_to():
"""Greater than or equal to y and less than or equal to z."""
p = are.not_equal_to(5)
ps = [p(x) for x in range(1, 6)]
assert ps == [True, True, True, True, False]

def test_not():
"""Not f."""
not_f = predicates._not(lambda x: 2*x)
assert not_f(5) == -10

def test_xor():
"""XOR f and g."""
p = are.above(3) ^ are.below(2)
ps = [p(x) for x in range(1, 6)]
assert ps == [True, False, False, True, True]

############
# Doctests #
Expand Down
2 changes: 1 addition & 1 deletion tests/test_tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -1906,4 +1906,4 @@ def test_no_records():
"""Test that Table returns nothing on an empty record"""
empty_table = Table()
records_empty_table = Table().from_records([])
assert empty_table == records_empty_table
assert empty_table == records_empty_table
2 changes: 2 additions & 0 deletions tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ def test_make_array():
assert test2.dtype == "int64"
test3 = ds.make_array("foo", "bar")
assert test3.dtype == "<U3"
test4 = ds.make_array(list(range(10)))
assert test4.dtype == "object"


def test_percentile():
Expand Down

0 comments on commit 5c4a99f

Please sign in to comment.