Esempio n. 1
0
        public void DownloadsAndPersistsTheStoreData()
        {
            var stores = StoreNumberFactory
                         .Create(10)
                         .Select(StoreInfoFactory.Create)
                         .ToArray();

            _downloader.Download(Arg.Any <ZipCode>())
            .Returns(stores);


            _processor.Process(_zipCode);


            _downloader
            .Received(1)
            .Download(_zipCode);

            _persistor
            .Received(1)
            .Persist(Arg.Is <IEnumerable <StoreInfo> >(_ => _.SequenceEqual(stores)));

            _zipCodeDataService
            .Received(1)
            .UpdateZipCode("55555");

            _deadlockDetector
            .Received(1)
            .UpdateStatus();
        }
        public void UpdatesExistingResponses()
        {
            CreatePersistenceInitializer().Initialize();

            var seededResponses = StoreNumberFactory
                                  .Create(50)
                                  .Select(StoreInfoFactory.Create)
                                  .ToArray();

            _dataService.CreateNew(seededResponses);
            ContainsAllStores(seededResponses).Should().BeTrue();

            var originalResponses = seededResponses.Take(5);


            var updatedResponses = originalResponses
                                   .Select(CreateUpdatedResponse)
                                   .ToArray();

            _dataService.Update(updatedResponses);
            ContainsAllStores(updatedResponses).Should().BeTrue();


            using (var context = ContextFactory.Create())
            {
                context.Stores.Count().Should().Be(50);
                foreach (var r in updatedResponses)
                {
                    context.ShouldContainStoreEquivalentTo(r);
                }
            }

            _dataService.Update(new StoreInfo[] { });
        }
        public void DoesNotDoResourceIntensiveOperationsWhenNoStoreShouldBePersisted()
        {
            var allStoreNumbers = StoreNumberFactory.Create(10).ToArray();
            var stores          = allStoreNumbers.Select(StoreInfoFactory.Create).ToArray();

            foreach (var storeNumber in allStoreNumbers)
            {
                _persistenceCalculator.WasPersistedRecently(storeNumber).Returns(true);
            }

            _storesPersistor.Persist(stores);

            _persistenceCalculator.DidNotReceiveWithAnyArgs().PreventFuturePersistence(Arg.Any <StoreNumber>());
            _dataService.DidNotReceiveWithAnyArgs().ContainsStore(Arg.Any <IEnumerable <StoreNumber> >());
            _dataService.DidNotReceiveWithAnyArgs().CreateNew(Arg.Any <IEnumerable <StoreInfo> >());
            _dataService.DidNotReceiveWithAnyArgs().Update(Arg.Any <IEnumerable <StoreInfo> >());
        }
        public void PersistsMultipleStores()
        {
            var allStoreNumbers = StoreNumberFactory.Create(10).ToArray();
            var stores          = allStoreNumbers.Select(StoreInfoFactory.Create).ToArray();

            var recentlyPersistedNumbers = new[] { allStoreNumbers[1], allStoreNumbers[3] };

            foreach (var storeNumber in recentlyPersistedNumbers)
            {
                _persistenceCalculator.WasPersistedRecently(storeNumber).Returns(true);
            }

            var numbersToPersist           = allStoreNumbers.Except(recentlyPersistedNumbers).ToArray();
            var numbersThatNeedToBeUpdated = new[] { numbersToPersist[0], numbersToPersist[1], numbersToPersist[2] };

            _dataService.ContainsStore(Arg.Is <IEnumerable <StoreNumber> >(_ => _.SequenceEqual(numbersToPersist)))
            .Returns(numbersThatNeedToBeUpdated);

            var numbersThatNeedToBeCreated = numbersToPersist.Except(numbersThatNeedToBeUpdated).ToArray();


            _storesPersistor.Persist(stores);


            _dataService.Received(1)
            .ContainsStore(Arg.Is <IEnumerable <StoreNumber> >(_ => _.SequenceEqual(numbersToPersist)));
            _dataService.Received(1)
            .Update(Arg.Is <IEnumerable <StoreInfo> >(_ => _.Select(s => s.StoreNumber).SequenceEqual(numbersThatNeedToBeUpdated)));
            _dataService.Received(1)
            .CreateNew(Arg.Is <IEnumerable <StoreInfo> >(_ => _.Select(s => s.StoreNumber).SequenceEqual(numbersThatNeedToBeCreated)));

            foreach (var storeNumber in allStoreNumbers)
            {
                _persistenceCalculator.Received(1).WasPersistedRecently(storeNumber);
            }
            foreach (var storeNumber in numbersToPersist)
            {
                _persistenceCalculator.Received(1).PreventFuturePersistence(storeNumber);
            }
            foreach (var storeNumber in recentlyPersistedNumbers)
            {
                _persistenceCalculator.DidNotReceive().PreventFuturePersistence(storeNumber);
            }
        }
        public void SavesNewResponses()
        {
            CreatePersistenceInitializer().Initialize();

            var storeNumbers = StoreNumberFactory
                               .Create(5)
                               .ToArray();
            var responses = storeNumbers
                            .Select(StoreInfoFactory.Create)
                            .ToArray();

            StoresTableShouldBeEmpty();

            _dataService.CreateNew(new StoreInfo[] { });

            StoresTableShouldBeEmpty();

            _dataService.CreateNew(responses);

            using (var context = ContextFactory.Create())
            {
                context.Stores.Count().Should().Be(responses.Length);

                foreach (var r in responses)
                {
                    context.ShouldContainStoreEquivalentTo(r);
                }
            }

            var foundStoreNumbers = (_dataService.ContainsStore(storeNumbers)).ToArray();

            foundStoreNumbers.Should().BeEquivalentTo(storeNumbers);

            var searchedNumbers = new List <StoreNumber>(storeNumbers);

            searchedNumbers.AddRange(new StoreNumber[] { "7777-3", "99999-0" });
            foundStoreNumbers = (_dataService.ContainsStore(searchedNumbers)).ToArray();
            foundStoreNumbers.Should().BeEquivalentTo(storeNumbers);

            _dataService.ContainsStore(new StoreNumber[] { "7777-3" }).Should().BeEmpty();

            _dataService.ContainsStore(new StoreNumber[] { }).Should().BeEmpty();
        }