Source

hydra / apps / externals / tests / test_views.py

Full commit
import unittest
ViewTestCase = require('lib/tests/helpers')['ViewTestCase']

class ViewByIdTestCase(ViewTestCase):

    func_name = 'by_id'

    def should_skip_docs_without_meta(self):
        doc = {'_id': 'foo', '_rev': 'bar'}
        self.assertFalse(self.mapfun(doc))

    def should_skip_docs_without_meta_externals(self):
        doc = {'_id': 'foo', '_rev': 'bar', '.meta': {'baz': 'boo'}}
        self.assertFalse(self.mapfun(doc))

    def should_require_external_id_field(self):
        doc = {'_id': 'foo', '_rev': 'bar',
               '.meta': {'externals': {'foo': 'bar'},
                         'type': 'generic'}}
        self.assertRaises(Exception, self.mapfun, doc)

    def should_emit_key_of_external_and_id(self):
        doc = {'_id': 'foo', '_rev': 'bar',
               '.meta': {'externals': {'baz': {'id': 'boo'}}}}
        result = self.mapfun(doc)
        self.assertEqual(len(result), 1)
        key, value = result[0]
        self.assertEqual(key, ['baz', 'boo'])

    def should_emit_multiple_records(self):
        doc = {'_id': 'foo', '_rev': 'bar',
               '.meta': {'externals': {'baz': {'id': 'boo'},
                                       'bar': {'id': 'boo'}}}}
        result = self.mapfun(doc)
        self.assertEqual(len(result), 2)

    def should_emit_value_with_rev_and_meta(self):
        doc = {'_id': 'foo', '_rev': 'bar',
               '.meta': {'externals': {'baz': {'id': 'boo'}}}}
        result = self.mapfun(doc)
        self.assertEqual(len(result), 1)
        key, value = result[0]
        self.assertEqual(value, {'rev': doc['_rev'], 'meta': doc['.meta']})

    def should_emit_all_externals(self):
        doc = {'_id': 'foo', '_rev': 'bar',
               '.meta': {'externals': {'baz': {'id': 'boo'},
                                       'boo': {'id': 'foo'}}}}
        result = self.mapfun(doc)
        self.assertEqual(len(result), 2)
        keys = [['baz', 'boo'], ['boo', 'foo']]
        values = [{'rev': doc['_rev'], 'meta': doc['.meta']}] * 2
        kvs = map(list, zip(keys, values))
        self.assertEqual(result, kvs)


class ViewByTypeAndIdTestCase(ViewTestCase):

    func_name = 'by_type_and_id'

    def should_skip_docs_without_meta(self):
        doc = {'_id': 'foo', '_rev': 'bar'}
        self.assertFalse(self.mapfun(doc))

    def should_skip_docs_without_meta_externals(self):
        doc = {'_id': 'foo', '_rev': 'bar', '.meta': {'baz': 'boo'}}
        self.assertFalse(self.mapfun(doc))

    def should_require_meta_type(self):
        doc = {'_id': 'foo', '_rev': 'bar',
               '.meta': {'externals': {'id': 'bar'}}}
        self.assertRaises(Exception, self.mapfun, doc)

    def should_require_external_id_field(self):
        doc = {'_id': 'foo', '_rev': 'bar',
               '.meta': {'externals': {'foo': 'bar'},
                         'type': 'generic'}}
        self.assertRaises(Exception, self.mapfun, doc)

    def should_emit_key_of_external_and_id(self):
        doc = {'_id': 'foo', '_rev': 'bar',
               '.meta': {'externals': {'baz': {'id': 'boo'}},
                         'type': 'generic'}}
        result = self.mapfun(doc)
        self.assertEqual(len(result), 1)
        key, value = result[0]
        self.assertEqual(key, ['baz', 'generic', 'boo'])

    def should_emit_value_with_rev_and_meta(self):
        doc = {'_id': 'foo', '_rev': 'bar',
               '.meta': {'externals': {'baz': {'id': 'boo'}},
                         'type': 'generic'}}
        result = self.mapfun(doc)
        self.assertEqual(len(result), 1)
        key, value = result[0]
        self.assertEqual(value, {'rev': doc['_rev'], 'meta': doc['.meta']})

    def should_emit_all_externals(self):
        doc = {'_id': 'foo', '_rev': 'bar',
               '.meta': {'externals': {'baz': {'id': 'boo'},
                                       'boo': {'id': 'foo'}},
                         'type': 'generic'}}
        result = self.server.map_doc(doc)
        keys = [['baz', 'generic', 'boo'], ['boo', 'generic', 'foo']]
        values = [{'rev': doc['_rev'], 'meta': doc['.meta']}] * 2
        kvs = [map(list, zip(keys, values))]
        self.assertEqual(result, kvs)


class ViewByHostTestCase(ViewTestCase):

    func_name = 'by_host'

    def should_skip_docs_without_meta(self):
        doc = {'_id': 'foo', '_rev': 'bar'}
        self.assertFalse(self.mapfun(doc))

    def should_skip_docs_without_meta_externals(self):
        doc = {'_id': 'foo', '_rev': 'bar', '.meta': {'baz': 'boo'}}
        self.assertFalse(self.mapfun(doc))

    def should_require_external_host_field(self):
        doc = {'_id': 'foo', '_rev': 'bar',
               '.meta': {'externals': {'foo': 'bar'}}}
        self.assertRaises(Exception, self.mapfun, doc)

    def should_emit_key_of_external_and_id(self):
        doc = {'_id': 'foo', '_rev': 'bar',
               '.meta': {'externals': {'baz': {'host': 'boo'}},
                         'type': 'generic'}}
        result = self.mapfun(doc)
        self.assertEqual(len(result), 1)
        key, value = result[0]
        self.assertEqual(key, ['baz', 'boo'])

    def should_emit_value_with_rev_and_meta(self):
        doc = {'_id': 'foo', '_rev': 'bar',
               '.meta': {'externals': {'baz': {'host': 'boo'}},
                         'type': 'generic'}}
        result = self.mapfun(doc)
        self.assertEqual(len(result), 1)
        key, value = result[0]
        self.assertEqual(value, {'rev': doc['_rev'], 'meta': doc['.meta']})

    def should_emit_all_externals(self):
        doc = {'_id': 'foo', '_rev': 'bar',
               '.meta': {'externals': {'baz': {'host': 'boo'},
                                       'boo': {'host': 'foo'}},
                         'type': 'generic'}}
        result = self.mapfun(doc)
        self.assertEqual(len(result), 2)
        keys = [['baz', 'boo'], ['boo', 'foo']]
        values = [{'rev': doc['_rev'], 'meta': doc['.meta']}] * 2
        kvs = map(list, zip(keys, values))
        self.assertEqual(result, kvs)

suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(ViewByIdTestCase, prefix='should'))
suite.addTest(unittest.makeSuite(ViewByTypeAndIdTestCase, prefix='should'))
suite.addTest(unittest.makeSuite(ViewByHostTestCase, prefix='should'))

exports['suite'] = suite