115 lines
4.4 KiB
Python
115 lines
4.4 KiB
Python
from readcsv import read_csv
|
|
from readcsv import header_map
|
|
from readcsv import select
|
|
from readcsv import row2dict
|
|
from readcsv import check_row
|
|
from readcsv import filter_table
|
|
|
|
table = read_csv('test1.csv')
|
|
sampledata = read_csv('2014-1000.csv')
|
|
|
|
|
|
def test_read_csv():
|
|
assert read_csv('test1.csv') == [['name', 'age', 'eye colour'],
|
|
['Bob', '5', 'blue'],
|
|
['Mary', '27', 'brown'],
|
|
['Vij', '54', 'green']]
|
|
|
|
|
|
def test_header_map_1():
|
|
hmap = header_map(table[0])
|
|
assert hmap == {'name': 0, 'age': 1, 'eye colour': 2}
|
|
|
|
|
|
def test_select_1():
|
|
assert select(table, {'name', 'eye colour'}) == [['name', 'eye colour'],
|
|
['Bob', 'blue'],
|
|
['Mary', 'brown'],
|
|
['Vij', 'green']]
|
|
|
|
|
|
def test_row2dict():
|
|
hmap = header_map(table[0])
|
|
assert row2dict(hmap, table[1]) == {'name': 'Bob', 'age': '5', 'eye colour': 'blue'}
|
|
|
|
|
|
def test_check_row():
|
|
row = {'name': 'Bob', 'age': '5', 'eye colour': 'blue'}
|
|
assert check_row(row, ('age', '==', 5))
|
|
assert not check_row(row, ('eye colour', '==', 5))
|
|
assert check_row(row, ('eye colour', '==', 'blue'))
|
|
assert check_row(row, ('age', '>=', 4))
|
|
assert check_row(row, ('age', '<=', 1000))
|
|
|
|
|
|
def test_check_row_logical():
|
|
row = {'name': 'Bob', 'age': '5', 'eye colour': 'blue'}
|
|
assert check_row(row, (('age', '==', 5), 'OR', ('eye colour', '==', 5)))
|
|
assert not check_row(row, (('age', '==', 5), 'AND', ('eye colour', '==', 5)))
|
|
|
|
|
|
def test_filter_table1():
|
|
assert filter_table(table, ('age', '>=', 0)) == [['name', 'age', 'eye colour'],
|
|
['Bob', '5', 'blue'],
|
|
['Mary', '27', 'brown'],
|
|
['Vij', '54', 'green']]
|
|
|
|
assert filter_table(table, ('age', '<=', 27)) == [['name', 'age', 'eye colour'],
|
|
['Bob', '5', 'blue'],
|
|
['Mary', '27', 'brown']]
|
|
|
|
assert filter_table(table, ('eye colour', '==', 'brown')) == [['name', 'age', 'eye colour'],
|
|
['Mary', '27', 'brown']]
|
|
|
|
assert filter_table(table, ('name', '==', 'Vij')) == [['name', 'age', 'eye colour'],
|
|
['Vij', '54', 'green']]
|
|
|
|
|
|
def test_filter_table2():
|
|
assert filter_table(table, (('age', '>=', 0), 'AND', ('age', '>=', '27'))) == [['name', 'age', 'eye colour'],
|
|
['Mary', '27', 'brown'],
|
|
['Vij', '54', 'green']]
|
|
|
|
assert filter_table(table, (('age', '<=', 27), 'AND', ('age', '>=', '27'))) == [['name', 'age', 'eye colour'],
|
|
['Mary', '27', 'brown']]
|
|
|
|
assert filter_table(table, (('eye colour', '==', 'brown'),
|
|
'OR',
|
|
('name', '==', 'Vij'))) == [['name', 'age', 'eye colour'],
|
|
['Mary', '27', 'brown'],
|
|
['Vij', '54', 'green']]
|
|
|
|
|
|
# Student Tests
|
|
table2 = read_csv('test2.csv')
|
|
hmap2 = header_map(table2[0])
|
|
|
|
|
|
def test_header_map2():
|
|
assert header_map(table2[0]) == {"name": 0, "100": 1}
|
|
|
|
|
|
def test_select2():
|
|
assert select(table2, [100]) == [["100"], ["500"], ["1000"]]
|
|
assert select(table2, ["name"]) == [["name"], ["teddy"], ["lovely"]]
|
|
assert select(table2, ["name", 100]) == [["name", "100"], ["teddy", "500"], ["lovely", "1000"]]
|
|
|
|
|
|
def test_row2dict2():
|
|
assert row2dict(hmap2, table2[1]) == {"name": "teddy", "100": "500"}
|
|
assert row2dict(hmap2, table2[2]) == {"name": "lovely", "100": "1000"}
|
|
|
|
|
|
def test_check_row_2():
|
|
row = {'name': 'Bob', 'age': '5', 'eye colour': 'blue'}
|
|
assert not check_row(row, ('age', '===', 5))
|
|
|
|
|
|
def test_check_row_3():
|
|
row = row2dict(hmap2, table2[1])
|
|
assert check_row(row, ("100", "==", 500))
|
|
|
|
|
|
def test_filter_table3():
|
|
assert filter_table(table2, ("100", ">=", 100)) == [["name", "100"], ["teddy", "500"], ["lovely", "1000"]]
|