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'])
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'])