def setUp(self):
        from Products.ZSPARQLMethod.Method import ZSPARQLMethod
        self.method = ZSPARQLMethod('sq', "Test Method", SPARQL_URL)

        from Products.StandardCacheManagers.RAMCacheManager import RAMCache
        self.cache = RAMCache()
        self.cache.__dict__.update({
            'threshold': 100,
            'cleanup_interval': 300,
            'request_vars': ()
        })
        self.method.ZCacheable_getCache = Mock(return_value=self.cache)
        self.method.ZCacheable_setManagerId('_cache_for_test')
    def setUp(self):
        from Products.ZSPARQLMethod.Method import ZSPARQLMethod
        self.method = ZSPARQLMethod('sq', "Test Method", SPARQL_URL)

        from Products.StandardCacheManagers.RAMCacheManager import RAMCache
        self.cache = RAMCache()
        self.cache.__dict__.update({
            'threshold': 100, 'cleanup_interval': 300, 'request_vars': ()})
        self.method.ZCacheable_getCache = Mock(return_value=self.cache)
        self.method.ZCacheable_setManagerId('_cache_for_test')
class CachingTest(unittest.TestCase):
    def setUp(self):
        from Products.ZSPARQLMethod.Method import ZSPARQLMethod
        self.method = ZSPARQLMethod('sq', "Test Method", SPARQL_URL)

        from Products.StandardCacheManagers.RAMCacheManager import RAMCache
        self.cache = RAMCache()
        self.cache.__dict__.update({
            'threshold': 100,
            'cleanup_interval': 300,
            'request_vars': ()
        })
        self.method.ZCacheable_getCache = Mock(return_value=self.cache)
        self.method.ZCacheable_setManagerId('_cache_for_test')

    def test_invalidate(self):
        self.cache.ZCache_invalidate = Mock()
        self.method.manage_edit(_mock_request())
        self.cache.ZCache_invalidate.assert_called_once_with(self.method)

    @patch('Products.ZSPARQLMethod.Method.query_and_get_result')
    def test_cached_queries(self, mock_query):
        onto_name = EIONET_RDF + '/ontology/name'
        self.method.query = "SELECT * WHERE {$subject <%s> ?value}" % onto_name
        self.method.arg_spec = u"subject:iri"
        mock_query.return_value = {
            'rows': [],
            'var_names': [],
            'has_result': True
        }

        self.method(subject=EIONET_RDF + '/languages/en')
        self.method(subject=EIONET_RDF + '/languages/da')
        self.method(subject=EIONET_RDF + '/languages/da')
        self.method(subject=EIONET_RDF + '/languages/en')
        self.method(subject=EIONET_RDF + '/languages/fr')
        self.method(subject=EIONET_RDF + '/languages/fr')
        self.method(subject=EIONET_RDF + '/languages/fr')
        self.method(subject=EIONET_RDF + '/languages/fr')
        self.method(subject=EIONET_RDF + '/languages/da')

        self.assertEqual(len(mock_query.call_args_list), 3)
class CachingTest(unittest.TestCase):

    def setUp(self):
        from Products.ZSPARQLMethod.Method import ZSPARQLMethod
        self.method = ZSPARQLMethod('sq', "Test Method", SPARQL_URL)

        from Products.StandardCacheManagers.RAMCacheManager import RAMCache
        self.cache = RAMCache()
        self.cache.__dict__.update({
            'threshold': 100, 'cleanup_interval': 300, 'request_vars': ()})
        self.method.ZCacheable_getCache = Mock(return_value=self.cache)
        self.method.ZCacheable_setManagerId('_cache_for_test')

    def test_invalidate(self):
        self.cache.ZCache_invalidate = Mock()
        self.method.manage_edit(_mock_request())
        self.cache.ZCache_invalidate.assert_called_once_with(self.method)

    @patch('Products.ZSPARQLMethod.Method.query_and_get_result')
    def test_cached_queries(self, mock_query):
        onto_name = EIONET_RDF + '/ontology/name'
        self.method.query = "SELECT * WHERE {$subject <%s> ?value}" % onto_name
        self.method.arg_spec = u"subject:iri"
        mock_query.return_value = {
            'rows': [], 'var_names': [], 'has_result': True}

        self.method(subject=EIONET_RDF + '/languages/en')
        self.method(subject=EIONET_RDF + '/languages/da')
        self.method(subject=EIONET_RDF + '/languages/da')
        self.method(subject=EIONET_RDF + '/languages/en')
        self.method(subject=EIONET_RDF + '/languages/fr')
        self.method(subject=EIONET_RDF + '/languages/fr')
        self.method(subject=EIONET_RDF + '/languages/fr')
        self.method(subject=EIONET_RDF + '/languages/fr')
        self.method(subject=EIONET_RDF + '/languages/da')

        self.assertEqual(len(mock_query.call_args_list), 3)
    def setUp(self):
        from Products.ZSPARQLMethod.Method import ZSPARQLMethod

        self.method = ZSPARQLMethod('sq', "Test Method", "")
        self.method.endpoint_url = "http://cr.eionet.europa.eu/sparql"
        self.method.query = mock_db.GET_LANG_BY_NAME
        self.method.arg_spec = u"lang_name:string"

        self.app = WsgiApp(self.method)

        wsgi_intercept.add_wsgi_intercept('test', 80, lambda: self.app)
        self.browser = wsgi_intercept.mechanize_intercept.Browser()

        self.validate_patch = patch('AccessControl.SecurityManagement'
                                    '.SecurityManager.validate')
        self.validate_patch.start().return_value = True

        self.mock_db = mock_db.MockSparql()
        self.mock_db.start()
 def setUp(self):
     from Products.ZSPARQLMethod.Method import ZSPARQLMethod
     self.method = ZSPARQLMethod('sq', "Test Method", SPARQL_URL)
     self.mock_db = mock_db.MockSparql()
     self.mock_db.start()
