def test_block_writer_multiple_attributes(self): # Given items = list() record_map = _Record.create() dog_map = _Record.create() dog_map.add(Slot.create_slot(Text.create_from('dog'), Text.create_from('bark'))) record_map.add(Attr.create_attr(Text.create_from('Animal'), dog_map)) cat_map = _Record.create() cat_map.add(Slot.create_slot(Text.create_from('cat'), Text.create_from('meow'))) record_map.add(Attr.create_attr(Text.create_from('Animal'), cat_map)) bird_map = _Record.create() bird_map.add(Slot.create_slot(Text.create_from('bird'), Text.create_from('chirp'))) record_map.add(Attr.create_attr(Text.create_from('Animal'), bird_map)) items.append(record_map) writer = _ReconWriter() # When actual = _BlockWriter._write(items, writer=writer, first=True) # Then self.assertIsInstance(actual, _OutputMessage) self.assertEqual('@Animal(dog:bark)@Animal(cat:meow)@Animal(bird:chirp)', actual._message)
def test_unlinked_form_cast(self): # Given form = _UnlinkedResponseForm() items = RecordMap.create() items.add( Slot.create_slot(Text.create_from('node'), Text.create_from('unlinked_node'))) items.add( Slot.create_slot(Text.create_from('lane'), Text.create_from('unlinked_lane'))) items.add( Slot.create_slot(Text.create_from('prio'), Num.create_from(20))) items.add( Slot.create_slot(Text.create_from('rate'), Num.create_from(25))) body = Attr.create_attr(Text.create_from('body'), Text.create_from('unlinked_body')) record_map = RecordMap.create() record_map.add(Attr.create_attr(Text.create_from('unlinked'), items)) record_map.add(body) # When actual = form._cast(record_map) # Then self.assertIsInstance(actual, _UnlinkedResponse) self.assertEqual('unlinked_node', actual._node_uri) self.assertEqual('unlinked_lane', actual._lane_uri) self.assertEqual(20, actual._prio) self.assertEqual(25, actual._rate) self.assertEqual(body, actual._body.get_item(0))
def test_link_form_cast(self): # Given form = _LinkRequestForm() items = RecordMap.create() items.add( Slot.create_slot(Text.create_from('node'), Text.create_from('link_node'))) items.add( Slot.create_slot(Text.create_from('lane'), Text.create_from('link_lane'))) items.add( Slot.create_slot(Text.create_from('prio'), Num.create_from(1))) items.add( Slot.create_slot(Text.create_from('rate'), Num.create_from(3))) body = Attr.create_attr(Text.create_from('body'), Text.create_from('link_body')) record_map = RecordMap.create() record_map.add(Attr.create_attr(Text.create_from('link'), items)) record_map.add(body) # When actual = form._cast(record_map) # Then self.assertIsInstance(actual, _LinkRequest) self.assertEqual('link_node', actual._node_uri) self.assertEqual('link_lane', actual._lane_uri) self.assertEqual(1, actual._prio) self.assertEqual(3, actual._rate) self.assertEqual(body, actual._body.get_item(0))
def test_lane_addressed_form_cast_missing_lane(self): # Given form = SyncedResponseForm() items = RecordMap.create() items.add(Attr.create_attr(Text.create_from('node'), Text.create_from('foo_node'))) record_map = RecordMap.create() record_map.add(Attr.create_attr(Text.create_from('synced'), items)) # When actual = form.cast(record_map) # Then self.assertEqual(None, actual)
def test_link_addressed_form_cast_missing_node(self): # Given form = _SyncRequestForm() items = RecordMap.create() items.add( Attr.create_attr(Text.create_from('lane'), Text.create_from('sync_foo_lane'))) record_map = RecordMap.create() record_map.add(Attr.create_attr(Text.create_from('sync'), items)) # When actual = form._cast(record_map) # Then self.assertEqual(None, actual)
async def test_downlink_model_receive_message_unlinked(self, mock_warn): # Given with SwimClient(execute_on_exception=MockExecuteOnException. get_mock_execute_on_exception()) as client: downlink = EventDownlinkModel(client) downlink.connection = MockConnection.get_mock_connection() downlink.connection.owner = downlink body = RecordMap.create() body.add(Attr.create_attr('laneNotFound', 'foo')) unlinked_message = UnlinkedResponse('unlinked_node', 'unlinked_lane', body=body) downlink.connection.messages_to_receive.append(unlinked_message) # When actual = downlink.open() while not MockExecuteOnException.get_mock_execute_on_exception( ).called: pass # Then self.assertEqual(downlink, actual) self.assertEqual('Lane "None" was not found on the remote agent!', mock_warn.call_args_list[0][0][0])
def _mold(self, envelope: Optional['_LaneAddressedEnvelope']) -> 'Value': if envelope is not None: headers = _Record.create()._add_slot('node', envelope._node_uri)._add_slot('lane', envelope._lane_uri) return Attr.create_attr(self._tag, headers)._concat(envelope._body) else: return _Item.extant()
def mold(self, envelope: Optional['LaneAddressedEnvelope']) -> 'Value': if envelope is not None: headers = Record.create().add_slot('node', envelope.node_uri).add_slot('lane', envelope.lane_uri) return Attr.create_attr(self.tag, headers).concat(envelope.body) else: return Item.extant()
def to_record(self) -> '_Record': key_slot = self.get_key_item() value_slot = self.get_value_item() update_record = RecordMap.create_record_map( Attr.create_attr(Text.create_from('update'), key_slot)) update_record.add(value_slot) return update_record
def test_event_form_cast(self): # Given form = EventMessageForm() items = RecordMap.create() items.add(Slot.create_slot(Text.create_from('node'), Text.create_from('event_node'))) items.add(Slot.create_slot(Text.create_from('lane'), Text.create_from('event_lane'))) body = Attr.create_attr(Text.create_from('body'), Text.create_from('event_body')) record_map = RecordMap.create() record_map.add(Attr.create_attr(Text.create_from('event'), items)) record_map.add(body) # When actual = form.cast(record_map) # Then self.assertIsInstance(actual, EventMessage) self.assertEqual('event_node', actual.node_uri) self.assertEqual('event_lane', actual.lane_uri) self.assertEqual(body, actual.body.get_item(0))
def test_write_item_attr(self): # Given item = Attr.create_attr(Text.create_from('Cat'), Text.create_from('Meow')) writer = _ReconWriter() # When actual = writer._write_item(item) # Then self.assertIsInstance(actual, str) self.assertEqual('@Cat(Meow)', actual)
def test_block_writer_attr(self): # Given items = list() items.append(Attr.create_attr(Text.create_from('dog'), Text.create_from('bark'))) writer = _ReconWriter() # When actual = _BlockWriter._write(items, writer=writer) # Then self.assertIsInstance(actual, _OutputMessage) self.assertEqual('@dog(bark)', actual._message)
def test_link_addressed_cast_missing_headers(self): # Given form = SyncRequestForm() items = RecordMap.create() record_map = RecordMap.create() record_map.add(Attr.create_attr(Text.create_from('sync'), items)) # When actual = form.cast(record_map) # Then self.assertEqual(None, actual)
def test_block_writer_nested_attributes(self): # Given items = list() name_record = _Record.create() name_record.add(Slot.create_slot(Text.create_from('Name'), Text.create_from('Collie'))) breed_record = _Record.create() breed_record.add(Attr.create_attr(Text.create_from('Breed'), name_record)) dog_record = _Record.create() dog_record.add(Slot.create_slot(Text.create_from('Dog'), breed_record)) species_record = _Record.create() species_record.add(Attr.create_attr(Text.create_from('Species'), dog_record)) animals_record = _Record.create() animals_record.add(Slot.create_slot(Text.create_from('Animals'), species_record)) items.append(Attr.create_attr(Text.create_from('Zoo'), animals_record)) writer = _ReconWriter() # When actual = _BlockWriter._write(items, writer=writer, first=True) # Then self.assertIsInstance(actual, _OutputMessage) self.assertEqual('@Zoo(Animals:@Species(Dog:@Breed(Name:Collie)))', actual._message)
def test_block_writer_slot_in_attr(self): # Given items = list() record_map = _Record.create() record_map.add(Slot.create_slot(Text.create_from('cat'), Text.create_from('meow'))) items.append(Attr.create_attr(Text.create_from('animal'), record_map)) writer = _ReconWriter() # When actual = _BlockWriter._write(items, writer=writer, first=True) # Then self.assertIsInstance(actual, _OutputMessage) self.assertEqual('@animal(cat:meow)', actual._message)
def test_sync_form_cast(self): # Given form = SyncRequestForm() items = RecordMap.create() items.add(Slot.create_slot(Text.create_from('node'), Text.create_from('sync_node'))) items.add(Slot.create_slot(Text.create_from('lane'), Text.create_from('sync_lane'))) items.add(Slot.create_slot(Text.create_from('prio'), Num.create_from(33.12))) items.add(Slot.create_slot(Text.create_from('rate'), Num.create_from(12.33))) body = Attr.create_attr(Text.create_from('body'), Text.create_from('sync_body')) record_map = RecordMap.create() record_map.add(Attr.create_attr(Text.create_from('sync'), items)) record_map.add(body) # When actual = form.cast(record_map) # Then self.assertIsInstance(actual, SyncRequest) self.assertEqual('sync_node', actual.node_uri) self.assertEqual('sync_lane', actual.lane_uri) self.assertEqual(33.12, actual.prio) self.assertEqual(12.33, actual.rate) self.assertEqual(body, actual.body.get_item(0))
def test_command_form_cast(self): # Given form = _CommandMessageForm() items = RecordMap.create() items.add( Slot.create_slot(Text.create_from('node'), Text.create_from('command_node'))) items.add( Slot.create_slot(Text.create_from('lane'), Text.create_from('command_lane'))) body = Attr.create_attr(Text.create_from('body'), Text.create_from('command_body')) record_map = RecordMap.create() record_map.add(Attr.create_attr(Text.create_from('command'), items)) record_map.add(body) # When actual = form._cast(record_map) # Then self.assertIsInstance(actual, _CommandMessage) self.assertEqual('command_node', actual._node_uri) self.assertEqual('command_lane', actual._lane_uri) self.assertEqual(body, actual._body.get_item(0))
def test_to_string(self): # Given value = RecordMap.create() value.add( Attr.create_attr( Text.create_from('remove'), RecordMap.create_record_map( Slot.create_slot(Text.create_from('key'), Text.create_from('foo'))))) # When actual = Recon.to_string(value) # Then self.assertEqual('@remove(key:foo)', actual)
def test_write_event_body_nested(self): # Given friend = RecordMap.create() friend.add( Attr.create_attr(Text.create_from('Person'), _Extant._get_extant())) friend.add( Slot.create_slot(Text.create_from('name'), Text.create_from('Sam/Spam'))) friend.add( Slot.create_slot(Text.create_from('age'), Num.create_from(1))) friend.add( Slot.create_slot(Text.create_from('salary'), Num.create_from(22))) body = RecordMap.create() body.add( Attr.create_attr(Text.create_from('Person'), _Extant._get_extant())) body.add( Slot.create_slot(Text.create_from('name'), Text.create_from('Par'))) body.add(Slot.create_slot(Text.create_from('age'), Num.create_from(11))) body.add( Slot.create_slot(Text.create_from('salary'), Num.create_from(-5.9))) body.add(Slot.create_slot(Text.create_from('friend'), friend)) envelope = _EventMessage('/this/is/spam', 'hello', body=body) person = '@Person{name:Par,age:11,salary:-5.9,friend:@Person{name:"Sam/Spam",age:1,salary:22}}' expected = '@event(node:"/this/is/spam",lane:hello)' + person # When actual = envelope._to_recon() # Then self.assertEqual(expected, actual)
def _mold(self, envelope: Optional['_LinkAddressedEnvelope']) -> 'Value': if envelope is not None: headers = _Record.create()._add_slot('node', envelope._node_uri)._add_slot('lane', envelope._lane_uri) prio = envelope._prio if prio != 0 and not math.isnan(prio): headers._add_slot('prio', Num.create_from(prio)) rate = envelope._rate if rate != 0 and not math.isnan(rate): headers._add_slot('rate', Num.create_from(rate)) return Attr.create_attr(self._tag, headers)._concat(envelope._body) else: return _Item.extant()
def test_write_command_body_remove(self): # Given envelope = _CommandMessage( '/unit/foo', 'shoppingCart', body=RecordMap.create_record_map( Attr.create_attr( Text.create_from('remove'), RecordMap.create_record_map( Slot.create_slot( Text.create_from('key'), Text.create_from('FromClientLink')))))) expected = '@command(node:"/unit/foo",lane:shoppingCart)@remove(key:FromClientLink)' # When actual = envelope._to_recon() # Then self.assertEqual(expected, actual)
def test_parse_literal_slot_existing_builder(self): # Given message = _InputMessage._create('{Foo: Bar}') builder = _ReconParser._create_value_builder() builder.add(Attr.create_attr('Baz', 'Qux')) parser = _ReconParser() # When actual = parser._parse_literal(message, builder) # Then self.assertIsInstance(actual, RecordMap) self.assertEqual(2, actual.size) self.assertIsInstance(actual.get_item(0), Attr) self.assertIsInstance(actual.get_item(0).key, Text) self.assertEqual('Baz', actual.get_item(0).key.value) self.assertIsInstance(actual.get_item(0).value, str) self.assertEqual('Qux', actual.get_item(0).value) self.assertIsInstance(actual.get_item(1), Slot) self.assertIsInstance(actual.get_item(1).key, Text) self.assertEqual('Foo', actual.get_item(1).key.value) self.assertIsInstance(actual.get_item(1).value, Text) self.assertEqual('Bar', actual.get_item(1).value.value)
async def test_write_event_body_object(self): # Given body = RecordMap.create() body.add( Attr.create_attr(Text.create_from('Person'), Extant.get_extant())) body.add( Slot.create_slot(Text.create_from('name'), Text.create_from('Bar'))) body.add(Slot.create_slot(Text.create_from('age'), Num.create_from(14))) body.add( Slot.create_slot(Text.create_from('salary'), Num.create_from(-5.9))) envelope = EventMessage('/this/is/spam', 'hello', body=body) expected = '@event(node:"/this/is/spam",lane:hello)@Person{name:Bar,age:14,salary:-5.9}' # When actual = await envelope.to_recon() # Then self.assertEqual(expected, actual)
def _create_attr(key: Any, value: Any = Value.extant()) -> 'Attr': return Attr.create_attr(key, value)
def to_record(self) -> '_Record': key_slot = self.get_key_item() remove_record = RecordMap.create_record_map( Attr.create_attr(Text.create_from('remove'), key_slot)) return remove_record