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)
def test_fixed_strings(postgres): adapter = PostgresAdapter(postgres.url(), 'fixed_strings_test') expected = np.array([('aaa ',), ('bbb ',), ('ccc ',)], dtype=[(str('fixed'), 'U10')]) result = adapter[:] np.testing.assert_array_equal(expected, result) adapter.field_types = ['U2'] result = adapter[:] expected = np.array([('aa',), ('bb',), ('cc',)], dtype=[(str('fixed'), 'U2')]) np.testing.assert_array_equal(expected, result) adapter.field_types = ['U'] result = adapter[:] expected = np.array([('aaa ',), ('bbb ',), ('ccc ',)], dtype=[(str('fixed'), 'U10')]) np.testing.assert_array_equal(expected, result)
def test_var_strings(postgres): adapter = PostgresAdapter(postgres.url(), 'var_strings_test') expected = np.array([('aaa', string.ascii_lowercase), ('bbb', string.ascii_uppercase), ('ccc', string.ascii_letters)], dtype=[(str('varchar'), 'U10'), (str('text'), 'O')]) result = adapter[:] np.testing.assert_array_equal(expected, result) adapter.field_types = ['U1', 'O'] expected = np.array([('a', string.ascii_lowercase), ('b', string.ascii_uppercase), ('c', string.ascii_letters)], dtype=[(str('varchar'), 'U1'), (str('text'), 'O')]) result = adapter[:] np.testing.assert_array_equal(expected, result) adapter.field_types = ['U', 'O'] expected = np.array([('aaa', string.ascii_lowercase), ('bbb', string.ascii_uppercase), ('ccc', string.ascii_letters)], dtype=[(str('varchar'), 'U10'), (str('text'), 'O')]) result = adapter[:] np.testing.assert_array_equal(expected, result)
def test_points(self): adapter = PostgresAdapter( self.postgresql_url, query='select point2d, point3d, point4d from points') result = adapter[:] expected = np.array([(u'POINT (0.000000 1.000000)', u'POINT (0.000000 1.000000 2.000000)', u'POINT (0.000000 1.000000 2.000000 3.000000)')], dtype=[('point2d', 'O'), ('point3d', 'O'), ('point4d', 'O')]) np.testing.assert_array_equal(expected, result) adapter.field_types = ['f8', 'O', 'f8'] result = adapter[:] expected = np.array( [([0.0, 1.0], u'POINT (0.000000 1.000000 2.000000)', [0.0, 1.0, 2.0, 3.0])], dtype=[('point2d', 'f8', (2, )), ('point3d', 'O'), ('point4d', 'f8', (4, ))]) np.testing.assert_array_equal(expected, result)
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}