Skip to content

Commit

Permalink
Merge 309ab97 into 9c0693d
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinjqiu committed Aug 1, 2016
2 parents 9c0693d + 309ab97 commit b43c87e
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 25 deletions.
17 changes: 17 additions & 0 deletions cdbcli/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,3 +304,20 @@ def edit(environment, couch_server, variables):
environment.current_db[doc_id] = updated_doc
except Exception as e:
raise RuntimeError(str(e))


@command_handler('touch', pattern='(?P<doc_id>[^\s]+)')
@require_current_db
def touch(environment, couch_server, variables):
"""touch <doc_id>
Create an empty document by doc_id.
"""
doc_id = variables.get('doc_id')

if not doc_id:
raise RuntimeError('Must specify doc_id')

doc = {'_id': doc_id}
environment.current_db.save(doc)
53 changes: 28 additions & 25 deletions tests/integration/test_repl.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import functools
import io
import json
import tempfile
Expand Down Expand Up @@ -27,6 +28,17 @@ def _get_pipe_output(pipe_output_temp_file_path, expect_empty_output=False): #
return output


def _assert_command_requires_current_db(command, environment, couch_server):
with pytest.raises(RuntimeError) as e:
eval_(environment, couch_server, command)
assert str(e.value) == 'No database selected.'


for command in ['exec blah', 'lv blah', 'vim blah', 'touch blah', 'info', 'cat blah', 'rm blah']:
test_name = 'test_requires_current_db_for_command_{}'.format(command.replace(' ', '_'))
globals()[test_name] = functools.partial(_assert_command_requires_current_db, command)


def test_non_admin_cannot_access_users(environment, non_admin_couch_server):
eval_(environment, non_admin_couch_server, 'cd _users')
with pytest.raises(RuntimeError) as e:
Expand All @@ -47,11 +59,6 @@ def abc(environment, couch_server):
assert handler is COMMANDS['duh'] is COMMANDS['huh']


def test_info_command_raises_error_when_no_current_db(environment, couch_server):
with pytest.raises(RuntimeError):
eval_(environment, couch_server, 'info')


def test_info_command_show_db_stats(environment, couch_server):
environment.current_db = couch_server.create('test')
eval_(environment, couch_server, 'info')
Expand Down Expand Up @@ -159,11 +166,6 @@ def test_cat_raises_error_when_no_docid_specified(environment, couch_server):
eval_(environment, couch_server, 'cat')


def test_cat_raises_error_when_no_current_db_selected(environment, couch_server):
with pytest.raises(RuntimeError):
eval_(environment, couch_server, 'cat cafebabe')


def test_cat_raises_error_when_no_doc_matching_id(environment, couch_server):
db = couch_server.create('test')
doc_id, _ = db.save({})
Expand Down Expand Up @@ -210,11 +212,6 @@ def test_mkdir_raises_exception_for_non_admin(environment, non_admin_couch_serve
eval_(environment, non_admin_couch_server, 'mkdir blah')


def test_exec_requires_current_db(environment, couch_server):
with pytest.raises(RuntimeError):
eval_(environment, couch_server, 'exec blah')


def test_exec_view_does_not_exist(environment, couch_server):
db = couch_server.create('test')
db.save(get_user_design_doc())
Expand All @@ -241,11 +238,6 @@ def test_exec_view(environment, couch_server, mocker):
assert expected == actual


def test_lv_requires_current_db(environment, couch_server):
with pytest.raises(RuntimeError):
eval_(environment, couch_server, 'lv blah')


def test_lv_requires_real_view_doc_id(environment, couch_server):
db = couch_server.create('test')
db.save(get_user_design_doc())
Expand Down Expand Up @@ -302,11 +294,6 @@ def test_man_command_shows_all_help(environment, couch_server):
assert handler.help in output


def test_edit_requires_current_db(environment, couch_server):
with pytest.raises(RuntimeError):
eval_(environment, couch_server, 'vim blah')


def _setup_edit_environment(environment, couch_server, mocker, file_content):
mkstemp_return = tempfile.mkstemp()
db = couch_server.create('test')
Expand Down Expand Up @@ -426,6 +413,22 @@ def test_pipe_error(environment, couch_server):
assert environment.output_stream is f


def test_touch_requires_doc_id(environment, couch_server):
db = couch_server.create('test')
environment.current_db = db
with pytest.raises(RuntimeError) as e:
eval_(environment, couch_server, 'touch ')
assert str(e.value) == 'Must specify doc_id'


def test_touch_creates_empty_doc(environment, couch_server):
db = couch_server.create('test')
environment.current_db = db
eval_(environment, couch_server, 'touch finding_nemo')
assert db['finding_nemo'] is not None
assert {'_id', '_rev'} == set(db['finding_nemo'].keys())


def test_repl_prompt_non_admin(environment, non_admin_couch_server):
config = Mock(username='None', host='localhost', database=None)
repl = Repl(non_admin_couch_server, config, environment)
Expand Down

0 comments on commit b43c87e

Please sign in to comment.