class QueryTest(unittest.TestCase):
    def setUp(self):
        from Products.ZSPARQLMethod.Method import ZSPARQLMethod
        self.method = ZSPARQLMethod('sq', "Test Method", SPARQL_URL)
        self.mock_db = mock_db.MockSparql()
        self.mock_db.start()

    def tearDown(self):
        self.mock_db.stop()

    def test_simple_query(self):
        from sparql import IRI
        self.method.query = mock_db.GET_LANGS
        result = self.method.execute()
        self.assertEqual(result['result']['rows'], [
            (IRI(EIONET_RDF + '/languages/en'), ),
            (IRI(EIONET_RDF + '/languages/de'), ),
        ])

    @patch('Products.ZSPARQLMethod.Method.sparql.pycurl')
    def test_timeout(self, mock_pycurl):
        class MockCurl(object):
            def perform(self):
                import sparql
                raise sparql.SparqlException(28, "timeout")

            def setopt(self, value, opt):
                pass

        def mock_Curl():
            return MockCurl()

        mock_pycurl.Curl = mock_Curl

        from Products.ZSPARQLMethod.Method import QueryTimeout
        self.method.query = mock_db.GET_LANGS

        self.assertRaises(QueryTimeout, self.method.execute)

    @patch('Products.ZSPARQLMethod.Method.sparql')
    def test_error(self, mock_sparql):
        self.method.query = mock_db.GET_LANGS

        class MyError(Exception):
            pass

        mock_sparql.query.side_effect = MyError

        result = self.method.execute()
        if MyError.__name__ not in result['exception']:
            raise self.failureException, "%s not raised" % MyError.__name__

    def test_query_with_arguments(self):
        self.method.query = mock_db.GET_LANG_BY_NAME
        result = self.method.execute(lang_name=sparql.Literal("Danish"))

        danish_iri = sparql.IRI(EIONET_RDF + '/languages/da')
        self.assertEqual(result['result']['rows'], [(danish_iri, )])

    def test_call(self):
        self.method.query = mock_db.GET_LANG_BY_NAME
        self.method.arg_spec = u"lang_name:n3term"
        result = self.method(lang_name='"Danish"')

        self.assertEqual(result[0], [EIONET_RDF + '/languages/da'])
 def setUp(self):
     from Products.ZSPARQLMethod.Method import ZSPARQLMethod
     self.method = ZSPARQLMethod('sq', "Test Method", SPARQL_URL)
     self.method.query = "SELECT ?o WHERE {?s ?p ?o}"
 def setUp(self):
     from Products.ZSPARQLMethod.Method import ZSPARQLMethod
     self.method = ZSPARQLMethod('sq', "Test Method", SPARQL_URL)
     self.mock_db = mock_db.MockSparql()
     self.mock_db.start()
class QueryTest(unittest.TestCase):
    def setUp(self):
        from Products.ZSPARQLMethod.Method import ZSPARQLMethod
        self.method = ZSPARQLMethod('sq', "Test Method", SPARQL_URL)
        self.mock_db = mock_db.MockSparql()
        self.mock_db.start()

    def tearDown(self):
        self.mock_db.stop()

    def test_simple_query(self):
        from sparql import IRI
        self.method.query = mock_db.GET_LANGS
        result = self.method.execute()
        self.assertEqual(result['result']['rows'], [
            (IRI(EIONET_RDF + '/languages/en'),),
            (IRI(EIONET_RDF + '/languages/de'),),
        ])

    @patch('Products.ZSPARQLMethod.Method.sparql.pycurl')
    def test_timeout(self, mock_pycurl):
        class MockCurl(object):
            def perform(self):
                import sparql
                raise sparql.SparqlException(28, "timeout")

            def setopt(self, value, opt):
                pass

        def mock_Curl():
            return MockCurl()


        mock_pycurl.Curl = mock_Curl

        from Products.ZSPARQLMethod.Method import QueryTimeout
        self.method.query = mock_db.GET_LANGS

        self.assertRaises(QueryTimeout, self.method.execute)

    @patch('Products.ZSPARQLMethod.Method.sparql')
    def test_error(self, mock_sparql):
        self.method.query = mock_db.GET_LANGS
        class MyError(Exception): pass
        mock_sparql.query.side_effect = MyError

        result = self.method.execute()
        if MyError.__name__ not in result['exception']:
            raise self.failureException, "%s not raised" % MyError.__name__

    def test_query_with_arguments(self):
        self.method.query = mock_db.GET_LANG_BY_NAME
        result = self.method.execute(lang_name=sparql.Literal("Danish"))

        danish_iri = sparql.IRI(EIONET_RDF+'/languages/da')
        self.assertEqual(result['result']['rows'], [(danish_iri,)])

    def test_call(self):
        self.method.query = mock_db.GET_LANG_BY_NAME
        self.method.arg_spec = u"lang_name:n3term"
        result = self.method(lang_name='"Danish"')

        self.assertEqual(result[0], [EIONET_RDF+'/languages/da'])