Esempio n. 1
0
        public void RemoveSet_ByKey_ClearsTheSetData()
        {
            // ARRANGE
            _realm.Write(() =>
            {
                var set1Val1 = new SetDto {
                    Key = "Set1<value1>", Value = "value1", ExpireAt = DateTimeOffset.UtcNow
                };
                _realm.Add(set1Val1);

                var set1Val2 = new SetDto {
                    Key = "Set1<value2>", Value = "value2", ExpireAt = DateTimeOffset.UtcNow
                };
                _realm.Add(set1Val2);

                var set2 = new SetDto {
                    Key = "Set2", Value = "value2", ExpireAt = DateTimeOffset.UtcNow
                };
                _realm.Add(set2);
            });

            // ACT
            _transaction.RemoveSet("Set1");

            // ASSERT
            var testSet1 = _realm.All <SetDto>().Where(s => s.Key.StartsWith("Set1")).ToList();

            Assert.AreEqual(0, testSet1.Count);

            var testSet2 = _realm.All <SetDto>().Where(s => s.Key.StartsWith("Set2")).ToList();

            Assert.AreEqual(1, testSet2.Count);
        }
Esempio n. 2
0
        public void ExpireSet_SetsSetExpirationData()
        {
            var set1 = new SetDto {
                Key = "Set1<value1>", Value = "value1"
            };

            _database.JobGraph.InsertOne(set1);

            var set2 = new SetDto {
                Key = "Set2<value2>", Value = "value2"
            };

            _database.JobGraph.InsertOne(set2);

            Commit(x => x.ExpireSet("Set1", TimeSpan.FromDays(1)));

            var testSet1 = GetTestSet(_database, "Set1").FirstOrDefault();
            var testSet2 = GetTestSet(_database, "Set2").FirstOrDefault();

            Assert.NotNull(testSet1);
            Assert.True(DateTime.UtcNow.AddMinutes(-1) < testSet1.ExpireAt &&
                        testSet1.ExpireAt <= DateTime.UtcNow.AddDays(1));

            Assert.NotNull(testSet2);
            Assert.Null(testSet2.ExpireAt);
        }
Esempio n. 3
0
        public void RemoveSet_ClearsTheSetData()
        {
            var set1Val1 = new SetDto {
                Key = "Set1<value1>", Value = "value1", ExpireAt = DateTime.UtcNow
            };

            _database.JobGraph.InsertOne(set1Val1);

            var set1Val2 = new SetDto {
                Key = "Set1<value2>", Value = "value2", ExpireAt = DateTime.UtcNow
            };

            _database.JobGraph.InsertOne(set1Val2);

            var set2 = new SetDto {
                Key = "Set2<value2>", Value = "value2", ExpireAt = DateTime.UtcNow
            };

            _database.JobGraph.InsertOne(set2);

            Commit(x => x.RemoveSet("Set1"));

            var testSet1 = GetTestSet(_database, "Set1");

            Assert.Equal(0, testSet1.Count);

            var testSet2 = GetTestSet(_database, "Set2");

            Assert.Equal(1, testSet2.Count);
        }
Esempio n. 4
0
        public void PersistSet_ClearsTheSetExpirationData()
        {
            var set1Val1 = new SetDto {
                Key = "Set1<value1>", Value = "value1", ExpireAt = DateTime.UtcNow
            };

            _database.JobGraph.InsertOne(set1Val1);

            var set1Val2 = new SetDto {
                Key = "Set1<value2>", Value = "value2", ExpireAt = DateTime.UtcNow
            };

            _database.JobGraph.InsertOne(set1Val2);

            var set2 = new SetDto {
                Key = "Set2<value1>", ExpireAt = DateTime.UtcNow
            };

            _database.JobGraph.InsertOne(set2);

            Commit(x => x.PersistSet("Set1"));

            var testSet1 = GetTestSet(_database, "Set1");

            Assert.All(testSet1, x => Assert.Null(x.ExpireAt));

            var testSet2 = GetTestSet(_database, set2.Key).Single();

            Assert.NotNull(testSet2.ExpireAt);
        }
