def test_map(): fk = Map(int32, Record([('a', int32)])) assert fk.key == int32 assert fk.value == Record([('a', int32)]) assert fk.value.dict == {'a': int32} assert fk.value.fields == (('a', int32), ) assert fk.to_numpy_dtype() == np.dtype('int32')
def test_unite_records(): assert (discover([{ 'name': 'Alice', 'balance': 100 }, { 'name': 'Bob', 'balance': '' }]) == 2 * Record([['balance', Option(int64)], ['name', string]])) assert (discover([{ 'name': 'Alice', 's': 'foo' }, { 'name': 'Bob', 's': None }]) == 2 * Record([['name', string], ['s', Option(string)]])) assert (discover([{ 'name': 'Alice', 's': 'foo', 'f': 1.0 }, { 'name': 'Bob', 's': None, 'f': None }]) == 2 * Record([['f', Option(float64)], ['name', string], ['s', Option(string)]]))
def test_record(): assert_dshape_equal( Record((('a', int32), ('b', float32))), Record((('a', int32), ('b', float32))), ) with pytest.raises(AssertionError) as e: assert_dshape_equal( Record((('a', int32), ('b', float32))), Record((('a', int32), ('b', int32))), ) assert "'float32' != 'int32'" in str(e) assert "_['b'].name" in str(e.value) with pytest.raises(AssertionError) as e: assert_dshape_equal( Record((('a', int32), ('b', float32))), Record((('a', int32), ('c', float32))), ) assert "'b' != 'c'" in str(e.value) assert_dshape_equal( Record((('b', float32), ('a', int32))), Record((('a', int32), ('b', float32))), check_record_order=False, ) with pytest.raises(AssertionError) as e: assert_dshape_equal( Record((('b', float32), ('a', float32))), Record((('a', int32), ('b', float32))), check_record_order=False, ) assert "'float32' != 'int32'" in str(e.value) assert "_['a']" in str(e.value)
def test_map(): fk = Map(int32, Record([('a', int32)])) assert fk.key == int32 assert fk.value == Record([('a', int32)]) assert fk.value.dict == {'a': int32} assert fk.value.fields == (('a', int32), ) with pytest.raises(TypeError): fk.to_numpy_dtype()
def test_unicode_record_names(names, typ): types = [int64, float64] record = Record(list(zip(names, types))) string_type, = set(map(type, record.names)) assert record.names == names assert record.types == types assert all(isinstance(s, string_type) for s in record.names)
def test_launder_raises(): with pytest.raises(TypeError) as exc: Record([('y', int)]) returned_err = str(exc) # Py 2/3 differences mean that 2 will error with <type 'int'> and 3 will # error with <class 'int'> assert "Received unsupported type" in returned_err assert "int" in returned_err
def build_datashape(sample_data): """ Build a datashape from sample_data In case of multiple dimensions, the firt is replace by 'var' to handle varing time samples in models """ if isinstance(sample_data, collections.Mapping): return Record((k,build_datashape(v)) for k,v in sample_data.items() ) else: ds = discover(sample_data) if len(ds) > 1: ds = var * ds.subarray(1) else: ds = Option( ds ) return ds
def c(): return Record([('y', '?int'), ('x', '?int')])
def test_record_with_unicode_name_as_numpy_dtype(): r = Record([(unicode('a'), 'int32')]) assert r.to_numpy_dtype() == np.dtype([('a', 'i4')])
def test_strings(self): self.assertEqual(Record([('x', 'real')]), Record([('x', real)]))
def test_map_parse(): result = dshape("var * {b: map[int32, {a: int64}]}") recmeasure = Map(dshape(int32), DataShape(Record([('a', int64)]))) assert result == DataShape(var, Record([('b', recmeasure)]))
def test_strings(): assert Record([('x', 'real')]) == Record([('x', real)])
def test_record_from_OrderedDict(): r = Record(OrderedDict([("a", "int32"), ("b", "float64")])) assert r.to_numpy_dtype() == np.dtype([("a", "i4"), ("b", "f8")])
def test_record_with_unicode_name_as_numpy_dtype(): r = Record([(unicode("a"), "int32")]) assert r.to_numpy_dtype() == np.dtype([("a", "i4")])
def test_slice_subshape_negative_start(): ds = var * Record([('a', 'int32')]) assert ds.subshape[-1:] == var * Record([('a', 'int32')])
def test_slice_subshape_bad_types(): ds = var * Record([('a', 'int32')]) with pytest.raises(TypeError): assert ds.subshape[1.0]
with pytest.raises(TypeError): assert ds.subshape[1.0] @pytest.mark.parametrize( ['base', 'expected'], zip([timedelta_, date_, datetime_], ['timedelta64[us]', 'datetime64[D]', 'datetime64[us]'])) def test_option_to_numpy_dtype(base, expected): assert Option(base).to_numpy_dtype() == np.dtype(expected) @pytest.mark.xfail(raises=TypeError, reason=('NumPy has no notion of missing for types other ' 'than timedelta, datetime, and date')) @pytest.mark.parametrize('base', [int32, float64, Record([('a', uint32)])]) def test_option_to_numpy_dtype_fails(base): Option(base).to_numpy_dtype() @pytest.mark.xfail(raises=NotImplementedError, reason='DataShape does not know about void types (yet?)') def test_from_numpy_dtype_fails(): x = np.zeros(2, np.dtype([('a', 'int32')])) CType.from_numpy_dtype(x[0].dtype) def test_ctype_alignment(): assert int32.alignment == 4
def test_funcproto_attrs(): f = dshape('(int32, ?float64) -> {a: ?string}').measure assert f.restype == DataShape(Record([('a', Option(String()))])) assert f.argtypes == (DataShape(int32), DataShape(Option(float64)))
def test_integers(): assert Record([(0, 'real')]) == Record([('0', real)])
def test_record_from_OrderedDict(): r = Record(OrderedDict([('a', 'int32'), ('b', 'float64')])) assert r.to_numpy_dtype() == np.dtype([('a', 'i4'), ('b', 'f8')])
def test_duplicate_field_names_fails(): fields = [('a', 'int32'), ('b', 'string'), ('a', 'float32')] with pytest.raises(ValueError): Record(fields)
def b(): return Record([('y', int), ('x', int)])
def setUp(self): self.a = Record([('x', int), ('y', int)]) self.b = Record([('y', int), ('x', int)])
def test_record_subshape_integer_index(): ds = DataShape(Record([('a', 'int32')])) assert ds.subshape[0] == int32
def test_record(): assert (discover({'name': 'Alice', 'amount': 100}) == Record([['amount', discover(100)], ['name', discover('Alice')]]))
def test_slice_subshape_negative_step(): ds = 30 * Record([('a', 'int32')]) assert ds.subshape[:-1] == 29 * Record([('a', 'int32')])
def b(): return Record([('y', 'int'), ('x', 'int')])