Esempio n. 1
0
        public void Load_with_empty_bin()
        {
            var es  = new InMemoryEventstore();
            var sut = new SelectDueCardContextManager(es);

            es.Record(new Event[] {
                new CardMovedTo {
                    CardId = "1", BinIndex = 1
                },
                new CardMovedTo {
                    CardId = "3", BinIndex = 3
                },
            });

            var result = sut.Load(new SelectDueCardCommand()).Ctx as SelectDueCardContextModel;

            result.Should().BeEquivalentTo(new SelectDueCardContextModel {
                Bins = new[] {
                    new string[0],
                    new[] { "1" },
                    new string[0],
                    new[] { "3" }
                },

                DueBinIndex = -1,

                Config = null
            });
        }
Esempio n. 2
0
        public static void Main(string[] args)
        {
            var es = new InMemoryEventstore();

            es.Record(new A());
            es.Record(new C());
            es.Record(new B());

            nsimpleeventstore.adapters.EventArchive.Write("myarchive.json", es.Replay().Events);

            var events = EventArchive.Read("myarchive.json");

            var es2 = new InMemoryEventstore(events);

            Console.WriteLine(es2.Replay().Events.Length);
        }
Esempio n. 3
0
        public void Load()
        {
            var es = new InMemoryEventstore();

            es.Record(new Event[] {
                new CardMovedTo {
                    CardId = "1", BinIndex = 0
                },
                new CardMovedTo {
                    CardId = "x", BinIndex = 1
                },
                new CardMovedTo {
                    CardId = "2", BinIndex = 2
                },
                new CardMovedTo {
                    CardId = "1", BinIndex = 1
                },
                new CardFoundMissing {
                    CardId = "x"
                }
            });

            var sut = new RegisterAnswerContextManager(es);

            var result = sut.Load(new RegisterAnswerCommand()).Ctx as RegisterAnswerContextModel;

            result.CardsInBins.Should().BeEquivalentTo(new Dictionary <string, int> {
                { "1", 1 },
                { "2", 2 }
            });
        }
Esempio n. 4
0
        public void Load_with_missing_due_card()
        {
            var es = new InMemoryEventstore();

            es.Record(new Event[] {
                new NewCardEncountered {
                    Question = "q3", Answer = "a3", Tags = "t3", Id = "3"
                },
                new CardMovedTo {
                    CardId = "3", BinIndex = 3
                },
                new DueCardSelected {
                    CardId = "3"
                },

                new CardFoundMissing {
                    CardId = "3"
                },
            });
            var sut = new DueCardContextManager(es);

            var result = sut.Load(new DueCardQuery()).Ctx as DueCardContextModel;

            result.DueCard.Should().BeNull();
        }
Esempio n. 5
0
        public void Load_with_no_due_card_because_it_was_moved()
        {
            var es = new InMemoryEventstore();

            es.Record(new Event[] {
                new NewCardEncountered {
                    Question = "q1", Answer = "a1", Tags = "t1", Id = "1"
                },
                new CardMovedTo {
                    CardId = "1", BinIndex = 1
                },
                new DueCardSelected {
                    CardId = "1"
                },

                new CardMovedTo {
                    CardId = "1", BinIndex = 2
                },
            });
            var sut = new DueCardContextManager(es);

            var result = sut.Load(new DueCardQuery()).Ctx as DueCardContextModel;

            result.DueCard.Should().BeNull();
        }
Esempio n. 6
0
        public void Load()
        {
            var es = new InMemoryEventstore();

            es.Record(new Event[] {
                new NewCardEncountered {
                    Question = "q1", Answer = "a1", Tags = "t1", Id = "1"
                },
                new CardMovedTo {
                    CardId = "1", BinIndex = 1
                },
                new DueCardSelected {
                    CardId = "1"
                },

                new NewCardEncountered {
                    Question = "q2", Answer = "a2", Tags = "t2", Id = "2"
                },
                new CardMovedTo {
                    CardId = "2", BinIndex = 2
                },
                new DueCardSelected {
                    CardId = "2"
                },

                new NewCardEncountered {
                    Question = "q3", Answer = "a3", Tags = "t3", Id = "3"
                },
                new CardMovedTo {
                    CardId = "3", BinIndex = 3
                },
                new DueCardSelected {
                    CardId = "3"
                },

                new CardFoundMissing {
                    CardId = "3"
                },

                new DueCardSelected {
                    CardId = "2"
                },

                new CardWasChanged {
                    CardId = "2", Question = "q2v2", Answer = "a2v2", Tags = "t2v2"
                },
            });
            var sut = new DueCardContextManager(es);

            var result = sut.Load(new DueCardQuery()).Ctx as DueCardContextModel;

            result.DueCard.Should().BeEquivalentTo(new DueCardFoundQueryResult {
                CardId   = "2",
                Question = "q2v2",
                Answer   = "a2v2",
                Tags     = "t2v2",
                BinIndex = 2
            });
        }