Esempio n. 5
0
        public void MapSetTest()
        {
            Set model;

            try
            {
                // Test exception is thrown.
                model = new Set(null);
                Assert.Fail();
            }
            catch (ArgumentNullException ex)
            {
                Assert.AreEqual("item", ex.ParamName);
            }
            catch
            {
                Assert.Fail();
            }

            var dto = new SetDto()
            {
                Block   = "block1",
                Booster = new object[2]
                {
                    new JValue("booster1"),
                    new JArray()
                    {
                        new JValue("booster2"),
                        new JValue("booster3"),
                        new JArray()
                        {
                            new JValue("booster4"),
                            new JValue("booster5")
                        }
                    }
                },
                Border             = "border1",
                Code               = "code1",
                Expansion          = "expansion1",
                GathererCode       = "gathererCode1",
                MagicCardsInfoCode = "magicCardsInfoCode",
                Name               = "name1",
                OldCode            = "oldCode1",
                OnlineOnly         = true,
                ReleaseDate        = "2016, 1, 1"
            };

            model = new Set(dto);

            Assert.AreEqual(dto.Block, model.Block);
            Assert.AreEqual(dto.Border, model.Border);
            Assert.AreEqual(dto.Code, model.Code);
            Assert.AreEqual(dto.Expansion, model.Expansion);
            Assert.AreEqual(dto.GathererCode, model.GathererCode);
            Assert.AreEqual(dto.MagicCardsInfoCode, model.MagicCardsInfoCode);
            Assert.AreEqual(dto.Name, model.Name);
            Assert.AreEqual(dto.OldCode, model.OldCode);
            Assert.AreEqual(dto.OnlineOnly, model.OnlineOnly);
            Assert.AreEqual(dto.ReleaseDate, model.ReleaseDate);
        }
Esempio n. 6
0
        private PaginationDataList <SetDto> GetPicBooksByAdmin(int?pageSize, int?pageRows, int setStatus, Admin admin)
        {
            List <SetDto> setDtoList = new List <SetDto>();
            var           orderItems = admin.Orders.SelectMany(e => e.OrderItems);
            var           payments   = admin.Orders.SelectMany(e => e.Payments);

            foreach (var orderItem in orderItems)
            {
                var allocatedNum = orderItem.TeacherAllocations.Sum(e => e.Credit);
                if (FilterSetBySetStatus(setStatus, allocatedNum, orderItem.Quantity))
                {
                    continue;
                }
                SetDto setDto = new SetDto()
                {
                    Id            = orderItem.SetId,
                    Synopsis      = orderItem.Set.Synopsis,
                    SetName       = orderItem.Set.SetName,
                    SetType       = HintInfo.StandardCourse,
                    Books         = _bookRepostitory.GetAllList(e => e.SetId == orderItem.SetId),
                    DateCreated   = payments.FirstOrDefault(e => e.OrderRef == orderItem.OrderRef)?.DateCreated,
                    SurplusCredit = allocatedNum + "/" + orderItem.Quantity
                };
                setDtoList.Add(setDto);
            }
            return(setDtoList.OrderByDescending(e => e.DateCreated).AsQueryable().ToPagination(pageSize, pageRows));
        }
Esempio n. 7
0
        public ISet MapSet(SetDto setDto)
        {
            if (setDto == null)
            {
                throw new ArgumentNullException(nameof(setDto));
            }

            var booster = new List <object>();

            if (setDto.Booster.ValueKind == JsonValueKind.Array)
            {
                booster = setDto.Booster
                          .EnumerateArray()
                          .Select(x => GetBoosterValue(x))
                          .ToList();
            }

            return(new Set
            {
                Block = setDto.Block,
                Booster = booster,
                Border = setDto.Border,
                Code = setDto.Code,
                Expansion = setDto.Expansion,
                GathererCode = setDto.GathererCode,
                MagicCardsInfoCode = setDto.MagicCardsInfoCode,
                Name = setDto.Name,
                OldCode = setDto.OldCode,
                OnlineOnly = setDto.OnlineOnly,
                ReleaseDate = setDto.ReleaseDate,
                Type = setDto.Type,
            });
        }
