示例#1
0
async def test_fetch(table: Table, table_name: str,
                     table_columns_names: List[str],
                     table_records: List[RecordType], is_mysql: bool,
                     db_uri: URL, event_loop: AbstractEventLoop) -> None:
    table_records_dicts = records_to_dicts(records=table_records, table=table)
    insert(records_dicts=table_records_dicts, table=table, db_uri=db_uri)

    async with get_connection(db_uri=db_uri,
                              is_mysql=is_mysql,
                              loop=event_loop) as connection:
        records = await fetch(table_name=table_name,
                              columns_names=table_columns_names,
                              is_mysql=is_mysql,
                              connection=connection)

    assert all(table_record in records for table_record in table_records)

    with pytest.raises(ValueError):
        async with get_connection(db_uri=db_uri,
                                  is_mysql=is_mysql,
                                  loop=event_loop) as connection:
            await fetch(table_name=table_name,
                        columns_names=[],
                        is_mysql=is_mysql,
                        connection=connection)
示例#2
0
async def test_group_wise_fetch(table: Table, table_name: str,
                                table_columns_names: List[str],
                                table_non_unique_columns_names: List[str],
                                table_primary_key: str,
                                table_similar_records: List[RecordType],
                                is_group_wise_maximum: bool, is_mysql: bool,
                                db_uri: URL,
                                event_loop: AbstractEventLoop) -> None:
    target_function = max if is_group_wise_maximum else min
    target_primary_key_value = target_function(
        record[0] for record in table_similar_records)

    table_similar_records_dicts = records_to_dicts(
        records=table_similar_records, table=table)
    insert(records_dicts=table_similar_records_dicts,
           table=table,
           db_uri=db_uri)

    async with get_connection(db_uri=db_uri,
                              is_mysql=is_mysql,
                              loop=event_loop) as connection:
        records = await group_wise_fetch(
            table_name=table_name,
            columns_names=table_columns_names,
            target_column_name=table_primary_key,
            groupings=table_non_unique_columns_names,
            is_maximum=is_group_wise_maximum,
            is_mysql=is_mysql,
            connection=connection)

    assert all(record in table_similar_records for record in records)
    assert len(records) == 1
    group_wise_maximum_record, = records
    group_wise_maximum_record_primary_key_value = group_wise_maximum_record[0]
    assert group_wise_maximum_record_primary_key_value == target_primary_key_value

    with pytest.raises(ValueError):
        async with get_connection(db_uri=db_uri,
                                  is_mysql=is_mysql,
                                  loop=event_loop) as connection:
            await group_wise_fetch(table_name=table_name,
                                   columns_names=[],
                                   target_column_name=table_primary_key,
                                   groupings=table_non_unique_columns_names,
                                   is_mysql=is_mysql,
                                   connection=connection)

    with pytest.raises(ValueError):
        async with get_connection(db_uri=db_uri,
                                  is_mysql=is_mysql,
                                  loop=event_loop) as connection:
            await group_wise_fetch(table_name=table_name,
                                   columns_names=table_columns_names,
                                   target_column_name=table_primary_key,
                                   groupings=[],
                                   is_mysql=is_mysql,
                                   connection=connection)
示例#3
0
async def test_fetch_max_connections(is_mysql: bool, db_uri: URL,
                                     event_loop: AbstractEventLoop) -> None:
    async with get_connection(db_uri=db_uri,
                              is_mysql=is_mysql,
                              loop=event_loop) as connection:
        max_connections = await fetch_max_connections(is_mysql=is_mysql,
                                                      connection=connection)

    assert isinstance(max_connections, int)
    assert max_connections > 0
示例#4
0
async def test_fetch_records_count(table: Table, table_name: str,
                                   table_records: List[RecordType],
                                   is_mysql: bool, db_uri: URL,
                                   event_loop: AbstractEventLoop) -> None:
    table_records_dicts = records_to_dicts(records=table_records, table=table)
    insert(records_dicts=table_records_dicts, table=table, db_uri=db_uri)

    async with get_connection(db_uri=db_uri,
                              is_mysql=is_mysql,
                              loop=event_loop) as connection:
        records_count = await fetch_records_count(table_name=table_name,
                                                  is_mysql=is_mysql,
                                                  connection=connection)

    assert records_count == len(table_records)
