calendar-webapp/test/calendarwebapp/testevent.d

168 lines
5 KiB
D
Raw Normal View History

2017-09-17 17:52:41 +02:00
module test.calendarwebapp.testevent;
import calendarwebapp.event;
import poodinis;
2017-09-27 14:53:50 +02:00
import std.array;
import std.algorithm : map;
2017-09-17 17:52:41 +02:00
import unit_threaded.mock;
import unit_threaded.should;
2017-10-30 02:31:48 +01:00
import vibe.data.bson : Bson, serializeToBson;
2017-09-17 17:52:41 +02:00
interface Collection
{
2017-10-30 02:31:48 +01:00
Bson findOne(string[string] query) @safe;
2017-09-17 17:52:41 +02:00
Bson[] find() @safe;
Bson[] find(Bson[string][string][][string] query) @safe;
void insert(Bson document) @safe;
2017-10-30 02:31:48 +01:00
void remove(string[string] selector) @safe;
2017-09-17 17:52:41 +02:00
}
class CollectionInjector : ValueInjector!Collection
{
private:
Collection[string] collections;
public:
void add(string key, Collection collection)
{
collections[key] = collection;
}
override Collection get(string key) @safe
{
return collections[key];
}
}
2017-10-26 20:25:03 +02:00
@("MongoDBEventStore.getEvent failure")
2017-09-17 17:52:41 +02:00
@system unittest
{
auto collection = mock!Collection;
auto container = new shared DependencyContainer;
container.register!(ValueInjector!Collection, CollectionInjector);
container.resolve!CollectionInjector.add("events", collection);
container.register!(EventStore, MongoDBEventStore!(Collection))(
RegistrationOption.doNotAddConcreteTypeRegistration);
collection.returnValue!"findOne"(Bson(null));
2017-10-30 02:31:48 +01:00
auto id = "599090de97355141140fc698";
2017-09-17 17:52:41 +02:00
collection.expect!"findOne"(["_id" : id]);
auto eventStore = container.resolve!(EventStore);
eventStore.getEvent(id).shouldThrowWithMessage!Exception("Expected object instead of null_");
collection.verify;
}
2017-09-27 14:53:50 +02:00
@("MongoDBEventStore.getEvent success")
2017-09-17 17:52:41 +02:00
@system unittest
{
auto collection = mock!Collection;
auto container = new shared DependencyContainer;
container.register!(ValueInjector!Collection, CollectionInjector);
container.resolve!CollectionInjector.add("events", collection);
container.register!(EventStore, MongoDBEventStore!(Collection))(
RegistrationOption.doNotAddConcreteTypeRegistration);
2017-10-30 02:31:48 +01:00
auto id = "599090de97355141140fc698";
2017-09-17 17:52:41 +02:00
Event event;
event.id = id;
collection.returnValue!"findOne"(event.serializeToBson);
collection.expect!"findOne"(["_id" : id]);
auto eventStore = container.resolve!(EventStore);
eventStore.getEvent(id).shouldEqual(event);
collection.verify;
}
2017-09-27 14:53:50 +02:00
@("MongoDBEventStore.addEvent")
2017-09-17 17:52:41 +02:00
@system unittest
{
auto collection = mock!Collection;
auto container = new shared DependencyContainer;
container.register!(ValueInjector!Collection, CollectionInjector);
container.resolve!CollectionInjector.add("events", collection);
container.register!(EventStore, MongoDBEventStore!(Collection))(
RegistrationOption.doNotAddConcreteTypeRegistration);
2017-10-30 02:31:48 +01:00
auto id = "599090de97355141140fc698";
2017-09-17 17:52:41 +02:00
Event event;
event.id = id;
auto serializedEvent = event.serializeToBson;
2017-09-27 14:53:50 +02:00
collection.returnValue!"findOne"(Bson(null), serializedEvent);
2017-09-17 17:52:41 +02:00
collection.expect!"findOne"(["_id" : id]);
collection.expect!"insert"(serializedEvent);
collection.expect!"findOne"(["_id" : id]);
auto eventStore = container.resolve!(EventStore);
eventStore.getEvent(id).shouldThrowWithMessage!Exception("Expected object instead of null_");
eventStore.addEvent(event);
eventStore.getEvent(id).shouldEqual(event);
collection.verify;
}
2017-09-27 14:53:50 +02:00
@("MongoDBEventStore.removeEvent")
@system unittest
{
auto collection = mock!Collection;
auto container = new shared DependencyContainer;
container.register!(ValueInjector!Collection, CollectionInjector);
container.resolve!CollectionInjector.add("events", collection);
container.register!(EventStore, MongoDBEventStore!(Collection))(
RegistrationOption.doNotAddConcreteTypeRegistration);
2017-10-30 02:31:48 +01:00
auto id = "599090de97355141140fc698";
2017-09-27 14:53:50 +02:00
Event event;
event.id = id;
collection.returnValue!"findOne"(event.serializeToBson, Bson(null));
collection.expect!"findOne"(["_id" : id]);
collection.expect!"remove"(["_id" : id]);
collection.expect!"findOne"(["_id" : id]);
auto eventStore = container.resolve!(EventStore);
eventStore.getEvent(id).shouldEqual(event);
eventStore.removeEvent(event.id);
eventStore.getEvent(id).shouldThrowWithMessage!Exception("Expected object instead of null_");
collection.verify;
}
@("MongoDBEventStore.getAllEvents")
@system unittest
{
auto collection = mock!Collection;
auto container = new shared DependencyContainer;
container.register!(ValueInjector!Collection, CollectionInjector);
container.resolve!CollectionInjector.add("events", collection);
container.register!(EventStore, MongoDBEventStore!(Collection))(
RegistrationOption.doNotAddConcreteTypeRegistration);
immutable ids = [
2017-10-30 02:31:48 +01:00
"599090de97355141140fc698", "599090de97355141140fc698", "59cb9ad8fc0ba5751c0df02b"
2017-09-27 14:53:50 +02:00
];
auto events = ids.map!(id => Event(id)).array;
collection.returnValue!"find"(events.map!serializeToBson.array);
collection.expect!"find"();
auto eventStore = container.resolve!(EventStore);
eventStore.getAllEvents.array.shouldEqual(events);
collection.verify;
}