Esempio n. 8
0
        private PaginationDataList <SetDto> GetPicBooksByTeacher(int?pageSize, int?pageRows, int setStatus, Admin admin)
        {
            List <SetDto> setDtoList = new List <SetDto>();

            foreach (var teacherAllocation in admin.TeacherAllocations)
            {
                int allocatedNum = teacherAllocation.StudentAllocations.Count;
                if (FilterSetBySetStatus(setStatus, allocatedNum, teacherAllocation.Credit))
                {
                    continue;
                }
                var    setInfo = _setRepostitory.GetAllIncluding(e => e.Books).FirstOrDefault(e => e.Id == teacherAllocation.SetId);
                SetDto setDto  = new SetDto()
                {
                    Id            = teacherAllocation.SetId,
                    SetName       = setInfo.SetName,
                    SetType       = HintInfo.StandardCourse,
                    Synopsis      = setInfo.Synopsis,
                    Books         = setInfo.Books,
                    DateCreated   = teacherAllocation.DateAllocated,
                    SurplusCredit = allocatedNum + "/" + teacherAllocation.Credit
                };
                setDtoList.Add(setDto);
            }
            return(setDtoList.OrderByDescending(e => e.DateCreated).AsQueryable().ToPagination(pageSize, pageRows));
        }
Esempio n. 9
0
        public void AddRangeToSet_AddToExistingSetData()
        {
            UseConnection(database =>
            {
                var set1Val1 = new SetDto {
                    Key = "Set1", Value = "value1", ExpireAt = database.GetServerTimeUtc()
                };
                database.Set.InsertOne(set1Val1);

                var set1Val2 = new SetDto {
                    Key = "Set1", Value = "value2", ExpireAt = database.GetServerTimeUtc()
                };
                database.Set.InsertOne(set1Val2);

                var set2 = new SetDto {
                    Key = "Set2", Value = "value2", ExpireAt = database.GetServerTimeUtc()
                };
                database.Set.InsertOne(set2);

                var values = new string[] { "test1", "test2", "test3" };
                Commit(database, x => x.AddRangeToSet(set1Val1.Key, values));

                var testSet1 = GetTestSet(database, set1Val1.Key);
                Assert.NotNull(testSet1);
                Assert.Equal(5, testSet1.Count);

                var testSet2 = GetTestSet(database, set2.Key);
                Assert.NotNull(testSet2);
                Assert.Equal(1, testSet2.Count);
            });
        }
Esempio n. 10
0
        /// <summary>
        /// Maps a single set DTO to the set model.
        /// </summary>
        /// <param name="item">The set DTO object.</param>
        private void MapSet(SetDto item)
        {
            if (item == null)
            {
                throw new ArgumentNullException("item");
            }

            Block = item.Block;

            if (item.Booster != null)
            {
                Booster = new List <object>();
                foreach (var booster in item.Booster)
                {
                    if (booster is string)
                    {
                        Booster.Add(booster);
                    }
                    else
                    {
                        Booster.Add(Set.CreateBoosterArray(booster));
                    }
                }
            }

            Border             = item.Border;
            Code               = item.Code;
            Expansion          = item.Expansion;
            GathererCode       = item.GathererCode;
            MagicCardsInfoCode = item.MagicCardsInfoCode;
            Name               = item.Name;
            OldCode            = item.OldCode;
            OnlineOnly         = item.OnlineOnly;
            ReleaseDate        = item.ReleaseDate;
        }
        public void RemoveSet_ClearsTheSetData()
        {
            ConnectionUtils.UseConnection(database =>
            {
                var set1Val1 = new SetDto {
                    Key = "Set1", Value = "value1", ExpireAt = DateTime.UtcNow
                };
                database.StateData.InsertOne(set1Val1);

                var set1Val2 = new SetDto {
                    Key = "Set1", Value = "value2", ExpireAt = DateTime.UtcNow
                };
                database.StateData.InsertOne(set1Val2);

                var set2 = new SetDto {
                    Key = "Set2", Value = "value2", ExpireAt = DateTime.UtcNow
                };
                database.StateData.InsertOne(set2);

                Commit(database, x => x.RemoveSet(set1Val1.Key));

                var testSet1 = GetTestSet(database, set1Val1.Key);
                Assert.Equal(0, testSet1.Count);

                var testSet2 = GetTestSet(database, set2.Key);
                Assert.Equal(1, testSet2.Count);
            });
        }
