Exemplo n.º 1
0
def test_period_constructor(cursor, expected_period):
    if isinstance(expected_period, tuple):
        params = Period(*expected_period)
    else:
        params = Period(expected_period)
    cursor.execute("INSERT INTO tbl_period (timetype) VALUES (%s)" % params)
    cursor.execute("SELECT timetype FROM tbl_period WHERE timetype=%s" % params)
    result = cursor.fetchone()[0]
    if isinstance(expected_period, tuple):
        assert result == Period(*expected_period)
    else:
        assert result == Period(expected_period)
Exemplo n.º 2
0
async def test_period_constructor(connection, expected_period):
    if isinstance(expected_period, tuple):
        params = Period(*expected_period)
    else:
        params = Period(expected_period)
    await connection.execute("INSERT INTO tbl_period (timetype) VALUES ($1)",
                             params)
    result = await connection.fetchval(
        "SELECT timetype FROM tbl_period WHERE timetype=$1", params)
    if isinstance(expected_period, tuple):
        assert result == Period(*expected_period)
    else:
        assert result == Period(expected_period)
Exemplo n.º 3
0
def test_period_accessors(cursor, expected_period):
    assert Period(expected_period).lower == parse('2019-09-01 00:00:00+01')
    assert Period(expected_period).upper == parse('2019-09-03 00:00:00+01')
    assert Period(expected_period).lower_inc == True
    assert Period(expected_period).upper_inc == True
    assert Period(expected_period).duration == timedelta(2)
    assert Period(expected_period).shift(timedelta(days=1)) == Period('[2019-09-02 00:00:00+01, 2019-09-04 00:00:00+01]')
Exemplo n.º 4
0
def test_timestampset_accessors(cursor, expected_timestampset):
    assert TimestampSet(expected_timestampset).timespan == timedelta(2)
    assert TimestampSet(expected_timestampset).period == Period('[2019-09-01 00:00:00+01, 2019-09-03 00:00:00+01]')
    assert TimestampSet(expected_timestampset).numTimestamps == 3
    assert TimestampSet(expected_timestampset).startTimestamp == parse('2019-09-01 00:00:00+01')
    assert TimestampSet(expected_timestampset).endTimestamp == parse('2019-09-03 00:00:00+01')
    assert TimestampSet(expected_timestampset).timestampN(2) == parse('2019-09-02 00:00:00+01')
    assert TimestampSet(expected_timestampset).timestamps == \
           [parse('2019-09-01 00:00:00+01'), parse('2019-09-02 00:00:00+01'), parse('2019-09-03 00:00:00+01')]
    assert TimestampSet(expected_timestampset).shift(timedelta(days=1)) == \
           TimestampSet('{2019-09-02 00:00:00+01, 2019-09-03 00:00:00+01, 2019-09-04 00:00:00+01}')
Exemplo n.º 5
0
def test_periodset_accessors(cursor, expected_periodset):
    assert PeriodSet(expected_periodset).duration == timedelta(1)
    assert PeriodSet(expected_periodset).timespan == timedelta(2)
    assert PeriodSet(expected_periodset).period == Period(
        '[2019-09-01 00:00:00+01, 2019-09-03 00:00:00+01]')
    assert PeriodSet(expected_periodset).numTimestamps == 3
    assert PeriodSet(expected_periodset).startTimestamp == parse(
        '2019-09-01 00:00:00+01')
    assert PeriodSet(expected_periodset).endTimestamp == parse(
        '2019-09-03 00:00:00+01')
    assert PeriodSet(expected_periodset).timestampN(2) == parse(
        '2019-09-02 00:00:00+01')
    assert PeriodSet(expected_periodset).timestamps == \
           [parse('2019-09-01 00:00:00+01'), parse('2019-09-02 00:00:00+01'), parse('2019-09-03 00:00:00+01')]
    assert PeriodSet(expected_periodset).numPeriods == 2
    assert PeriodSet(expected_periodset).startPeriod == Period(
        '[2019-09-01 00:00:00+01, 2019-09-01 00:00:00+01]')
    assert PeriodSet(expected_periodset).endPeriod == Period(
        '[2019-09-02 00:00:00+01, 2019-09-03 00:00:00+01]')
    assert PeriodSet(expected_periodset).periodN(2) == Period(
        '[2019-09-02 00:00:00+01, 2019-09-03 00:00:00+01]')
    assert PeriodSet(expected_periodset).periods == [
        Period('[2019-09-01 00:00:00+01, 2019-09-01 00:00:00+01]'),
        Period('[2019-09-02 00:00:00+01, 2019-09-03 00:00:00+01]')
    ]
    assert PeriodSet(expected_periodset).shift(timedelta(days=1)) == \
           PeriodSet('{[2019-09-02 00:00:00+01, 2019-09-02 00:00:00+01],  '
                     '[2019-09-03 00:00:00+01, 2019-09-04 00:00:00+01]}')
Exemplo n.º 6
0
        "SELECT timetype FROM tbl_period WHERE timetype=$1", params)
    if isinstance(expected_period, tuple):
        assert result == Period(*expected_period)
    else:
        assert result == Period(expected_period)


@pytest.mark.parametrize('expected_periodset', [
    '{[2019-09-08 00:00:00+01, 2019-09-10 00:00:00+01]}',
    '{[2019-09-08 00:00:00+01, 2019-09-10 00:00:00+01], [2019-09-11 00:00:00+01, 2019-09-12 00:00:00+01]}',
    [
        '[2019-09-08 00:00:00+01, 2019-09-10 00:00:00+01]',
        '[2019-09-11 00:00:00+01, 2019-09-12 00:00:00+01]'
    ],
    [
        Period('[2019-09-08 00:00:00+01, 2019-09-10 00:00:00+01]'),
        Period('[2019-09-11 00:00:00+01, 2019-09-12 00:00:00+01]')
    ],
    ('[2019-09-08 00:00:00+01, 2019-09-10 00:00:00+01]',
     '[2019-09-11 00:00:00+01, 2019-09-12 00:00:00+01]'),
    (Period('[2019-09-08 00:00:00+01, 2019-09-10 00:00:00+01]'),
     Period('[2019-09-11 00:00:00+01, 2019-09-12 00:00:00+01]')),
])
async def test_periodset_constructor(connection, expected_periodset):
    if isinstance(expected_periodset, tuple):
        params = PeriodSet(*expected_periodset)
    else:
        params = PeriodSet(expected_periodset)
    await connection.execute(
        "INSERT INTO tbl_periodset (timetype) VALUES ($1)", params)
    result = await connection.fetchval(