public void ShouldUseTheProjectionToGetTheLatestVersion() { //Given var events = new List <object>(); Guid id = Guid.NewGuid(); const string name = "test"; const string newName = "second"; var bus = new E05.Test.Infrastructure.Bus(); var eventsSerializer = EventsSerializer.GetEventSerializer(); var eventStore = new EventStore(bus, eventsSerializer); var commandHandler = new InventoryCommandHandler(bus, eventStore); var projection = new ItemsProjection(bus); bus.Send(new CreateInventoryItem(id, name)); var lastProjection = projection.GetById(id); //When bus.Send(new ModifyItemName(id, newName, lastProjection.Version)); //Then var items = projection.GetAll().ToList(); Assert.AreEqual(1, items.Count); var kvp = items[0]; Assert.AreEqual(id, kvp.Id); Assert.AreEqual(newName, kvp.Name); Assert.AreEqual(1, kvp.Version); }
public void ShouldSaveEventsOnMOdificationWithVersion() { //Given Guid id = Guid.NewGuid(); const string name = "test"; const string newName = "second"; var eventStore = new EventStore(); var target = new InventoryCommandHandler(eventStore); //When target.Handle(new CreateInventoryItem(id, name)); target.Handle(new ModifyItemName(id, newName, 0)); //Then Assert.AreEqual(2, eventStore.Events.Count); Assert.AreEqual(id, eventStore.Events[0].Id); Assert.AreEqual(0, eventStore.Events[0].Version); var inventoryItemCreated = eventStore.Events[0].Data as InventoryItemCreated; Assert.AreEqual(id, inventoryItemCreated.Id); Assert.AreEqual(name, inventoryItemCreated.Name); Assert.AreEqual(id, eventStore.Events[1].Id); Assert.AreEqual(1, eventStore.Events[1].Version); var itemNameModified = eventStore.Events[1].Data as ItemNameModified; Assert.AreEqual(id, itemNameModified.Id); Assert.AreEqual(newName, itemNameModified.NewName); }
public void ShouldSendDataViaBus() { //Given var events = new List <object>(); Guid id = Guid.NewGuid(); const string name = "test"; const string newName = "second"; var bus = new E05.Test.Infrastructure.Bus(); bus.AddListener(ob => { events.Add(ob); }); var eventStore = new EventStore(bus); var target = new InventoryCommandHandler(bus, eventStore); //When bus.Send(new CreateInventoryItem(id, name)); bus.Send(new ModifyItemName(id, newName, 0)); //Then Assert.AreEqual(4, events.Count); var inventoryItemCreated = events[1] as InventoryItemCreated; Assert.AreEqual(id, inventoryItemCreated.Id); Assert.AreEqual(name, inventoryItemCreated.Name); var itemNameModified = events[3] as ItemNameModified; Assert.AreEqual(id, itemNameModified.Id); Assert.AreEqual(newName, itemNameModified.NewName); }
public void ShouldThrowExceptionWithWrongVersion() { //Given Guid id = Guid.NewGuid(); const string name = "test"; const string newName = "second"; const int wrongExpectedVersion = 1; var eventStore = new EventStore(); var target = new InventoryCommandHandler(eventStore); //When target.Handle(new CreateInventoryItem(id, name)); Assert.ThrowsException <ConcurrencyException>(() => target.Handle(new ModifyItemName(id, newName, wrongExpectedVersion))); }
public void ShouldRehydrate() { //Given Guid id = Guid.NewGuid(); const string name = "test"; var target = new EventStore(); var commandHandler = new InventoryCommandHandler(target); commandHandler.Handle(new CreateInventoryItem(id, name)); //When var result = target.GetById <InventoryAggregateRoot>(id); //Then Assert.AreEqual(id, result.Id); }
public void ShouldGenerateEventsOnCreation() { //Given Guid id = Guid.NewGuid(); const string name = "test"; var target = new InventoryCommandHandler(); //When target.Handle(new CreateInventoryItem(id, name)); //Then Assert.AreEqual(1, target.Events.Count); Assert.AreEqual(id, target.Events[0].Id); var inventoryItemCreated = target.Events[0].Data as InventoryItemCreated; Assert.AreEqual(id, inventoryItemCreated.Id); Assert.AreEqual(name, inventoryItemCreated.Name); }
public void UnpredictableResult() { //Given Guid id = Guid.NewGuid(); const string name = "test"; var target = new EventStore(); var commandHandler = new InventoryCommandHandler(target); commandHandler.Handle(new CreateInventoryItem(id, name)); //When var user01Result = target.GetById <InventoryAggregateRoot>(id); var user02Result = target.GetById <InventoryAggregateRoot>(id); user02Result.ChangeName("02"); user01Result.ChangeName("01"); //Then Assert.Inconclusive("Who will be saved??"); }
public void ShouldGenerateSnapshot() { //Given var events = new List <object>(); Guid id = Guid.NewGuid(); const string name = "test"; var bus = new E05.Test.Infrastructure.Bus(); var snapshotStore = new SnapshotStore(); var eventsSerializer = EventsSerializer.GetEventSerializer(); var eventStore = new EventStore(bus, eventsSerializer, snapshotStore); var commandHandler = new InventoryCommandHandler(bus, eventStore); var projection = new ItemsProjection(bus); bus.Send(new CreateInventoryItem(id, name)); //When var i = 0; for (; i < 11; i++) { var lastProjection = projection.GetById(id); bus.Send(new ModifyItemName(id, "new" + i, lastProjection.Version)); } //Then var items = projection.GetAll().ToList(); Assert.AreEqual(1, items.Count); var kvp = items[0]; Assert.AreEqual(id, kvp.Id); Assert.AreEqual("new10", kvp.Name); Assert.AreEqual(11, kvp.Version); var snapshotData = snapshotStore.GetSnapshot(id); Assert.AreEqual(10, snapshotData.Version); var snapshot = JsonConvert.DeserializeObject <InventorySnapshot>(snapshotData.Data); Assert.AreEqual("new9", snapshot.Name); }