Esempio n. 12
0
        public void AddRangeToSet_SetExists_AddToExistingSetData()
        {
            // ARRANGE
            var set1Val1 = new SetDto {
                Key = "Set1<value1>", Value = "value1", ExpireAt = DateTimeOffset.UtcNow
            };

            _realm.Write(() =>
            {
                _realm.Add(set1Val1);
            });


            var set1Val2 = new SetDto {
                Key = "Set1<value2>", Value = "value2", ExpireAt = DateTimeOffset.UtcNow
            };

            _realm.Write(() =>
            {
                _realm.Add(set1Val2);
            });


            var set2 = new SetDto {
                Key = "Set2", Value = "value2", ExpireAt = DateTimeOffset.UtcNow
            };

            _realm.Write(() =>
            {
                _realm.Add(set2);
            });


            var values = new[] { "test1", "test2", "test3" };

            // ACT
            _transaction.AddRangeToSet(set1Val1.Key, values);
            _transaction.Commit();

            // ASSERT

            var testSet1     = _realm.All <SetDto>().Where(s => s.Key.StartsWith("Set1")).ToList();
            var valuesToTest = new List <string>(values)
            {
                "value1", "value2"
            };

            Assert.NotNull(testSet1);
            // verify all values are present in testSet1
            Assert.True(testSet1.Select(s => s.Value.ToString()).All(value => valuesToTest.Contains(value)));
            Assert.AreEqual(5, testSet1.Count);

            var testSet2 = _realm.All <SetDto>().Where(s => s.Key.StartsWith("Set2")).ToList();

            Assert.NotNull(testSet2);
            Assert.AreEqual(1, testSet2.Count);
        }
Esempio n. 13
0
        /// <inheritdoc/>
        public void CreateSet(ISet set)
        {
            SetDto setDto = SetDto.ToDto(set);

            this.Context.Sets.Add(setDto);
            int count = this.Context.SaveChanges();

            if (count != 1)
            {
                throw new ApplicationException($"Unexpectedly created {count} rows");
            }
        }
Esempio n. 14
0
        public int CreateSet(SetDto set)
        {
            var tempSet = _setRepostitory.FirstOrDefault(x => x.SetName == set.SetName);

            if (tempSet != null)
            {
                throw new UserFriendlyException(ExceptionInfo.SetNameExists);
            }
            Set setEntity = ObjectMapper.Map <Set>(set);

            return(_setRepostitory.InsertAndGetId(setEntity));
        }
Esempio n. 15
0
        public void AddToSet_AddsARecord_IfThereIsNo_SuchKeyAndValue()
        {
            UseConnection(database =>
            {
                Commit(database, x => x.AddToSet("my-key", "my-value"));

                SetDto record = database.JobGraph.OfType <SetDto>().Find(new BsonDocument()).ToList().Single();

                Assert.Equal("my-key<my-value>", record.Key);
                Assert.Equal(0.0, record.Score, 2);
            });
        }
Esempio n. 16
0
        public void AddToSet_WithScore_AddsARecordWithScore_WhenBothKeyAndValueAreNotExist()
        {
            UseConnection(database =>
            {
                Commit(database, x => x.AddToSet("my-key", "my-value", 3.2));

                SetDto record = database.JobGraph.OfType <SetDto>().Find(new BsonDocument()).ToList().Single();

                Assert.Equal("my-key<my-value>", record.Key);
                Assert.Equal(3.2, record.Score, 3);
            });
        }
Esempio n. 17
0
        public static Set ToDomain(this SetDto dto)
        {
            var result = new Set()
            {
                Id     = dto.id,
                Weight = dto.weight,
                Reps   = dto.reps,
                RPE    = dto.rpe,
            };

            return(result);
        }
Esempio n. 18
0
        public void AddToSet_WithScore_AddsARecordWithScore_WhenBothKeyAndValueAreNotExist()
        {
            UseConnection(database =>
            {
                Commit(database, x => x.AddToSet("my-key", "my-value", 3.2));

                SetDto record = AsyncHelper.RunSync(() => database.Set.Find(new BsonDocument()).ToListAsync()).Single();

                Assert.Equal("my-key", record.Key);
                Assert.Equal("my-value", record.Value);
                Assert.Equal(3.2, record.Score, 3);
            });
        }
