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_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_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_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},
def test_choice_in_struct():
class SOne(Struct):
a = Choice((Integer, Float))
b = String
one = SOne(a=12, b="abc")
assert one.check().ok()
assert one.interpolate()[0].a().unwrap() == Integer(12)
two = SOne(a="1{{q}}2", b="hi there")
assert not two.check().ok()
refs = two.interpolate()[1]
assert refs == [Ref.from_address('q')]
two_int = two.bind(q="34")
assert two_int.check().ok()
assert two_int.a().unwrap() == Integer(1342)
def test_choice_in_stache():
class Foo(Struct):
x = Integer
class Bar(Struct):
a = Choice("StringOrFoo", [Integer, String, Foo])
b = String
stringbar = Bar(a="hello", b="{{a}} world!")
assert stringbar.check().ok()
assert json.loads(stringbar.json_dumps()) == {'a': 'hello', 'b': 'hello world!'}
intbar = Bar(a=4, b="{{a}} world!")
assert intbar.check().ok()
assert json.loads(intbar.json_dumps()) == {'a': 4, 'b': '4 world!'}
foobar = Bar(a=Foo(x=5), b='{{a}} world!')
assert foobar.check().ok()
assert json.loads(foobar.json_dumps()) == {'a': {'x': 5}, 'b': 'Foo(x=5) world!'}
ports = Map(String, Integer)
# TODO(wickman) Move the underlying replacement mechanism to %task_id%
task_id = String
# TODO(wickman) Move underlying mechanism to %user%
user = String
class Resources(Struct):
cpu = Required(Float)
ram = Required(Integer)
disk = Required(Integer)
class Constraint(Struct):
order = List(String)
class Process(Struct):
cmdline = Required(String)
name = Required(String)
# This is currently unused but reserved for future use by Thermos.
resources = Resources
# optionals
max_failures = Default(Integer, 1) # maximum number of failed process runs
# before process is failed.
daemon = Default(Boolean, False)
ephemeral = Default(Boolean, False)
min_duration = Default(Integer, 5) # integer seconds
# TODO(wickman) Move the underlying replacement mechanism to %task_id%
task_id = String
# TODO(wickman) Move underlying mechanism to %user%
user = String
class Resources(Struct):
cpu = Required(Float)
ram = Required(Integer)
disk = Required(Integer)
gpu = Default(Integer, 0)
class Constraint(Struct):
order = List(String)
class RotatePolicy(Struct):
log_size = Default(Integer, 100*MB)
backups = Default(Integer, 5)
LoggerDestination = Enum('file', 'console', 'both', 'none')
LoggerMode = Enum('standard', 'rotate')
class Logger(Struct):
destination = Default(LoggerDestination, LoggerDestination('file'))
class Constraint(Struct):
order = List(String)
class RotatePolicy(Struct):
log_size = Default(Integer, 100*MB)
backups = Default(Integer, 5)
LoggerDestination = Enum('file', 'console', 'both', 'none')
LoggerMode = Enum('standard', 'rotate')
class Logger(Struct):
destination = Default(LoggerDestination, LoggerDestination('file'))
mode = Default(LoggerMode, LoggerMode('standard'))
rotate = RotatePolicy
class Process(Struct):
cmdline = Required(String)
name = Required(String)
# This is currently unused but reserved for future use by Thermos.
resources = Resources
# optionals
max_failures = Default(Integer, 1) # maximum number of failed process runs
# before process is failed.
daemon = Default(Boolean, False)
GB = 1024 * MB
TB = 1024 * GB
class ThermosContext(Struct):
# TODO(wickman) Move the underlying replacement mechanism to %port% replacements
ports = Map(String, Integer)
# TODO(wickman) Move the underlying replacement mechanism to %task_id%
task_id = String
# TODO(wickman) Move underlying mechanism to %user%
user = String
class Resources(Struct):
cpu = Required(Float)
ram = Required(Integer)
disk = Required(Integer)
class Constraint(Struct):
order = List(String)
class Process(Struct):
cmdline = Required(String)
name = Required(String)
# This is currently unused but reserved for future use by Thermos.
resources = Resources