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_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_block_writer_slot_multiple(self): # Given items = list() items.append(Slot.create_slot(Text.create_from('dog'), Text.create_from('bark'))) items.append(Slot.create_slot(Text.create_from('cat'), Text.create_from('meow'))) writer = _ReconWriter() # When actual = _BlockWriter._write(items, writer=writer, first=True) # Then self.assertIsInstance(actual, _OutputMessage) self.assertEqual('dog:bark,cat:meow', actual._message)
def test_block_writer_slot_in_attr_and_slot(self): # Given items = list() record_map = _Record.create() record_map.add(Slot.create_slot(Text.create_from('dog'), Text.create_from('bark'))) items.append(Attr.create_attr(Text.create_from('animal'), record_map)) items.append(Slot.create_slot(Text.create_from('cat'), Text.create_from('meow'))) writer = _ReconWriter() # When actual = _BlockWriter._write(items, writer=writer, first=True) # Then self.assertIsInstance(actual, _OutputMessage) self.assertEqual('@animal(dog:bark){cat:meow}', actual._message)
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 get_key_item(self) -> 'Record': key_slot = RecordMap.create() key_slot.add( Slot.create_slot( Text.create_from('key'), RecordConverter.get_converter().object_to_record(self.key))) return key_slot
def test_write_item_slot(self): # Given item = Slot.create_slot(Text.create_from('Age'), Num.create_from(32)) writer = _ReconWriter() # When actual = writer._write_item(item) # Then self.assertIsInstance(actual, str) self.assertEqual('Age:32', actual)
def test_block_writer_slot_single_not_first(self): # Given items = list() items.append(Slot.create_slot(Text.create_from('cat'), Text.create_from('meow'))) writer = _ReconWriter() # When actual = _BlockWriter._write(items, writer=writer, first=False) # Then self.assertIsInstance(actual, _OutputMessage) self.assertEqual(',cat:meow', actual._message)
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_attr_writer_existing_key_and_value_slot(self): # Given key = Text.create_from('animal') value = _Record.create() value.add(Slot.create_slot(Text.create_from('dog'), Text.create_from('bark'))) writer = _ReconWriter() # When actual = _AttrWriter._write(key=key, writer=writer, value=value) # Then self.assertIsInstance(actual, _OutputMessage) self.assertEqual('@animal(dog:bark)', actual._message)
def test_write_value_record(self): # Given record = _Record.create() record.add(Slot.create_slot(Text.create_from('Cow'), Text.create_from('Moo'))) writer = _ReconWriter() # When actual = writer._write_value(record) # Then self.assertIsInstance(actual, _OutputMessage) self.assertEqual(7, actual._size) self.assertEqual('Cow:Moo', actual._value) self.assertEqual('o', actual._last_char)
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))
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 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 get_key_item(self) -> '_Record': """ Convert the request key into an Item object. :return: - Request key as an Item object. """ key_slot = RecordMap.create() key_slot.add( Slot.create_slot( Text.create_from('key'), RecordConverter.get_converter().object_to_record(self.key))) return key_slot
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_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 _create_slot(key: Any, value: Any = None) -> 'Slot': return Slot.create_slot(key, value)