def test_create_table_duplicate_fields(): with pytest.raises(TableCreationError, match='already exist'): Table(columns=[ Column('id', IntType), Column('name', StringType), Column('id', FloatType) ])
def create_table(): table = Table(columns=( Column('id', IntType), Column('name', StringType), Column('age', FloatType), )) table.insert(dict(id=1, name='George', age=35.5)) table.insert(dict(id=2, name='Fred', age=25.)) return table
def test_insert(): table = Table(columns=(Column('id', IntType), Column('name', StringType), Column('weight', FloatType),)) assert len(table.rows) == 0 first_arg = dict(id=1, name='John', weight=70.) second_arg = dict(id=2, name='Steven', weight=75.) table.insert(first_arg) table.insert(second_arg) assert len(table.rows) == 2 assert table.rows[0] == first_arg assert table.rows[1] == second_arg
def test_insert_mutability(): table = Table(columns=(Column('id', IntType),)) value = dict(id=3) table.insert(value) assert table.rows[0] == value value['id'] = 5 value['name'] = 'Test name' assert table.rows[0]['id'] == 3 assert 'name' not in table.rows[0] assert len(table.rows) == 1
def test_table_creation(): table = Table(columns=( Column('id', IntType), Column('name', StringType), Column('weight', FloatType), )) assert len(table.fields) == 3 assert table.fields['id'].index == 0 assert table.fields['id'].name == 'id' assert table.fields['id'].type == IntType assert table.fields['name'].index == 1 assert table.fields['name'].name == 'name' assert table.fields['name'].type == StringType assert table.fields['weight'].index == 2 assert table.fields['weight'].name == 'weight' assert table.fields['weight'].type == FloatType assert [i.name for i in table.index_to_name] == ['id', 'name', 'weight']
def test_select_compare_fields(): table = Table(columns=( Column('id', IntType), Column('planned', IntType), Column('factually', IntType), )) table.insert(dict(id=1, planned=5, factually=10)) table.insert(dict(id=2, planned=5, factually=5)) table.insert(dict(id=3, planned=5, factually=4)) assert table.select( ['id'], table.fields['planned'] < table.fields['factually']) == [[1]] assert table.select( ['id'], table.fields['planned'] > table.fields['factually']) == [[3]] assert table.select( ['id'], table.fields['planned'] == table.fields['factually']) == [[2]] assert table.select( ['id'], table.fields['planned'] >= table.fields['factually']) == [[2], [3]] assert table.select( ['id'], table.fields['planned'] != table.fields['factually']) == [[1], [3]]
def test_create_table_with_wrong_column_type(wrong_value, expected_exc): with pytest.raises(TableCreationError, match=expected_exc): Table(columns=[wrong_value]) with pytest.raises(TableCreationError, match=expected_exc): Table(columns=[Column('name', StringType), wrong_value])
def test_create_empty_table(): with pytest.raises(TableCreationError, match='empty'): Table(columns=[])
def test_insert_wrong_type_binary(value): table = Table(columns=(Column('file', BinaryType),)) with pytest.raises(TableInsertError, match='invalid value for field'): table.insert(dict(file=value))
def test_insert_wrong_type_str(value): table = Table(columns=(Column('name', StringType),)) with pytest.raises(TableInsertError, match='invalid value for field'): table.insert(dict(name=value))
def test_insert_wrong_type_datetime(value): table = Table(columns=(Column('birthday', DatetimeType),)) with pytest.raises(TableInsertError, match='invalid value for field'): table.insert(dict(birthday=value))
def test_insert_wrong_type_float(value): table = Table(columns=(Column('temperature', FloatType),)) with pytest.raises(TableInsertError, match='invalid value for field'): table.insert(dict(temperature=value))
def test_insert_wrong_type_int(value): table = Table(columns=(Column('id', IntType),)) with pytest.raises(TableInsertError, match='invalid value for field'): table.insert(dict(id=value))
def test_insert_missing_field_name(): table = Table(columns=(Column('id', IntType), Column('name', StringType),)) with pytest.raises(TableInsertError, match='missing field name'): table.insert(dict(id=1, test='str'))
def test_insert_wrong_number_of_args(value): table = Table(columns=(Column('id', IntType), Column('name', StringType),)) with pytest.raises(TableInsertError, match='number .* not equal to'): table.insert(value)
def test_insert_not_dict(value): table = Table(columns=(Column('id', IntType),)) with pytest.raises(TableInsertError, match='should be dictionary'): # noinspection PyTypeChecker table.insert(value)