-
Notifications
You must be signed in to change notification settings - Fork 194
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unit tests for randomtable, dummytable, and their supporting func…
…tions and classes.
- Loading branch information
Showing
1 changed file
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import random | ||
from functools import partial | ||
|
||
from petl.util.random import randomseed, randomtable, RandomTable, dummytable, DummyTable | ||
|
||
|
||
def test_randomseed(): | ||
""" | ||
Ensure that randomseed provides a non-empty string that changes. | ||
""" | ||
s1 = randomseed() | ||
s2 = randomseed() | ||
|
||
assert isinstance(s1, str) | ||
assert s1 != "" | ||
assert s1 != s2 | ||
|
||
|
||
def test_randomtable(): | ||
""" | ||
Ensure that randomtable provides a table with the right number of rows and columns. | ||
""" | ||
columns, rows = 3, 10 | ||
table = randomtable(columns, rows) | ||
|
||
assert len(table[0]) == columns | ||
assert len(table) == rows + 1 | ||
|
||
|
||
def test_randomtable_class(): | ||
""" | ||
Ensure that RandomTable provides a table with the right number of rows and columns. | ||
""" | ||
columns, rows = 4, 60 | ||
table = RandomTable(numflds=columns, numrows=rows) | ||
|
||
assert len(table[0]) == columns | ||
assert len(table) == rows + 1 | ||
|
||
|
||
def test_dummytable_custom_fields(): | ||
""" | ||
Ensure that dummytable provides a table with the right number of rows | ||
and that it accepts and uses custom column names provided. | ||
""" | ||
columns = ( | ||
('count', partial(random.randint, 0, 100)), | ||
('pet', partial(random.choice, ('dog', 'cat', 'cow'))), | ||
('color', partial(random.choice, ('yellow', 'orange', 'brown'))), | ||
('value', random.random) | ||
) | ||
rows = 35 | ||
|
||
table = dummytable(numrows=rows, fields=columns) | ||
assert table[0] == ('count', 'pet', 'color', 'value') | ||
assert len(table) == rows + 1 | ||
|
||
|
||
def test_dummytable_no_seed(): | ||
""" | ||
Ensure that dummytable provides a table with the right number of rows | ||
and columns when not provided with a seed. | ||
""" | ||
rows = 35 | ||
|
||
table = dummytable(numrows=rows) | ||
assert len(table[0]) == 3 | ||
assert len(table) == rows + 1 | ||
|
||
|
||
def test_dummytable_int_seed(): | ||
""" | ||
Ensure that dummytable provides a table with the right number of rows | ||
and columns when provided with an integer as a seed. | ||
""" | ||
rows = 35 | ||
seed = 42 | ||
table = dummytable(numrows=rows, seed=seed) | ||
assert len(table[0]) == 3 | ||
assert len(table) == rows + 1 | ||
|
||
|
||
def test_dummytable_class(): | ||
""" | ||
Ensure that DummyTable provides a table with the right number of rows | ||
and columns. | ||
""" | ||
rows = 70 | ||
table = DummyTable(numrows=rows) | ||
|
||
assert len(table) == rows + 1 |