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_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