def __init__(self):
        """Initialise the dataModel."""
        self.attribute_country = Attribute("country", str, True)
        self.attribute_city = Attribute("city", str, True)

        super().__init__("thermometer_datamodel",
                         [self.attribute_country, self.attribute_city])
示例#2
0
def test_data_model():
    """Test data model definitions."""
    params = dict(name="test", type_=str, is_required=True)
    data_model = DataModel("test", [Attribute(**params)])

    data_model._check_validity()
    with pytest.raises(
            ValueError,
            match=
            "Invalid input value for type 'DataModel': duplicated attribute name.",
    ):
        data_model = DataModel(
            "test",
            [Attribute(**params), Attribute(**params)])
        data_model._check_validity()

    assert data_model == DataModel("test", [Attribute(**params)])
    assert data_model != DataModel("not test", [Attribute(**params)])

    assert (
        str(data_model) ==
        "DataModel(name=test,attributes={'test': \"Attribute(name=test,type=<class 'str'>,is_required=True)\"},description=)"
    )

    data_model_pb = data_model.encode()
    actual_data_model = DataModel.decode(data_model_pb)
    assert actual_data_model == data_model
示例#3
0
    def test_pickable_query(self):
        """Test that an istance of the Query class is pickable."""
        attribute_foo = Attribute("foo", int, True, "a foo attribute.")
        attribute_bar = Attribute("bar", str, True, "a bar attribute.")
        data_model_foobar = DataModel(
            "foobar", [attribute_foo, attribute_bar], "A foobar data model."
        )

        query = Query(
            [
                And(
                    [
                        Or(
                            [
                                Not(Constraint("foo", ConstraintType("==", 1))),
                                Not(Constraint("bar", ConstraintType("==", "baz"))),
                            ]
                        ),
                        Constraint("foo", ConstraintType("<", 2)),
                    ]
                )
            ],
            data_model_foobar,
        )
        try:
            pickle.dumps(query)
        except Exception:
            pytest.fail("Error during pickling.")
示例#4
0
 def test_query_check(self):
     """Test that the query.check() method works."""
     attribute_foo = Attribute("foo", int, True, "a foo attribute.")
     attribute_bar = Attribute("bar", str, True, "a bar attribute.")
     data_model_foobar = DataModel(
         "foobar", [attribute_foo, attribute_bar], "A foobar data model."
     )
     description_foobar = Description(
         {"foo": 1, "bar": "baz"}, data_model=data_model_foobar
     )
     query = Query(
         [
             And(
                 [
                     Or(
                         [
                             Not(Constraint("foo", ConstraintType("==", 1))),
                             Not(Constraint("bar", ConstraintType("==", "baz"))),
                         ]
                     ),
                     Constraint("foo", ConstraintType("<", 2)),
                 ]
             )
         ],
         data_model_foobar,
     )
     assert not query.check(description=description_foobar)
    def __init__(self):
        """Initialise the dataModel."""
        self.ATTRIBUTE_COUNTRY = Attribute("country", str, True)
        self.ATTRIBUTE_CITY = Attribute("city", str, True)

        super().__init__("weather_station_datamodel",
                         [self.ATTRIBUTE_COUNTRY, self.ATTRIBUTE_CITY])
示例#6
0
    def test_query(self):
        """Test that the translation for the Query class works."""
        attribute_foo = Attribute("foo", int, True, "a foo attribute.")
        attribute_bar = Attribute("bar", str, True, "a bar attribute.")
        data_model_foobar = DataModel(
            "foobar", [attribute_foo, attribute_bar], "A foobar data model."
        )

        query = Query(
            [
                And(
                    [
                        Or(
                            [
                                Not(Constraint("foo", ConstraintType("==", 1))),
                                Not(Constraint("bar", ConstraintType("==", "baz"))),
                            ]
                        ),
                        Constraint("foo", ConstraintType("<", 2)),
                    ]
                )
            ],
            data_model_foobar,
        )

        oef_query = OEFObjectTranslator.to_oef_query(query)
        expected_query = OEFObjectTranslator.from_oef_query(oef_query)
        actual_query = query
        assert expected_query == actual_query
