Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_choice_triple():
Triple = Choice((Integer, Float, String))
one = Triple(123)
two = Triple(123.456)
three = Triple("123.abc")
assert one.check().ok()
assert two.check().ok()
assert three.check().ok()
def test_choice_default():
"""Ensure that choices with a default work correctly."""
class Dumb(Struct):
one = String
class ChoiceDefaultStruct(Struct):
a = Default(Choice("IntOrDumb", [Dumb, Integer]), 28)
b = Integer
class OtherStruct(Struct):
first = ChoiceDefaultStruct
second = String
v = OtherStruct(second="hello")
assert v.check()
assert json.loads(v.json_dumps()) == {"second": "hello"}
w = v(first=ChoiceDefaultStruct())
assert w.check()
assert json.loads(w.json_dumps()) == {'first': {'a': 28}, 'second': 'hello'}
x = v(first=ChoiceDefaultStruct(a=296, b=36))
assert x.check()
assert json.loads(x.json_dumps()) == {'first': {'a': 296, 'b': 36},
'second': 'hello'}
def test_repr():
class Dumb(Struct):
one = String
class ChoiceDefaultStruct(Struct):
a = Default(Choice("IntOrDumb", [Dumb, Integer]), 28)
b = Integer
class OtherStruct(Struct):
first = ChoiceDefaultStruct
second = String
C = Choice([String, List(Integer)])
testvalone = C("hello")
testvaltwo = C([1, 2, 3])
assert repr(testvalone) == "Choice_String_IntegerList('hello')"
assert repr(testvaltwo) == "Choice_String_IntegerList([1, 2, 3])"
def test_choice_error():
IntFloat = Choice((Integer, Float))
one = IntFloat(123)
two = IntFloat(123.456)
three = IntFloat("123.abc")
assert one.check().ok()
assert two.check().ok()
assert not three.check().ok()
def test_get_choice_in_struct():
class Foo(Struct):
foo = Required(String)
class Bar(Struct):
bar = Required(String)
Item = Choice("Item", (Foo, Bar))
class Qux(Struct):
item = Choice([String, List(Item)])
b = Qux(item=[Foo(foo="fubar")])
assert b.get() == frozendict({'item': (frozendict({'foo': u'fubar'}),)})
def test_json_choice():
"""Make sure that serializing to JSON works for structs with choices."""
class Foo(Struct):
a = String
b = Integer
class Yuck(Struct):
one = Choice([Foo, Integer])
two = String
z = Yuck(one=Foo(a="1", b=2), two="hello")
assert z.check().ok()
d = json.loads(z.json_dumps())
assert d == {"two": "hello", "one": {"a": "1", "b": 2}}
def test_choice_type():
IntStr = Choice("IntStrFloat", (Integer, String))
one = IntStr(123)
two = IntStr("123")
three = IntStr("abc")
assert one == IntStr(123)
assert two == IntStr("123")
assert three == IntStr("abc")
assert one == two
assert not one == three
assert one != three
assert not one != two
assert one.unwrap() == Integer(123)
assert two.unwrap() == Integer(123)
assert three.unwrap() == String("abc")
def test_choice_string_enum():
TestEnum = Enum("TestEnum", ("A", "B", "C"))
TestChoice = Choice("TestChoice", (TestEnum, String))
v = TestChoice("A")
assert isinstance(v.interpolate()[0], TestEnum)
assert isinstance(TestChoice("Q").interpolate()[0], String)
image_id = Required(String)
class DockerImage(Struct):
name = Required(String)
tag = Required(String)
Mode = Enum('RO', 'RW')
class Volume(Struct):
container_path = Required(String)
host_path = Required(String)
mode = Required(Mode)
class Mesos(Struct):
image = Choice([AppcImage, DockerImage])
volumes = Default(List(Volume), [])
class Container(Struct):
docker = Docker
class PartitionPolicy(Struct):
reschedule = Default(Boolean, True)
delay_secs = Default(Integer, 0)
class Metadata(Struct):
key = Required(String)
value = Required(String)
production = Default(Boolean, False)
priority = Default(Integer, 0)
health_check_config = Default(HealthCheckConfig, HealthCheckConfig())
# TODO(wickman) Make Default(Any, LifecycleConfig()) once pystachio #17 is addressed.
lifecycle = Default(LifecycleConfig, DefaultLifecycleConfig)
task_links = Map(String, String) # Unsupported. See AURORA-739
executor_config = Default(ExecutorConfig, ExecutorConfig())
enable_hooks = Default(Boolean, False) # enable client API hooks; from env python-list 'hooks'
partition_policy = PartitionPolicy
sla_policy = Choice([CoordinatorSlaPolicy, CountSlaPolicy, PercentageSlaPolicy])
# Specifying a `Container` with a `docker` property for Docker jobs is deprecated, instead just
# specify the value of the container property to be a `Docker` container directly.
container = Choice([Container, Docker, Mesos])
Job = MesosJob
Service = Job(service = True)