<s> import pytest <EOL> from pystachio . basic import * <EOL> from pystachio . composite import * <EOL> from pystachio . container import List , Map <EOL> from pystachio . naming import Ref <EOL> def ref ( address ) : <EOL> return Ref . from_address ( address ) <EOL> def test_basic_types ( ) : <EOL> class Resources ( Struct ) : <EOL> cpu = Float <EOL> ram = Integer <EOL> assert Resources ( ) . check ( ) . ok ( ) <EOL> assert Resources ( cpu = <NUM_LIT:1.0> ) . check ( ) . ok ( ) <EOL> assert Resources ( cpu = <NUM_LIT:1.0> , ram = <NUM_LIT:100> ) . check ( ) . ok ( ) <EOL> assert Resources ( cpu = <NUM_LIT:1> , ram = <NUM_LIT:100> ) . check ( ) . ok ( ) <EOL> assert Resources ( cpu = '<STR_LIT:1.0>' , ram = <NUM_LIT:100> ) . check ( ) . ok ( ) <EOL> def test_bad_inputs ( ) : <EOL> class Resources ( Struct ) : <EOL> cpu = Float <EOL> ram = Required ( Integer ) <EOL> with pytest . raises ( AttributeError ) : <EOL> Resources ( herp = "<STR_LIT>" ) <EOL> with pytest . raises ( AttributeError ) : <EOL> Resources ( { '<STR_LIT:foo>' : '<STR_LIT:bar>' } ) <EOL> with pytest . raises ( ValueError ) : <EOL> Resources ( None ) <EOL> def test_nested_composites ( ) : <EOL> class Resources ( Struct ) : <EOL> cpu = Float <EOL> ram = Integer <EOL> class Process ( Struct ) : <EOL> name = String <EOL> resources = Resources <EOL> assert Process ( ) . check ( ) . ok ( ) <EOL> assert Process ( name = "<STR_LIT>" ) . check ( ) . ok ( ) <EOL> assert Process ( resources = Resources ( ) ) . check ( ) . ok ( ) <EOL> assert Process ( resources = Resources ( cpu = <NUM_LIT:1.0> ) ) . check ( ) . ok ( ) <EOL> assert Process ( resources = Resources ( cpu = <NUM_LIT:1> ) ) . check ( ) . ok ( ) <EOL> assert Process ( name = <NUM_LIT:15> ) ( resources = Resources ( cpu = <NUM_LIT:1.0> ) ) . check ( ) . ok ( ) <EOL> repr ( Process ( name = <NUM_LIT:15> ) ( resources = Resources ( cpu = <NUM_LIT:1.0> ) ) ) <EOL> repr ( Process . TYPEMAP ) <EOL> def test_typesig ( ) : <EOL> class Process1 ( Struct ) : <EOL> name = String <EOL> class Process2 ( Struct ) : <EOL> name = Required ( String ) <EOL> class Process3 ( Struct ) : <EOL> name = Default ( String , "<STR_LIT:foo>" ) <EOL> class Process4 ( Struct ) : <EOL> name = String <EOL> assert Process1 . TYPEMAP [ '<STR_LIT:name>' ] == Process4 . TYPEMAP [ '<STR_LIT:name>' ] <EOL> assert Process1 . TYPEMAP [ '<STR_LIT:name>' ] != Process2 . TYPEMAP [ '<STR_LIT:name>' ] <EOL> assert Process1 . TYPEMAP [ '<STR_LIT:name>' ] != Process3 . TYPEMAP [ '<STR_LIT:name>' ] <EOL> assert Process2 . TYPEMAP [ '<STR_LIT:name>' ] != Process3 . TYPEMAP [ '<STR_LIT:name>' ] <EOL> repr ( Process1 . TYPEMAP [ '<STR_LIT:name>' ] ) <EOL> def test_defaults ( ) : <EOL> class Resources ( Struct ) : <EOL> cpu = Default ( Float , <NUM_LIT:1.0> ) <EOL> ram = Integer <EOL> assert Resources ( ) == Resources ( cpu = <NUM_LIT:1.0> ) <EOL> assert Resources ( cpu = <NUM_LIT> ) . _schema_data [ '<STR_LIT>' ] == Float ( <NUM_LIT> ) <EOL> class Process ( Struct ) : <EOL> name = String <EOL> resources = Default ( Resources , Resources ( ram = <NUM_LIT:10> ) ) <EOL> assert Process ( ) . check ( ) . ok ( ) <EOL> assert Process ( ) == Process ( resources = Resources ( cpu = <NUM_LIT:1.0> , ram = <NUM_LIT:10> ) ) <EOL> assert Process ( ) != Process ( resources = Resources ( ) ) <EOL> assert Process ( ) ( resources = Empty ) . check ( ) . ok ( ) <EOL> def test_composite_interpolation ( ) : <EOL> class Resources ( Struct ) : <EOL> cpu = Required ( Float ) <EOL> ram = Integer <EOL> disk = Integer <EOL> class Process ( Struct ) : <EOL> name = Required ( String ) <EOL> resources = Map ( String , Resources ) <EOL> p = Process ( name = "<STR_LIT:hello>" ) <EOL> assert p ( resources = { '<STR_LIT:foo>' : Resources ( ) } ) == p ( resources = { '<STR_LIT>' : Resources ( ) } ) . bind ( whee = '<STR_LIT:foo>' ) <EOL> assert p ( resources = { '<STR_LIT>' : Resources ( cpu = '<STR_LIT>' ) } ) . bind ( whee = <NUM_LIT:1.0> ) == p ( resources = { '<STR_LIT:1.0>' : Resources ( cpu = <NUM_LIT:1.0> ) } ) <EOL> def test_internal_interpolate ( ) : <EOL> class Process ( Struct ) : <EOL> name = Required ( String ) <EOL> cmdline = Required ( String ) <EOL> class Task ( Struct ) : <EOL> name = Default ( String , '<STR_LIT>' ) <EOL> processes = Required ( List ( Process ) ) <EOL> class Job ( Struct ) : <EOL> name = Default ( String , '<STR_LIT>' ) <EOL> task = Required ( Task ) <EOL> assert Task ( ) . name ( ) == String ( '<STR_LIT>' ) <EOL> assert Task ( processes = [ Process ( name = '<STR_LIT>' , cmdline = '<STR_LIT>' ) ] ) . name ( ) == String ( '<STR_LIT>' ) <EOL> assert Task ( processes = [ Process ( name = '<STR_LIT>' , cmdline = '<STR_LIT>' ) , <EOL> Process ( name = '<STR_LIT>' , cmdline = '<STR_LIT>' ) ] ) . name ( ) == String ( '<STR_LIT>' ) <EOL> assert Job ( task = Task ( processes = [ Process ( name = "<STR_LIT>" ) ] ) ) . name ( ) == String ( '<STR_LIT>' ) <EOL> def test_find ( ) : <EOL> class Resources ( Struct ) : <EOL> cpu = Required ( Float ) <EOL> ram = Integer <EOL> disks = List ( String ) <EOL> class Process ( Struct ) : <EOL> name = Required ( String ) <EOL> resources = Map ( String , Resources ) <EOL> res0 = Resources ( cpu = <NUM_LIT:0.0> , ram = <NUM_LIT:0> ) <EOL> res1 = Resources ( cpu = <NUM_LIT:1.0> , ram = <NUM_LIT:1> , disks = [ '<STR_LIT>' ] ) <EOL> res2 = Resources ( cpu = <NUM_LIT> , ram = <NUM_LIT:2> , disks = [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> proc = Process ( name = "<STR_LIT:hello>" , resources = { <EOL> '<STR_LIT>' : res0 , <EOL> '<STR_LIT>' : res1 , <EOL> '<STR_LIT>' : res2 <EOL> } ) <EOL> with pytest . raises ( Namable . NotFound ) : <EOL> proc . find ( ref ( '<STR_LIT>' ) ) <EOL> assert proc . find ( ref ( '<STR_LIT:name>' ) ) == String ( '<STR_LIT:hello>' ) <EOL> assert proc . find ( ref ( '<STR_LIT>' ) ) == Float ( <NUM_LIT:0.0> ) <EOL> assert proc . find ( ref ( '<STR_LIT>' ) ) == Integer ( <NUM_LIT:0> ) <EOL> with pytest . raises ( Namable . NotFound ) : <EOL> proc . find ( ref ( '<STR_LIT>' ) ) <EOL> with pytest . raises ( Namable . NamingError ) : <EOL> proc . find ( ref ( '<STR_LIT>' ) ) <EOL> with pytest . raises ( Namable . NamingError ) : <EOL> proc . find ( ref ( '<STR_LIT>' ) ) <EOL> with pytest . raises ( Namable . Unnamable ) : <EOL> proc . find ( ref ( '<STR_LIT>' ) ) <EOL> with pytest . raises ( Namable . Unnamable ) : <EOL> proc . find ( ref ( '<STR_LIT>' ) ) <EOL> assert proc . find ( ref ( '<STR_LIT>' ) ) == Integer ( <NUM_LIT:1> ) <EOL> assert proc . find ( ref ( '<STR_LIT>' ) ) == String ( '<STR_LIT>' ) <EOL> assert proc . find ( ref ( '<STR_LIT>' ) ) == String ( '<STR_LIT>' ) <EOL> assert proc . find ( ref ( '<STR_LIT>' ) ) == String ( '<STR_LIT>' ) <EOL> def test_getattr_functions ( ) : <EOL> class Resources ( Struct ) : <EOL> cpu = Required ( Float ) <EOL> ram = Integer <EOL> disk = Integer <EOL> class Process ( Struct ) : <EOL> name = Required ( String ) <EOL> resources = Map ( String , Resources ) <EOL> assert Process ( name = "<STR_LIT:hello>" ) . name ( ) == String ( '<STR_LIT:hello>' ) <EOL> assert Process ( ) . has_name ( ) is False <EOL> assert Process ( name = "<STR_LIT:hello>" ) . has_name ( ) is True <EOL> p = Process ( name = "<STR_LIT:hello>" ) <EOL> p1 = p ( resources = { '<STR_LIT:foo>' : Resources ( ) } ) <EOL> p2 = p ( resources = { '<STR_LIT>' : Resources ( ) } ) . bind ( whee = '<STR_LIT:foo>' ) <EOL> assert p1 . has_resources ( ) <EOL> assert p2 . has_resources ( ) <EOL> assert String ( '<STR_LIT:foo>' ) in p1 . resources ( ) <EOL> assert String ( '<STR_LIT:foo>' ) in p2 . resources ( ) <EOL> def test_getattr_bad_cases ( ) : <EOL> class Tricky ( Struct ) : <EOL> has_stuff = Integer <EOL> t = Tricky ( ) <EOL> assert t . has_has_stuff ( ) is False <EOL> assert t . has_stuff ( ) is Empty <EOL> with pytest . raises ( AttributeError ) : <EOL> t . this_should_properly_raise <EOL> def test_self_super ( ) : <EOL> class Child ( Struct ) : <EOL> value = Integer <EOL> class Parent ( Struct ) : <EOL> child = Child <EOL> value = Integer <EOL> class Grandparent ( Struct ) : <EOL> parent = Parent <EOL> value = Integer <EOL> parent = Parent ( child = Child ( value = '<STR_LIT>' ) , value = <NUM_LIT> ) <EOL> parent , _ = parent . interpolate ( ) <EOL> assert parent . child ( ) . value ( ) . get ( ) == <NUM_LIT> <EOL> grandparent = Grandparent ( parent = Parent ( child = Child ( value = '<STR_LIT>' ) ) , value = <NUM_LIT> ) <EOL> grandparent , _ = grandparent . interpolate ( ) <EOL> assert grandparent . parent ( ) . child ( ) . value ( ) . get ( ) == <NUM_LIT> <EOL> parent = Parent ( child = Child ( value = <NUM_LIT> ) , value = '<STR_LIT>' ) <EOL> parent , _ = parent . interpolate ( ) <EOL> assert parent . child ( ) . value ( ) . get ( ) == <NUM_LIT> <EOL> parent = Parent ( child = Child ( value = <NUM_LIT> ) , value = '<STR_LIT>' ) <EOL> parent , _ = parent . interpolate ( ) <EOL> assert parent . child ( ) . value ( ) . get ( ) == <NUM_LIT> </s>
