Пример #1
0
class TestCaseInstanceCreateResource(unittest.TestCase):
    @patch("brainiak.utils.sparql.property_must_map_a_unique_value",
           return_value=True)
    @patch("brainiak.instance.create_instance.query_create_instances")
    @patch("brainiak.instance.create_instance.is_insert_response_successful",
           return_value=False)
    @patch("brainiak.instance.create_instance.get_cached_schema",
           return_value=mock_schema({"rdfs:label": "string"},
                                    id="http://somedomain/class"))
    @patch("brainiak.utils.sparql.is_value_already_used", return_value=True)
    def test_instance_not_inserted(self, mock_value_uniqueness,
                                   mock_get_cached_schema,
                                   mocked_response_successful,
                                   mocked_query_create_instances,
                                   mocked_property_must_map_a_unique_value):
        handler = MockHandler()
        params = ParamDict(handler,
                           class_uri="http://somedomain/class",
                           graph_uri="http://somedomain/graph")
        instance_data = {"http://www.w3.org/2000/01/rdf-schema#label": "teste"}
        with self.assertRaises(HTTPError) as e:
            create_instance(params, instance_data, "http://uri-teste")
            expected = [
                "The property (http://www.w3.org/2000/01/rdf-schema#label) defined in the schema (http://somedomain/class) must map a unique value. The value provided (teste) is already used by another instance."
            ]
            self.assertEqual(json.loads(str(e.exception)), expected)
Пример #2
0
 def test_assemble_instance_json_with_object_labels(self):
     bindings = [
         {
             u'predicate': {u'type': u'uri', u'value': u'http://www.w3.org/2000/01/rdf-schema#label'},
             u'object': {u'type': u'literal', u'value': u'Cricket becomes the most popular sport of Brazil'}
         },
         {
             u'predicate': {u'type': u'uri', u'value': u'http://www.w3.org/1999/02/22-rdf-syntax-ns#type'},
             u'object': {u'type': u'uri', u'value': u'http://dbpedia.org/ontology/News'},
             u'object_label': {u'type': u'literal', u'value': u'News'}
         },
         {
             u'predicate': {u'type': u'uri', u'value': u'http://brmedia.com/related_to'},
             u'object': {u'type': u'uri', u'value': u'http://dbpedia.org/ontology/Cricket'},
             u'object_label': {u'type': u'literal', u'value': u'Cricket'}
         }
     ]
     class_schema = mock_schema({
         u'http://www.w3.org/2000/01/rdf-schema#label': "string",
         u'http://www.w3.org/1999/02/22-rdf-syntax-ns#type': "string",
         u'http://brmedia.com/related_to': "string_uri"
     }, "http://dbpedia.org/ontology/News")
     computed = get_instance.build_items_dict(bindings, "http://dbpedia.org/ontology/News", 1, class_schema)
     expected = {
         u'http://www.w3.org/2000/01/rdf-schema#label': u'Cricket becomes the most popular sport of Brazil',
         u'http://www.w3.org/1999/02/22-rdf-syntax-ns#type': 'http://dbpedia.org/ontology/News',
         u'http://brmedia.com/related_to': {"@id": "http://dbpedia.org/ontology/Cricket", "title": u"Cricket"}
     }
     self.assertEqual(computed, expected)
 def test_assemble_instance_json_with_object_labels(self):
     bindings = [{
         u'predicate': {
             u'type': u'uri',
             u'value': u'http://www.w3.org/2000/01/rdf-schema#label'
         },
         u'object': {
             u'type': u'literal',
             u'value': u'Cricket becomes the most popular sport of Brazil'
         }
     }, {
         u'predicate': {
             u'type': u'uri',
             u'value': u'http://www.w3.org/1999/02/22-rdf-syntax-ns#type'
         },
         u'object': {
             u'type': u'uri',
             u'value': u'http://dbpedia.org/ontology/News'
         },
         u'object_label': {
             u'type': u'literal',
             u'value': u'News'
         }
     }, {
         u'predicate': {
             u'type': u'uri',
             u'value': u'http://brmedia.com/related_to'
         },
         u'object': {
             u'type': u'uri',
             u'value': u'http://dbpedia.org/ontology/Cricket'
         },
         u'object_label': {
             u'type': u'literal',
             u'value': u'Cricket'
         }
     }]
     class_schema = mock_schema(
         {
             u'http://www.w3.org/2000/01/rdf-schema#label': "string",
             u'http://www.w3.org/1999/02/22-rdf-syntax-ns#type': "string",
             u'http://brmedia.com/related_to': "string_uri"
         }, "http://dbpedia.org/ontology/News")
     computed = get_instance.build_items_dict(
         bindings, "http://dbpedia.org/ontology/News", 1, class_schema)
     expected = {
         u'http://www.w3.org/2000/01/rdf-schema#label':
         u'Cricket becomes the most popular sport of Brazil',
         u'http://www.w3.org/1999/02/22-rdf-syntax-ns#type':
         'http://dbpedia.org/ontology/News',
         u'http://brmedia.com/related_to': {
             "@id": "http://dbpedia.org/ontology/Cricket",
             "title": u"Cricket"
         }
     }
     self.assertEqual(computed, expected)
