diff --git a/source/calendarwebapp/jsonexport.d b/source/calendarwebapp/jsonexport.d index 21540ee..274fffd 100644 --- a/source/calendarwebapp/jsonexport.d +++ b/source/calendarwebapp/jsonexport.d @@ -3,10 +3,14 @@ module calendarwebapp.jsonexport; import calendarwebapp.event : Event, EventStore; import core.time; + +import std.algorithm.iteration : each; + import std.datetime.date; import std.datetime.interval; import std.datetime.systime; +import std.format : format; import poodinis : Autowire; import vibe.data.serialization : name; @@ -19,19 +23,23 @@ private: public: this(in Date begin, in Date end) + in + { + assert(begin < end, + "DayJSONManager: begin (%s) needs to be earlier than end (%s)".format(begin, end)); + } + do { this.begin = begin; this.end = end; + Interval!Date(this.begin, this.end).fwdRange(date => date + 1.dur!"days") + .each!(date => events[date] = []); } void addEvent(Event event) { if (Interval!Date(begin, end).contains(event.begin)) { - if (event.end.isNull) - { - event.end = event.begin; - } events[event.begin] ~= event; } } @@ -75,6 +83,25 @@ public: } } +struct DayData +{ + short year; + Month month; + string monthName; + ubyte day; + @name("daytype") DayType dayType; + Event[] eventList; + @name("wday") string weekDayName; + Line[] lines; +} + +enum DayType +{ + Workday, + Holiday, + Weekend +} + private: string toGerString(Month m) @@ -129,13 +156,6 @@ string toGerString(DayOfWeek d) } } -enum DayType -{ - Workday, - Holiday, - Weekend -} - DayType dayType(DayOfWeek dayOfWeek) { switch (dayOfWeek) with (DayOfWeek) @@ -149,18 +169,6 @@ DayType dayType(DayOfWeek dayOfWeek) } } -struct DayData -{ - short year; - Month month; - string monthName; - ubyte day; - @name("daytype") DayType dayType; - Event[] eventList; - @name("wday") string weekDayName; - Line[] lines; -} - struct Line { } diff --git a/test/calendarwebapp/testjsonexport.d b/test/calendarwebapp/testjsonexport.d index 35aa00f..1bfe2f7 100644 --- a/test/calendarwebapp/testjsonexport.d +++ b/test/calendarwebapp/testjsonexport.d @@ -5,17 +5,133 @@ import calendarwebapp.jsonexport; import poodinis; -import std.algorithm; -import std.datetime.date : Date; +import core.exception : AssertError; + +import std.algorithm.iteration : each; +import std.datetime.date : Date, Month; import std.exception : enforce; import std.range.interfaces : InputRange, inputRangeObject; +import std.range.primitives : empty; import unit_threaded; -@("calendarwebapp.JSONExporter") +@("JSONExporter.empty") @system unittest { auto container = new shared DependencyContainer(); container.register!(EventStore, StubEventStore); - container.register!(JSONExporter); + container.register!JSONExporter; + auto exporter = container.resolve!JSONExporter; + exporter.write.each!(dayData => dayData.eventList.empty.shouldBeTrue); +} + +@("JSONExporter.1 event") +@system unittest +{ + auto container = new shared DependencyContainer(); + container.register!(EventStore, StubEventStore); + container.register!JSONExporter; + auto exporter = container.resolve!JSONExporter; + auto eventStore = container.resolve!EventStore; + immutable event = Event("599090de97355141140fc698", Date(2018, 1, 14)); + eventStore.addEvent(event); + exporter.write.each!(dayData => (dayData.year == 2018 + && dayData.month == Month.jan && dayData.day == 14) ? dayData.eventList.shouldEqual([event]) + : dayData.eventList.empty.shouldBeTrue); +} + +@("DayJSONManager with begin > end") +@system unittest +{ + DayJSONManager(Date(2018, 1, 14), Date(2018, 1, 13)).shouldThrow!AssertError; +} + +@("DayJSONManager with begin = end") +@system unittest +{ + DayJSONManager(Date(2018, 1, 14), Date(2018, 1, 14)).shouldThrow!AssertError; +} + +@("DayJSONManager.getDayData with date < begin and 0 events") +@system unittest +{ + auto dayJSONManager = DayJSONManager(Date(2018, 1, 14), Date(2018, 1, 16)); + dayJSONManager.getDayData(Date(2018, 1, 13)).shouldThrow; +} + +@("DayJSONManager.getDayData with date > end and 0 events") +@system unittest +{ + auto dayJSONManager = DayJSONManager(Date(2018, 1, 14), Date(2018, 1, 16)); + dayJSONManager.getDayData(Date(2018, 1, 17)).shouldThrow; +} + +@("DayJSONManager.getDayData with date = end and 0 events") +@system unittest +{ + auto dayJSONManager = DayJSONManager(Date(2018, 1, 14), Date(2018, 1, 16)); + dayJSONManager.getDayData(Date(2018, 1, 16)).shouldThrow; +} + +@("DayJSONManager.getDayData with date = begin and 0 events") +@system unittest +{ + auto dayJSONManager = DayJSONManager(Date(2018, 1, 14), Date(2018, 1, 16)); + dayJSONManager.getDayData(Date(2018, 1, 14)).shouldEqual(DayData(2018, + Month.jan, "Januar", 14, DayType.Holiday, [], "Sonntag", [])); +} + +@("DayJSONManager.getDayData with begin < date < end and 0 events") +@system unittest +{ + auto dayJSONManager = DayJSONManager(Date(2018, 1, 14), Date(2018, 1, 16)); + dayJSONManager.getDayData(Date(2018, 1, 15)).shouldEqual(DayData(2018, + Month.jan, "Januar", 15, DayType.Workday, [], "Montag", [])); +} + +@("DayJSONManager.getDayData with date < begin and 1 event") +@system unittest +{ + auto dayJSONManager = DayJSONManager(Date(2018, 1, 14), Date(2018, 1, 16)); + immutable event = Event("599090de97355141140fc698", Date(2018, 1, 14)); + dayJSONManager.addEvent(event); + dayJSONManager.getDayData(Date(2018, 1, 13)).shouldThrow; +} + +@("DayJSONManager.getDayData with date > end and 1 event") +@system unittest +{ + auto dayJSONManager = DayJSONManager(Date(2018, 1, 14), Date(2018, 1, 16)); + immutable event = Event("599090de97355141140fc698", Date(2018, 1, 14)); + dayJSONManager.addEvent(event); + dayJSONManager.getDayData(Date(2018, 1, 17)).shouldThrow; +} + +@("DayJSONManager.getDayData with date = end and 1 event") +@system unittest +{ + auto dayJSONManager = DayJSONManager(Date(2018, 1, 14), Date(2018, 1, 16)); + immutable event = Event("599090de97355141140fc698", Date(2018, 1, 14)); + dayJSONManager.addEvent(event); + dayJSONManager.getDayData(Date(2018, 1, 16)).shouldThrow; +} + +@("DayJSONManager.getDayData with date = begin and 1 event") +@system unittest +{ + auto dayJSONManager = DayJSONManager(Date(2018, 1, 14), Date(2018, 1, 15)); + immutable event = Event("599090de97355141140fc698", Date(2018, 1, 14)); + dayJSONManager.addEvent(event); + dayJSONManager.getDayData(Date(2018, 1, 14)).shouldEqual(DayData(2018, + Month.jan, "Januar", 14, DayType.Holiday, [event], "Sonntag", [])); +} + +@("DayJSONManager.getDayData with begin < date < end and 1 event") +@system unittest +{ + auto dayJSONManager = DayJSONManager(Date(2018, 1, 14), Date(2018, 1, 16)); + immutable event = Event("599090de97355141140fc698", Date(2018, 1, 15)); + dayJSONManager.addEvent(event); + dayJSONManager.getDayData(Date(2018, 1, 15)).shouldEqual(DayData(2018, + Month.jan, "Januar", 15, DayType.Workday, [event], "Montag", [])); }