Exemplo n.º 1
0
    def test_table_reflection(self, connection):
        mro.load_database(connection)

        table1 = mro.table1.select_one()
        table2 = mro.table2.select_one()

        assert table1.__class__.__name__ == 'table1'
        assert table2.__class__.__name__ == 'table2'

        text = 'Hello World!'
        number = 1
        with pytest.raises(TypeError) as excinfo:
            table1.column1 = text
        assert excinfo.value.args[0] == 'Value should be of type [int] not [{}]'.format(text.__class__.__name__ )

        table1.column1 = number
        table1.column2 = text

        assert table1.column1 == number
        assert table1.column2 == text

        with pytest.raises(TypeError) as excinfo:
            table2.column1 = number
        assert excinfo.value.args[0] == 'Value should be of type [str] not [{}]'.format(number.__class__.__name__  )

        table2.column1 = text
        table2.column2 = number

        assert table2.column1 == text
        assert table2.column2 == number
Exemplo n.º 2
0
    def test_insert_many(self, connection):
        mro.load_database(connection)

        mro.table1.delete()

        table = mro.table1.insert_many(
            ['column1', 'column2', 'column3'],
            [
                [1, 'Hi!', 7],
                [2, 'Hi2!', 13],
                [3, 'Hi3!', 21]
            ])

        tables = mro.table1.select()

        assert 3 == len(tables)   

        assert tables[0].column1 == 1
        assert tables[0].column2 == 'Hi!'
        assert tables[0].column3 == 7

        assert tables[1].column1 == 2
        assert tables[1].column2 == 'Hi2!'
        assert tables[1].column3 == 13

        assert tables[2].column1 == 3
        assert tables[2].column2 == 'Hi3!'
        assert tables[2].column3 == 21
Exemplo n.º 3
0
def connection(request):
    connection = con.connect()
    request.addfinalizer(con.disconnect)

    cursor = connection.cursor()

    con.drop_tables()

    cursor.execute("""create table table1 (
    id serial unique, 
    name varchar(20) not null,
    value varchar(20),
    primary key (id, name)
    );""")

    cursor.execute("""create table table2 (
    id serial, 
    name varchar(20) not null,
    table1_id integer,
    primary key (id),
    foreign key (table1_id) references table1(id)
    );""")

    cursor.execute("""create table table3 (
    value varchar(20) not null
    );""")

    connection.commit()

    mro.load_database(connection)

    create_test_data(connection)

    return connection
Exemplo n.º 4
0
    def test_insert_check_default_values(self, connection):

        mro.load_database(connection)

        table_count = mro.table1.select_count()

        table = mro.table1(column1 = 3, column2 = 'Hi!')

        table = mro.table1(column1 = 3, column2 = 'Hi!', column3 = 11, created_date = datetime.now().date())

        tables = mro.table1.select()

        for table in tables:
            assert isinstance(table.id, int)
            assert table.id != None
            assert isinstance(table.created_date, date)
            assert table.created_date != None
            assert isinstance(table.column1, int)
            assert table.column1 != None
            assert isinstance(table.column2, str)
            assert table.column2 != None
            assert table.column3 == None or isinstance(table.column3, int)

        table = mro.table3(column3 = 'Hi56!', column4 = '{"data": 1}')

        table = mro.table3.select_one("column3 = 'Hi56!'")

        assert isinstance(table.column1, str)
        assert table.column1 == 'ABC DEF'
        assert isinstance(table.column3, str)
        assert table.column3 != None
        assert isinstance(table.column4, str)
        assert table.column4 != None
Exemplo n.º 5
0
    def test_create_object(self, connection):
        mro.load_database(connection)

        table_count = mro.table1.select_count()

        table = mro.table1(column1 = 3, column2 = 'Hi!', column3 = 11)

        assert table.column1 == 3
        assert table.column2 == 'Hi!'
        assert table.column3 == 11

        table = mro.table1(column2 = 'Hi2!')

        assert table.column1 == 1
        assert table.column2 == 'Hi2!'
        assert table.column3 == None

        kwargs = {'column1': 5, 'column2': 'Hi3!', 'column3': 78}
        table = mro.table1(**kwargs)

        assert table.column1 == 5
        assert table.column2 == 'Hi3!'
        assert table.column3 == 78

        tables = mro.table1.select()

        assert table_count + 3 ==len(tables)        

        assert tables[4].column1 == 5
        assert tables[4].column2 == 'Hi3!'
        assert tables[4].column3 == 78
Exemplo n.º 6
0
def connection(request):
    connection = con.connect()
    request.addfinalizer(con.disconnect)

    cursor = connection.cursor()

    con.drop_tables()

    cursor.execute("""create table test_type (
    id serial primary key,
    "varchar" varchar(15), 
    "varchar2" varchar(20), 
    "varchar_not_null" varchar(20) not null default 'abc',
    "integer" integer,
    "boolean" boolean,
    "time" time,
    "date" date,
    "timestamp" timestamp,
    "json" json,
    "jsonb" jsonb,
    "text" text,
    "double" double precision,
    "real" real)""")
    connection.commit()

    mro.load_database(connection)

    return connection
Exemplo n.º 7
0
    def test_table_select_filter(self, connection):
        mro.load_database(connection)

        tables = mro.table1.select('column1 = %d' % 2)

        assert len(tables) == 1

        assert tables[0].column1 == 2
        assert tables[0].column2 == 'Hello World2!'
        assert tables[0].column3 == 3

        tables = mro.table2.select("column1 = '%d'" % 1)

        assert len(tables) == 0
Exemplo n.º 8
0
    def test_table_delete_filter(self, connection):
        mro.load_database(connection)

        table_count = mro.table1.select_count()
        tables = mro.table1.select('column1 = %d' % 2)

        assert len(tables) == 1

        assert tables[0].column1 == 2
        assert tables[0].column2 == 'Hello World2!'
        assert tables[0].column3 == 3

        mro.table1.delete('column1 = %d' % 2)

        assert table_count - 1 == mro.table1.select_count()
Exemplo n.º 9
0
    def test_table_reflection(self, connection):
        mro.load_database(connection)

        tables = mro.table1.select()

        assert len(tables) == 2

        assert isinstance(tables[0].created_date, date)
        assert tables[0].column1 == 1
        assert tables[0].column2 == 'Hello World!'
        assert tables[0].column3 == 2

        assert tables[1].column1 == 2
        assert tables[1].column2 == 'Hello World2!'
        assert tables[1].column3 == 3

        tables = mro.table2.select()

        assert len(tables) == 1

        assert tables[0].column1 == 'Hello World3!'
        assert tables[0].column2 == 4
        assert tables[0].column3 == 'Hello World4!'
Exemplo n.º 10
0
    def test_insert_with_only_primary_key_no_kwargs(self, connection):
        mro.load_database(connection)

        table_count = mro.table1()