Exemple #1
0
def test_query_attribute_unicode(store_session):
    """
    Test that query_attribute calls ResultProxy with string arguments.

    query_attribute sets up and returns ResultProxy instance. Here we test
    that it doesn't pass unicode keywords to it, these don't work
    in Python 2.6.2.
    """
    def mock_get_by(self, **kwargs):
        """ Verify that all passed keywords are strings. """

        for keyword in list(kwargs.keys()):
            assert isinstance(keyword, basestring), \
                "Passed non-string keyword: %s" % keyword

    _, session = store_session
    resource = session.get_resource("http://p1", surf.ns.FOAF.Person)

    RP = surf.resource.result_proxy.ResultProxy
    try:
        # Patch ResultProxy with mock get_by method
        original_get_by, RP.get_by = RP.get_by, mock_get_by
        resource.query_attribute(u"foaf_knows")
    except Exception as e:
        pytest.fail(error_message(e), pytrace=True)
    finally:
        # Regardless of results, revert our patch so other tests are not
        # affected.
        RP.get_by = original_get_by
def test_close_unicode_exception():
    """
    Test that closing a store handles exceptions.
    """
    class MockReader(RDFReader):
        def _load(self, subject, direct, context):
            pass

        def _instances_by_attribute(self, concept, attributes, direct,
                                    context):
            pass

        def _is_present(self, subject, context):
            pass

        def _get(self, subject, attribute, direct, context):
            pass

        def _concept(self, subject):
            pass

        def close(self):
            raise Exception(u"Some unicode: ā")

    class MockWriter(RDFWriter):
        def _set_triple(self, s=None, p=None, o=None, context=None):
            pass

        def _update(self, *resources):
            pass

        def _save(self, *resources):
            pass

        def _remove(self, *resources, **kwargs):
            pass

        def _clear(self, context=None):
            pass

        def _add_triple(self, s=None, p=None, o=None, context=None):
            pass

        def _size(self):
            pass

        def close(self):
            raise Exception(u"Some unicode: ā")

    try:
        reader = MockReader()
        store = Store(reader, MockWriter(reader), log_level=logging.NOTSET)
        logging.disable(logging.ERROR)
        store.close()
        logging.disable(logging.NOTSET)
    except Exception as e:
        pytest.fail(error_message(e), pytrace=True)
def test_order_desc(store_value):
    """
    Test order, desc.
    """
    store, value = store_value
    try:
        store.expect_args({"order": "some_attr", "desc": True})
        list(value.order("some_attr").desc())
    except Exception as e:
        pytest.fail(error_message(e), pytrace=True)
    def close(self):
        """ Close the `store`.

        Both the `reader` and the `writer` plugins are closed.
        See :func:`surf.plugin.writer.RDFWriter.close`
        and :func:`surf.plugin.reader.RDFReader.close` methods.

        """
        try:
            self.reader.close()
            debug('reader closed successfully')
        except Exception as e:
            error("Error on closing the reader: %s", error_message(e))

        try:
            self.writer.close()
            debug('writer closed successfully')
        except Exception as e:
            error("Error on closing the writer: %s", error_message(e))
def test_full(store_value):
    """
    Test full().
    """
    store, value = store_value
    try:
        store.expect_args({'full': True, 'direct_only': True})
        list(value.full(direct_only=True))
    except Exception as e:
        pytest.fail(error_message(e), pytrace=True)
def test_limit_offset(store_value):
    """
    Test limit, offset.
    """
    store, value = store_value
    try:
        store.expect_args({"limit": 10, "offset": 5})
        list(value.limit(10).offset(5))
    except Exception as e:
        pytest.fail(error_message(e), pytrace=True)
def test_context(store_value):
    """
    Test context.
    """
    store, value = store_value
    try:
        store.expect_args({"context": "my_context"})
        list(value.context("my_context"))
    except Exception as e:
        pytest.fail(error_message(e), pytrace=True)
Exemple #8
0
def test_filter(store_proxy):
    """
    Test filter.
    """
    store, proxy = store_proxy
    try:
        store.expect_args({"filter": [(surf.ns.FOAF["name"], Literal(u"f"), True)]})
        list(proxy.filter(foaf_name="f"))
    except Exception as e:
        pytest.fail(error_message(e), pytrace=True)