示例#5
0
async def test_insert(table: Table, table_name: str,
                      table_columns_names: List[str],
                      table_records: List[RecordType], is_mysql: bool,
                      db_uri: URL, event_loop: AbstractEventLoop) -> None:
    async with get_connection(db_uri=db_uri,
                              is_mysql=is_mysql,
                              loop=event_loop) as connection:
        await insert(table_name=table_name,
                     columns_names=table_columns_names,
                     records=table_records,
                     is_mysql=is_mysql,
                     connection=connection)

    records = fetch(table=table, db_uri=db_uri)

    assert all(table_record in records for table_record in table_records)
示例#6
0
async def test_insert_returning(table_name: str,
                                table_columns_names: List[str],
                                table_primary_key: str,
                                table_records: List[RecordType],
                                is_mysql: bool, db_uri: URL,
                                event_loop: AbstractEventLoop) -> None:
    async with get_connection(db_uri=db_uri,
                              is_mysql=is_mysql,
                              loop=event_loop) as connection:
        records = await insert_returning(
            table_name=table_name,
            columns_names=table_columns_names,
            returning_columns_names=table_columns_names,
            primary_key=table_primary_key,
            records=table_records,
            connection=connection,
            is_mysql=is_mysql)

    assert all(table_record in records for table_record in table_records)
示例#7
0
async def test_transactions(table: Table, table_name: str,
                            table_columns_names: List[str],
                            table_records: List[RecordType], is_mysql: bool,
                            db_uri: URL,
                            event_loop: AbstractEventLoop) -> None:
    async with get_connection(db_uri=db_uri,
                              is_mysql=is_mysql,
                              loop=event_loop) as connection:
        with suppress(Exception):
            async with begin_transaction(connection=connection,
                                         is_mysql=is_mysql):
                await insert(table_name=table_name,
                             columns_names=table_columns_names,
                             records=table_records,
                             is_mysql=is_mysql,
                             connection=connection)
                raise Exception()

        records = fetch(table=table, db_uri=db_uri)
        assert not records
示例#8
0
async def test_update(table: Table, table_name: str,
                      table_records_updates: UpdatesType,
                      table_records: List[RecordType], is_mysql: bool,
                      db_uri: URL, event_loop: AbstractEventLoop) -> None:
    table_records_dicts = records_to_dicts(records=table_records, table=table)
    insert(records_dicts=table_records_dicts, table=table, db_uri=db_uri)
    async with get_connection(db_uri=db_uri,
                              is_mysql=is_mysql,
                              loop=event_loop) as connection:
        await update(table_name=table_name,
                     updates=table_records_updates,
                     is_mysql=is_mysql,
                     connection=connection)

    records = fetch(table=table, db_uri=db_uri)
    records_dicts = records_to_dicts(records=records, table=table)
    assert all(
        is_sub_dictionary(sub_dictionary=table_records_updates,
                          super_dictionary=record_dict)
        for record_dict in records_dicts)
示例#9
0
async def test_group_wise_fetch_records_count(
        table: Table, table_name: str,
        table_non_unique_columns_names: List[str], table_primary_key: str,
        table_similar_records: List[RecordType], is_group_wise_maximum: bool,
        is_mysql: bool, db_uri: URL, event_loop: AbstractEventLoop) -> None:
    table_similar_records_dicts = records_to_dicts(
        records=table_similar_records, table=table)
    insert(records_dicts=table_similar_records_dicts,
           table=table,
           db_uri=db_uri)

    async with get_connection(db_uri=db_uri,
                              is_mysql=is_mysql,
                              loop=event_loop) as connection:
        records_count = await group_wise_fetch_records_count(
            table_name=table_name,
            target_column_name=table_primary_key,
            groupings=table_non_unique_columns_names,
            is_maximum=is_group_wise_maximum,
            is_mysql=is_mysql,
            connection=connection)

    assert records_count == 1