~clint-fewbar/+junk/python-pymongo-packaging

« back to all changes in this revision

Viewing changes to tests/test_schemaless.py

  • Committer: Clint Byrum
  • Date: 2012-01-25 21:53:57 UTC
  • Revision ID: clint@ubuntu.com-20120125215357-j603u9d5alv1bt9v
Tags: upstream-0.7.2
ImportĀ upstreamĀ versionĀ 0.7.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
# -*- coding: utf-8 -*-
 
3
# Copyright (c) 2009-2011, Nicolas Clairon
 
4
# All rights reserved.
 
5
# Redistribution and use in source and binary forms, with or without
 
6
# modification, are permitted provided that the following conditions are met:
 
7
#
 
8
#     * Redistributions of source code must retain the above copyright
 
9
#       notice, this list of conditions and the following disclaimer.
 
10
#     * Redistributions in binary form must reproduce the above copyright
 
11
#       notice, this list of conditions and the following disclaimer in the
 
12
#       documentation and/or other materials provided with the distribution.
 
13
#     * Neither the name of the University of California, Berkeley nor the
 
14
#       names of its contributors may be used to endorse or promote products
 
15
#       derived from this software without specific prior written permission.
 
16
#
 
17
# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
 
18
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 
19
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 
20
# DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
 
21
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 
22
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 
23
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 
24
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 
25
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 
26
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
27
 
 
28
import unittest
 
29
 
 
30
from mongokit import *
 
31
import datetime
 
32
 
 
33
 
 
34
class SchemaLessTestCase(unittest.TestCase):
 
35
 
 
36
    def setUp(self):
 
37
        self.connection = Connection()
 
38
        self.col = self.connection['test']['mongokit']
 
39
        
 
40
    def tearDown(self):
 
41
        self.connection.drop_database('test')
 
42
        self.connection.drop_database('othertest')
 
43
 
 
44
    def test_simple_schemaless(self):
 
45
        @self.connection.register
 
46
        class MyDoc(Document):
 
47
            use_schemaless = True
 
48
            structure = {
 
49
                'foo': unicode,
 
50
                'bar': int,
 
51
            }
 
52
 
 
53
        doc = self.col.MyDoc()
 
54
        self.assertEqual('foo' in doc, True)
 
55
        self.assertEqual('bar' in doc, True)
 
56
        self.assertEqual('egg' in doc, False)
 
57
        doc['foo'] = u'bla'
 
58
        doc['bar'] = 3
 
59
        doc['egg'] = 9
 
60
        doc.save()
 
61
        
 
62
        doc = self.col.find_one()
 
63
        self.assertEqual('foo' in doc, True)
 
64
        self.assertEqual('bar' in doc, True)
 
65
        self.assertEqual('egg' in doc, True)
 
66
 
 
67
        doc = self.col.MyDoc.find_one()
 
68
        self.assertEqual('foo' in doc, True)
 
69
        self.assertEqual('bar' in doc, True)
 
70
        self.assertEqual('egg' in doc, True)
 
71
        doc['foo'] = 2
 
72
        self.assertRaises(SchemaTypeError, doc.save)
 
73
        doc.pop('foo')
 
74
        doc.pop('bar')
 
75
        doc.save()
 
76
        doc = self.col.MyDoc.find_one()
 
77
        self.assertEqual(doc.keys(), ['_id', 'egg'])
 
78
 
 
79
        doc = self.col.MyDoc({'_id':1, 'foo':u'bla'})
 
80
        doc.save()
 
81
        
 
82
 
 
83
    def test_schemaless_with_required(self):
 
84
        @self.connection.register
 
85
        class MyDoc(Document):
 
86
            use_schemaless = True
 
87
            structure = {
 
88
                'foo': unicode,
 
89
                'bar': int,
 
90
            }
 
91
            required_fields = ['foo']
 
92
 
 
93
        doc = self.col.MyDoc()
 
94
        self.assertEqual('foo' in doc, True)
 
95
        self.assertEqual('bar' in doc, True)
 
96
        self.assertEqual('egg' in doc, False)
 
97
        doc['foo'] = u'bla'
 
98
        doc['bar'] = 3
 
99
        doc['egg'] = 9
 
100
        doc.save()
 
101
        
 
102
        doc = self.col.MyDoc()
 
103
        doc.pop('foo')
 
104
        doc['bar'] = 3
 
105
        doc['egg'] = 9
 
106
        self.assertRaises(RequireFieldError, doc.save)
 
107
        
 
108
        doc = self.col.find_one()
 
109
        doc.pop('foo')
 
110
        self.col.save(doc)
 
111
 
 
112
        doc = self.col.MyDoc.find_one()
 
113
        self.assertEqual('foo' in doc, False)
 
114
        self.assertEqual('bar' in doc, True)
 
115
        self.assertEqual('egg' in doc, True)
 
116
        doc['bar'] = 2
 
117
        self.assertRaises(RequireFieldError, doc.save)
 
118
        doc['foo'] = u'arf'
 
119
        doc.save()
 
120
 
 
121
    def test_schemaless_no_structure(self):
 
122
        @self.connection.register
 
123
        class MyDoc(Document):
 
124
            use_schemaless = True
 
125
 
 
126
        doc = self.col.MyDoc()
 
127
        self.assertEqual('foo' in doc, False)
 
128
        self.assertEqual('bar' in doc, False)
 
129
        doc['_id'] = u'foo'
 
130
        doc['foo'] = u'bla'
 
131
        doc['bar'] = 3
 
132
        doc.save()
 
133
        
 
134
        doc = self.col.find_one()
 
135
        self.assertEqual('foo' in doc, True)
 
136
        self.assertEqual('bar' in doc, True)
 
137
 
 
138
        doc = self.col.MyDoc.find_one()
 
139
        self.assertEqual('foo' in doc, True)
 
140
        self.assertEqual('bar' in doc, True)
 
141
        self.assertEqual(doc, {'_id': 'foo', 'foo':'bla', 'bar':3})
 
142
 
 
143
    def test_schemaless_senario2(self):
 
144
        @self.connection.register
 
145
        class User(Document):
 
146
            __collection__ = 'mongokit'
 
147
            __database__ = 'test'
 
148
            use_schemaless = True
 
149
            structure = {
 
150
                'name': unicode,
 
151
                'password': unicode,
 
152
                'last_name': unicode,
 
153
                'first_name': unicode,
 
154
                'email': unicode,
 
155
                'last_login': datetime.datetime,
 
156
            }
 
157
            use_dot_notation = True
 
158
 
 
159
        self.connection.User.collection.save({'name': u'namlook', 'password': u'test', 'email': u'n@c.com'})
 
160
 
 
161
        found_attribute = self.connection.User.find_one({'name':'namlook'})
 
162
        found_attribute.last_login = datetime.datetime.utcnow()
 
163
        found_attribute.save()