Пример #1
0
def test_create_table_duplicate_fields():
    with pytest.raises(TableCreationError, match='already exist'):
        Table(columns=[
            Column('id', IntType),
            Column('name', StringType),
            Column('id', FloatType)
        ])
Пример #2
0
 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
Пример #3
0
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
Пример #4
0
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']
Пример #5
0
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_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]]
Пример #7
0
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])
Пример #8
0
    assert [i.name for i in table.index_to_name] == ['id', 'name', 'weight']


def test_create_empty_table():
    with pytest.raises(TableCreationError, match='empty'):
        Table(columns=[])


# noinspection PyTypeChecker
@pytest.mark.parametrize('wrong_value,expected_exc', [
    [Column, 'not column'],
    [int, 'not column'],
    [1, 'not column'],
    ['test', 'not column'],
    [('id', 123), 'not column'],
    [Column('id', IntType()), 'type should be subclass'],
    [Column('id', int), 'type should be subclass'],
    [Column(123, IntType), 'name should be string'],
])
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_table_duplicate_fields():
    with pytest.raises(TableCreationError, match='already exist'):
        Table(columns=[
            Column('id', IntType),
Пример #9
0
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))
Пример #10
0
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))
Пример #11
0
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))
Пример #12
0
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))
Пример #13
0
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))
Пример #14
0
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'))
Пример #15
0
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)
Пример #16
0
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)