示例#7
0
def test_attribute():
    """Test data model Attribute."""
    params = dict(name="test", type_=str, is_required=True)

    assert Attribute(**params) == Attribute(**params)
    assert Attribute(**params) is not None
    assert Attribute(**params) != Attribute(name="another", type_=int, is_required=True)
示例#8
0
def test_constraints_expressions():
    """Test constraint expressions: And, Or, Not."""
    expression = And([
        ConstraintType(ConstraintTypes.EQUAL, 12),
        ConstraintType(ConstraintTypes.EQUAL, 12),
    ])
    expression.check_validity()
    assert expression.check(12)
    expression.is_valid(Attribute("test", int, True))

    expression = Or([
        ConstraintType(ConstraintTypes.EQUAL, 12),
        ConstraintType(ConstraintTypes.EQUAL, 13),
    ])
    expression.check_validity()
    assert expression.check(12)

    expression.is_valid(Attribute("test", int, True))

    expression = Not(
        And([
            ConstraintType(ConstraintTypes.EQUAL, 12),
            ConstraintType(ConstraintTypes.EQUAL, 12),
        ]))
    expression.check_validity()
    assert expression.check(13)
    expression.is_valid(Attribute("test", int, True))
示例#9
0
def test_constraints_expression():
    """Test constraint expressions: And, Or, Not, Constraint."""
    and_expression = And([
        Constraint("number", ConstraintType(ConstraintTypes.LESS_THAN, 15)),
        Constraint("number", ConstraintType(ConstraintTypes.GREATER_THAN, 10)),
    ])
    and_expression.check_validity()
    assert and_expression.check(Description({"number": 12}))
    assert and_expression.is_valid(
        DataModel("some_name", [Attribute("number", int, True)]))
    and_expression_pb = ConstraintExpr._encode(and_expression)
    actual_and_expression = ConstraintExpr._decode(and_expression_pb)
    assert actual_and_expression == and_expression

    or_expression = Or([
        Constraint("number", ConstraintType(ConstraintTypes.EQUAL, 12)),
        Constraint("number", ConstraintType(ConstraintTypes.EQUAL, 13)),
    ])
    or_expression.check_validity()
    assert or_expression.check(Description({"number": 12}))
    assert or_expression.is_valid(
        DataModel("some_name", [Attribute("number", int, True)]))
    or_expression_pb = ConstraintExpr._encode(or_expression)
    actual_or_expression = ConstraintExpr._decode(or_expression_pb)
    assert actual_or_expression == or_expression

    not_expression = Not(
        And([
            Constraint("number", ConstraintType(ConstraintTypes.EQUAL, 12)),
            Constraint("number", ConstraintType(ConstraintTypes.EQUAL, 12)),
        ]))
    not_expression.check_validity()
    assert not_expression.check(Description({"number": 13}))
    assert not_expression.is_valid(
        DataModel("some_name", [Attribute("number", int, True)]))
    not_expression_pb = ConstraintExpr._encode(not_expression)
    actual_not_expression = ConstraintExpr._decode(not_expression_pb)
    assert actual_not_expression == not_expression

    # constraint
    constraint_expression = Constraint("author",
                                       ConstraintType("==", "Stephen King"))
    constraint_expression.check_validity()
    assert constraint_expression.check(Description({"author": "Stephen King"}))
    assert constraint_expression.is_valid(
        DataModel("some_name", [Attribute("author", str, True)]))
    constraint_expression_pb = ConstraintExpr._encode(constraint_expression)
    actual_constraint_expression = ConstraintExpr._decode(
        constraint_expression_pb)
    assert actual_constraint_expression == constraint_expression

    incorrect_expression = Location(1.1, 2.2)
    with pytest.raises(
            ValueError,
            match=
            f"Invalid expression type. Expected either of 'And', 'Or', 'Not', 'Constraint'. Found {type(incorrect_expression)}.",
    ):
        ConstraintExpr._encode(incorrect_expression)
