Exemplo n.º 1
0
    def setUp(self):
        """
        Set up test attributes
        """
        self.ddoc = mock.Mock()
        self.ddoc.r_session = 'mocked-session'
        self.ddoc.document_url = 'http://mock.example.com/my_db/_design/ddoc001'

        self.view = QueryIndexView(
            self.ddoc,
            'view001',
            {'fields': {'name': 'asc', 'age': 'asc'}},
            '_count',
            options = {'def': {'fields': ['name', 'age']}, 'w': 2}
        )
Exemplo n.º 2
0
class QueryIndexViewTests(unittest.TestCase):
    """
    QueryIndexView class unit tests.  These tests use a mocked DesignDocument
    since a QueryIndexView object is not callable so an actual connection
    is not necessary.
    """

    def setUp(self):
        """
        Set up test attributes
        """
        self.ddoc = mock.Mock()
        self.ddoc.r_session = 'mocked-session'
        self.ddoc.document_url = 'http://mock.example.com/my_db/_design/ddoc001'

        self.view = QueryIndexView(
            self.ddoc,
            'view001',
            {'fields': {'name': 'asc', 'age': 'asc'}},
            '_count',
            options = {'def': {'fields': ['name', 'age']}, 'w': 2}
        )

    def test_constructor(self):
        """
        Test constructing a QueryIndexView
        """
        self.assertIsInstance(self.view, QueryIndexView)
        self.assertEqual(self.view.design_doc, self.ddoc)
        self.assertEqual(self.view.view_name, 'view001')
        self.assertIsNone(self.view.result)
        self.assertEqual(self.view, {
            'map': {'fields': {'name': 'asc', 'age': 'asc'}},
            'reduce': '_count',
            'options': {'def': {'fields': ['name', 'age']}, 'w': 2}
        })

    def test_map_getter(self):
        """
        Test that the map getter works
        """
        self.assertEqual(
            self.view.map,
            {'fields': {'name': 'asc', 'age': 'asc'}}
        )
        self.assertEqual(self.view.map, self.view['map'])

    def test_map_setter(self):
        """
        Test that the map setter works
        """
        self.view.map = {'fields': {'name': 'desc', 'age': 'desc'}}
        self.assertEqual(
            self.view.map,
            {'fields': {'name': 'desc', 'age': 'desc'}}
        )
        self.assertEqual(self.view.map, self.view['map'])

    def test_map_setter_failure(self):
        """
        Test that the map setter fails if a dict is not supplied
        """
        try:
            self.view.map = 'function (doc) {\n  emit(doc._id, 1);\n}'
            self.fail('Above statement should raise an Exception')
        except CloudantArgumentError as err:
            self.assertEqual(
                str(err),
                'The map property must be a dictionary'
            )

    def test_reduce_getter(self):
        """
        Test that the reduce getter works
        """
        self.assertEqual(self.view.reduce, '_count')
        self.assertEqual(self.view.reduce, self.view['reduce'])

    def test_reduce_setter(self):
        """
        Test that the reduce setter works
        """
        self.view.reduce = '_sum'
        self.assertEqual(self.view.reduce, '_sum')
        self.assertEqual(self.view.reduce, self.view['reduce'])

    def test_reduce_setter_failure(self):
        """
        Test that the reduce setter fails if a string is not supplied
        """
        with self.assertRaises(CloudantArgumentError) as cm:
            self.view.reduce = {'_count'}
        err = cm.exception
        self.assertEqual(str(err), 'The reduce property must be a string')

    def test_callable_disabled(self):
        """
        Test that the callable for QueryIndexView does not execute.
        """
        with self.assertRaises(CloudantException) as cm:
            self.view()
        err = cm.exception
        self.assertEqual(
            str(err),
            'A QueryIndexView is not callable.  '
            'If you wish to execute a query '
            'use the database \'get_query_result\' convenience method.'
        )

    def test_make_result_disabled(self):
        """
        Test that the make_result method for QueryIndexView does not execute.
        """
        with self.assertRaises(CloudantException) as cm:
            self.view.make_result()
        err = cm.exception
        self.assertEqual(
            str(err),
            'Cannot make a result using a QueryIndexView.  If you wish to '
            'execute a query use the database \'get_query_result\' '
            'convenience method.'
        )

    def test_custom_result_disabled(self):
        """
        Test that the custom_result context manager for QueryIndexView does not
        execute.
        """
        with self.assertRaises(CloudantException) as cm:
            with self.view.custom_result() as result:
                pass
        err = cm.exception
        self.assertEqual(
            str(err),
            'Cannot make a result using a QueryIndexView.  If you wish to '
            'execute a query use the database \'get_query_result\' '
            'convenience method.'
        )