2
# -*- coding: utf-8 -*-
3
# Copyright (c) 2009-2011, Nicolas Clairon
5
# Redistribution and use in source and binary forms, with or without
6
# modification, are permitted provided that the following conditions are met:
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.
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.
30
from mongokit import *
34
class SchemaLessTestCase(unittest.TestCase):
37
self.connection = Connection()
38
self.col = self.connection['test']['mongokit']
41
self.connection.drop_database('test')
42
self.connection.drop_database('othertest')
44
def test_simple_schemaless(self):
45
@self.connection.register
46
class MyDoc(Document):
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)
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)
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)
72
self.assertRaises(SchemaTypeError, doc.save)
76
doc = self.col.MyDoc.find_one()
77
self.assertEqual(doc.keys(), ['_id', 'egg'])
79
doc = self.col.MyDoc({'_id':1, 'foo':u'bla'})
83
def test_schemaless_with_required(self):
84
@self.connection.register
85
class MyDoc(Document):
91
required_fields = ['foo']
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)
102
doc = self.col.MyDoc()
106
self.assertRaises(RequireFieldError, doc.save)
108
doc = self.col.find_one()
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)
117
self.assertRaises(RequireFieldError, doc.save)
121
def test_schemaless_no_structure(self):
122
@self.connection.register
123
class MyDoc(Document):
124
use_schemaless = True
126
doc = self.col.MyDoc()
127
self.assertEqual('foo' in doc, False)
128
self.assertEqual('bar' in doc, False)
134
doc = self.col.find_one()
135
self.assertEqual('foo' in doc, True)
136
self.assertEqual('bar' in doc, True)
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})
143
def test_schemaless_senario2(self):
144
@self.connection.register
145
class User(Document):
146
__collection__ = 'mongokit'
147
__database__ = 'test'
148
use_schemaless = True
152
'last_name': unicode,
153
'first_name': unicode,
155
'last_login': datetime.datetime,
157
use_dot_notation = True
159
self.connection.User.collection.save({'name': u'namlook', 'password': u'test', 'email': u'n@c.com'})
161
found_attribute = self.connection.User.find_one({'name':'namlook'})
162
found_attribute.last_login = datetime.datetime.utcnow()
163
found_attribute.save()