Beispiel #1
0
        private List <Parameter> SetParameters()
        {
            MemoryCacheEntryOptions cacheEntryOptions = CreateEntryOptions();
            var stackGameContext = new StackGameContext();

            var parameters = stackGameContext.Parameters.ToList();

            //add cache Item with options of callback
            memoryCache.Set(PARAMETERS_KEY, parameters, cacheEntryOptions);

            logger.LogInformation("Parameters seted");

            return(parameters);
        }
        public async Task Services_ParametersService_Should_Return_Parameter()
        {
            // Setup
            DbContextOptions <StackGameContext> options = GenerateDbContextOptions();
            Parameter parameter = new()
            {
                Id          = 988,
                Key         = "test_parameter",
                Description = "Test parameter in memory database",
                Value       = "Working!!!"
            };

            // Seed
            using (StackGameContext dbContext = new StackGameContext(options))
            {
                await dbContext.Parameters.AddAsync(parameter);

                await dbContext.SaveChangesAsync();
            }
            var _memoryCache = new MemoryCache(new MemoryCacheOptions());
            var _logger      = Mocks.MocksCreator.CreateLoggerMock <global::Services.ParametersService>();

            using var dbContext2 = new StackGameContext(options);
            var parametersService = new global::Services.ParametersService(_memoryCache, _logger.Object, dbContext2);

            var result = await parametersService.GetParameterAsync(parameter.Key);

            var parameterRetrived = result.Value;

            Assert.IsNotNull(result);
            Assert.IsTrue(result.IsSuccess);
            Assert.IsFalse(result.IsFailed);
            Assert.IsNotNull(parameterRetrived);

            Assert.AreEqual(parameter.Id, parameterRetrived.Id);
            Assert.AreEqual(parameter.Description, parameterRetrived.Description);
            Assert.AreEqual(parameter.Value, parameterRetrived.Value);
        }
        public async Task Services_ParametersService_Should_Return_Error_Result_When_Parameter_Key_Does_Not_Exists()
        {
            // Setup
            DbContextOptions <StackGameContext> options = GenerateDbContextOptions();
            var _memoryCache = new MemoryCache(new MemoryCacheOptions());
            var _logger      = Mocks.MocksCreator.CreateLoggerMock <global::Services.ParametersService>();

            using var dbContext = new StackGameContext(options);
            var parametersService = new global::Services.ParametersService(_memoryCache, _logger.Object, dbContext);
            var key    = "THIS_KEY_DOES_NOT_EXISTS";
            var result = await parametersService.GetParameterAsync(key);


            Assert.IsNotNull(result);
            Assert.IsTrue(result.IsFailed);
            Assert.IsFalse(result.IsSuccess);
            Assert.Throws <InvalidOperationException>(delegate { var _ = result.Value; });
            Assert.IsTrue(result.HasError <ParameterNotFoundError>());
            ParameterNotFoundError error = (ParameterNotFoundError)result.Errors.Find(x => x.GetType() == typeof(ParameterNotFoundError));

            Assert.IsNotNull(error);
            Assert.AreEqual(key, error.Key);
        }
Beispiel #4
0
 public ParametersService(IMemoryCache _memoryCache, ILogger <ParametersService> _logger, StackGameContext _stackGameContext)
 {
     memoryCache      = _memoryCache;
     logger           = _logger;
     stackGameContext = _stackGameContext;
 }
 public ProductService(StackGameContext stackGameContext)
 {
     this.stackGameContext = stackGameContext;
 }