Пример #1
0
        public void ShouldQueryCacheTest()
        {
            ConsoleLogger logger = new ConsoleLogger {
                AddDetails = false, UseColors = true
            };

            logger.StartLoggingThread();
            string        schemaName = "TheSchemaName_".RandomLetters(5);
            DaoRepository repo       = new DaoRepository(new SQLiteDatabase(".", nameof(ShouldQueryCacheTest)), logger, schemaName);

            repo.AddType(typeof(TestData));
            CachingRepository cachingRepo = new CachingRepository(repo, logger);

            TestData data = new TestData {
                Name = "Bryan", SerialNumber = "12345A", OtherInformation = "is having an existential crisis"
            };

            data.Save <TestData>(cachingRepo);

            bool?queriedSource = false;
            bool?queriedCache  = false;

            cachingRepo.SubscribeOnce(nameof(CachingRepository.QueriedSource), (o, a) =>
            {
                OutLineFormat("Queried source: {0}", ConsoleColor.Yellow, a.PropertiesToLine());
                queriedSource = true;
            });
            cachingRepo.SubscribeOnce(nameof(CachingRepository.QueriedCache), (o, a) =>
            {
                OutLineFormat("Queried cache: {0}", ConsoleColor.Green, a.PropertiesToLine());
                queriedCache = true;
            });
            IEnumerable <TestData> result = cachingRepo.Query <TestData>(td => td.SerialNumber.Equals(data.SerialNumber));

            Expect.AreEqual(1, result.Count());
            result = cachingRepo.Query <TestData>(td => td.SerialNumber.Equals(data.SerialNumber));

            Expect.AreEqual(1, result.Count());
            Expect.IsTrue(queriedSource.Value, "didn't query source");
            Expect.IsTrue(queriedCache.Value, "didn't query cache");
        }
Пример #2
0
        public void SavingKeyHashRepoDataShouldntDuplicate()
        {
            ConsoleLogger logger = new ConsoleLogger {
                AddDetails = false, UseColors = true
            };

            logger.StartLoggingThread();
            string        schemaName = "TheSchemaName_".RandomLetters(5);
            DaoRepository repo       = new DaoRepository(new SQLiteDatabase(".", nameof(SavingKeyHashRepoDataShouldntDuplicate)), logger, schemaName);

            repo.WarningsAsErrors = false;
            repo.AddType(typeof(KeyHashRepoTestData));
            CachingRepository cachingRepo = new CachingRepository(repo, logger);

            string nameOne          = 32.RandomLetters();
            string valueOne         = 16.RandomLetters();
            KeyHashRepoTestData one = new KeyHashRepoTestData {
                Name = nameOne, SomeOtherUniqueProperty = valueOne
            };
            KeyHashRepoTestData two = new KeyHashRepoTestData {
                Name = nameOne, SomeOtherUniqueProperty = valueOne
            };

            Expect.AreEqual(one, two);

            one.Save <KeyHashRepoTestData>(cachingRepo);
            var queryParams = new { Name = nameOne };

            cachingRepo.Cache <KeyHashRepoTestData>(queryParams);
            List <KeyHashRepoTestData> retrieved = cachingRepo.Query <KeyHashRepoTestData>(queryParams).ToList();

            Expect.AreEqual(1, retrieved.Count);
            two.Save <KeyHashRepoTestData>(cachingRepo);
            retrieved = cachingRepo.Query <KeyHashRepoTestData>(queryParams).ToList();
            Expect.AreEqual(1, retrieved.Count);
        }