def test_successful_close():
    """
    Test that store handles successful reader and writer closes.
    """
    class MockReader(RDFReader):
        def _load(self, subject, direct, context):
            pass

        def _instances_by_attribute(self, concept, attributes, direct,
                                    context):
            pass

        def _is_present(self, subject, context):
            pass

        def _get(self, subject, attribute, direct, context):
            pass

        def _concept(self, subject):
            pass

        def close(self):
            pass

    class MockWriter(RDFWriter):
        def _set_triple(self, s=None, p=None, o=None, context=None):
            pass

        def _update(self, *resources):
            pass

        def _save(self, *resources):
            pass

        def _remove(self, *resources, **kwargs):
            pass

        def _clear(self, context=None):
            pass

        def _add_triple(self, s=None, p=None, o=None, context=None):
            pass

        def _size(self):
            pass

        def close(self):
            pass

    try:
        reader = MockReader()
        store = Store(reader, MockWriter(reader), log_level=logging.NOTSET)
        store.close()
    except Exception as e:
        pytest.fail(error_message(e), pytrace=True)
def test_get_by(store_value):
    """
    Test get_by.
    """
    store, value = store_value
    try:
        expected = [(surf.ns.FOAF["name"], Literal(u"Jane"), True)]
        store.expect_args({"get_by": expected})
        list(value.get_by(foaf_name="Jane"))
    except Exception as e:
        pytest.fail(error_message(e), pytrace=True)
def test_get_by_resource(store_value):
    """
    Test that get_by accepts Resources as values.
    """
    store, value = store_value
    resource = MockResource()
    try:
        expected = [(surf.ns.FOAF["knows"], resource.subject, True)]
        store.expect_args({"get_by": expected})
        list(value.get_by(foaf_knows=resource))
    except Exception as e:
        pytest.fail(error_message(e), pytrace=True)
Exemple #12
0
def test_instance(store_session):
    """
    Test Resource._instance().
    """

    try:
        _, session = store_session
        Thing = session.get_class(surf.ns.OWL.Thing)

        subject = surf.ns.SURF.test1
        Thing._instance(subject, [surf.ns.OWL.Thing], store=Thing.store_key)
    except Exception as e:
        pytest.fail(error_message(e), pytrace=True)
Exemple #13
0
def test_to_table():
    """
    Test _to_table with empty bindings.
    """

    data = {'results': {'bindings': [{'c': {}}]}}

    # This should not raise exception.
    try:
        store = surf.store.Store(reader="sparql_protocol")
        store.reader._to_table(data)
    except Exception as e:
        pytest.fail(error_message(e), pytrace=True)
def test_close_multiples_stores():
    """
    Test that closing a session with multiple stores work.
    """
    try:
        store1 = Store()
        session = Session(store1)

        store2 = Store()
        session["store2"] = store2

        # Should not fail.
        session.close()
    except Exception as e:
        pytest.fail(error_message(e), pytrace=True)
Exemple #15
0
def test_rdflib_store():
    """
    Create a SuRF rdflib based store
    """

    kwargs = {"reader": "rdflib", "writer": "rdflib"}

    if False:  # use_default_context:
        kwargs["default_context"] = "http://surf_test_graph/dummy2"

    try:
        store = surf.Store(**kwargs)
        session = surf.Session(store)

        # clean store
        store.clear()
    except Exception as e:
        pytest.fail(error_message(e), pytrace=True)
def test_multiples():
    """
    Test synchronization between empty attribute and rdf_direct.
    """

    store = Store(log_level=logging.NOTSET)
    session = Session(store)

    Person = session.get_class(surf.ns.FOAF.Person)

    rob = session.get_resource("http://Robert", Person)
    rob.foaf_name = "Robert"
    michael = session.get_resource("http://Michael", Person)
    michael.foaf_name = "Michael"

    try:
        store.save(rob, michael)
        store.update(rob, michael)
        store.remove(rob, michael)
    except Exception as e:
        pytest.fail(error_message(e), pytrace=True)
def test_convert_unicode_exception():
    """
    Test RDFQueryReader.convert() handles exceptions with unicode.
    """
    class MyQueryReader(RDFQueryReader):
        def _ask(self, result):
            pass

        def _execute(self, query):
            pass

        # We want convert() to catch an exception.
        # Cannot override __convert and throw from there,
        # but we know __convert calls _to_table...
        def _to_table(self, _):
            warnings.simplefilter("ignore")
            raise Exception(u"This is unicode: ā")

    try:
        logging.disable(logging.ERROR)
        MyQueryReader().convert(None)
        logging.disable(logging.NOTSET)
    except Exception as e:
        pytest.fail(error_message(e), pytrace=True)