Esempio n. 1
0
        public async Task AllAsync_NullSets_SuccessWithEmptyList()
        {
            // arrange
            _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);

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

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

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

            // assert
            Assert.True(result.IsSuccess);
            Assert.Empty(result.Value);
            _mockRepository.VerifyAll();
        }
Esempio n. 2
0
        public async Task Reset_Success()
        {
            _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 rootSetList = new RootSetDto()
            {
                Set = new SetDto(),
            };

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

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

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

            service.Where(x => x.Name, "test");

            // act
            service.Reset();

            // assert
            await service.AllAsync();

            httpTest.ShouldHaveCalled("https://api.magicthegathering.io/v1/sets");
        }
Esempio n. 3
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. 4
0
        public async Task Where_AddsQueryParameters_Success()
        {
            const string NAME  = "name1";
            const string BLOCK = "12345";

            _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 rootSetList = new RootSetListDto()
            {
                Sets = new List <SetDto> {
                    new SetDto()
                },
            };

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

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

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

            // act
            service
            .Where(x => x.Name, NAME)
            .Where(x => x.Block, BLOCK);

            // assert
            await service.AllAsync();

            httpTest
            .ShouldHaveCalled("https://api.magicthegathering.io/v1/sets*")
            .WithQueryParams("name", "block");
        }
Esempio n. 5
0
        public async Task AllAsync_NullSetListDto_Failure()
        {
            // arrange
            _mockRateLimit.Setup(x => x.IsTurnedOn).Returns(false);

            _mockHeaderManager.Setup(x => x.Update(It.IsAny <IReadOnlyNameValueList <string> >()));

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

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

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

            // assert
            Assert.False(result.IsSuccess);
            _mockRepository.VerifyAll();
        }
Esempio n. 6
0
        async Task GetSet()
        {
            var setService = new SetService();

            var s = await setService.AllAsync();

            if (s.IsSuccess)
            {
                var exp = s.Value.Select(d => d.Name).ToList <string>();

                SetWinFormControll(SetComboBox, () =>
                {
                    progressBar1.Style  = ProgressBarStyle.Blocks;
                    SetComboBox.Enabled = true;
                    SetComboBox.Items.AddRange(exp.ToArray <string>());
                    SetComboBox.SelectedItem = SetComboBox.Items[0];
                });
            }
            else
            {
                throw new Exception("FAIL");
            }
        }
Esempio n. 7
0
        static void Main()
        {
            var    deckList = @"deck_list.txt";
            string line;
            var    cardNames = new List <string>();

            using (var file = new StreamReader(deckList))
            {
                var expr = @"^\d*\s";
                while ((line = file.ReadLine()) != null)
                {
                    cardNames.Add(Regex.Replace(line, expr, string.Empty));
                }
            }

            //Gather all card sets
            var setService  = new SetService();
            var setsAwaiter = setService.AllAsync();

            var tasks   = new List <Task <Exceptional <List <Card> > > >();
            var service = new CardService();

            //Async gather all of the card details
            foreach (var name in cardNames)
            {
                tasks.Add(service
                          .Where(x => x.Name, name)
                          .AllAsync());
            }

            Task.WaitAll(tasks.ToArray());
            var sets = setsAwaiter.Result.Value.ToDictionary(x => x.Code, y => y.Name);

            var cards = new Dictionary <string, List <Card> >();

            foreach (var task in tasks)
            {
                cards.Add(task.Result.Value.First().Name,
                          task.Result.Value
                          .Where(x => x.Set[0] != 'P' && x.Set[0].ToString() + x.Set[1].ToString() != "WC")
                          .ToList()
                          );
            }

            //Count the number of sets within the pulled cards
            var setCounts = GetSetCounts(cards, sets);

            var output = new Dictionary <string, List <Card> >();

            foreach (var item in cards)
            {
                foreach (var set in setCounts)
                {
                    if (item.Value.Where(x => x.Set == set.Key).Any())
                    {
                        if (output.ContainsKey(set.Key))
                        {
                            output[set.Key].Add(item.Value.Where(x => x.Set == set.Key).First());
                        }
                        else
                        {
                            output[set.Key] = new List <Card>(new[] { item.Value.Where(x => x.Set == set.Key).First() });
                        }

                        break;
                    }
                }
            }

            WriteOutputToFile(output, sets);
        }
        public async Task AllAsyncTest()
        {
            var sets = new List <SetDto>()
            {
                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 = "magicCardsInfoCode1",
                    Name               = "name1",
                    OldCode            = "oldCode1",
                    OnlineOnly         = true,
                    ReleaseDate        = "2016, 1, 1"
                },
                new SetDto()
                {
                    Block   = "block2",
                    Booster = new object[2]
                    {
                        new JValue("booster1"),
                        new JArray()
                        {
                            new JValue("booster2"),
                            new JValue("booster3"),
                            new JArray()
                            {
                                new JValue("booster4"),
                                new JValue("booster5")
                            }
                        }
                    },
                    Border             = "border2",
                    Code               = "code2",
                    Expansion          = "expansion2",
                    GathererCode       = "gathererCode2",
                    MagicCardsInfoCode = "magicCardsInfoCode2",
                    Name               = "name2",
                    OldCode            = "oldCode2",
                    OnlineOnly         = true,
                    ReleaseDate        = "2016, 2, 2"
                }
            };

            // Test the All method.
            var moqAdapter = new Mock <IMtgApiServiceAdapter>();

            moqAdapter
            .SetupSequence(x => x.WebGetAsync <RootSetListDto>(It.IsAny <Uri>()))
            .Throws <ArgumentNullException>()
            .Throws(new MtgApiException <BadRequestException>("bad request"))
            .Throws(new MtgApiException <ForbiddenException>("forbidden"))
            .Throws(new MtgApiException <InternalServerErrorException>("server error"))
            .Throws(new MtgApiException <NotFoundException>("not found"))
            .Throws(new MtgApiException <ServiceUnavailableException>("unavailable"))
            .Throws <Exception>()
            .ReturnsAsync(new RootSetListDto()
            {
                Sets = sets
            });

            var service = new SetService(moqAdapter.Object, ApiVersion.V1_0, false);

            service = service.Where(x => x.Name, "name1");

            var result = await service.AllAsync();

            Assert.False(result.IsSuccess);
            Assert.Equal("Value cannot be null.", result.Exception.Message);

            result = await service.AllAsync();

            Assert.False(result.IsSuccess);
            Assert.Equal("bad request", result.Exception.Message);

            result = await service.AllAsync();

            Assert.False(result.IsSuccess);
            Assert.Equal("forbidden", result.Exception.Message);

            result = await service.AllAsync();

            Assert.False(result.IsSuccess);
            Assert.Equal("server error", result.Exception.Message);

            result = await service.AllAsync();

            Assert.False(result.IsSuccess);
            Assert.Equal("not found", result.Exception.Message);

            result = await service.AllAsync();

            Assert.False(result.IsSuccess);
            Assert.Equal("unavailable", result.Exception.Message);

            result = await service.AllAsync();

            Assert.False(result.IsSuccess);
            Assert.IsType <Exception>(result.Exception);

            result = await service.AllAsync();

            Assert.True(result.IsSuccess);
            Assert.Null(result.Exception);
            Assert.NotNull(result.Value);
        }