public void RefreshGenomeStatsCache_UpdatesCache_WhenInvokedAndCacheAlreadyExists()
        {
            var rOptions = TestDbContextOptionsFactory.GetTestReadDbOptions();
            var cOptions = TestDbContextOptionsFactory.GetTestCommandDbOptions();

            using var readContext    = new ReadGenomeContext(rOptions);
            using var commandContext = new CommandGenomeContext(cOptions);
            var testGenomes = new List <Genome>()
            {
                new Genome(new[] { "ACGT", "TGCA" }, false),
                new Genome(new[] { "AAAA", "BBBB" }, true),
            };

            readContext.Genomes.AddRange(testGenomes);
            readContext.SaveChanges();
            var testStats = new StatsModel()
            {
                Id          = 1,
                HumanCount  = 1,
                MutantCount = 0,
                Ratio       = 0,
                Changed     = DateTime.UtcNow.AddDays(-1)
            };

            commandContext.StatsModels.Add(testStats);
            commandContext.SaveChanges();
            var statsRepository = new StatsRepository(readContext, commandContext);

            statsRepository.RefreshGenomeStatsCache();
            var result = commandContext.StatsModels.Find(1);

            Assert.Equal(1, result.HumanCount);
            Assert.Equal(1, result.MutantCount);
            Assert.Equal(0.5, result.Ratio);
        }
예제 #2
0
        /// <summary>
        /// Refresh (or creates) the GenomeStats cache with current data
        /// </summary>
        public StatsModel RefreshGenomeStatsCache()
        {
            var cachedStats = _commandContext.StatsModels.Find(1);

            if (cachedStats == null)
            {
                cachedStats = new StatsModel()
                {
                    Id = 1
                };
                _commandContext.StatsModels.Add(cachedStats);
                _commandContext.SaveChanges();
            }

            var newStats = _readContext.Genomes.GroupBy(genome => genome.IsMutant)
                           .Select(group => new
            {
                IsMutant = group.Key,
                Count    = group.LongCount()
            }).OrderBy(x => x.IsMutant).ToList();

            if (newStats.Any())
            {
                if (newStats.FirstOrDefault(x => x.IsMutant) != null)
                {
                    cachedStats.MutantCount = newStats.First(x => x.IsMutant).Count;
                }
                if (newStats.FirstOrDefault(x => !x.IsMutant) != null)
                {
                    cachedStats.HumanCount = newStats.First(x => !x.IsMutant).Count;
                }
                cachedStats.Ratio   = (double)cachedStats.MutantCount / (cachedStats.HumanCount + cachedStats.MutantCount);
                cachedStats.Changed = DateTime.UtcNow;
            }

            _commandContext.StatsModels.Update(cachedStats);
            _commandContext.SaveChanges();
            return(cachedStats);
        }
예제 #3
0
        public void CreateGenome_DoesNotInsertGenome_WhenItAlreadyExistsInDb()
        {
            var rOptions = TestDbContextOptionsFactory.GetTestReadDbOptions();
            var cOptions = TestDbContextOptionsFactory.GetTestCommandDbOptions();

            using var readContext    = new ReadGenomeContext(rOptions);
            using var commandContext = new CommandGenomeContext(cOptions);
            var testGenome = new Genome(new[] { "AC", "GT" }, false);

            commandContext.Genomes.Add(testGenome);
            commandContext.SaveChanges();
            var genomeRepository = new GenomeRepository(commandContext, readContext);

            var result = genomeRepository.CreateGenome(testGenome);

            Assert.False(result.Result, "'CreateGenome' method inserted a row, but it shouldn't have");
        }