Esempio n. 7
0
        public void InMemoryEventstore_acceptance_test()
        {
            using (var sut = new InMemoryEventstore())
            {
                Event e0 = new TodoAdded("do dishes");
                sut.Record(e0);
                sut.Record(new TodoAdded("walk dog"));

                Event e2 = new TodoAdded("write report");
                sut.Record(new Event[] { e2, new TodoCategorized(e2.Id, "work") });
                sut.Record(new TodoDone(e0.Id));

                var result = sut.Replay();
                var todos  = result.Events.Aggregate(new Dictionary <string, ToDoItem>(), Map);

                Assert.Equal(2, todos.Count);
                Assert.Equal("write report", todos[e2.Id].Subject);
                Assert.Contains("work", todos[e2.Id].Categories);


                Dictionary <string, ToDoItem> Map(Dictionary <string, ToDoItem> items, Event e)
                {
                    switch (e)
                    {
                    case TodoAdded a:
                        items[a.Id] = new ToDoItem {
                            Id = a.Id, Subject = a.Subject
                        };
                        break;

                    case TodoDone d:
                        items.Remove(d.EntityId);
                        break;

                    case TodoCategorized c:
                        foreach (var cat in c.Categories)
                        {
                            items[c.EntityId].Categories.Add(cat);
                        }
                        break;
                    }

                    return(items);
                }
            }
        }
        public void Initialize_record_and_replay()
        {
            var sut = new InMemoryEventstore(new[]
            {
                new TestEvent {
                    Text = "foo"
                },
                new TestEvent {
                    Text = "bar"
                }
            });

            sut.Record(new TestEvent {
                Text = "baz"
            });

            var result = sut.Replay().Events.Select(e => (e as TestEvent).Text);

            Assert.Equal(new[] { "foo", "bar", "baz" }, result);
        }
Esempio n. 9
0
        public void Load_without_cards()
        {
            var es  = new InMemoryEventstore();
            var sut = new SelectDueCardContextManager(es);

            es.Record(new Event[] {
            });

            var result = sut.Load(new SelectDueCardCommand()).Ctx as SelectDueCardContextModel;

            result.Should().BeEquivalentTo(new SelectDueCardContextModel {
                Bins = new[] {
                    new string[0], // bin 0 and
                    new string[0]  // bin 1 always get created
                },

                DueBinIndex = -1,

                Config = null
            });
        }
        public void Load_message_context()
        {
            var es = new InMemoryEventstore();

            es.Record(new Event[]
            {
                new BoxConfigured {
                    Bins = new[] { new BoxConfigured.Bin {
                                       LowerDueThreshold = 1, UpperDueThreshold = 2
                                   } }
                },

                new NewCardEncountered {
                    Question = "q1", Answer = "a1", Tags = "t1", Id = "1"
                },
                new CardMovedTo {
                    CardId = "1", BinIndex = 0
                },
                new NewCardEncountered {
                    Question = "q2", Answer = "a2", Tags = "t2,t3", Id = "2"
                },
                new CardMovedTo {
                    CardId = "2", BinIndex = 3
                },
                new NewCardEncountered {
                    Question = "q3", Answer = "a3", Tags = "", Id = "3"
                },
                new CardWasChanged {
                    Question = "q1v2", Answer = "a1v2", Tags = "t1", CardId = "1"
                },
                new CardMovedTo {
                    CardId = "1", BinIndex = 2
                },
                new CardFoundMissing {
                    CardId = "3"
                },

                new BoxConfigured {
                    Bins = new[] { new BoxConfigured.Bin {
                                       LowerDueThreshold = 10, UpperDueThreshold = 20
                                   } }
                }
            });
            var sut = new SyncContextManagement(es);


            var result = sut.Load(new SyncCommand());


            var ctxModel = result.Ctx as SyncContextModel;

            ctxModel.Config.Should().BeEquivalentTo(new FlashcardboxConfig {
                Bins = new[] { new FlashcardboxConfig.Bin {
                                   LowerDueThreshold = 10,
                                   UpperDueThreshold = 20
                               } }
            });

            ctxModel.Flashcards.Should().BeEquivalentTo(new Dictionary <string, (string binIndex, string hash)> {
                { "1", ("2", FlashcardHash.Calculate("q1v2", "a1v2", "t1")) },
                { "2", ("3", FlashcardHash.Calculate("q2", "a2", "t2,t3")) }
            });
Esempio n. 11
0
        public void Load()
        {
            var es  = new InMemoryEventstore();
            var sut = new SelectDueCardContextManager(es);

            es.Record(new Event[] {
                new BoxConfigured {
                    Bins = new[] {
                        new BoxConfigured.Bin {
                            LowerDueThreshold = 10,
                            UpperDueThreshold = 20
                        },
                    }
                },

                new CardMovedTo {
                    CardId = "1", BinIndex = 1
                },
                new CardMovedTo {
                    CardId = "2.1", BinIndex = 2
                },
                new CardMovedTo {
                    CardId = "2.2", BinIndex = 1
                },
                new DueCardSelected {
                    CardId = "2.2", BinIndex = 1
                },

                new CardMovedTo {
                    CardId = "3.1", BinIndex = 3
                },
                new CardMovedTo {
                    CardId = "3.2", BinIndex = 3
                },
                new CardMovedTo {
                    CardId = "3.3", BinIndex = 1
                },
                new CardMovedTo {
                    CardId = "3.x", BinIndex = 3
                },

                new CardMovedTo {
                    CardId = "2.2", BinIndex = 2
                },

                new CardFoundMissing {
                    CardId = "3.x"
                },
                new CardMovedTo {
                    CardId = "3.3", BinIndex = 3
                },
                new DueCardSelected {
                    CardId = "3.3", BinIndex = 3
                }
            });

            var result = sut.Load(new SelectDueCardCommand()).Ctx as SelectDueCardContextModel;

            result.Should().BeEquivalentTo(new SelectDueCardContextModel {
                Bins = new[] {
                    new string[0],
                    new[] { "1" },
                    new[] { "2.1", "2.2" },
                    new[] { "3.1", "3.2", "3.3" }
                },

                DueBinIndex = 3,

                Config = new FlashcardboxConfig {
                    Bins = new [] {
                        new FlashcardboxConfig.Bin {
                            LowerDueThreshold = 10,
                            UpperDueThreshold = 20
                        }
                    }
                }
            });
        }