Esempio n. 19
0
        public void AddToSet_AddsARecord_IfThereIsNo_SuchKeyAndValue()
        {
            UseConnection(database =>
            {
                Commit(database, x => x.AddToSet("my-key", "my-value"));

                SetDto record = AsyncHelper.RunSync(() => database.Set.Find(new BsonDocument()).ToListAsync()).Single();

                Assert.Equal("my-key", record.Key);
                Assert.Equal("my-value", record.Value);
                Assert.Equal(0.0, record.Score, 2);
            });
        }
Esempio n. 20
0
        public override void Rule(IDbAccessor omsAccessor, SetDto set)
        {
            var templateCodes = set.Key.Select(c => c.TemplateCode).ToList();
            var templateCode  = omsAccessor.Get <RemindTemplateModel>().Where(c => templateCodes.Any(d => d == c.TemplateCode)).ToList();

            templateCode.ForEach(c =>
            {
                // 标题/提醒库存 不同时,视为更新
                if (c.TemplateTitle != set.TemplateTitle || c.RemindStock != set.RemindStock)
                {
                    c.IsUpdate = true;
                }
            });
        }
        public void AddToSet_WithScore_UpdatesAScore_WhenBothKeyAndValueAreExist()
        {
            ConnectionUtils.UseConnection(database =>
            {
                Commit(database, x =>
                {
                    x.AddToSet("my-key", "my-value");
                    x.AddToSet("my-key", "my-value", 3.2);
                });

                SetDto record = database.StateData.OfType <SetDto>().Find(new BsonDocument()).ToList().Single();

                Assert.Equal(3.2, record.Score, 3);
            });
        }
Esempio n. 22
0
        public async Task <IActionResult> Submit([FromBody] SubmitRequest request)
        {
            try
            {
                var set = await setService.Update(request.SetID, request.Matches);

                dbContext.Entry(set).Reference(s => s.League).Load();
                var response = SetDto.Create(set, set.League.GameID);
                return(CodeResultAndLog(HttpStatusCode.OK, response, $"Set {set.ID} updated."));
            }
            catch (Exception exception)
            {
                return(GetExceptionResult(exception, request));
            }
        }
Esempio n. 23
0
        public async Task <IActionResult> Get(int setID)
        {
            var set = await dbContext.Sets
                      .Include(s => s.Matches).ThenInclude(m => m.MatchCharacters).AsNoTracking()
                      .Include(s => s.League).AsNoTracking()
                      .FirstOrDefaultAsync(s => s.ID == setID);

            if (set == null)
            {
                return(CodeResultAndLog(HttpStatusCode.NotFound, $"Could not find Set with ID '{setID}'."));
            }

            var dto = SetDto.Create(set, set.League.GameID);

            return(CodeResult(HttpStatusCode.OK, dto));
        }
 public override void AddRangeToSet(string key, IList <string> items)
 {
     this.QueueCommand(() =>
     {
         var existingSet = _data.GetEnumeration <SetDto>().Where(s => s.Key == key).Select(s => s.Value).ToList();
         foreach (var item in items.Where(i => !existingSet.Contains(i)))
         {
             var newSet = new SetDto
             {
                 Id    = AutoIncrementIdGenerator.GenerateId(typeof(SetDto)),
                 Key   = key,
                 Value = item
             };
             _data.Create(newSet);
         }
     });
 }
Esempio n. 25
0
        public void PersistSet_ClearsTheSetExpirationData_WhenKeyContainsRegexSpecialChars()
        {
            var key = "some+-[regex]?-#set";


            var set1 = new SetDto {
                Key = $"{key}<value1>", ExpireAt = DateTime.UtcNow
            };

            _database.JobGraph.InsertOne(set1);

            Commit(x => x.PersistSet(key));

            var testSet1 = GetTestSet(_database, key).First();

            Assert.Null(testSet1.ExpireAt);
        }
