def test_inline(): a = parse( """ type Point = { x : int; y : int } type Space = { a: Point; b: Point } """ ) a = parse( """ type Person = { name : string; age : int; height : int; weight : int } type RGBA = { r: int32; g: int32; b: int32; a: int8 } """ )
def test_simple_parse(): x = parse('2, 3, int32') y = parse('300 , 400, {x: int64; y: int32}') assert type(x) == DataShape assert type(y) == DataShape assert type(y[0]) == Fixed assert type(y[1]) == Fixed assert type(y[2]) == Record rec = y[2] assert rec['x'] == int64 assert rec['y'] == int32
def test_simple_parse(self): x = parse('2, 3, int32') y = parse('300 , 400, {x: int64; y: int32}') assert type(x) == datashape.DataShape assert type(y) == datashape.DataShape assert type(y[0]) == datashape.Fixed assert type(y[1]) == datashape.Fixed assert type(y[2]) == datashape.Record rec = y[-1] assert rec.fields['x'] == datashape.int64 assert rec.fields['y'] == datashape.int32
def test_nested(): a = parse(''' type Space = { a: { x: int; y: int }; b: { x: int; y: int } } ''')
def test_fields_with_reserved_names(): # Should be able to name a field 'type', 'int64' # or any other word otherwise reserved in the # datashape language x = parse("""{ type: bool; blob: bool; bool: bool; int: int32; float: float32; double: float64; int8: int8; int16: int16; int32: int32; int64: int64; uint8: uint8; uint16: uint16; uint32: uint32; uint64: uint64; float16: float32; float32: float32; float64: float64; float128: float64; complex64: float32; cfloat32: float32; complex128: float64; cfloat64: float64; string: string; object: string; datetime: string; datetime64: string; timedelta: string; timedelta64: string; json: string; }""")
def test_parameterized(): a = parse(''' type T x y = { a: x; b: y } ''')
def test_free_variables(self): p = parse('N, M, 800, 600, int32') assert type(p[0]) == datashape.TypeVar assert type(p[1]) == datashape.TypeVar assert type(p[2]) == datashape.Fixed assert type(p[3]) == datashape.Fixed assert type(p[4]) == datashape.CType
def test_free_variables(): p = parse('N, M, 800, 600, int32') assert type(p[0]) == TypeVar assert type(p[1]) == TypeVar assert type(p[2]) == Fixed assert type(p[3]) == Fixed assert type(p[4]) == CType
def test_nested(): a = parse( """ type Space = { a: { x: int; y: int }; b: { x: int; y: int } } """ )
def test_trailing_semi(): a = parse(''' type a = { a: int; b: float; c: (int,int) } ''') b = parse(''' type a = { a: int; b: float; c: (int,int); } ''') assert a == b
def test_parameterized(): a = parse( """ type T x y = { a: x; b: y } """ )
def test_multiline(): a = parse(''' type f a = b type g a = b type a = { a: int; b: float; c: (int,int); } ''')
def test_trailing_semi(): a = parse( """ type a = { a: int; b: float; c: (int,int) } """ ) b = parse( """ type a = { a: int; b: float; c: (int,int); } """ ) assert a == b
def test_stress(): parse('type big = F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(F(x))))))))))))))))') parse('type big = {x:{x:{x:{x:{x:{x:{x:{x:{x:{x:int32}}}}}}}}}}') # whitespace insensitivity parse('''type big = { x:{ x :{ x: {x:{x: {x:{x:{ x:{x :{x :int32 }}} }} }}}}} ''')
def test_stress(): parse("type big = Union(Union(Union(Union(Union(Union(Union(Union(x)))))))) ") parse("type big = {x:{x:{x:{x:{x:{x:{x:{x:{x:{x:int32}}}}}}}}}}") # whitespace insensitivity parse( """type big = { x:{ x :{ x: {x:{x: {x:{x:{ x:{x :{x :int32 }}} }} }}}}} """ )
def test_compound_record1(): p = parse('6, {x:int; y:float; z:str}') assert type(p[0]) == Fixed assert type(p[1]) == Record
def test_ellipsis2(self): x = parse('A..., T') assert type(x.parameters[0]) == datashape.Ellipsis assert type(x.parameters[1]) == datashape.TypeVar assert type(x.parameters[0].typevar) == datashape.TypeVar
def test_constraints(self): x = parse('A, B : numeric') assert type(x.parameters[0]) == datashape.TypeVar assert type(x.parameters[1]) == datashape.Implements assert type(x.parameters[1].typeset == integral)
def test_parse_vars(self): x = parse('Range(1,2), int32') assert x[0].lower == 1 assert x[0].upper == 2
def test_all_the_strings(): parse("a") parse("a, b") parse("a, b, c") parse("a, b") parse("a, b , d") parse("800, 600, RGBA") parse("type foo = c") parse("type foo = c") parse("type a b c = d,e,f") parse("type a b c = d, e ") parse("type foo a b = c, d") parse("type foo a b = c,d,e") parse("type foo a b = c,d,e, f") parse("type foo a b = c, d, e, f") parse("type foo b = c, d, e, f") parse("type a b c = d, e") parse("type a b c = bar, foo") parse("type bar A = A") parse("type bar A = A, B") parse("type bar A = 800, 600, A, A") parse("type bar A B = 800, 600, A, B") parse("type bar A B = {}") parse("type bar A B = {A:B}") # parse('type baz = F(A,B)') # parse('type baz = F(A,F(A))') parse( """ type foo = { A: B; C: D; E: (A,B) } """ ) parse( """ type bar a b = { A : B; C : D; E : (a,b) } """ ) parse("type empty = {} ") parse( """ type Stock = { name : string; min : int64; max : int64; mid : int64; volume : float; close : float; open : float } """ )
def test_parse_either(): x = parse('Either(int64, float64)') assert x[0].a == int64 assert x[0].b == float64
def test_parse_equality(): x = parse('800, 600, int64') y = parse('800, 600, int64') assert x._equal(y)
def test_compound_record1(self): p = parse('6, {x:int32; y:float64; z:string}') assert type(p[0]) == datashape.Fixed assert type(p[1]) == datashape.Record
def test_function_signature(self): res = parse("A, int32 -> B, float64 -> T, T, X") self.assertIsInstance(res, Function) self.assertEqual(str(res), 'A, int32 -> B, float64 -> T, T, X')
def test_kiva_datashape(self): # A slightly more complicated datashape which should parse x = parse("""5, VarDim, { id: int64; name: string; description: { languages: VarDim, string(2); texts: json; }; status: string; funded_amount: float64; basket_amount: json; paid_amount: json; image: { id: int64; template_id: int64; }; video: json; activity: string; sector: string; use: string; delinquent: bool; location: { country_code: string(2); country: string; town: json; geo: { level: string; pairs: string; type: string; }; }; partner_id: int64; posted_date: json; planned_expiration_date: json; loan_amount: float64; currency_exchange_loss_amount: json; borrowers: VarDim, { first_name: string; last_name: string; gender: string(1); pictured: bool; }; terms: { disbursal_date: json; disbursal_currency: string(3,'A'); disbursal_amount: float64; loan_amount: float64; local_payments: VarDim, { due_date: json; amount: float64; }; scheduled_payments: VarDim, { due_date: json; amount: float64; }; loss_liability: { nonpayment: string; currency_exchange: string; currency_exchange_coverage_rate: json; }; }; payments: VarDim, { amount: float64; local_amount: float64; processed_date: json; settlement_date: json; rounded_local_amount: float64; currency_exchange_loss_amount: float64; payment_id: int64; comment: json; }; funded_date: json; paid_date: json; journal_totals: { entries: int64; bulkEntries: int64; }; } """)
def test_compound_record1(): p = parse('6, {x:int32; y:float64; z:string}') assert type(p[0]) == Fixed assert type(p[1]) == Record
def test_parse_vars(): x = parse('Range(1,2), int32') assert x[0].lower == 1 assert x[0].upper == 2
def test_parse_either(): x = parse('Either(int64, float64)') assert type(x) == Either assert x.a == int64 assert x.b == float64
def test_compound_record2(): p = parse('{ a: { x: int; y: int }; b: {w: int; u: int } }') assert type(p[0]) == Record
def test_compound_record2(): p = parse('{ a: { x: int; y: int }; b: {w: int; u: int } }') assert type(p) == Record
def test_compound_record2(self): p = parse('{ a: { x: int32; y: int32 }; b: {w: int32; u: int32 } }') assert type(p) == datashape.Record
def test_parse_vars(): x = parse('Range(1,2)') assert x[0].lower == 1 assert x[0].upper == 2
def test_kiva_datashape(): # A slightly more complicated datashape which should parse x = parse("""5, VarDim, { id: int64; name: string; description: { languages: VarDim, string(2); texts: json; }; status: string; funded_amount: float64; basket_amount: json; paid_amount: json; image: { id: int64; template_id: int64; }; video: json; activity: string; sector: string; use: string; delinquent: bool; location: { country_code: string(2); country: string; town: json; geo: { level: string; pairs: string; type: string; }; }; partner_id: int64; posted_date: json; planned_expiration_date: json; loan_amount: float64; currency_exchange_loss_amount: json; borrowers: VarDim, { first_name: string; last_name: string; gender: string(1); pictured: bool; }; terms: { disbursal_date: json; disbursal_currency: string(3,'A'); disbursal_amount: float64; loan_amount: float64; local_payments: VarDim, { due_date: json; amount: float64; }; scheduled_payments: VarDim, { due_date: json; amount: float64; }; loss_liability: { nonpayment: string; currency_exchange: string; currency_exchange_coverage_rate: json; }; }; payments: VarDim, { amount: float64; local_amount: float64; processed_date: json; settlement_date: json; rounded_local_amount: float64; currency_exchange_loss_amount: float64; payment_id: int64; comment: json; }; funded_date: json; paid_date: json; journal_totals: { entries: int64; bulkEntries: int64; }; } """)
def test_parse_equality(self): x = parse('800, 600, int64') y = parse('800, 600, int64') assert x == y