Esempio n. 1
0
def test_field_types(postgres):
    adapter = PostgresAdapter(postgres.url(), 'casts_test')
    adapter.field_names = ['a', 'b', 'c']

    assert(adapter.field_types == ['U10', 'i4', 'f8'])

    adapter.field_types = ['i4', 'f4', 'U10']
    assert(adapter.field_types == ['i4', 'f4', 'U10'])

    adapter.field_types = {'a': 'i4'}
    assert(adapter.field_types == ['i4', 'i4', 'f8'])

    adapter.field_types = {1: 'f8'}
    assert(adapter.field_types == ['U10', 'f8', 'f8'])

    adapter.field_types = ['i4', 'f4', 'U10']
    expected = np.zeros((CASTS_TEST_NUM_RECORDS,), dtype=[(str('a'), str('i4')), (str('b'), str('f4')), (str('c'), str('U10'))])
    for i in range(CASTS_TEST_NUM_RECORDS):
        expected[i] = (i, i, float('{0}.{0}'.format(i)))
    result = adapter[:]
    np.testing.assert_array_equal(expected, result)

    adapter.field_types = {'a': 'f4'}
    expected = np.zeros((CASTS_TEST_NUM_RECORDS,), dtype=[(str('a'), str('f4')), (str('b'), str('i4')), (str('c'), str('f8'))])
    for i in range(CASTS_TEST_NUM_RECORDS):
        expected[i] = (i, i, float('{0}.{0}'.format(i)))
    result = adapter[:]
    np.testing.assert_array_equal(expected, result)
Esempio n. 2
0
def test_geometric_types(postgres):
    adapter = PostgresAdapter(postgres.url(), 'geometric_test')
    adapter.field_shapes = {'path': 5}
    adapter.field_names = {4: 'path2'}
    expected = np.array([((1.1, 2.2),
                          [1, 2, 3],
                          [1, 2, 3, 4],
                          [3, 4, 1, 2],
                          [(1, 2), (3, 4), (5, 6), (0, 0), (0, 0)],
                          [(1.0, 2.0), (3.0, 4.0), (5.0, 6.0)],
                          [1, 2, 3])],
                        dtype=[(str('point'), 'f8', 2),
                               (str('line'), 'f8', 3),
                               (str('lseg'), 'f8', 4),
                               (str('box'), 'f8', 4), 
                               (str('path2'), 'f8', (5, 2)),
                               (str('polygon'), 'O'),
                               (str('circle'), 'f8', 3)])
    result = adapter[:]
    np.testing.assert_array_equal(expected, result)

    adapter.field_shapes = {'path2': 5}
    result = adapter[:]
    np.testing.assert_array_equal(expected, result)

    adapter.field_shapes = {4: 5}
    result = adapter[:]
    np.testing.assert_array_equal(expected, result)
Esempio n. 3
0
def test_field_names(postgres):
    adapter = PostgresAdapter(postgres.url(), 'ints_test')

    adapter.field_names = ['a', 'b', 'c']
    assert(adapter.field_names == ['a', 'b', 'c'])

    adapter.field_names = {1: 'b'}
    assert(adapter.field_names == ['int2', 'b', 'int8'])

    adapter.field_names = ['a', 'b', 'c']
    expected = np.array([(np.iinfo(np.int16).min,
                          np.iinfo(np.int32).min,
                          np.iinfo(np.int64).min),
                         (0, 0, 0),
                         (np.iinfo(np.int16).max,
                          np.iinfo(np.int32).max,
                          np.iinfo(np.int64).max)],
                        dtype=[(str('a'), str('i2')), (str('b'), str('i4')), (str('c'), str('i8'))])
    result = adapter[:]
    np.testing.assert_array_equal(expected, result)

    adapter = PostgresAdapter(postgres.url(), 'ints_test', field_filter=['int2', 'int4'])
    adapter.field_names = ['a', 'b']
    expected = np.array([(np.iinfo(np.int16).min,
                          np.iinfo(np.int32).min),
                         (0, 0),
                         (np.iinfo(np.int16).max,
                          np.iinfo(np.int32).max)],
                        dtype=[(str('a'), str('i2')), (str('b'), str('i4'))])
    result = adapter[:]
    np.testing.assert_array_equal(expected, result)

    adapter = PostgresAdapter(postgres.url(), 'ints_test', field_filter=['int2'])
    with pytest.raises(ValueError):
        adapter.field_names = ['a', 'b']

    adapter = PostgresAdapter(postgres.url(), 'ints_test')
    adapter.field_names = {0: 'a'}
    expected = np.array([(np.iinfo(np.int16).min,
                          np.iinfo(np.int32).min,
                          np.iinfo(np.int64).min),
                         (0, 0, 0),
                         (np.iinfo(np.int16).max,
                          np.iinfo(np.int32).max,
                          np.iinfo(np.int64).max)],
                        dtype=[(str('a'), str('i2')), (str('int4'), str('i4')), (str('int8'), str('i8'))])
    result = adapter[:]
    np.testing.assert_array_equal(expected, result)
