コード例 #1
0
        public void WhereTest()
        {
            SetService service = new SetService();

            try
            {
                // Test sending a null parameter.
                service.Where <int>(null, 1);
                Assert.True(false);
            }
            catch (ArgumentNullException ex)
            {
                Assert.Equal("property", ex.ParamName);
            }
            catch
            {
                Assert.True(false);
            }

            try
            {
                // Test sending a null parameter.
                service.Where(x => x.Name, null);
                Assert.True(false);
            }
            catch (ArgumentNullException ex)
            {
                Assert.Equal("value", ex.ParamName);
            }
            catch
            {
                Assert.True(false);
            }
        }
コード例 #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");
        }
コード例 #3
0
        public void WhereTest()
        {
            SetService service = new SetService();

            try
            {
                // Test sending a null parameter.
                service.Where <int>(null, 1);
                Assert.Fail();
            }
            catch (ArgumentNullException ex)
            {
                Assert.AreEqual("property", ex.ParamName);
            }
            catch
            {
                Assert.Fail();
            }

            try
            {
                // Test sending a null parameter.
                service.Where(x => x.Name, null);
                Assert.Fail();
            }
            catch (ArgumentNullException ex)
            {
                Assert.AreEqual("value", ex.ParamName);
            }
            catch
            {
                Assert.Fail();
            }

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

            PrivateObject privateObject = new PrivateObject(service);
            var           whereQuery    = privateObject.GetFieldOrProperty("_whereQueries") as NameValueCollection;

            Assert.AreEqual(1, whereQuery.Count);
            Assert.AreEqual("name", whereQuery.AllKeys[0]);
            Assert.AreEqual("test", whereQuery["name"]);
        }
コード例 #4
0
        public void Where_DefaultValue_Throws()
        {
            var service = new SetService(
                _mockHeaderManager.Object,
                _mockModelMapper.Object,
                ApiVersion.V1,
                _mockRateLimit.Object);

            // act
            // assert
            Assert.Throws <ArgumentNullException>(() => service.Where(x => x.Name, null));
        }
コード例 #5
0
        public void Where_NullProperty_Throws()
        {
            const string NAME = "name1";

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

            // act
            // assert
            Assert.Throws <ArgumentNullException>(() => service.Where(_ => null, NAME));
        }
コード例 #6
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");
        }
コード例 #7
0
        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);
        }