From e7e135d7ea5919938e2a53384c57b56ca5e5ec99 Mon Sep 17 00:00:00 2001 From: Johannes Loher Date: Wed, 27 Sep 2017 14:53:50 +0200 Subject: [PATCH] Added more tests for event.d --- dub.json | 10 ++--- test/calendarwebapp/testevent.d | 67 +++++++++++++++++++++++++++++++-- 2 files changed, 68 insertions(+), 9 deletions(-) diff --git a/dub.json b/dub.json index c8225ba..9276297 100644 --- a/dub.json +++ b/dub.json @@ -4,8 +4,8 @@ "Johannes Loher" ], "dependencies": { - "vibe-d": "0.8.1", - "poodinis": "8.0.1" + "vibe-d": "~>0.8.1", + "poodinis": "~>8.0.1" }, "description": "A simple webapplication to edit and view calendar entries", "copyright": "Copyright © 2017, Johannes Loher", @@ -26,11 +26,11 @@ "mainSourceFile": "generated/ut.d", "sourcePaths": ["test"], "dependencies": { - "unit-threaded": "0.7.30" - } + "unit-threaded": "~>0.7.31" + } } ], "versions": [ "VibeUseOpenSSL11" ] -} \ No newline at end of file +} diff --git a/test/calendarwebapp/testevent.d b/test/calendarwebapp/testevent.d index 2e4122c..3a1daf6 100644 --- a/test/calendarwebapp/testevent.d +++ b/test/calendarwebapp/testevent.d @@ -4,6 +4,9 @@ import calendarwebapp.event; import poodinis; +import std.array; +import std.algorithm : map; + import unit_threaded.mock; import unit_threaded.should; @@ -35,7 +38,7 @@ public: } } -@("Test failing getEventMongoDBEventStore.getEvent") +@("getEventMongoDBEventStore.getEvent failure") @system unittest { auto collection = mock!Collection; @@ -55,7 +58,7 @@ public: collection.verify; } -@("Test successful MongoDBEventStore.getEvent") +@("MongoDBEventStore.getEvent success") @system unittest { auto collection = mock!Collection; @@ -78,7 +81,7 @@ public: collection.verify; } -@("Test MongoDBEventStore.addEvent") +@("MongoDBEventStore.addEvent") @system unittest { auto collection = mock!Collection; @@ -93,7 +96,7 @@ public: event.id = id; auto serializedEvent = event.serializeToBson; - collection.returnValue!"findOne"(Bson(null), event.serializeToBson); + collection.returnValue!"findOne"(Bson(null), serializedEvent); collection.expect!"findOne"(["_id" : id]); collection.expect!"insert"(serializedEvent); @@ -107,3 +110,59 @@ public: collection.verify; } + +@("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); + + auto id = BsonObjectID.fromString("599090de97355141140fc698"); + 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 = [ + BsonObjectID.fromString("599090de97355141140fc698"), BsonObjectID.fromString("599090de97355141140fc698"), + BsonObjectID.fromString("59cb9ad8fc0ba5751c0df02b") + ]; + 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; +}