def test_duplicated_interactions(): """Duplicated Interactions are rejected on a Thing.""" thing = Thing(id=uuid.uuid4().urn) interaction_01 = Action(thing=thing, name="my_interaction") interaction_02 = Action(thing=thing, name="AnotherInteraction") interaction_03 = Action(thing=thing, name="my_interaction") thing.add_interaction(interaction_01) thing.add_interaction(interaction_02) with pytest.raises(ValueError): thing.add_interaction(interaction_03)
def test_duplicated_forms(): """Duplicated Forms are rejected on an Interaction.""" thing = Thing(id=uuid.uuid4().urn) interaction = Action(thing=thing, name="my_interaction") thing.add_interaction(interaction) href_01 = "/href-01" href_02 = "/href-02" mtype_01 = "application/json" mtype_02 = "text/html" form_01 = Form(interaction=interaction, protocol=Protocols.HTTP, href=href_01, content_type=mtype_01) form_02 = Form(interaction=interaction, protocol=Protocols.HTTP, href=href_01, content_type=mtype_01) form_03 = Form(interaction=interaction, protocol=Protocols.HTTP, href=href_01, content_type=mtype_02) form_04 = Form(interaction=interaction, protocol=Protocols.HTTP, href=href_02, content_type=mtype_01) form_05 = Form(interaction=interaction, protocol=Protocols.HTTP, href=href_02, content_type=mtype_02) form_06 = Form(interaction=interaction, protocol=Protocols.HTTP, href=href_02, content_type=mtype_02) interaction.add_form(form_01) with pytest.raises(ValueError): interaction.add_form(form_02) interaction.add_form(form_03) interaction.add_form(form_04) interaction.add_form(form_05) with pytest.raises(ValueError): interaction.add_form(form_06)
def test_find_interaction(): """Interactions may be retrieved by name on a Thing.""" thing = Thing(id=uuid.uuid4().urn) interaction_01 = Action(thing=thing, name="my_interaction") interaction_02 = Action(thing=thing, name="AnotherInteraction") thing.add_interaction(interaction_01) thing.add_interaction(interaction_02) assert thing.find_interaction(interaction_01.name) is interaction_01 assert thing.find_interaction(interaction_02.name) is interaction_02 assert thing.find_interaction(slugify( interaction_01.name)) is interaction_01 assert thing.find_interaction(slugify( interaction_02.name)) is interaction_02
def test_remove_interaction(): """Interactions may be removed from a Thing by name.""" thing = Thing(id=uuid.uuid4().urn) interaction_01 = Action(thing=thing, name="my_interaction") interaction_02 = Action(thing=thing, name="AnotherInteraction") interaction_03 = Action(thing=thing, name="YetAnother_interaction") thing.add_interaction(interaction_01) thing.add_interaction(interaction_02) thing.add_interaction(interaction_03) assert thing.find_interaction(interaction_01.name) is not None assert thing.find_interaction(interaction_02.name) is not None assert thing.find_interaction(interaction_03.name) is not None thing.remove_interaction(interaction_01.name) thing.remove_interaction(slugify(interaction_03.name)) assert thing.find_interaction(interaction_01.name) is None assert thing.find_interaction(interaction_02.name) is not None assert thing.find_interaction(interaction_03.name) is None
def test_from_thing(): """ThingDescription objects can be built from Thing objects.""" fake = Faker() thing_id = uuid.uuid4().urn action_id = uuid.uuid4().hex prop_id = uuid.uuid4().hex event_id = uuid.uuid4().hex action_form_href = fake.url() prop_form_href = fake.url() thing = Thing(id=thing_id) action = Action(thing=thing, name=action_id) action_form = Form(interaction=action, protocol=Protocols.HTTP, href=action_form_href) action.add_form(action_form) thing.add_interaction(action) prop = Property(thing=thing, name=prop_id, type="string") prop_form = Form(interaction=prop, protocol=Protocols.HTTP, href=prop_form_href) prop.add_form(prop_form) thing.add_interaction(prop) event = Event(thing=thing, name=event_id) thing.add_interaction(event) json_td = ThingDescription.from_thing(thing) td_dict = json_td.to_dict() assert td_dict["id"] == thing.id assert td_dict["title"] == thing.title assert len(td_dict["properties"]) == 1 assert len(td_dict["actions"]) == 1 assert len(td_dict["events"]) == 1 assert len(td_dict["actions"][action_id]["forms"]) == 1 assert len(td_dict["properties"][prop_id]["forms"]) == 1 assert td_dict["actions"][action_id]["forms"][0][ "href"] == action_form_href assert td_dict["properties"][prop_id]["forms"][0]["href"] == prop_form_href