Esempio n. 26
0
        public async Task AllAsync_Success()
        {
            // arrange
            const string SET_NAME = "setname1";

            _mockRateLimit.Setup(x => x.IsTurnedOn).Returns(false);

            _mockHeaderManager.Setup(x => x.Update(It.IsAny <IReadOnlyNameValueList <string> >()));
            _mockHeaderManager.Setup(x => x.Get <int>(ResponseHeader.TotalCount)).Returns(2000);
            _mockHeaderManager.Setup(x => x.Get <int>(ResponseHeader.PageSize)).Returns(1000);

            var setDto = new SetDto()
            {
                Name = SET_NAME
            };
            var rootSetList = new RootSetListDto()
            {
                Sets = new List <SetDto> {
                    setDto
                },
            };

            _mockModelMapper.Setup(x => x.MapSet(It.IsAny <SetDto>())).Returns(new Set()
            {
                Name = SET_NAME
            });

            using var httpTest = new HttpTest();
            httpTest.RespondWithJson(rootSetList);

            var service = new SetService(
                _mockHeaderManager.Object,
                _mockModelMapper.Object,
                ApiVersion.V1,
                _mockRateLimit.Object);

            // act
            var result = await service.AllAsync();

            // assert
            Assert.True(result.IsSuccess);
            _mockRepository.VerifyAll();
        }
Esempio n. 27
0
        public void ExpireSet_SetsSetExpirationData_WhenKeyContainsRegexSpecialChars()
        {
            var key = "some+-[regex]?-#set";


            var set1 = new SetDto {
                Key = $"{key}<value1>", Value = "value1"
            };

            _database.JobGraph.InsertOne(set1);

            Commit(x => x.ExpireSet(key, TimeSpan.FromDays(1)));

            var testSet1 = GetTestSet(_database, key).FirstOrDefault();

            Assert.NotNull(testSet1);
            Assert.True(DateTime.UtcNow.AddMinutes(-1) < testSet1.ExpireAt &&
                        testSet1.ExpireAt <= DateTime.UtcNow.AddDays(1));
        }
Esempio n. 28
0
        public void AddRangeToSet_AddToExistingSetData()
        {
            // ASSERT
            var set1Val1 = new SetDto {
                Key = "Set1<value1>", Value = "value1", ExpireAt = DateTime.UtcNow
            };

            _database.JobGraph.InsertOne(set1Val1);

            var set1Val2 = new SetDto {
                Key = "Set1<value2>", Value = "value2", ExpireAt = DateTime.UtcNow
            };

            _database.JobGraph.InsertOne(set1Val2);

            var set2 = new SetDto {
                Key = "Set2<value2>", Value = "value2", ExpireAt = DateTime.UtcNow
            };

            _database.JobGraph.InsertOne(set2);

            var values = new[] { "test1", "test2", "test3" };

            // ACT
            Commit(x => x.AddRangeToSet("Set1", values));

            var testSet1     = GetTestSet(_database, "Set1");
            var valuesToTest = new List <string>(values)
            {
                "value1", "value2"
            };

            Assert.NotNull(testSet1);
            // verify all values are present in testSet1
            Assert.True(testSet1.Select(s => s.Value).All(value => valuesToTest.Contains(value)));
            Assert.Equal(5, testSet1.Count);

            var testSet2 = GetTestSet(_database, set2.Key);

            Assert.NotNull(testSet2);
            Assert.Equal(1, testSet2.Count);
        }
        public override void AddToSet(string key, string value, double score)
        {
            QueueCommand(() =>
            {
                var set = _data.GetEnumeration <SetDto>().SingleOrDefault(s => s.Key == key && s.Value == value);
                if (set == null)
                {
                    set = new SetDto
                    {
                        Id    = AutoIncrementIdGenerator.GenerateId(typeof(SetDto)),
                        Key   = key,
                        Value = value
                    };

                    _data.Create(set);
                }

                set.Score = (long)score;
            });
        }
Esempio n. 30
0
        public override string GetFirstByLowestScoreFromSet(string key, double fromScore, double toScore)
        {
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }

            if (toScore < fromScore)
            {
                throw new ArgumentException("The `toScore` value must be higher or equal to the `fromScore` value.");
            }

            SetDto set = AsyncHelper.RunSync(() => _database.Set
                                             .Find(Builders <SetDto> .Filter.Eq(_ => _.Key, key) &
                                                   Builders <SetDto> .Filter.Gte(_ => _.Score, fromScore) &
                                                   Builders <SetDto> .Filter.Lte(_ => _.Score, toScore))
                                             .Sort(Builders <SetDto> .Sort.Ascending(_ => _.Score))
                                             .FirstOrDefaultAsync());

            return(set != null ? set.Value : null);
        }