-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_mapper.py
123 lines (109 loc) · 4.42 KB
/
test_mapper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# -*- coding: utf-8 -*-
#
# Copyright (c) 2009 Benoit Chesneau <[email protected]>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#
__author__ = '[email protected] (Jannis Andrija Schnitzer)'
from datetime import datetime
import unittest
from httplib2 import Http
from couchdbkit.resource import ResourceNotFound
from mapper import Mapper, map
from couchdbkit.client import Server
from couchdbkit.schema import Document
class Greeting(object):
def __init__(self, author, content):
self.author = author
self.content = content
def printout(self):
print self.author+':', self.content
def __eq__(self, other):
return self.author == other.author and self.content == other.content
url = 'http://127.0.0.1:5984'
db_name = 'couchdbkit_test'
# alternatively
#url = 'http://username:topsecret@localhost:5984/'
#db_name = 'some_other_db'
class MapperTestCase(unittest.TestCase):
def setUp(self):
self.server = Server(url)
self.db = map(self.server.get_or_create_db(db_name))
# TODO: create greeting/all view
design_doc = {
'_id': '_design/greeting',
'language': 'javascript',
'views': {
'all': {
"map": """
function (doc)
{
if (doc.doc_type == "Greeting")
emit(doc._id, doc);
}
"""
}
}
}
self.db.save_doc(design_doc)
def tearDown(self):
self.server.delete_db(db_name)
def testMapping(self):
# introduce our class to the mapper
self.db.add(Greeting)
self.assert_(issubclass(self.db.classes['Greeting'], (Greeting, Document)))
# TODO: more stuff
g = Greeting('Jannis', 'welcome to the mapping world')
self.db['g'] = g
mapped_g = self.db['g']
self.assert_(mapped_g._id == 'g')
self.assert_(isinstance(mapped_g, Greeting))
self.assert_(g == mapped_g)
greetings = [
Greeting('Jannis', 'this is the first test greeting'),
Greeting('Jannis again', 'this is the second test greeting')
]
greetings[0]._id = 'first test greeting'
greetings[1]._id = 'second test greeting'
self.db.bulk_save(greetings)
self.assert_([self.db['first test greeting'],
self.db['second test greeting']] == greetings)
h = Greeting('Xjs', 'Hi!')
h2 = self.db.save_doc(h)
h2_id = h2._id
self.assertEquals(h, self.db[h2_id])
self.db.delete_doc(h2)
self.assertRaises(ResourceNotFound, self.db.__getitem__, h2_id)
self.db.copy_doc(mapped_g, 'another_g')
self.assertEquals(g, self.db['another_g'])
def testViewResults(self):
def frobnicate(obj):
"""Helper function that returns a greeting which is ``frobnicated''
(this simulates an action being performed).
Used as test for the wrapper func - because Mapper.view overwrites
the mapper func passed to Database.view with a custom one in order
to give back mapped objects but still retain the ability to pass
a custom wrapper function."""
greeting = obj['value']
greeting.frobnicated = True
return greeting
# view generated in setUp
all_greetings = self.db.view('greeting/all', wrapper=frobnicate)
for greeting in all_greetings:
# Test if we get a Greeting class back, not a Document or even dict
self.assert_(isinstance(greeting, Greeting))
# Test if custom wrapper function worked though Mapper mapped the
# document (i. e. set a wrapper function itself)
self.assert_(greeting.frobnicated)
if __name__ == '__main__':
unittest.main()