Пример #1
0
    def test_validations(self, mock_re):
        """
            Test method to check if the functions returns None if class_dict is a base class
            or has syntax errors
        """

        class_dict = self.doc["supportedClass"][0]
        exclude_list = ['http://www.w3.org/ns/hydra/core#Resource',
                        'http://www.w3.org/ns/hydra/core#Collection',
                        'vocab:EntryPoint']

        entrypoint = doc_maker.get_entrypoint(self.doc)
        class_id = class_dict.pop("@id", None)

        # Check if returning None when class id is a BaseClass or an EntryPoint
        for id_ in exclude_list:
            class_dict["@id"] = id_
            self.assertEqual((None, None, None),
                             doc_maker.create_class(entrypoint, class_dict))

        class_dict["@id"] = class_id

        # Check if returning None when any key is not of proper format
        mock_re.match.return_value = None
        self.assertEqual((None, None, None), doc_maker.create_class(entrypoint, class_dict))
Пример #2
0
    def test_validations(self):
        """
            Test method to check if proper exceptions are raised when entrypoint has missing keys or
            contains syntax errors
        """

        class_dict = self.doc["supportedClass"][0]
        entrypoint = doc_maker.get_entrypoint(self.doc)

        # check if exception raised is proper when supportedProperty key is not in entrypoint
        properties = entrypoint.pop("supportedProperty")
        self.assertRaises(
            SyntaxError, doc_maker.collection_in_endpoint, class_dict, entrypoint)

        # check if proper exception is raised when property key is not present
        property_ = properties[0].pop("property")
        entrypoint["supportedProperty"] = properties
        self.assertRaises(
            SyntaxError, doc_maker.collection_in_endpoint, class_dict, entrypoint)

        # check if exception is raised when no label key is found in property
        properties[0]["property"] = property_
        label = property_.pop("label")
        self.assertRaises(
            SyntaxError, doc_maker.collection_in_endpoint, class_dict, entrypoint)
        property_["label"] = label
Пример #3
0
    def test_output(self, mock_class):
        """
            Test method to check if HydraClass is instantiated with proper arguments and
            properties and operations have been added to it.
        """

        entrypoint = doc_maker.get_entrypoint(self.doc)
        class_dict = {
            "@id": "vocab:Pet",
            "@type": "hydra:Class",
            "title": "Pet",
            "description": "Pet",
            "supportedProperty": [
                {
                    "@type": "SupportedProperty",
                    "property": "",
                    "readable": "true",
                    "required": "false",
                    "title": "id",
                    "writeable": "true"
                }
            ],
            "supportedOperation": [
                {
                    "@type": "http://schema.org/UpdateAction",
                    "expects": "vocab:Pet",
                    "method": "POST",
                    "possibleStatus": [
                        {
                            "title": "Invalid input",
                            "description": "Invalid input",
                            "statusCode": 405
                        }
                    ],
                    "returns": "null",
                    "expectsHeader": [],
                    "returnsHeader": [],
                    "title": "Add a new pet to the store"
                }
            ],
        }
        expected_collection = True
        expected_path = '/pet'

        # run the function and check if HydraClass has been instantiated
        class_, collection, collection_path = doc_maker.create_class(
            entrypoint, class_dict)
        mock_class.assert_called_once_with('Pet', 'Pet', 'Pet', None, False)

        # check if properties and operations has been added to the hydra class
        self.assertEqual(mock_class.return_value.add_supported_op.call_count,
                         len(class_dict["supportedOperation"]))
        self.assertEqual(mock_class.return_value.add_supported_prop.call_count,
                         len(class_dict["supportedProperty"]))

        self.assertEqual(collection, expected_collection)
        self.assertEqual(collection_path, expected_path)

        self.assertIsInstance(class_, doc_writer.HydraClass)
Пример #4
0
    def test_output(self):
        """
            Test method to check if proper output is obtained when class title is manipulated
        """

        entrypoint = doc_maker.get_entrypoint(self.doc)
        class_dict = {
            "@id": "vocab:Pet",
            "@type": "hydra:Class",
            "title": "Pet",
            "description": "Pet",
            "supportedProperty": [],
            "supportedOperation": [],
        }

        expected_output = (True, '/pet')
        self.assertEqual(doc_maker.collection_in_endpoint(
            class_dict, entrypoint), expected_output)

        # Only the title of the class is needed in the method
        class_dict["title"] = "Order"
        expected_output = (False, None)
        self.assertEqual(doc_maker.collection_in_endpoint(
            class_dict, entrypoint), expected_output)