Skip to content

Commit

Permalink
KV fixes
Browse files Browse the repository at this point in the history
- Make get_item make a little more sense
- rstrip the / on a non-path key with a trailing slash
- Handle the response body encoding a little better for CAS
- Add additional acceptance tests for KV
  • Loading branch information
gmr committed May 13, 2015
1 parent b8b0c6a commit 4177a97
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 8 deletions.
14 changes: 7 additions & 7 deletions consulate/api/kv.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def get(self, item, default=None, raw=False):
return response.get('Value', default)
return response or default

def get_record(self, item, default=None):
def get_record(self, item):
"""Get the full record from the Key/Value service, returning
all fields including the flag.
Expand All @@ -135,10 +135,7 @@ def get_record(self, item, default=None):
:raises: KeyError
"""
try:
return self._get_item(item)
except KeyError:
return default
return self._get_item(item)

def find(self, prefix, separator=None):
"""Find all keys with the specified prefix, returning a dict of
Expand Down Expand Up @@ -337,7 +334,10 @@ def _get_modify_index(self, item, value, replace):
index = 0
if response.status_code == 200:
index = response.body.get('ModifyIndex')
if response.body.get('Value') == value:
rvalue = response.body.get('Value')
if isinstance(rvalue, bytes):
rvalue = rvalue.encode('utf-8')
if rvalue == value:
return None
if not replace:
return None
Expand Down Expand Up @@ -374,7 +374,7 @@ def _set_item(self, item, value, flags=None, replace=True):
"""
value = self._prepare_value(value)
if value and item.endswith('/'):
item = item.lstrip('/')
item = item.rstrip('/')

index = self._get_modify_index(item, value, replace)
if index is None:
Expand Down
43 changes: 42 additions & 1 deletion tests/acceptance-tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,23 @@ def test_get_item_raises_key_error(self, key):

class TestKVSet(BaseTestCase):

@generate_key
def test_set_item_del_item(self, key):
self.consul.kv[key] = 'foo'
del self.consul.kv[key]
self.assertNotIn(key, self.consul.kv)

@generate_key
def test_set_item_get_item_bool_value(self, key):
self.consul.kv[key] = True
self.assertTrue(self.consul.kv[key])

@generate_key
def test_set_path_with_value(self, key):
path = 'path/{0}/'.format(key)
self.consul.kv.set(path, 'bar')
self.assertEqual('bar', self.consul.kv[path[:-1]])

@generate_key
def test_set_item_get_item_int_value(self, key):
self.consul.kv[key] = 128
Expand Down Expand Up @@ -152,6 +164,30 @@ def test_set_item_get_item_str_value(self, key):
self.consul.kv.set(key, 'foo')
self.assertEqual(self.consul.kv.get(key), 'foo')

@generate_key
def test_set_item_get_record(self, key):
self.consul.kv.set_record(key, 12, 'record')
record = self.consul.kv.get_record(key)
self.assertEqual('record', record['Value'])
self.assertEqual(12, record['Flags'])
self.assertIsInstance(record, dict)

@generate_key
def test_get_record_fail(self, key):
self.assertEqual(self.consul.kv.get_record(key), None)

@generate_key
def test_set_record_no_replace_get_item_str_value(self, key):
self.consul.kv.set(key, 'foo')
self.consul.kv.set_record(key, 0, 'foo', False)
self.assertEqual(self.consul.kv.get(key), 'foo')

@generate_key
def test_set_record_same_value_get_item_str_value(self, key):
self.consul.kv.set(key, 'foo')
self.consul.kv.set_record(key, 0, 'foo', True)
self.assertEqual(self.consul.kv.get(key), 'foo')

@generate_key
def test_set_item_get_item_dict_value(self, key):
value = {'foo': 'bar'}
Expand All @@ -170,9 +206,14 @@ def test_set_item_get_item_unicode_value(self, key):
def test_set_item_get_item_unicode_value(self, key):
self.consul.kv.set(key, '✈')
response = self.consul.kv.get(key)
print(repr(response))
self.assertEqual(response, '✈')

@generate_key
def test_set_item_in_records(self, key):
self.consul.kv.set(key, 'zomg')
expectation = (key, 0, 'zomg')
self.assertIn(expectation, self.consul.kv.records())


class TestSession(unittest.TestCase):

Expand Down

0 comments on commit 4177a97

Please sign in to comment.