Skip to content

Commit 8b1640b

Browse files
author
mstampfer
committed
Snapshot specific symbol versions - Tests
1 parent 01e9985 commit 8b1640b

File tree

3 files changed

+64
-1
lines changed

3 files changed

+64
-1
lines changed

arctic/store/version_store.py

+2
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,8 @@ def snapshot(self, snap_name, metadata=None, skip_symbols=None, versions=None):
678678
an optional dictionary of metadata to persist along with the symbol.
679679
skip_symbols : `collections.Iterable`
680680
optional symbols to be excluded from the snapshot
681+
versions: `dict`
682+
an optional dictionary of versions of the symbols to be snapshot
681683
"""
682684
# Ensure the user doesn't insert duplicates
683685
snapshot = self._snapshots.find_one({'name': snap_name})

tests/integration/store/test_version_store.py

+40
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,46 @@ def test_snapshot(library):
491491
assert versions[2]['snapshots'] == ['current']
492492

493493

494+
def test_snapshot_with_versions(library):
495+
""" Test snapshot of write versions consistency """
496+
library.write(symbol, ts1)
497+
library.write(symbol, ts2)
498+
499+
# ensure snapshot of previous version is taken
500+
library.snapshot('previous', versions={symbol: 1})
501+
versions = library.list_versions(symbol)
502+
assert versions[0]['snapshots'] == []
503+
assert versions[1]['snapshots'] == ['previous']
504+
assert_frame_equal(library.read(symbol, as_of='previous').data, ts1)
505+
506+
# ensure new snapshots are ordered after previous ones
507+
library.snapshot('new')
508+
versions = library.list_versions(symbol)
509+
assert versions[0]['snapshots'] == ['new']
510+
assert versions[0]['version'] == 2
511+
assert_frame_equal(library.read(symbol, as_of='new').data, ts2)
512+
513+
assert versions[1]['snapshots'] == ['previous']
514+
assert versions[1]['version'] == 1
515+
assert_frame_equal(library.read(symbol, as_of='previous').data, ts1)
516+
517+
# ensure snapshot of previous version doesn't overwrite current version
518+
library.write(symbol, ts1, prune_previous_version=True)
519+
library.snapshot('another', versions={symbol: 1})
520+
versions = library.list_versions(symbol)
521+
522+
assert versions[0]['snapshots'] == []
523+
assert versions[0]['version'] == 3
524+
assert_frame_equal(library.read(symbol).data, ts1)
525+
526+
assert versions[1]['snapshots'] == ['new']
527+
assert versions[1]['version'] == 2
528+
529+
assert versions[2]['snapshots'] == ['previous', 'another']
530+
assert versions[2]['version'] == 1
531+
assert_frame_equal(library.read(symbol, as_of='another').data, ts1)
532+
533+
494534
def test_snapshot_exclusion(library):
495535
library.write(symbol, ts1)
496536
library.snapshot('current', skip_symbols=[symbol])

tests/unit/store/test_version_store.py

+22-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from arctic.store import version_store
1212
from arctic.store.version_store import VersionStore, VersionedItem
1313
from arctic.arctic import ArcticLibraryBinding, Arctic
14-
from arctic.exceptions import ConcurrentModificationException
14+
from arctic.exceptions import ConcurrentModificationException, DuplicateSnapshotException
1515
from pymongo.errors import OperationFailure
1616
from pymongo.collection import Collection
1717

@@ -202,3 +202,24 @@ def test_read_reports_random_errors():
202202
VersionStore.read(self, sentinel.symbol, sentinel.as_of, sentinel.from_version)
203203
assert 'bad' in str(e)
204204
assert le.call_count == 1
205+
206+
207+
def test_snapshot():
208+
vs = create_autospec(VersionStore, _snapshots=Mock(),
209+
_collection=Mock(),
210+
_versions=Mock())
211+
vs._snapshots.find_one.return_value = False
212+
vs._versions.update_one.__name__ = 'name'
213+
vs._snapshots.insert_one.__name__ = 'name'
214+
vs.list_symbols.return_value = ['foo', 'bar']
215+
VersionStore.snapshot(vs, "symbol")
216+
assert vs._read_metadata.call_args_list == [call('foo', as_of=None, read_preference=ReadPreference.PRIMARY),
217+
call('bar', as_of=None, read_preference=ReadPreference.PRIMARY)]
218+
219+
220+
def test_snapshot_duplicate_raises_exception():
221+
vs = create_autospec(VersionStore, _snapshots=Mock())
222+
with pytest.raises(DuplicateSnapshotException) as e:
223+
vs._snapshots.find_one.return_value = True
224+
VersionStore.snapshot(vs, 'symbol')
225+
assert "Snapshot 'symbol' already exists" in str(e.value)

0 commit comments

Comments
 (0)