class V: source = 9001 found = Found() @hp.memoized_property def sender(s): sender = mock.Mock( name="sender", source=s.source, found=s.found, spec=["source", "seq", "found"], ) sender.seq.return_value = 1 return sender
async def assertSpecificSerials(s, serials, found, missing): a = mock.Mock(name="a") broadcast = mock.Mock(name="broadcast") ignore_lost = mock.Mock(name="ignore_lost") raise_on_none = mock.Mock(name="raise_on_none") f = Found() for target, info in found.items(): f[target] = info _find_specific_serials = pytest.helpers.AsyncMock(name="_find_specific_serials") _find_specific_serials.return_value = f with mock.patch.object( s.communication, "_find_specific_serials", _find_specific_serials ): f2, m = await s.communication.find_specific_serials( serials, ignore_lost=ignore_lost, raise_on_none=raise_on_none, broadcast=broadcast, a=a, ) assert f2 is f assert m == missing _find_specific_serials.assert_called_once_with( serials, ignore_lost=ignore_lost, raise_on_none=raise_on_none, broadcast=broadcast, a=a, ) _find_specific_serials.reset_mock() with mock.patch.object( s.communication, "_find_specific_serials", _find_specific_serials ): f3, m = await s.communication.find_specific_serials( serials, broadcast=broadcast, a=a ) assert f3 is f assert m == missing _find_specific_serials.assert_called_once_with( serials, ignore_lost=False, raise_on_none=False, broadcast=broadcast, a=a )
_search_retry_iterator.assert_called_once_with(timeout) return mocks async it "can use hard coded discovery", V: V.transport_target.discovery_options = ( NoEnvDiscoveryOptions.FieldSpec().empty_normalise( hardcoded_discovery={ "d073d5000001": "192.168.0.1", "d073d5000002": "192.168.0.2", } ) ) assert V.session.found == Found() fn = await V.session._do_search(None, 20) assert sorted(fn) == sorted( [binascii.unhexlify(s) for s in ("d073d5000001", "d073d5000002")] ) assert V.session.found.serials == ["d073d5000001", "d073d5000002"] assert V.session.found["d073d5000001"] == { Services.UDP: await V.session.make_transport( "d073d5000001", Services.UDP, {"host": "192.168.0.1", "port": 56700} ) } assert V.session.found["d073d5000002"] == { Services.UDP: await V.session.make_transport( "d073d5000002", Services.UDP, {"host": "192.168.0.2", "port": 56700}
def found(): return Found()
import binascii import pytest @pytest.fixture() def found(): return Found() describe "Found": it "starts empty", found: assert found.found == {} assert not found assert len(found) == 0 assert found.serials == [] assert found == Found() assert list(found) == [] it "can be cloned": found = Found() found["d073d5000001"] = {"one": 1, "two": 2} found["d073d5000002"] = {"three": 3, "four": 4} h = lambda serial: binascii.unhexlify(serial)[:6] found2 = found.clone() del found2["d073d5000001"]["one"] assert found.found == { h("d073d5000001"): {"one": 1, "two": 2}, h("d073d5000002"): {"three": 3, "four": 4}, }
options = {"comms": V.communication, "other": other} class Thing(dictobj.Spec): other = dictobj.Field(sb.overridden("{other}"), formatted=True) comms = dictobj.Field(sb.overridden("{comms}"), formatted=True) thing = Thing.FieldSpec(formatter=MergedOptionStringFormatter).normalise( Meta(options, []).at("thing"), {} ) assert thing.comms is V.communication assert thing.other == str(other) async it "takes in a transport_target", V: assert V.communication.transport_target is V.transport_target assert V.communication.found == Found() assert isinstance(V.communication.receiver, Receiver) async it "has a stop fut", V: assert not V.communication.stop_fut.done() V.communication.stop_fut.cancel() assert not V.final_future.cancelled() comm2 = Communication(V.transport_target) assert not comm2.stop_fut.done() V.final_future.set_result(1) assert comm2.stop_fut.cancelled() async it "calls setup", V: called = []
async def find(s, *args, **kwargs): found = Found() found["d073d5000000"] = mock.Mock(name="service") return found, ["d073d5000000"]
async def search(s, found, accept_found, find_timeout=1): return await item.search( s.sender, found, accept_found, s.packets, s.broadcast_address, find_timeout, s.kwargs, ) return V() async it "returns without looking if we have all the targets", V: found = Found() found[V.target1] = V.found_info1 found[V.target2] = V.found_info2 f, missing = await V.search(found, False) assert f is found assert missing == [] assert len(V.find_specific_serials.mock_calls) == 0 async it "returns without looking if accept_found is True", V: found = Found() found[V.target1] = V.found_info1 found[V.target2] = V.found_info2 f, missing = await V.search(found, True) assert len(V.find_specific_serials.mock_calls) == 0