public void GivenTheCacheIsInitialisingThenTheRepositoryIsRead()
        {
            var mock = new Mock <IRepository>();
            var sut  = new WordSaltCache();

            sut.Init(mock.Object);

            mock.Verify(r => r.Get <WordMetric>(null, null, null), Times.Once);
        }
        public void AKeyCanBeAddedToTheCache()
        {
            var key  = Root.Any.String();
            var salt = Root.Any.String();
            var mock = new Mock <IRepository>();
            var sut  = new WordSaltCache();

            sut.Init(mock.Object);

            sut.TryAdd(key, salt);

            Assert.AreEqual(salt, sut.Get(key));
        }
        public void GivenAKeyExistsInTheCacheAGetWillReturnTheSalt()
        {
            var key  = Root.Any.String();
            var salt = Root.Any.String();

            var mock = new Mock <IRepository>();

            mock.Setup(r => r.Get <WordMetric>(null, null, null)).Returns(new[]
            {
                new WordMetric(Root.Any.String(), key, Root.Any.Integer(), salt)
            });

            var sut = new WordSaltCache();

            sut.Init(mock.Object);

            var result = sut.Get(key);

            Assert.AreEqual(salt, result);
        }
Example #4
0
        private static void DBInteraction()
        {
            // Initial read
            using (var context = new CountVonCountDbContext())
            {
                var persist = new EntityFrameworkRepository <CountVonCountDbContext>(context);

                Console.WriteLine(@"Word Metrics");
                foreach (var result in persist.Get <WordMetric>())
                {
                    Console.WriteLine(result);
                }

                Console.WriteLine(@"Tmp Metrics");
                foreach (var wordMetric in persist.Get <TmpWordMetric>())
                {
                    Console.WriteLine(wordMetric);
                }
            }

            // Words Repo
            Console.WriteLine(@"Words Repo : Word Metrics");
            foreach (var wordMetric in (new WordsRepository()).GetTop(10))
            {
                Console.WriteLine(wordMetric);
            }


            // Write
            using (var saltContext = new CountVonCountDbContext())
            {
                var wordSaltCache = new WordSaltCache();
                wordSaltCache.Init(new EntityFrameworkRepository <CountVonCountDbContext>(saltContext));

                using (var context = new CountVonCountDbContext())
                {
                    var words = new[] { new WordMetric("Id1", "MyWord", 3, null) };

                    var repository = new EntityFrameworkRepository <CountVonCountDbContext>(context,
                                                                                            new HashRepository(new PBKDF2Provider(),
                                                                                                               wordSaltCache));
                    repository.Upsert(words);
                    context.SaveChanges();
                }

                using (var context = new CountVonCountDbContext())
                {
                    var words = new[] { new WordMetric("Id1", "MyWord", 3, null) };

                    var repository = new EntityFrameworkRepository <CountVonCountDbContext>(context,
                                                                                            new HashRepository(new PBKDF2Provider(),
                                                                                                               wordSaltCache));
                    repository.Upsert(words);
                    context.SaveChanges();
                }
            }


            Console.WriteLine(@"Complete");

            // Assert
            using (var context = new CountVonCountDbContext())
            {
                var persist = new EntityFrameworkRepository <CountVonCountDbContext>(context);

                Expression <Func <WordMetric, bool> > where = wordM => wordM.Count > 10;
                IOrderedQueryable <WordMetric> OrderBy(IQueryable <WordMetric> entities) => entities.OrderByDescending(x => x.Count);

                const int take = 100;

                var results = persist.Get(@where, OrderBy, take);

                foreach (var result in results)
                {
                    Console.WriteLine($@"repo : {result}");
                }
            }
        }