Packages
commanded
0.14.0
1.4.10
1.4.9
1.4.8
1.4.7
1.4.6
1.4.3
1.4.2
1.4.1
1.4.0
1.4.0-rc.0
1.3.1
1.3.0
1.2.0
1.1.1
1.1.0
1.0.1
1.0.0
1.0.0-rc.1
1.0.0-rc.0
0.19.1
0.19.0
0.18.1
0.18.0
0.17.5
0.17.4
0.17.3
0.17.2
0.17.1
0.17.0
0.16.0
0.16.0-rc.1
0.16.0-rc.0
0.15.1
0.15.0
0.14.0
0.14.0-rc.0
0.13.0
0.12.0
0.11.0
0.10.0
0.9.0
0.8.5
0.8.4
0.8.3
0.8.1
0.8.0
0.7.1
0.6.2
0.6.1
0.6.0
0.4.0
0.3.1
0.3.0
0.2.1
0.2.0
0.1.0
Use Commanded to build your own Elixir applications following the CQRS/ES pattern.
Current section
Files
Jump to
Current section
Files
test/event_store_adapter/snapshot_test.exs
defmodule Commanded.EventStore.Adapter.SnapshotTest do
use Commanded.StorageCase
alias Commanded.EventStore
alias Commanded.EventStore.SnapshotData
defmodule BankAccountOpened, do: defstruct [:account_number, :initial_balance]
describe "record a snapshot" do
test "should record the snapshot" do
snapshot = build_snapshot_data(100)
assert :ok = EventStore.record_snapshot(snapshot)
end
end
describe "read a snapshot" do
test "should read the snapshot" do
snapshot1 = build_snapshot_data(100)
snapshot2 = build_snapshot_data(101)
snapshot3 = build_snapshot_data(102)
assert :ok == EventStore.record_snapshot(snapshot1)
assert :ok == EventStore.record_snapshot(snapshot2)
assert :ok == EventStore.record_snapshot(snapshot3)
{:ok, snapshot} = EventStore.read_snapshot(snapshot3.source_uuid)
assert build_snapshot_created_at_seconds_to_zero(snapshot) == build_snapshot_created_at_seconds_to_zero(snapshot3)
end
test "should error when snapshot does not exist" do
{:error, :snapshot_not_found} = EventStore.read_snapshot("doesnotexist")
end
end
describe "delete a snapshot" do
test "should delete the snapshot" do
snapshot1 = build_snapshot_data(100)
assert :ok == EventStore.record_snapshot(snapshot1)
{:ok, snapshot} = EventStore.read_snapshot(snapshot1.source_uuid)
assert build_snapshot_created_at_seconds_to_zero(snapshot) == build_snapshot_created_at_seconds_to_zero(snapshot1)
assert :ok == EventStore.delete_snapshot(snapshot1.source_uuid)
assert {:error, :snapshot_not_found} == EventStore.read_snapshot(snapshot1.source_uuid)
end
end
defp build_snapshot_data(account_number) do
%SnapshotData{
source_uuid: UUID.uuid4,
source_version: account_number,
source_type: "Elixir.Commanded.EventStore.Adapter.SnapshotTest.BankAccountOpened",
data: %BankAccountOpened{account_number: account_number, initial_balance: 1_000},
metadata: nil,
created_at: DateTime.to_naive(DateTime.utc_now())
}
end
defp build_snapshot_created_at_seconds_to_zero(snapshot) do
%NaiveDateTime{year: year, month: month, day: day, hour: hour, minute: min} = snapshot.created_at
{:ok, zeroed_created_at} = NaiveDateTime.new(year, month, day, hour, min, 0, 0)
%SnapshotData{snapshot | created_at: zeroed_created_at}
end
end