示例#10
0
 def test_pickable_data_model(self):
     """Test that an istance of the DataModel class is pickable."""
     attribute_foo = Attribute("foo", int, True, "a foo attribute.")
     attribute_bar = Attribute("bar", str, True, "a bar attribute.")
     data_model_foobar = DataModel("foobar", [attribute_foo, attribute_bar],
                                   "A foobar data model.")
     try:
         pickle.dumps(data_model_foobar)
     except Exception:
         pytest.fail("Error during pickling.")
示例#11
0
 def test_data_model(self):
     """Test that the translation for the DataModel class works."""
     attribute_foo = Attribute("foo", int, True, "a foo attribute.")
     attribute_bar = Attribute("bar", str, True, "a bar attribute.")
     data_model_foobar = DataModel(
         "foobar", [attribute_foo, attribute_bar], "A foobar data model."
     )
     oef_data_model = OEFObjectTranslator.to_oef_data_model(data_model_foobar)
     expected_data_model = OEFObjectTranslator.from_oef_data_model(oef_data_model)
     actual_data_model = data_model_foobar
     assert expected_data_model == actual_data_model
示例#12
0
 def test_not_check(self):
     """Test the not().check function."""
     attribute_foo = Attribute("foo", int, True, "a foo attribute.")
     attribute_bar = Attribute("bar", str, True, "a bar attribute.")
     data_model_foobar = DataModel(
         "foobar", [attribute_foo, attribute_bar], "A foobar data model."
     )
     description_foobar = Description(
         {"foo": 1, "bar": "baz"}, data_model=data_model_foobar
     )
     no_constraint = Not(Constraint("foo", ConstraintType("==", 5)))
     assert no_constraint.check(description_foobar)
    def __init__(self):
        """Initialise the dataModel."""
        self.ATTRIBUTE_LATITUDE = Attribute("latitude", float, True)
        self.ATTRIBUTE_LONGITUDE = Attribute("longitude", float, True)
        self.ATTRIBUTE_UNIQUE_ID = Attribute("unique_id", str, True)

        super().__init__(
            "carpark_detection_datamodel",
            [
                self.ATTRIBUTE_LATITUDE,
                self.ATTRIBUTE_LONGITUDE,
                self.ATTRIBUTE_UNIQUE_ID,
            ],
        )
示例#14
0
def test_oef_object_transator():
    """Test oef description and description tranlations."""
    foo_datamodel = DataModel(
        "foo",
        [
            Attribute("bar", int, True, "A bar attribute."),
            Attribute("location", Location, True, "A location attribute."),
        ],
    )
    desc = Description(
        {"bar": 1, "location": Location(10.0, 10.0)}, data_model=foo_datamodel
    )
    oef_desc = OEFObjectTranslator.to_oef_description(desc)
    new_desc = OEFObjectTranslator.from_oef_description(oef_desc)
    assert desc.values["location"] == new_desc.values["location"]
示例#15
0
 def test_pickable_attribute(self):
     """Test that an istance of the Attribute class is pickable."""
     attribute_foo = Attribute("foo", int, True, "a foo attribute.")
     try:
         pickle.dumps(attribute_foo)
     except Exception:
         pytest.fail("Error during pickling.")
示例#16
0
        def test_search_services_with_query_with_model(self):
            """Test that a search services request can be sent correctly.

            In this test, the query has a simple data model.
            """
            data_model = DataModel("foobar", [Attribute("foo", str, True)])
            search_query = Query(
                [Constraint("foo", ConstraintType("==", "bar"))], model=data_model
            )
            oef_search_request = OefSearchMessage(
                performative=OefSearchMessage.Performative.SEARCH_SERVICES,
                dialogue_reference=self.oef_search_dialogues.new_self_initiated_dialogue_reference(),
                query=search_query,
            )
            oef_search_request.counterparty = DEFAULT_OEF
            sending_dialogue = self.oef_search_dialogues.update(oef_search_request)
            self.multiplexer.put(
                Envelope(
                    to=DEFAULT_OEF,
                    sender=FETCHAI_ADDRESS_ONE,
                    protocol_id=OefSearchMessage.protocol_id,
                    message=oef_search_request,
                )
            )

            envelope = self.multiplexer.get(block=True, timeout=5.0)
            oef_search_response = envelope.message
            oef_search_dialogue = self.oef_search_dialogues.update(oef_search_response)
            assert (
                oef_search_response.performative
                == OefSearchMessage.Performative.SEARCH_RESULT
            )
            assert oef_search_dialogue is not None
            assert oef_search_dialogue == sending_dialogue
            assert oef_search_response.agents == ()
