Example #1
0
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)
Example #2
0
def test_interaction_invalid_name():
    """Invalid names for Interaction objects are rejected."""

    names_valid = ["safename", "safename02", "SafeName_03", "Safe_Name-04"]

    names_invalid = ["!unsafename", "unsafe_name_ñ", "unsafe name", "?"]

    thing = Thing(id=uuid.uuid4().urn)

    for name in names_valid:
        Action(thing=thing, name=name)

    for name in names_invalid:
        with pytest.raises(ValueError):
            Action(thing=thing, name=name)
Example #3
0
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
Example #4
0
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
Example #5
0
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
Example #6
0
    def _init_fragment_interactions(self):
        """Adds the interactions declared in the ThingFragment to the instance private dicts."""

        for name, prop_fragment in six.iteritems(self._thing_fragment.properties):
            prop = Property(thing=self, name=name, init_dict=prop_fragment)
            self.add_interaction(prop)

        for name, action_fragment in six.iteritems(self._thing_fragment.actions):
            action = Action(thing=self, name=name, init_dict=action_fragment)
            self.add_interaction(action)

        for name, event_fragment in six.iteritems(self._thing_fragment.events):
            event = Event(thing=self, name=name, init_dict=event_fragment)
            self.add_interaction(event)
Example #7
0
    def add_action(self, name, action_init, action_handler=None):
        """Adds an Action to the Thing object as defined by the action
        argument of type ThingActionInit and updates th,e Thing Description."""

        if isinstance(action_init, dict):
            action_init = ActionFragmentDict(action_init)

        action = Action(thing=self._thing, name=name, init_dict=action_init)

        self._thing.add_interaction(action)

        event_data = ThingDescriptionChangeEventInit(
            td_change_type=TDChangeType.ACTION,
            method=TDChangeMethod.ADD,
            name=name,
            data=action_init.to_dict(),
            description=ThingDescription.from_thing(self.thing).to_dict())

        self._events_stream.on_next(
            ThingDescriptionChangeEmittedEvent(init=event_data))

        if action_handler:
            self.set_action_handler(name, action_handler)
Example #8
0
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)