public void DeterminesWhenAStoreWasRecentlyPersisted()
        {
            _cacheWithExpiration.Contains("11111-0").Returns(true);

            _calculator.WasPersistedRecently("11111-0").Should().BeTrue();
            _calculator.WasPersistedRecently("22222-0").Should().BeFalse();
        }
        public void Persist(IEnumerable <StoreInfo> storesEnumerableParam)
        {
            var allStores       = (storesEnumerableParam ?? new StoreInfo[] {}).ToArray();
            var storesToPersist = allStores
                                  .Where(_ => !_persistenceCalculator.WasPersistedRecently(_.StoreNumber))
                                  .ToArray();
            var numbersToPersist = storesToPersist.Select(_ => _.StoreNumber).ToArray();

            Logger.LogInfo("Saving Stores", "totalCount", allStores.Length, "count", numbersToPersist.Length);

            if (numbersToPersist.Length == 0)
            {
                return;
            }

            var numbersToUpdate = (_dataService.ContainsStore(numbersToPersist)).ToArray();
            var numbersToCreate = numbersToPersist.Except(numbersToUpdate).ToArray();

            var storesToUpdate = storesToPersist.Where(_ => numbersToUpdate.Contains(_.StoreNumber)).ToArray();
            var storesToCreate = storesToPersist.Where(_ => numbersToCreate.Contains(_.StoreNumber)).ToArray();

            Logger.LogDebug("Creating Stores", "count", storesToCreate.Length);
            _dataService.CreateNew(storesToCreate);

            Logger.LogDebug("Updating Stores", "count", storesToUpdate.Length);
            _dataService.Update(storesToUpdate);

            foreach (var storeNumber in numbersToPersist)
            {
                _persistenceCalculator.PreventFuturePersistence(storeNumber);
            }
        }
        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);
            }
        }