<commit_before>#!/usr/bin/env python
"""
_design_doc_test_

"""
import mock
import unittest

from cloudant.design_document import DesignDocument
from cloudant.document import Document


class DesignDocTests(unittest.TestCase):
    """
    tests for design doc object

    """
    @mock.patch.object(Document, 'fetch')
    def test_design_doc(self, mock_fetch):
        """test overridden methods work as expected"""
        mock_database = mock.Mock()
        ddoc = DesignDocument(mock_database, '_design/unittest')
        ddoc['views'] = {
            'view1' : {'map': "MAP", 'reduce': 'REDUCE'}
        }
        ddoc.fetch()

        self.failUnless(mock_fetch.called)
        views = [ x for _,x in ddoc.iterviews() ]
        self.assertEqual(len(views), 1)
        view = views[0]
        self.failUnless('view1' in view)
        self.assertEqual(view['view1']['map'], 'MAP')
        self.assertEqual(view['view1']['reduce'], 'REDUCE')
        self.failUnless('view1' in ddoc.views)

    def test_ddoc_add_view(self):
        mock_database = mock.Mock()
        with mock.patch('cloudant.design_document.DesignDocument.save') as mock_save:
            ddoc = DesignDocument(mock_database, '_design/unittest')
            ddoc['views'] = {
                'view1': {'map': "MAP", 'reduce': 'REDUCE'}
            }
            ddoc.add_view('view2', "MAP2")
            self.failUnless('view2' in ddoc['views'])
            self.assertEqual(ddoc['views']['view2'].map, 'MAP2')
            self.assertEqual(ddoc['views']['view2'].reduce, None)
            self.failUnless(mock_save.called)

    def test_list_views(self):
        mock_database = mock.Mock()
        ddoc = DesignDocument(mock_database, '_design/unittest')
        ddoc['views'] = {<commit_msg>Modify design doc tests based on recent fix to views<commit_after>#!/usr/bin/env python
"""
_design_doc_test_

"""
import mock
import unittest

from cloudant.design_document import DesignDocument
from cloudant.document import Document


class DesignDocTests(unittest.TestCase):
    """
    tests for design doc object

    """
    @mock.patch.object(Document, 'fetch')
    def test_design_doc(self, mock_fetch):
        """test overridden methods work as expected"""
        mock_database = mock.Mock()
        ddoc = DesignDocument(mock_database, '_design/unittest')
        ddoc['views'] = {
            'view1' : {'map': "MAP", 'reduce': 'REDUCE'}
        }
        ddoc.fetch()

        self.failUnless(mock_fetch.called)
        views = [ x for x in ddoc.iterviews() ]
        self.assertEqual(len(views), 1)
        view = views[0]
        self.failUnless('view1' in view)
        funcs = view[1]
        self.assertEqual(funcs['map'], 'MAP')
        self.assertEqual(funcs['reduce'], 'REDUCE')
        self.failUnless('view1' in ddoc.views)

    def test_new_ddoc_add_view(self):
        mock_database = mock.Mock()
        with mock.patch('cloudant.design_document.DesignDocument.save') as mock_save:
            ddoc = DesignDocument(mock_database, '_design/unittest')
            ddoc.add_view('view1', "MAP")
            self.failUnless('view1' in ddoc['views'])
            self.assertEqual(ddoc['views']['view1'].map, 'MAP')
            self.assertEqual(ddoc['views']['view1'].reduce, None)
            self.failUnless(mock_save.called)

    def test_existing_ddoc_add_view(self):
        mock_database = mock.Mock()
        with mock.patch('cloudant.design_document.DesignDocument.save') as mock_save:
            ddoc = DesignDocument(mock_database, '_design/unittest')
            ddoc['views'] = {
                'view1': {'map': "MAP", 'reduce': 'REDUCE'}
            }
            ddoc.add_view('view2', "MAP2")
            self.failUnless('view2' in ddoc['views'])
            self.assertEqual(ddoc['views']['view2'].map, 'MAP2')
            self.assertEqual(ddoc['views']['view2'].reduce, None)
            self.failUnless(mock_save.called)

    def test_list_views(self):
        mock_database = mock.Mock()
        ddoc = DesignDocument(mock_database, '_design/unittest')
        ddoc['views'] = {
{'fraction_non_alphanumeric': 0.14096916299559473, 'fraction_numerical': 0.0075868820362212435, 'mean_word_length': 2.739249771271729, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3941070', 'n_tokens_mistral': 1263, 'n_tokens_neox': 1231, 'n_words': 234}