Example #1
0
def test_send_broadcast_async(mocks: Mocks, branch: yogi.Branch):
    """Verifies that a broadcast can be sent asynchronously"""
    called = False

    def fn(branch, enc, data, datasize, retry, handler_fn, userarg):
        assert branch == 8888
        assert enc == yogi.Encoding.JSON
        assert c_char_p(data).value == b'{}'
        assert datasize == 3
        assert retry == 1
        assert handler_fn
        handler_fn(yogi.ErrorCode.OK, 345, userarg)
        return 111

    def handler_fn(res, oid):
        assert isinstance(res, yogi.Success)
        assert isinstance(oid, yogi.OperationId)
        assert oid.value == 345
        nonlocal called
        called = True

    mocks.MOCK_BranchSendBroadcastAsync(fn)
    assert branch.send_broadcast_async(yogi.JsonView({}), handler_fn, retry=True).value == 111
    assert called

    def fn2(branch, enc, data, datasize, retry, handler_fn, userarg):
        assert retry == 0
        handler_fn(yogi.ErrorCode.BUSY, 345, userarg)
        return 222

    def handler_fn2(res, oid):
        assert res.error_code == yogi.ErrorCode.BUSY

    mocks.MOCK_BranchSendBroadcastAsync(fn2)
    assert branch.send_broadcast_async(yogi.JsonView({}), handler_fn2, retry=False).value == 222
    def test_comparison_operators(self):
        a = yogi.JsonView("[1]")
        b = yogi.JsonView("[1]")
        c = yogi.JsonView("[2]")

        self.assertTrue(a == b)
        self.assertFalse(a == c)

        self.assertFalse(a != b)
        self.assertTrue(a != c)
Example #3
0
    def test_comparison_operators_json_view(self):
        view = yogi.PayloadView(yogi.JsonView("[1]"))
        a = yogi.JsonView("[1]")
        b = yogi.JsonView("[2]")

        self.assertTrue(view == a)
        self.assertFalse(view == b)

        self.assertFalse(view != a)
        self.assertTrue(view != b)
def test_comparison_operators_json_view():
    """Verifies that a PayloadView instance can be compared to a JsonView for equality"""
    view = yogi.PayloadView(yogi.JsonView("[1]"))
    a = yogi.JsonView("[1]")
    b = yogi.JsonView("[2]")

    assert view == a
    assert not view == b

    assert not view != a
    assert view != b
Example #5
0
def test_comparison_operators():
    """Checks the equal and non-equal comparison operators"""
    a = yogi.JsonView("[1]")
    b = yogi.JsonView("[1]")
    c = yogi.JsonView("[2]")

    assert a == b
    assert not a == c

    assert not a != b
    assert a != c
Example #6
0
def test_send_broadcast(mocks: Mocks, branch: yogi.Branch):
    """Verifies that a broadcast can be sent synchronously"""
    def fn(branch, enc, data, datasize, block):
        assert branch == 8888
        assert enc == yogi.Encoding.JSON
        assert c_char_p(data).value == b'{}'
        assert datasize == 3
        assert block == 1
        return yogi.ErrorCode.OK

    mocks.MOCK_BranchSendBroadcast(fn)
    assert branch.send_broadcast(yogi.JsonView({}), block=True)

    def fn2(branch, enc, data, datasize, block):
        assert block == 0
        return yogi.ErrorCode.TX_QUEUE_FULL

    mocks.MOCK_BranchSendBroadcast(fn2)
    assert not branch.send_broadcast(yogi.JsonView({}), block=False)
Example #7
0
def test_len_operator():
    """Verifies that len() returns a sensible value"""
    view = yogi.JsonView("[1]")
    assert len(view) == view.size
Example #8
0
def test_from_object():
    """Verifies that a JsonView can be constructed from an object"""
    view = yogi.JsonView([5])
    assert view.data == '[5]\0'.encode()
    assert view.size == 4
Example #9
0
def test_from_string():
    """Verifies that a JsonView can be constructed from a string"""
    view = yogi.JsonView("[1]")
    assert view.data == '[1]\0'.encode()
    assert view.size == 4
 def setUp(self):
     self.context = yogi.Context()
     self.json_view = yogi.JsonView("[1,2,3]")
     self.big_json_view = yogi.JsonView(self.make_big_json_data())
     self.msgpack_view = yogi.MsgpackView(bytes([0x93, 0x1, 0x2, 0x3]))
 def test_len_operator(self):
     view = yogi.JsonView("[1]")
     self.assertEqual(len(view), view.size)
 def test_from_object(self):
     view = yogi.JsonView([5])
     self.assertEqual(view.data, '[5]\0'.encode())
     self.assertEqual(view.size, 4)
 def test_from_string(self):
     view = yogi.JsonView("[1]")
     self.assertEqual(view.data, '[1]\0'.encode())
     self.assertEqual(view.size, 4)
def test_from_json_view():
    """Verifies that a payload can be constructed from a JsonView"""
    jsn = yogi.JsonView("[1]")
    view = yogi.PayloadView(jsn)
    assert view.data == jsn.data
    assert view.size == jsn.size
 def test_hash(self):
     a = yogi.JsonView("[1]")
     b = yogi.JsonView("[2]")
     self.assertNotEqual(hash(a), hash(b))
Example #16
0
def test_hash():
    """Verifies that a hash can be computed over the JsonView"""
    a = yogi.JsonView("[1]")
    b = yogi.JsonView("[2]")
    assert hash(a) != hash(b)
Example #17
0
 def test_from_json_view(self):
     jsn = yogi.JsonView("[1]")
     view = yogi.PayloadView(jsn)
     self.assertEqual(view.data, jsn.data)
     self.assertEqual(view.size, jsn.size)