Esempio n. 1
0
 def test_callable_nested_in_dict(self):
     """ Nested callable defaults.
     """
     spec = translate({'content': {'text': lambda: t('hello')}})
     data = {}
     expected = {'content': {'text': t('hello')}}
     assert merge_defaults(spec, data) == expected
Esempio n. 2
0
 def test_callable_nested_in_dict(self):
     """ Nested callable defaults.
     """
     spec = translate({"content": {"text": lambda: t("hello")}})
     data = {}
     expected = {"content": {"text": t("hello")}}
     assert merge_defaults(spec, data) == expected
Esempio n. 3
0
    def test_equality(self):
        """Documents are equal if all these conditions are met:

        * both inherit to the same class;
        * both are stored in the same collection;
        * both have assigned ids and ids are equal.
        """
        a = self.Entry(title=t('Hello'))
        b = self.Entry(title=t('Hello'))
        assert a != b
        a.save(self.db)
        assert a != b
        c = self.Entry.get_one(self.db)
        assert a == c
        b.save(self.db)
        assert a != b
        d = dict(title=t('Hello'))
        assert a != d

        class E(mongo.Document):
            structure = self.Entry.structure
        e = E(title=t('Hello'))
        assert a != e

        class F(mongo.Document):
            collection = 'comments'
            structure = self.Entry.structure
        e = F(title=t('Hello'))
        e.save(self.db)
        assert a != e
Esempio n. 4
0
 def test_result_set_ids(self):
     self.collection.insert({'title': t('Foo')})
     self.collection.insert({'title': t('Bar')})
     results = self.Entry.find(self.db)
     ids_manual = [x.get_id() for x in results]
     # new object because caching is not supported
     ids = self.Entry.find(self.db).ids()
     assert ids_manual == list(ids)
Esempio n. 5
0
def test_normalize_list_of_dicts():
    f = manipulation.normalize_list_of_dicts

    assert [{'x': 'a'}] == f([{'x': 'a'}], default_key='x')
    assert [{'x': 'a'}] == f( {'x': 'a'},  default_key='x')
    assert [{'x': 'a'}] == f(     t('a'),  default_key='x')
    assert [{'x': 'a'}, {'x': 'b'}] == f([{'x': 'a'}, t('b')], default_key='x')
    assert [] == f(None, default_key='x')
    assert [{'x': t('y')}] == f(None, default_key='x', default_value=t('y'))

    # edge cases (may need revision)
    assert [{'x': 1}] == f({'x': 1}, default_key='y')
    assert [] == f(None, default_key='y')
    assert 123 == f(123, default_key='x')
Esempio n. 6
0
def test_normalize_list_of_dicts():
    f = manipulation.normalize_list_of_dicts

    assert [{'x': 'a'}] == f([{'x': 'a'}], default_key='x')
    assert [{'x': 'a'}] == f({'x': 'a'}, default_key='x')
    assert [{'x': 'a'}] == f(t('a'), default_key='x')
    assert [{'x': 'a'}, {'x': 'b'}] == f([{'x': 'a'}, t('b')], default_key='x')
    assert [] == f(None, default_key='x')
    assert [{'x': t('y')}] == f(None, default_key='x', default_value=t('y'))

    # edge cases (may need revision)
    assert [{'x': 1}] == f({'x': 1}, default_key='y')
    assert [] == f(None, default_key='y')
    assert 123 == f(123, default_key='x')
Esempio n. 7
0
 def test_index_custom(self):
     "Index for _id is created on first save to a collection"
     with pytest.raises_regexp(pymongo.errors.OperationFailure, 'no collection'):
         self.collection.index_information()
     class IndexedEntry(self.Entry):
         indexes = {'title': None}
     IndexedEntry(title=t('Hello')).save(self.db)
     assert 'title_1' in self.collection.index_information()
Esempio n. 8
0
    def test_id(self):
        entry = self.Entry(title=t('Hello'))
        assert entry['_id'] is None
        assert entry.get_id() is None

        # save the first time
        obj_id = entry.save(self.db)
        assert obj_id == entry['_id']
        assert self.Entry.find(self.db).count() == 1
        assert [entry] == list(self.Entry.find(self.db, {'_id': obj_id}))

        # update
        entry.title = t('Bye')
        same_id = entry.save(self.db)
        assert obj_id == same_id
        assert obj_id == entry['_id']
        assert obj_id == entry.get_id()
        assert self.Entry.find(self.db).count() == 1
Esempio n. 9
0
    def test_callable_defaults_custom_func(self):
        class Event(mongo.Document):
            structure = {
                'text': lambda: t('hello')
            }

        event = Event(text=t('albatross'))
        event.validate()
        assert isinstance(event.text, t)
        assert event.text == t('albatross')

        event = Event()
        event.validate()
        assert isinstance(event.text, t)
        assert event.text == t('hello')

        with pytest.raises(ValidationError):
            event = Event(text=123)
            event.validate()
Esempio n. 10
0
    def test_type_in_dict_in_dict(self):
        spec = translate({"a": {"b": int}})

        # key is absent; should be inserted
        assert spec.get_default_for({}) == {"a": {"b": None}}
        # same with nested key
        assert spec.get_default_for({"a": {}}) == {"a": {"b": None}}

        # key is present but value is None; should be overridden with defaults
        #
        #   XXX do we really need to override *present* values in data
        #       even if they are None?
        #
        assert spec.get_default_for({"a": None}) == {"a": {"b": None}}
        assert spec.get_default_for({"a": {"b": None}}) == {"a": {"b": None}}

        # key is present, value is not None; leave as is
        # (even if it won't pass validation)
        assert spec.get_default_for({"a": {"b": 1234}}) == {"a": {"b": 1234}}
        assert spec.get_default_for({"a": t("bogus string")}) == {"a": t("bogus string")}