示例#17
0
 def test_attribute(self):
     """Test that the translation for the Attribute class works."""
     attribute = Attribute("foo", int, True, "a foo attribute.")
     oef_attribute = OEFObjectTranslator.to_oef_attribute(attribute)
     expected_attribute = OEFObjectTranslator.from_oef_attribute(oef_attribute)
     actual_attribute = attribute
     assert expected_attribute == actual_attribute
        def test_search_services_with_query_with_model(self):
            """Test that a search services request can be sent correctly.

            In this test, the query has a simple data model.
            """
            request_id = 2
            data_model = DataModel("foobar", [Attribute("foo", str, True)])
            search_query = Query(
                [Constraint("foo", ConstraintType("==", "bar"))], model=data_model
            )
            search_request = OEFMessage(
                type=OEFMessage.Type.SEARCH_SERVICES, id=request_id, query=search_query
            )
            self.multiplexer.put(
                Envelope(
                    to=DEFAULT_OEF,
                    sender=self.crypto1.address,
                    protocol_id=OEFMessage.protocol_id,
                    message=OEFSerializer().encode(search_request),
                )
            )

            envelope = self.multiplexer.get(block=True, timeout=5.0)
            search_result = OEFSerializer().decode(envelope.message)
            assert search_result.get("type") == OEFMessage.Type.SEARCH_RESULT
            assert search_result.get("id") == request_id
            assert search_result.get("agents") == []
示例#19
0
def test_ml_messge_consistency():
    """Test the consistency of the message."""
    dm = DataModel("ml_datamodel", [Attribute("dataset_id", str, True)])
    query = Query([Constraint("dataset_id", ConstraintType("==", "fmnist"))], model=dm)
    msg = MlTradeMessage(performative=MlTradeMessage.Performative.CFP, query=query)
    with mock.patch.object(MlTradeMessage.Performative, "__eq__", return_value=False):
        assert not msg._is_consistent()
示例#20
0
    async def test_bad_register_service(self):
        """Test register service fails on bad values provided."""
        bad_location_model = DataModel(
            "not_location_agent",
            [
                Attribute("non_location", Location, True,
                          "The location where the agent is.")
            ],
            "A data model to describe location of an agent.",
        )
        agent_location = Location(52.2057092, 2.1183431)
        service_instance = {"non_location": agent_location}
        service_description = Description(service_instance,
                                          data_model=bad_location_model)
        message, sending_dialogue = self.oef_search_dialogues.create(
            counterparty=str(SOEFConnection.connection_id.to_any()),
            performative=OefSearchMessage.Performative.REGISTER_SERVICE,
            service_description=service_description,
        )
        envelope = Envelope(
            to=message.to,
            sender=message.sender,
            protocol_id=message.protocol_id,
            message=message,
        )
        await self.connection.send(envelope)

        expected_envelope = await asyncio.wait_for(self.connection.receive(),
                                                   timeout=1)
        assert expected_envelope
        assert (expected_envelope.message.performative ==
                OefSearchMessage.Performative.OEF_ERROR)
        message = expected_envelope.message
        receiving_dialogue = self.oef_search_dialogues.update(message)
        assert sending_dialogue == receiving_dialogue
示例#21
0
    async def test_bad_register_service(self):
        """Test register service fails on bad values provided."""
        bad_location_model = DataModel(
            "not_location_agent",
            [
                Attribute("non_location", Location, True,
                          "The location where the agent is.")
            ],
            "A data model to describe location of an agent.",
        )
        agent_location = Location(52.2057092, 2.1183431)
        service_instance = {"non_location": agent_location}
        service_description = Description(service_instance,
                                          data_model=bad_location_model)
        message = OefSearchMessage(
            performative=OefSearchMessage.Performative.REGISTER_SERVICE,
            service_description=service_description,
        )
        envelope = Envelope(
            to="soef",
            sender=self.crypto.address,
            protocol_id=message.protocol_id,
            message=message,
        )
        await self.connection.send(envelope)

        expected_envelope = await asyncio.wait_for(self.connection.receive(),
                                                   timeout=1)
        assert expected_envelope
        assert (expected_envelope.message.performative ==
                OefSearchMessage.Performative.OEF_ERROR)
