| 1 |
import random as pyrandom |
| 2 |
import time |
| 3 |
from functools import partial |
| 4 |
|
| 5 |
from petl.util.random import randomseed, randomtable, RandomTable, dummytable, DummyTable |
| 6 |
|
| 7 |
|
| 8 |
def test_randomseed(): |
| 9 |
""" |
| 10 |
Ensure that randomseed provides a non-empty string that changes. |
| 11 |
""" |
| 12 |
seed_1 = randomseed() |
| 13 |
time.sleep(1) |
| 14 |
seed_2 = randomseed() |
| 15 |
|
| 16 |
assert isinstance(seed_1, str) |
| 17 |
assert seed_1 != "" |
| 18 |
assert seed_1 != seed_2 |
| 19 |
|
| 20 |
|
| 21 |
def test_randomtable(): |
| 22 |
""" |
| 23 |
Ensure that randomtable provides a table with the right number of rows and columns. |
| 24 |
""" |
| 25 |
columns, rows = 3, 10 |
| 26 |
table = randomtable(columns, rows) |
| 27 |
|
| 28 |
assert len(table[0]) == columns |
| 29 |
assert len(table) == rows + 1 |
| 30 |
|
| 31 |
|
| 32 |
def test_randomtable_class(): |
| 33 |
""" |
| 34 |
Ensure that RandomTable provides a table with the right number of rows and columns. |
| 35 |
""" |
| 36 |
columns, rows = 4, 60 |
| 37 |
table = RandomTable(numflds=columns, numrows=rows) |
| 38 |
|
| 39 |
assert len(table[0]) == columns |
| 40 |
assert len(table) == rows + 1 |
| 41 |
|
| 42 |
|
| 43 |
def test_dummytable_custom_fields(): |
| 44 |
""" |
| 45 |
Ensure that dummytable provides a table with the right number of rows |
| 46 |
and that it accepts and uses custom column names provided. |
| 47 |
""" |
| 48 |
columns = ( |
| 49 |
('count', partial(pyrandom.randint, 0, 100)), |
| 50 |
('pet', partial(pyrandom.choice, ['dog', 'cat', 'cow', ])), |
| 51 |
('color', partial(pyrandom.choice, ['yellow', 'orange', 'brown'])), |
| 52 |
('value', pyrandom.random), |
| 53 |
) |
| 54 |
rows = 35 |
| 55 |
|
| 56 |
table = dummytable(numrows=rows, fields=columns) |
| 57 |
assert table[0] == ('count', 'pet', 'color', 'value') |
| 58 |
assert len(table) == rows + 1 |
| 59 |
|
| 60 |
|
| 61 |
def test_dummytable_no_seed(): |
| 62 |
""" |
| 63 |
Ensure that dummytable provides a table with the right number of rows |
| 64 |
and columns when not provided with a seed. |
| 65 |
""" |
| 66 |
rows = 35 |
| 67 |
|
| 68 |
table = dummytable(numrows=rows) |
| 69 |
assert len(table[0]) == 3 |
| 70 |
assert len(table) == rows + 1 |
| 71 |
|
| 72 |
|
| 73 |
def test_dummytable_int_seed(): |
| 74 |
""" |
| 75 |
Ensure that dummytable provides a table with the right number of rows |
| 76 |
and columns when provided with an integer as a seed. |
| 77 |
""" |
| 78 |
rows = 35 |
| 79 |
seed = 42 |
| 80 |
table = dummytable(numrows=rows, seed=seed) |
| 81 |
assert len(table[0]) == 3 |
| 82 |
assert len(table) == rows + 1 |
| 83 |
|
| 84 |
|
| 85 |
def test_dummytable_class(): |
| 86 |
""" |
| 87 |
Ensure that DummyTable provides a table with the right number of rows |
| 88 |
and columns. |
| 89 |
""" |
| 90 |
rows = 70 |
| 91 |
table = DummyTable(numrows=rows) |
| 92 |
|
| 93 |
assert len(table) == rows + 1 |