Esempio n. 11
0
    def test_dot_expanded(self):
        entry = self.Entry(self.data)

        # getattr -> getitem
        assert entry['title'] == self.data['title']
        assert entry['title'] == entry.title
        with pytest.raises(AttributeError):
            entry.nonexistent_key
        assert entry['author']['first_name'] == entry.author.first_name

        # setattr -> setitem
        entry.title = t('Bye!')
        assert entry.title == t('Bye!')
        assert entry.title == entry['title']

        entry.author.first_name = t('Joan')
        assert entry.author.first_name == t('Joan')
        assert entry.author.first_name == entry['author']['first_name']

        assert entry.comments[0].text == entry['comments'][0]['text']
Esempio n. 12
0
    def test_remove(self):
        self.collection.insert({'title': t('Hello')})

        entries = self.Entry.find(self.db)
        assert entries.count() == 1

        entry = entries[0]
        entry.remove(self.db)

        entries = self.Entry.find(self.db)
        assert entries.count() == 0
Esempio n. 13
0
    def test_merge_dictof_dictof_isa(self):
        raw_spec = {"content": {"text": t("hello")}}

        spec = translate(raw_spec)

        # make sure translation went as expected
        assert spec == DictOf([(Equals("content"), DictOf([(Equals("text"), IsA(t, default=t("hello")))]))])

        # make sure merging works as expected for nested dict
        assert raw_spec == spec.get_default_for({"content": {}})

        # make sure merging works as expected for nested *and* root dicts
        assert raw_spec == spec.get_default_for({})
Esempio n. 14
0
    def test_merge_dictof_dictof_isa(self):
        raw_spec = {
            'content': {
                'text': t('hello'),
            },
        }

        spec = translate(raw_spec)

        # make sure translation went as expected
        assert spec == DictOf([
            (Equals('content'),
             DictOf([
                 (Equals('text'), IsA(t, default=t('hello'))),
             ])),
        ])

        # make sure merging works as expected for nested dict
        assert raw_spec == spec.get_default_for({'content': {}})

        # make sure merging works as expected for nested *and* root dicts
        assert raw_spec == spec.get_default_for({})
Esempio n. 15
0
    def test_type_in_dict_in_dict(self):
        spec = translate({'a': {'b': int}})

        # key is absent; should be inserted
        assert spec.get_default_for({}) == {'a': {'b': None}}
        # same with nested key
        assert spec.get_default_for({'a': {}}) == {'a': {'b': None}}

        # key is present but value is None; should be overridden with defaults
        #
        #   XXX do we really need to override *present* values in data
        #       even if they are None?
        #
        assert spec.get_default_for({'a': None}) == {'a': {'b': None}}
        assert spec.get_default_for({'a': {'b': None}}) == {'a': {'b': None}}

        # key is present, value is not None; leave as is
        # (even if it won't pass validation)
        assert spec.get_default_for({'a': {'b': 1234}}) == {'a': {'b': 1234}}
        assert spec.get_default_for({'a': t('bogus string')}) == {
            'a': t('bogus string')
        }
Esempio n. 16
0
    def test_callable_defaults_custom_func_nested(self):
        # Issue #1  https://bitbucket.org/neithere/monk/issue/1/callable-defaults-in-nested-structures
        class Event(mongo.Document):
            structure = {
                'content': {
                    'text': lambda: t('hello')
                }
            }

        event = Event(content=dict(text=t('albatross')))
        event.validate()
        assert isinstance(event.content.text, t)
        assert event.content.text == t('albatross')

        event = Event()
        event.validate()
        assert isinstance(event.content.text, t)
        assert event.content.text == t('hello')

        with pytest.raises(ValidationError):
            event = Event(content=dict(text=123))
            event.validate()
Esempio n. 17
0
 def test_query(self):
     self.collection.insert({'title': t('Hello world!')})
     entries = self.Entry.find(self.db, {'title': t('Hello world!')})
     assert entries.count() == 1
     entry = entries[0]
     assert entry.title == t('Hello world!')
Esempio n. 18
0
    def test_type_in_dict(self):
        spec = translate({"a": t})

        assert spec.get_default_for({}) == {"a": None}
        assert spec.get_default_for({"a": None}) == {"a": None}
        assert spec.get_default_for({"a": t("a")}) == {"a": t("a")}
Esempio n. 19
0
 def test_insert(self):
     entry = self.Entry(title=t('Hello'))
     entry.save(self.db)
     assert self.collection.find().count() == 1
     assert self.collection.find({'title': t('Hello')}).count() == 1
Esempio n. 20
0
    def test_type_in_dict(self):
        spec = translate({'a': t})

        assert spec.get_default_for({}) == {'a': None}
        assert spec.get_default_for({'a': None}) == {'a': None}
        assert spec.get_default_for({'a': t('a')}) == {'a': t('a')}
Esempio n. 21
0
 def test_get_ref(self):
     entry = self.Entry(title=t('Hello'))
     assert entry.get_ref() is None
     entry.save(self.db)
     assert entry.get_ref() == DBRef(self.Entry.collection, entry.get_id())
Esempio n. 22
0
 def test_index_id(self):
     "Index for _id is created on first save to a collection"
     with pytest.raises_regexp(pymongo.errors.OperationFailure, 'no collection'):
         self.collection.index_information()
     self.Entry(title=t('entry')).save(self.db)
     assert '_id_' in self.collection.index_information()