示例#22
0
 def test_and_check(self):
     """Test the and().check function."""
     attribute_foo = Attribute("foo", int, True, "a foo attribute.")
     attribute_bar = Attribute("bar", str, True, "a bar attribute.")
     data_model_foobar = DataModel("foobar", [attribute_foo, attribute_bar],
                                   "A foobar data model.")
     description_foobar = Description({
         "foo": 1,
         "bar": "baz"
     },
                                      data_model=data_model_foobar)
     constraint = And([
         (Constraint("foo", ConstraintType("==", 1))),
         (Constraint("bar", ConstraintType("==", "baz"))),
     ])
     assert constraint.check(description_foobar)
示例#23
0
def test_generate_data_model():
    """Test model generated from description."""
    params = dict(name="test", type_=str, is_required=True)

    data_model = DataModel("test", [Attribute(**params)])

    assert generate_data_model("test", {"test": "str"}) == data_model
示例#24
0
        def test_search_services_with_query_with_model(self):
            """Test that a search services request can be sent correctly.

            In this test, the query has a simple data model.
            """
            data_model = DataModel("foobar", [Attribute("foo", str, True)])
            search_query = Query(
                [Constraint("foo", ConstraintType("==", "bar"))],
                model=data_model)
            oef_search_request, sending_dialogue = self.oef_search_dialogues.create(
                counterparty=str(self.connection.connection_id),
                performative=OefSearchMessage.Performative.SEARCH_SERVICES,
                query=search_query,
            )
            self.multiplexer.put(
                Envelope(
                    to=oef_search_request.to,
                    sender=oef_search_request.sender,
                    protocol_id=oef_search_request.protocol_id,
                    message=oef_search_request,
                ))

            envelope = self.multiplexer.get(block=True, timeout=5.0)
            oef_search_response = envelope.message
            oef_search_dialogue = self.oef_search_dialogues.update(
                oef_search_response)
            assert (oef_search_response.performative ==
                    OefSearchMessage.Performative.SEARCH_RESULT)
            assert oef_search_dialogue is not None
            assert oef_search_dialogue == sending_dialogue
            assert oef_search_response.agents == ()
示例#25
0
    def setup(cls):
        """Setup the test class."""
        super().setup()
        cls.oef_search_handler = cast(
            OefSearchHandler, cls._skill.skill_context.handlers.oef_search)
        cls.strategy = cast(Strategy, cls._skill.skill_context.strategy)
        cls.logger = cls._skill.skill_context.logger

        cls.oef_search_dialogues = cast(
            OefSearchDialogues, cls._skill.skill_context.oef_search_dialogues)

        cls.data = b"some_body"
        cls.query = Query(
            [
                Constraint("some_attribute",
                           ConstraintType("==", "some_service"))
            ],
            DataModel(
                "some_name",
                [
                    Attribute("some_attribute", str, False,
                              "Some attribute descriptions.")
                ],
            ),
        )
        cls.mocked_description = Description({"foo1": 1, "bar1": 2})

        cls.list_of_messages = (DialogueMessage(
            OefSearchMessage.Performative.SEARCH_SERVICES,
            {"query": cls.query}), )
