Skip to content

Basic update handler support. #268

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Expect POST when calling update handlers.
  • Loading branch information
mefyl committed Aug 12, 2015
commit 651acd95699eae2a6afb92bd7821b750a395f100
71 changes: 53 additions & 18 deletions couchdb/tests/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,38 +102,46 @@ def test_reduce_empty(self):
self.assertEqual(output.getvalue(),
b'[true, [0]]\n')

def test_update(self):
import json
commands = [
[
'ddoc',
'new',
'_design/test_update',
{
'_id': '_design/test_update',
'_rev': '8-d7379de23a751dc2a19e5638a7bbc5cc',
'language': 'python',
'updates': {
'inc': '''\
def command_ddoc_add(self):
return [
'ddoc',
'new',
'_design/test_update',
{
'_id': '_design/test_update',
'_rev': '8-d7379de23a751dc2a19e5638a7bbc5cc',
'language': 'python',
'updates': {
'inc': '''\
def fun(obj, req):
if obj is not None:
obj['field'] += 1
return [obj, {"body": "."}]
''',
}
},
],
}
},
]

def test_update(self):
import json
request = {
'method': 'POST',
'query': {},
'body': '',
}
commands = [
self.command_ddoc_add(),
[
'ddoc',
'_design/test_update',
['updates', 'inc'],
[None, {}]
[None, request]
],
[
'ddoc',
'_design/test_update',
['updates', 'inc'],
[{'field': 41, 'other_field': 'x'}, {}]
[{'field': 41, 'other_field': 'x'}, request]
],
]
input = StringIO(b'\n'.join(json.dumps(c).encode('utf-8')
Expand All @@ -151,6 +159,33 @@ def fun(obj, req):
results[2],
['up', {'field': 42, 'other_field': 'x'}, {'body': '.'}])

def test_update_wrong_method(self):
import json
request = {
'method': 'GET',
'query': {},
'body': '',
}
commands = [
self.command_ddoc_add(),
[
'ddoc',
'_design/test_update',
['updates', 'inc'],
[None, request]
],
]
input = StringIO(b'\n'.join(json.dumps(c).encode('utf-8')
for c in commands))
output = StringIO()
view.run(input=input, output=output)
results = [
json.loads(l.decode('utf-8'))
for l in output.getvalue().strip().split(b'\n')
]
self.assertEqual(len(results), 2)
self.assertEqual(results[1][2]['status'], 405)


def suite():
suite = unittest.TestSuite()
Expand Down
15 changes: 11 additions & 4 deletions couchdb/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,17 @@ def ddoc(*cmd):
ddoc = ddocs[cmd[0]]
action = cmd[1]
if action[0] == 'updates':
fun = ddoc['updates'][action[1]]
doc, body = fun(*cmd[2])
res = ['up', doc, body]
sys.stderr.flush()
if cmd[2][1]['method'] != 'POST':
return [
'up',
None,
{'status': 405, 'body': 'Method not allowed'},
]
else:
fun = ddoc['updates'][action[1]]
doc, body = fun(*cmd[2])
res = ['up', doc, body]
sys.stderr.flush()
return res


Expand Down