Пример #4
0
    def test_build_items_dict_with_super_property_and_same_value(self):
        bindings = self.prepare_input_and_expected_output(object_value="Rio de Janeiro")
        class_schema = mock_schema({
            u'birthPlace': "string",
            u'birthCity': "string",
        }, "http://class.uri")

        expected = {
            "birthCity": "Rio de Janeiro",
            'http://www.w3.org/1999/02/22-rdf-syntax-ns#type': 'http://class.uri'
        }
        response = get_instance.build_items_dict(bindings, "http://class.uri", False, class_schema)

        self.assertEqual(response, expected)
Пример #5
0
 def test_assemble_instance_json_with_an_object_which_does_not_have_label(self, mlogger, mdebug, msettings):
     bindings = [
         {
             u'predicate': {u'type': u'uri', u'value': u'http://brmedia.com/related_to'},
             u'object': {u'type': u'uri', u'value': u'http://dbpedia.org/ontology/Cricket'},
         }
     ]
     class_schema = mock_schema({
         u'http://www.w3.org/2000/01/rdf-schema#label': "string",
         u'http://www.w3.org/1999/02/22-rdf-syntax-ns#type': "string",
         u'http://brmedia.com/related_to': "string_uri"
     }, "http://dbpedia.org/ontology/News")
     get_instance.build_items_dict(bindings, "http://dbpedia.org/ontology/News", 1, class_schema)
     expected_msg = "The predicate http://brmedia.com/related_to refers to an object http://dbpedia.org/ontology/Cricket which doesn't have a label. Set expand_object_properties=0 if you don't care about this ontological inconsistency."
     self.assertTrue(mdebug.called)
     self.assertEqual(mdebug.call_args[0][0], expected_msg)
    def test_build_items_dict_with_super_property_and_same_value(self):
        bindings = self.prepare_input_and_expected_output(
            object_value="Rio de Janeiro")
        class_schema = mock_schema(
            {
                u'birthPlace': "string",
                u'birthCity': "string",
            }, "http://class.uri")

        expected = {
            "birthCity": "Rio de Janeiro",
            'http://www.w3.org/1999/02/22-rdf-syntax-ns#type':
            'http://class.uri'
        }
        response = get_instance.build_items_dict(bindings, "http://class.uri",
                                                 False, class_schema)

        self.assertEqual(response, expected)
 def test_assemble_instance_json_with_an_object_which_does_not_have_label(
         self, mlogger, mdebug, msettings):
     bindings = [{
         u'predicate': {
             u'type': u'uri',
             u'value': u'http://brmedia.com/related_to'
         },
         u'object': {
             u'type': u'uri',
             u'value': u'http://dbpedia.org/ontology/Cricket'
         },
     }]
     class_schema = mock_schema(
         {
             u'http://www.w3.org/2000/01/rdf-schema#label': "string",
             u'http://www.w3.org/1999/02/22-rdf-syntax-ns#type': "string",
             u'http://brmedia.com/related_to': "string_uri"
         }, "http://dbpedia.org/ontology/News")
     get_instance.build_items_dict(bindings,
                                   "http://dbpedia.org/ontology/News", 1,
                                   class_schema)
     expected_msg = "The predicate http://brmedia.com/related_to refers to an object http://dbpedia.org/ontology/Cricket which doesn't have a label. Set expand_object_properties=0 if you don't care about this ontological inconsistency."
     self.assertTrue(mdebug.called)
     self.assertEqual(mdebug.call_args[0][0], expected_msg)