示例#26
0
        def test_search_services_with_distance_query(self):
            """Test that a search services request can be sent correctly.

            In this test, the query has a simple data model.
            """
            tour_eiffel = Location(48.8581064, 2.29447)
            attribute = Attribute("latlon", Location, True)
            data_model = DataModel("geolocation", [attribute])
            search_query = Query(
                [
                    Constraint(attribute.name,
                               ConstraintType("distance", (tour_eiffel, 1.0)))
                ],
                model=data_model,
            )
            oef_search_request, sending_dialogue = self.oef_search_dialogues.create(
                counterparty=str(self.connection.connection_id),
                performative=OefSearchMessage.Performative.SEARCH_SERVICES,
                query=search_query,
            )
            self.multiplexer.put(
                Envelope(
                    to=oef_search_request.to,
                    sender=oef_search_request.sender,
                    message=oef_search_request,
                ))
            envelope = self.multiplexer.get(block=True, timeout=5.0)
            oef_search_response = envelope.message
            oef_search_dialogue = self.oef_search_dialogues.update(
                oef_search_response)
            assert (oef_search_response.performative ==
                    OefSearchMessage.Performative.SEARCH_RESULT)
            assert oef_search_dialogue is not None
            assert oef_search_dialogue == sending_dialogue
            assert oef_search_response.agents == ()
示例#27
0
        def test_search_services_with_query_with_model(self):
            """Test that a search services request can be sent correctly.

            In this test, the query has a simple data model.
            """
            request_id = 1
            data_model = DataModel("foobar", [Attribute("foo", str, True)])
            search_query = Query(
                [Constraint("foo", ConstraintType("==", "bar"))], model=data_model
            )
            search_request = OefSearchMessage(
                performative=OefSearchMessage.Performative.SEARCH_SERVICES,
                dialogue_reference=(str(request_id), ""),
                query=search_query,
            )
            self.multiplexer.put(
                Envelope(
                    to=DEFAULT_OEF,
                    sender=self.crypto1.address,
                    protocol_id=OefSearchMessage.protocol_id,
                    message=OefSearchSerializer().encode(search_request),
                )
            )

            envelope = self.multiplexer.get(block=True, timeout=5.0)
            search_result = OefSearchSerializer().decode(envelope.message)
            assert (
                search_result.performative
                == OefSearchMessage.Performative.SEARCH_RESULT
            )
            assert search_result.dialogue_reference[0] == str(request_id)
            assert search_result.agents == ()
示例#28
0
 def test_description(self):
     """Test that the translation for the Description class works."""
     attribute_foo = Attribute("foo", int, True, "a foo attribute.")
     attribute_bar = Attribute("bar", str, True, "a bar attribute.")
     data_model_foobar = DataModel(
         "foobar", [attribute_foo, attribute_bar], "A foobar data model."
     )
     description_foobar = Description(
         {"foo": 1, "bar": "baz"}, data_model=data_model_foobar
     )
     oef_description = OEFObjectTranslator.to_oef_description(description_foobar)
     expected_description = OEFObjectTranslator.from_oef_description(oef_description)
     actual_description = description_foobar
     assert expected_description == actual_description
     m_desc = iter(description_foobar.values)
     assert next(m_desc) == "foo"
     assert {"foo", "bar"} == set(iter(description_foobar))
示例#29
0
 def from_oef_attribute(cls, oef_attribute: OEFAttribute) -> Attribute:
     """From an OEF attribute to our attribute."""
     return Attribute(
         oef_attribute.name,
         oef_attribute.type,
         oef_attribute.required,
         oef_attribute.description,
     )
示例#30
0
 def test_build_goods_datamodel_demand(self):
     """Test the _build_goods_datamodel of Helpers module for a demand."""
     good_ids = ["1", "2"]
     is_supply = False
     attributes = [
         Attribute("1", int, True, "A good on offer."),
         Attribute("2", int, True, "A good on offer."),
         Attribute("ledger_id", str, True, "The ledger for transacting."),
         Attribute(
             "currency_id",
             str,
             True,
             "The currency for pricing and transacting the goods.",
         ),
         Attribute("price", int, False,
                   "The price of the goods in the currency."),
         Attribute(
             "fee",
             int,
             False,
             "The transaction fee payable by the buyer in the currency.",
         ),
         Attribute("nonce", str, False,
                   "The nonce to distinguish identical descriptions."),
     ]
     expected_data_model = DataModel(DEMAND_DATAMODEL_NAME, attributes)
     actual_data_model = _build_goods_datamodel(good_ids, is_supply)
     assert actual_data_model == expected_data_model