Esempio n. 4
0
def test_dataframe(postgres):
    adapter = PostgresAdapter(postgres.url(), table='ints_test', dataframe=True)
    expected = pd.DataFrame({'int2': np.array([np.iinfo(np.int16).min, 0, np.iinfo(np.int16).max], dtype='i2'),
                             'int4': np.array([np.iinfo(np.int32).min, 0, np.iinfo(np.int32).max], dtype='i4'),
                             'int8': np.array([np.iinfo(np.int64).min, 0, np.iinfo(np.int64).max], dtype='i8')})
    result = adapter[:]
    np.testing.assert_array_equal(expected, result)
    
    adapter = PostgresAdapter(postgres.url(), 'casts_test', dataframe=True)
    expected = np.zeros((CASTS_TEST_NUM_RECORDS,), dtype=[(str('char'), str('O')),
                                                          (str('int4'), str('i4')),
                                                          (str('float8'), str('f8'))])
    for i in range(CASTS_TEST_NUM_RECORDS):
        expected[i] = (str(i).ljust(10), i, float('{0}.{0}'.format(i)))
    expected = pd.DataFrame.from_records(expected, index=np.arange(CASTS_TEST_NUM_RECORDS, dtype='u8'))
    result = adapter[:]
    assert_frame_equal(expected, result)

    adapter = PostgresAdapter(postgres.url(), 'casts_test', dataframe=True, field_filter=['int4', 'float8'])
    adapter.field_types = ['i2', 'f4']
    adapter.field_names = ['a', 'b']
    expected = np.zeros((CASTS_TEST_NUM_RECORDS,), dtype=[(str('a'), str('i2')), (str('b'), str('f4'))])
    for i in range(CASTS_TEST_NUM_RECORDS):
        expected[i] = (i, float('{0}.{0}'.format(i)))
    expected = pd.DataFrame.from_records(expected, index=np.arange(CASTS_TEST_NUM_RECORDS, dtype='u8'))
    result = adapter[:]
    assert_frame_equal(expected, result)

    adapter.field_types = {'a': 'f4'}
    expected = np.zeros((CASTS_TEST_NUM_RECORDS,), dtype=[(str('a'), str('f4')), (str('b'), str('f8'))])
    for i in range(CASTS_TEST_NUM_RECORDS):
        expected[i] = (i, float('{0}.{0}'.format(i)))
    expected = pd.DataFrame.from_records(expected, index=np.arange(CASTS_TEST_NUM_RECORDS, dtype='u8'))
    result = adapter[:]
    assert_frame_equal(expected, result)

    adapter = PostgresAdapter(postgres.url(),
                              'geometric_test',
                              dataframe=True,
                              field_filter=['point', 'line', 'polygon'])
    result = adapter[:]
    point_data = np.empty(1, dtype='O')
    point_data[0] = [1.1, 2.2]
    line_data = np.empty(1, dtype='O')
    line_data[0] = [1.0, 2.0, 3.0]
    polygon_data = np.empty(1, dtype='O')
    polygon_data[0] = [(1.0, 2.0), (3.0, 4.0), (5.0, 6.0)]
    expected = pd.DataFrame(OrderedDict([('point', point_data),
                                         ('line', line_data),
                                         ('polygon', polygon_data)]),
                                        index=np.array([0], dtype='u8'))
    assert_frame_equal(expected, result)

    adapter = PostgresAdapter(postgres.url(), 'fixed_strings_test', dataframe=True)
    result = adapter[:]
    expected = pd.DataFrame(['aaa       ', 'bbb       ', 'ccc       '],
                            columns=['fixed'],
                            index=np.array([0, 1, 2], dtype='u8'))
    assert_frame_equal(expected, result)

    with pytest.raises(RuntimeError):
        adapter.field_shapes = {'fixed': 2}