Example #1
0
def test_compat_blessed_key_deeply_nested():
    class BaseAttribution(Record, abc.ABC):  # noqa: B024
        def __post_init__(self, *args, **kwargs) -> None:
            self.data_store = None

    class AdjustData(Record):
        activity_kind: str

    class Event(Record):
        category: str
        event: str
        data: AdjustData

    class AdjustRecord(BaseAttribution):
        event: Event

    x = AdjustRecord(
        Event(
            category="foo",
            event="bar",
            data=AdjustData("baz"),
        ))
    value = x.dumps(serializer="json")
    value_dict = json.loads(value)
    value_dict["event"]["__faust"]["ns"] = "x.y.z"
    model = AdjustRecord.from_data(value_dict)
    assert isinstance(model.event, Event)
    assert isinstance(model.event.data, AdjustData)
Example #2
0
def test_compat_blessed_key_deeply_nested():
    class BaseAttribution(Record, abc.ABC):
        def __post_init__(self, *args, **kwargs) -> None:
            self.data_store = None

    class AdjustData(Record):
        activity_kind: str

    class Event(Record):
        category: str
        event: str
        data: AdjustData

    class AdjustRecord(BaseAttribution):
        event: Event

    x = AdjustRecord(
        Event(
            category='foo',
            event='bar',
            data=AdjustData('baz'),
        ))
    value = x.dumps(serializer='json')
    value_dict = json.loads(value)
    value_dict['event']['__faust']['ns'] = 'x.y.z'
    model = AdjustRecord.from_data(value_dict)
    assert isinstance(model.event, Event)
    assert isinstance(model.event.data, AdjustData)
Example #3
0
def test_json():
    account = Account(id=1, name=2)
    user = User(1, 2, account)

    payload = json.dumps(user)
    deser = json.loads(payload)
    assert deser == {
        "id": 1,
        "username": 2,
        "account": {
            "id": 1,
            "name": 2,
            "active": True,
            "__faust": {
                "ns": Account._options.namespace
            },
        },
        "__faust": {
            "ns": User._options.namespace
        },
    }

    assert user.__json__() == {
        "id": 1,
        "username": 2,
        "account": account,
        "__faust": {
            "ns": User._options.namespace
        },
    }

    assert User.from_data(deser) == user
Example #4
0
def test_json():
    account = Account(id=1, name=2)
    user = User(1, 2, account)

    payload = json.dumps(user)
    deser = json.loads(payload)
    assert deser == {
        'id': 1,
        'username': 2,
        'account': {
            'id': 1,
            'name': 2,
            'active': True,
            '__faust': {
                'ns': Account._options.namespace
            },
        },
        '__faust': {
            'ns': User._options.namespace
        },
    }

    assert user.__json__() == {
        'id': 1,
        'username': 2,
        'account': account,
        '__faust': {
            'ns': User._options.namespace
        },
    }

    assert User.from_data(deser) == user
Example #5
0
def test_combinators(input: Mapping[str, str]) -> None:
    s = json() | _binary()
    assert repr(s).replace("u'", "'") == 'json() | binary()'

    d = s.dumps(input)
    assert isinstance(d, bytes)
    assert _json.loads(want_str(base64.b64decode(d))) == input
Example #6
0
 def to_python(self,
               conf: _Settings,
               value: DictArg[T]) -> Mapping[str, T]:
     if isinstance(value, str):
         return json.loads(value)
     elif isinstance(value, Mapping):
         return value
     return dict(value)
Example #7
0
 def call_faust_cli(*args: str) -> Tuple[str, str]:
     p = subprocess.Popen(
         [str(executable)] + list(partial_args) + list(args),
         stdout=subprocess.PIPE,
         stderr=subprocess.PIPE,
         shell=False,
     )
     stdout, stderr = p.communicate()
     if json:
         return loads(stdout), stderr
     return stdout, stderr
Example #8
0
def test_maybe_model():
    class X(Record):
        x: int
        y: int

    assert maybe_model("foo") == "foo"
    assert maybe_model(1) == 1
    assert maybe_model(1.01) == 1.01

    x1 = X(10, 20)
    assert maybe_model(json.loads(x1.dumps(serializer="json"))) == x1
Example #9
0
def test_maybe_model():
    class X(Record):
        x: int
        y: int

    assert maybe_model('foo') == 'foo'
    assert maybe_model(1) == 1
    assert maybe_model(1.01) == 1.01

    x1 = X(10, 20)
    assert maybe_model(json.loads(x1.dumps(serializer='json'))) == x1
Example #10
0
 def call_faust_cli(*args: str) -> Tuple[str, str]:
     p = subprocess.Popen(
         [sys.executable, str(executable)] + list(partial_args) +
         list(args),
         stdout=subprocess.PIPE,
         stderr=subprocess.PIPE,
         shell=False,
     )
     stdout, stderr = p.communicate()
     if json:
         print(f"JSON RET: {p.returncode} {stdout!r} {stderr!r}")
         ret = p.returncode, loads(stdout), stderr
         return ret
     print(f"TEXT RET: {p.returncode} {stdout!r} {stderr!r}")
     return p.returncode, stdout, stderr
Example #11
0
def test_paramters_with_custom_init():
    class Point(Record, include_metadata=False):
        x: int
        y: int

        def __init__(self, x, y, **kwargs):
            self.x = x
            self.y = y

    p = Point(30, 10)
    assert p.x == 30
    assert p.y == 10

    payload = p.dumps(serializer="json")
    assert payload == b'{"x":30,"y":10}'

    data = json.loads(payload)
    p2 = Point.from_data(data)
    assert p2.x == 30
    assert p2.y == 10
Example #12
0
def test_parameters_with_custom_init_and_super():
    class Point(Record, include_metadata=False):
        x: int
        y: int

        def __post_init__(self):
            self.z = self.x + self.y

    p = Point(30, 10)
    assert p.x == 30
    assert p.y == 10
    assert p.z == 40

    payload = p.dumps(serializer="json")
    assert payload == b'{"x":30,"y":10}'

    data = json.loads(payload)
    p2 = Point.from_data(data)
    assert p2.x == 30
    assert p2.y == 10
    assert p2.z == 40
Example #13
0
 def _loads(self, s: bytes) -> Any:
     return _json.loads(want_str(s))
Example #14
0
 def get_value(self, message):
     return json.loads(message.value.decode())
Example #15
0
 def get_key(self, message):
     return json.loads(message.key.decode())