Пример #1
0
        public void cache_should_save_data()
        {
            var cache = new RepositoryCache();
            var aggregate = User.CreateUser(FakeUser.TestLogin, FakeUser.TestPassword, new IdentityGenerator());

            cache.Add(aggregate);
            cache.Get(aggregate.Id).Should().Be(aggregate);
        }
Пример #2
0
        public void cache_should_save_multiple_data()
        {
            var cache = new RepositoryCache();

            IAggregate aggregate = User.CreateUser(FakeUser.TestLogin, FakeUser.TestPassword, new IdentityGenerator());
            cache.Add(aggregate);

            aggregate = Competence.Create("test", new IdentityGenerator());
            cache.Add(aggregate);

            cache.Get(aggregate.Id).Should().Be(aggregate);
        }
        public void initial_builder_should_fail_if_no_aggregate_found()
        {
            var employee = FakeEmployee.ArrangeEmployee(2);
            var newCompetences = FakeEmployee.ArrangeCompetencies(3);
            employee.AddCompetences(newCompetences);

            var events = ((IAggregate)employee).GetUncommittedEvents().ToList();

            var cache = new RepositoryCache();
            IEventHandler<DomainEvent> eventHandler = new InitialAggregateBuilder(new AggregateFactory(), cache);

            var firstCompetenceChange = events[1];
            eventHandler.Invoking(i => i.Handle(firstCompetenceChange)).ShouldThrow<AggregateVersionException>();
        }
Пример #4
0
        public void cache_should_not_allow_duplicate_add()
        {
            var id = Guid.NewGuid();
            var generator = Substitute.For<IIdentityGenerator>();
            generator.NewId().Returns(id);

            var cache = new RepositoryCache();

            var aggregate = User.CreateUser(FakeUser.TestLogin, FakeUser.TestPassword, generator);
            cache.Add(aggregate);

            aggregate = User.CreateUser(FakeUser.TestLogin, FakeUser.TestPassword, generator);
            cache.Invoking(c => c.Add(aggregate)).ShouldThrow<AggregateVersionException>();
        }
        public void initial_builder_should_handle_creation_event()
        {
            var cache = new RepositoryCache();
            IEventHandler<DomainEvent> eventHandler = new InitialAggregateBuilder(new AggregateFactory(), cache);

            for (var i = 0; i < 3; i++)
            {
                var userCreated = FakeUser.ArrangeCreated();
                eventHandler.Handle(userCreated);

                var user = (User)cache.Get(userCreated.AggregateId);

                user.Should().NotBeNull();
                FakeUser.AssertUserCreated(user);
            }
        }
        public void initial_builder_should_fail_for_creation_event_for_existing_aggregate()
        {
            var employee = FakeEmployee.ArrangeEmployee(2);
            var newCompetences = FakeEmployee.ArrangeCompetencies(3);
            employee.AddCompetences(newCompetences);

            var events = ((IAggregate)employee).GetUncommittedEvents().ToList();

            var cache = new RepositoryCache();
            IEventHandler<DomainEvent> eventHandler = new InitialAggregateBuilder(new AggregateFactory(), cache);

            cache.Add(employee);

            var creationEvent = events[0];
            eventHandler.Invoking(i => i.Handle(creationEvent)).ShouldNotThrow<AggregateVersionException>();
        }
        public void initial_builder_should_handle_change_event()
        {
            var cache = new RepositoryCache();

            IEventHandler<DomainEvent> eventHandler = new InitialAggregateBuilder(new AggregateFactory(), cache);

            var created = FakeEmployee.ArrangeCreated();
            var competencies = FakeEmployee.ArrangeCompetenciesChanged(created.AggregateId);

            eventHandler.Handle(created);
            eventHandler.Handle(competencies);

            var employee = cache.Get(created.AggregateId).As<Employee>();
            employee.Should().NotBeNull();
            employee.Id.Should().Be(created.AggregateId);
            employee.Competencies.Select(i => i.CompetenceId).Should().BeEquivalentTo(competencies.Competencies.Select(i => i.CompetenceId));
        }
        public void repository_should_save_user()
        {
            var eventStore = Substitute.For<IEventStore>();
            var cache = new RepositoryCache();

            StoreRepository repository = new StoreRepository(eventStore, cache, new AggregateFactory());

            IEnumerable<DomainEvent> eventsToSave = null;
            eventStore.WhenForAnyArgs(i => i.SaveEvents(null, Guid.Empty)).Do(i => eventsToSave = i.Arg<IEnumerable<DomainEvent>>());

            var user = User.CreateUser(FakeUser.TestLogin, FakeUser.TestPassword, new IdentityGenerator());
            repository.Save(user, Guid.NewGuid());

            cache.Get(user.Id).Should().Be(user);

            var userCreated = eventsToSave.First().As<EventStore.Messages.UserEvents.Created>();

            userCreated.Should().NotBeNull();
            userCreated.Login.Should().Be(FakeUser.TestLogin);
        }
        public void repository_shouldnt_save_users_with_same_id()
        {
            var eventStore = Substitute.For<IEventStore>();
            var identityGenerator = Substitute.For<IIdentityGenerator>();

            var guid = Guid.NewGuid();
            identityGenerator.NewId().Returns(i => guid);

            var cache = new RepositoryCache();

            StoreRepository repository = new StoreRepository(eventStore, cache, new AggregateFactory());

            var @event = FakeUser.ArrangeCreated();

            var user = User.CreateUser(FakeUser.TestLogin, FakeUser.TestPassword, identityGenerator);
            repository.Save(user, Guid.NewGuid());

            user = User.CreateUser(FakeUser.TestLogin, FakeUser.TestPassword, identityGenerator);
            repository.Invoking(i => i.Save(user, Guid.NewGuid())).ShouldThrow<AggregateVersionException>();
        }
        public void initial_builder_should_handle_two_users_created_event()
        {
            var cache = new RepositoryCache();
            IEventHandler<DomainEvent> eventHandler = new InitialAggregateBuilder(new AggregateFactory(), cache);

            var userCreated = FakeUser.ArrangeCreated();
            var secondCreated = FakeUser.ArrangeCreated(2);

            eventHandler.Handle(userCreated);
            eventHandler.Handle(secondCreated);

            var users = cache.GetAll().ToList();
            users.Count.Should().Be(2);

            FakeUser.AssertUserCreated((User)cache.Get(userCreated.AggregateId));
            users[0].Id.Should().NotBe(users[1].Id);
        }
        public void initial_builder_should_skip_uncommited_events()
        {
            var employee = FakeEmployee.ArrangeEmployee(2);
            var newCompetences = FakeEmployee.ArrangeCompetencies(3);
            employee.AddCompetences(newCompetences);

            var events = ((IAggregate)employee).GetUncommittedEvents().ToList();

            var cache = new RepositoryCache();
            IEventHandler<DomainEvent> eventHandler = new InitialAggregateBuilder(new AggregateFactory(), cache);

            cache.Add(employee);

            var firstCompetenceChange = events[1];
            eventHandler.Handle(firstCompetenceChange);

            employee.Competencies.Count.Should().Be(5);
        }
Пример #12
0
 public void cache_should_return_null_if_nothing_found()
 {
     var cache = new RepositoryCache();
     var notExistingId = Guid.NewGuid();
     cache.Get(notExistingId).Should().BeNull();
 }