Пример #8
0
class BusNotificationTestCase(TornadoAsyncHTTPTestCase, QueryTestCase):

    maxDiff = None
    # The class variables below are handled by QueryTestCase
    graph_uri = "http://somegraph.org/"
    fixtures = ["tests/sample/instances.n3"]

    def get_app(self):
        return server.Application()

    def setUp(self):
        super(BusNotificationTestCase, self).setUp()
        self.original_log = handlers.logger
        self.original_do_notify_bus = settings.NOTIFY_BUS
        settings.NOTIFY_BUS = True

    def tearDown(self):
        settings.NOTIFY_BUS = self.original_do_notify_bus

    @patch("brainiak.handlers.notify_bus")
    @patch("brainiak.handlers.logger")
    @patch("brainiak.event_bus.logger")
    def test_notify_event_bus_on_put(self, log, log2, mock_notify_bus):
        expected_message = {
            "instance": "http://tatipedia.org/new_york",
            "klass": "http://tatipedia.org/Place",
            "graph": u"http://somegraph.org/",
            "action": "PUT",
            "instance_data": ANY
        }

        actual_new_york = self.fetch(
            '/anything/Place/new_york?class_prefix=http://tatipedia.org/&instance_prefix=http://tatipedia.org/&graph_uri=http://somegraph.org/',
            method='GET')
        self.assertEqual(actual_new_york.code, 200)

        actual_new_york_dict = json.loads(actual_new_york.body)
        actual_new_york_dict[
            "http://tatipedia.org/comment"] = "Some random comment"

        modified_new_york = self.fetch(
            '/anything/Place/new_york?class_prefix=http://tatipedia.org/&instance_prefix=http://tatipedia.org/&graph_uri=http://somegraph.org/',
            method='PUT',
            body=json.dumps(actual_new_york_dict))
        self.assertEqual(modified_new_york.code, 200)
        mock_notify_bus.assert_called_with(**expected_message)

    @patch("brainiak.handlers.notify_bus")
    @patch("brainiak.event_bus.logger")
    @patch("brainiak.handlers.logger")
    def test_notify_event_bus_on_delete(self, log, log2, mock_notify_bus):
        expected_message = {
            "instance": "http://tatipedia.org/new_york",
            "klass": "http://tatipedia.org/Place",
            "graph": "http://somegraph.org/",
            "action": "DELETE"
        }

        deleted_new_york = self.fetch(
            '/anything/Place/new_york?class_prefix=http://tatipedia.org/&instance_prefix=http://tatipedia.org/&graph_uri=http://somegraph.org/',
            method='DELETE')
        self.assertEqual(deleted_new_york.code, 204)
        mock_notify_bus.assert_called_with(**expected_message)

    @patch("brainiak.handlers.notify_bus")
    @patch("brainiak.event_bus.logger")
    @patch("brainiak.handlers.logger")
    @patch("brainiak.instance.create_instance.get_cached_schema",
           return_value=mock_schema(
               {
                   "rdfs:label": "string",
                   "http://tatipedia.org/stadium": "string"
               }, "http://tatipedia.org/SoccerClub"))
    def test_notify_event_bus_on_post(self, mock_schema, log, log2,
                                      mock_notify_bus):
        CSA_FOOTBALL_TEAM = {
            "@context": {
                "rdfs": "http://www.w3.org/2000/01/rdf-schema#",
                "tpedia": "http://tatipedia.org/"
            },
            u"rdfs:label": u"Centro Sportivo Alagoano",
            u"tpedia:stadium": u"Estádio Rei Pelé"
        }

        expected_message = {
            "klass": "http://tatipedia.org/SoccerClub",
            "graph": "http://somegraph.org/",
            "action": "POST",
            "instance": ANY,
            "instance_data": ANY
        }
        response = self.fetch(
            '/tpedia/SoccerClub/?class_prefix=http://tatipedia.org/&graph_uri=http://somegraph.org/',
            method='POST',
            body=json.dumps(CSA_FOOTBALL_TEAM))
        self.assertEqual(response.code, 201)
        mock_notify_bus.assert_called_with(**expected_message)

    @patch("brainiak.handlers.logger")
    @patch("brainiak.event_bus.logger")
    @patch("brainiak.handlers.notify_bus",
           side_effect=MiddlewareError('Mocked failure'))
    def test_notify_bus_not_connected_exception(self, fake_send, log, log2):
        deleted_new_york = self.fetch(
            '/anything/Place/new_york?class_prefix=http://tatipedia.org/&instance_prefix=http://tatipedia.org/&graph_uri=http://somegraph.org/',
            method='DELETE')
        self.assertEqual(deleted_new_york.code, 500)

    @patch("brainiak.event_bus.logger")
    @patch("brainiak.handlers.logger")
    @patch("brainiak.handlers.notify_bus",
           side_effect=NotificationFailure('Mocked failure'))
    def test_notify_bus_connection_closed_exception(self, fake_send, log,
                                                    log2):
        deleted_new_york = self.fetch(
            '/anything/Place/new_york?class_prefix=http://tatipedia.org/&instance_prefix=http://tatipedia.org/&graph_uri=http://somegraph.org/',
            method='DELETE')
        self.assertEqual(deleted_new_york.code, 500)

    @patch("brainiak.event_bus.logger")
    @patch("brainiak.handlers.logger")
    @patch("brainiak.handlers.notify_bus",
           side_effect=MiddlewareError('Mocked failure'))
    def test_notify_bus_protocol_exception(self, fake_send, log, log2):
        deleted_new_york = self.fetch(
            '/anything/Place/new_york?class_prefix=http://tatipedia.org/&instance_prefix=http://tatipedia.org/&graph_uri=http://somegraph.org/',
            method='DELETE')
        self.assertEqual(deleted_new_york.code, 500)