Skip to content
This repository has been archived by the owner on Jan 25, 2022. It is now read-only.

Commit

Permalink
Make sure we can sync on an empty remote collection.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rémy HUBSCHER committed Feb 10, 2016
1 parent c1d3156 commit a505431
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
19 changes: 16 additions & 3 deletions kinto_signer/tests/test_updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,18 @@ def test_collection_records_asks_storage_for_last_modified_records(self):
filters=[Filter('last_modified', 1234, COMPARISON.GT)])

def test_get_remote_last_modified(self):
headers = {'ETag': '"1234"'}
headers = {'ETag': '"1234"', 'Total-Records': '10'}
self.remote.session.request.return_value = (None, headers)
self.updater.get_remote_last_modified()
collection_timestamp, count = self.updater.get_remote_last_modified()
self.remote.session.request.assert_called_with(
'get', '/buckets/buck/collections/coll/records')
assert collection_timestamp == 1234
assert count == 10

def test_update_remote(self):
records = [{'id': idx, 'foo': 'bar %s' % idx} for idx in range(1, 3)]
self.updater.get_remote_last_modified = mock.MagicMock(
return_value=1234)
return_value=(1234, 2))
self.updater.get_collection_records = mock.MagicMock(
return_value=records)

Expand All @@ -83,6 +85,17 @@ def test_update_remote(self):
batch.__enter__().patch_collection.assert_called_with(
data={'signature': 'signature'})

def test_update_remote_on_empty_remote(self):
records = [{'id': idx, 'foo': 'bar %s' % idx} for idx in range(1, 3)]
self.updater.get_remote_last_modified = mock.MagicMock(
return_value=(1234, 0))
self.updater.get_collection_records = mock.MagicMock(
return_value=records)

self.updater.update_remote("hash", "signature")

self.updater.get_collection_records.assert_called_with(None)

def test_sign_and_update_remote(self):
records = [{'id': idx, 'foo': 'bar %s' % idx}
for idx in range(1, 3)]
Expand Down
10 changes: 8 additions & 2 deletions kinto_signer/updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,17 @@ def get_collection_records(self, last_modified=None):
def get_remote_last_modified(self):
endpoint = self.remote._get_endpoint('records')
# XXX Replace with a HEAD request.

_, headers = self.remote.session.request('get', endpoint)
return int(headers['ETag'].strip('"'))
collection_timestamp = int(headers['ETag'].strip('"'))
records_count = int(headers['Total-Records'])

return collection_timestamp, records_count

def update_remote(self, new_hash, signature):
last_modified = self.get_remote_last_modified()
last_modified, records_count = self.get_remote_last_modified()
if records_count == 0:
last_modified = None
new_records = self.get_collection_records(last_modified)

# Update the remote collection.
Expand Down

0 comments on commit a505431

Please sign in to comment.