|
| 1 | +import random |
| 2 | +from functools import partial |
| 3 | + |
| 4 | +from petl.util.random import randomseed, randomtable, RandomTable, dummytable |
| 5 | + |
| 6 | + |
| 7 | +def test_randomseed(): |
| 8 | + s = randomseed() |
| 9 | + |
| 10 | + assert isinstance(s, str) |
| 11 | + assert s != "" |
| 12 | + |
| 13 | + |
| 14 | +def test_randomtable(): |
| 15 | + columns, rows = 3, 10 |
| 16 | + table = randomtable(columns, rows) |
| 17 | + |
| 18 | + assert len(table[0]) == columns |
| 19 | + assert len(table) == rows + 1 |
| 20 | + |
| 21 | + |
| 22 | +def test_randomtable_class(): |
| 23 | + columns, rows = 4, 60 |
| 24 | + table = RandomTable(numflds=columns, numrows=rows) |
| 25 | + |
| 26 | + assert len(table[0]) == columns |
| 27 | + assert len(table) == rows + 1 |
| 28 | + |
| 29 | + |
| 30 | +def test_dummytable_custom_fields(): |
| 31 | + columns = ( |
| 32 | + ('count', partial(random.randint, 0, 100)), |
| 33 | + ('pet', partial(random.choice, ('dog', 'cat', 'cow'))), |
| 34 | + ('color', partial(random.choice, ('yellow', 'orange', 'brown'))), |
| 35 | + ('value', random.random) |
| 36 | + ) |
| 37 | + rows = 35 |
| 38 | + |
| 39 | + table = dummytable( |
| 40 | + numrows=rows, |
| 41 | + fields=columns, |
| 42 | + ) |
| 43 | + assert len(table[0]) == 4 |
| 44 | + assert len(table) == rows + 1 |
| 45 | + |
| 46 | + |
| 47 | +def test_dummytable_no_seed(): |
| 48 | + rows = 35 |
| 49 | + |
| 50 | + table = dummytable( |
| 51 | + numrows=rows, |
| 52 | + ) |
| 53 | + assert len(table[0]) == 3 |
| 54 | + assert len(table) == rows + 1 |
| 55 | + |
| 56 | + |
| 57 | +def test_dummytable_int_seed(): |
| 58 | + rows = 35 |
| 59 | + |
| 60 | + table = dummytable( |
| 61 | + numrows=rows, |
| 62 | + seed=42 |
| 63 | + ) |
| 64 | + assert len(table[0]) == 3 |
| 65 | + assert len(table) == rows + 1 |
| 66 | + |
| 67 | + |
| 68 | +def test_dummytable_class(): |
| 69 | + rows = 70 |
| 70 | + table = dummytable(numrows=rows) |
| 71 | + |
| 72 | + assert len(table) == rows + 1 |
0 commit comments