Ejemplo n.º 1
0
func TestStoringNewEvent(t *testing.T) {
	Convey("Given an event store", t, func() {
		dataStoreDirectory := "/tmp/hist-test-filestore-data"
		var eventStore hist.Eventstore
		var err error
		eventStore, err = FileStore(dataStoreDirectory)
		if err != nil {
			panic(err)
		}
		Convey("and an event which doesn't exist yet", func() {
			aggregateType := "TestSaveEventAggregate"
			id, _ := uuid.NewV4()
			aggregateID := id.String()
			data := []byte("Here's a test event.")
			Convey("when save event is called", func() {
				eventStore.Save(aggregateType, aggregateID, "EventType", data)
				Convey("then an aggregate  with the event exists.", func() {
					events, err := eventStore.Get(aggregateType, aggregateID)
					if err != nil {
						panic(err)
					}
					So(len(events) > 0, ShouldBeTrue)
				})
			})
		})
	})
}
Ejemplo n.º 2
0
func updatePortsOfCallHistory(shipName string, changes []interface{}) {
	var store hist.Eventstore
	var err error
	if useDynamo {
		store, err = dynamostore.DynamoStore(eventstoreTable, region, endpoint)
		failIf(err)
	} else {
		store, err = filestore.FileStore(dataStoreDirectory)
		failIf(err)
	}
	for _, event := range changes {
		jsonEvent, err := json.Marshal(event)
		failIf(err)
		store.Save("Ship", shipName, reflect.TypeOf(event).String(), []byte(jsonEvent))
	}
}
Ejemplo n.º 3
0
func TestSavingToMissingDataDirectory(t *testing.T) {
	Convey("Given an event store with a data directory that doesn't exist", t, func() {
		dataStoreDirectory := "/tmp/hist-filestore-test-saving-missing-directory"
		var eventStore hist.Eventstore
		var err error
		eventStore, err = FileStore(dataStoreDirectory)
		if err != nil {
			panic(err)
		}
		deleteAllData(dataStoreDirectory)
		Convey("saving an event should not succeed", func() {
			err = eventStore.Save("SomeAggregate", "12356", "EventType", []byte("event data."))
			So(err, ShouldNotBeNil)
			So(err.Error(), ShouldEqual, "No data directory")
		})
	})
}
Ejemplo n.º 4
0
func TestGettingMoreThanOneEvent(t *testing.T) {
	Convey("Given an event store", t, func() {
		dataStoreDirectory := "/tmp/hist-filestore-test-data"
		var eventStore hist.Eventstore
		var err error
		eventStore, err = FileStore(dataStoreDirectory)
		if err != nil {
			panic(err)
		}
		Convey("and three events", func() {
			expectedAggregateType := "TestGetMoreThanOneEventAggregate"
			id, _ := uuid.NewV4()
			aggregateID := id.String()
			data := []byte("{\"ShipID\":\"Seagull\",\"Location\":\"At Sea\"}")
			eventStore.Save(expectedAggregateType, aggregateID, "EventType", data)

			data = []byte("Second \n event.")
			eventStore.Save(expectedAggregateType, aggregateID, "EventType", data)

			data = []byte("Third event.")
			eventStore.Save(expectedAggregateType, aggregateID, "EventType", data)

			Convey("when get events is called", func() {
				Convey("then an aggregate  with three events is returned.", func() {
					events, err := eventStore.Get(expectedAggregateType, aggregateID)
					if err != nil {
						panic(err)
					}
					So(len(events), ShouldEqual, 3)
				})
			})
		})
	})
}
Ejemplo n.º 5
0
func TestGettingOneEvent(t *testing.T) {
	Convey("Given an event store", t, func() {
		dataStoreDirectory := "/tmp/hist-test-filestore-data"
		var eventStore hist.Eventstore
		var err error
		eventStore, err = FileStore(dataStoreDirectory)
		if err != nil {
			panic(err)
		}
		Convey("and an event", func() {
			expectedAggregateType := "TestGetEventAggregate"
			expectedEventType := "EventType"
			expectedEventData := "An event to get."
			id, _ := uuid.NewV4()
			aggregateID := id.String()
			data := []byte(expectedEventData)
			eventStore.Save(expectedAggregateType, aggregateID, expectedEventType, data)
			Convey("when get events is called", func() {
				Convey("then an aggregate  with the event is returned.", func() {
					events, err := eventStore.Get(expectedAggregateType, aggregateID)
					if err != nil {
						panic(err)
					}
					So(len(events), ShouldEqual, 1)
					event := events[0]
					Convey("and the type of the event is correct.", func() {
						So(event.Type, ShouldEqual, expectedEventType)
					})
					Convey("and the event data returned are correct.", func() {
						So(string(event.Data), ShouldEqual, expectedEventData)
					})
				})
			})
		})
	})
}
Ejemplo n.º 6
0
func TestMandatoryParameters(t *testing.T) {
	dataStoreDirectory := "/tmp/hist-test-filestore-data"
	var eventStore hist.Eventstore
	var err error
	eventStore, err = FileStore(dataStoreDirectory)
	if err != nil {
		panic(err)
	}
	Convey("When saving an event without an aggregate type", t, func() {
		err = eventStore.Save("", "id", "EventType", []byte("Event data."))
		Convey("then an error occurs.", func() {
			So(err.Error(), ShouldEqual, "aggregateType cannot be blank")
		})
	})
	Convey("When saving an event without an aggregate id", t, func() {
		err = eventStore.Save("SomeAggregate", "", "EventType", []byte("Event data."))
		Convey("then an error occurs.", func() {
			So(err.Error(), ShouldEqual, "aggregateID cannot be blank")
		})
	})
	Convey("When saving an event without an event type", t, func() {
		err = eventStore.Save("SomeAggregate", "1234", "", []byte("Event data."))
		Convey("then an error occurs.", func() {
			So(err.Error(), ShouldEqual, "eventType cannot be blank")
		})
	})
	Convey("When saving an event without any data", t, func() {
		aggregateType := "GravelAggregate"
		id, _ := uuid.NewV4()
		aggregateID := id.String()
		var data []byte
		err = eventStore.Save(aggregateType, aggregateID, "EventType", data)
		Convey("then an error occurs.", func() {
			So(err, ShouldNotBeNil)
		})
	})
}