コード例 #1
0
        public void Execute_WithNullApiKey_ThrowsException()
        {
            Config.Reset();
            Config.ApiKey = null;
            var categoryMapService = new CategoryMapService();

            try {
                CategoryMap categoryMap = categoryMapService.Show(4).Execute();
                Assert.Fail("Expected a WineApiStatusException to be thrown!");
            }
            catch (WineApiStatusException ex) {
                Assert.That(ex.Status.ReturnCode, Is.EqualTo(ReturnCode.UnableToAuthorize));
            }
        }
コード例 #2
0
        public void Execute_ShowWineTypes_Succeeds()
        {
            var categoryMapService = new CategoryMapService();

            CategoryMap categoryMap = categoryMapService.Show(4).Execute();

            Assert.That(categoryMap, Is.Not.Null);
            Assert.That(categoryMap.Status.ReturnCode, Is.EqualTo(ReturnCode.Success));
            Assert.That(categoryMap.Status.Messages, Has.Length.EqualTo(0));
            Assert.That(categoryMap.Categories, Has.Length.EqualTo(1));
            Assert.That(categoryMap.Categories[0].Id, Is.EqualTo(4));
            Assert.That(categoryMap.Categories[0].Name, Is.EqualTo("Wine Type"));
            Assert.That(categoryMap.Categories[0].Refinements, Has.Length.GreaterThan(0));
        }
コード例 #3
0
        public void Execute_WithEmptyVersion_ThrowsException()
        {
            Config.Reset();
            Config.ApiKey = WineApiTestsConstants.API_KEY;
            Config.Version = string.Empty;
            var categoryMapService = new CategoryMapService();

            try {
                CategoryMap categoryMap = categoryMapService.Show(4).Execute();
                Assert.Fail("Expected a WineApiServiceException to be thrown!");
            }
            catch (WineApiServiceException) {
            }
        }
コード例 #4
0
        public void Execute_BadJson_ThrowsException()
        {
            var stubUrlInvoker = MockRepository.GenerateStub<IUrlInvoker>();

            stubUrlInvoker.Stub(stub => stub.InvokeUrl(string.Empty))
                .IgnoreArguments()
                .Return(Utils.GetTestData("BadJson"));

            var categoryMapService = new CategoryMapService();
            categoryMapService.UrlInvoker = stubUrlInvoker;

            try {
                CategoryMap categoryMap = categoryMapService.Show(4).Execute();
                Assert.Fail("Expected a WineApiServiceException to be thrown!");
            }
            catch (WineApiServiceException ex) {
                Assert.That(ex.InnerException, Is.Not.Null);
            }
        }
コード例 #5
0
        public void Execute_StatusWithUnknownReturnCode_ThrowsException()
        {
            var stubUrlInvoker = MockRepository.GenerateStub<IUrlInvoker>();

            stubUrlInvoker.Stub(stub => stub.InvokeUrl(string.Empty))
                .IgnoreArguments()
                .Return(Utils.GetTestData("StatusWithUnknownReturnCode"));

            var categoryMapService = new CategoryMapService();
            categoryMapService.UrlInvoker = stubUrlInvoker;

            try {
                CategoryMap categoryMap = categoryMapService.Show(4).Execute();
                Assert.Fail("Expected a WineApiStatusException to be thrown!");
            }
            catch (WineApiStatusException ex) {
                Assert.That(ex.Status, Is.Not.Null);
                Assert.That(ex.Status.ReturnCode, Is.EqualTo((ReturnCode)123));
                Assert.That((int)ex.Status.ReturnCode, Is.EqualTo(123));
            }
        }
コード例 #6
0
        public void Execute_ShowWineTypes_Succeeds()
        {
            var stubUrlInvoker = MockRepository.GenerateStub<IUrlInvoker>();

            stubUrlInvoker.Stub(stub => stub.InvokeUrl(string.Empty))
                .IgnoreArguments()
                .Return(Utils.GetTestData("WineTypes"));

            var categoryMapService = new CategoryMapService();
            categoryMapService.UrlInvoker = stubUrlInvoker;

            CategoryMap categoryMap = categoryMapService.Show(4).Execute();

            stubUrlInvoker.AssertWasCalled(stub => stub.InvokeUrl(Arg<string>.Matches(s => s.Contains("show=(4)"))));
            Assert.That(categoryMap, Is.Not.Null);
            Assert.That(categoryMap.Status.ReturnCode, Is.EqualTo(ReturnCode.Success));
            Assert.That(categoryMap.Status.Messages, Has.Length.EqualTo(0));
            Assert.That(categoryMap.Categories, Has.Length.EqualTo(1));
            Assert.That(categoryMap.Categories[0].Id, Is.EqualTo(4));
            Assert.That(categoryMap.Categories[0].Name, Is.EqualTo("Wine Type"));
            Assert.That(categoryMap.Categories[0].Refinements, Has.Length.GreaterThan(0));
        }
コード例 #7
0
        public void Execute_StatusWithReturnCode200And2Messages_ThrowsException()
        {
            var stubUrlInvoker = MockRepository.GenerateStub<IUrlInvoker>();

            stubUrlInvoker.Stub(stub => stub.InvokeUrl(string.Empty))
                .IgnoreArguments()
                .Return(Utils.GetTestData("StatusWithReturnCode200And2Messages"));

            var categoryMapService = new CategoryMapService();
            categoryMapService.UrlInvoker = stubUrlInvoker;

            try {
                CategoryMap categoryMap = categoryMapService.Show(4).Execute();
                Assert.Fail("Expected a WineApiStatusException to be thrown!");
            }
            catch (WineApiStatusException ex) {
                Assert.That(ex.Status, Is.Not.Null);
                Assert.That(ex.Status.ReturnCode, Is.EqualTo(ReturnCode.UnableToAuthorize));
                Assert.That(ex.Status.Messages, Has.Length.EqualTo(2));
                Assert.That(ex.Status.Messages[0], Is.EqualTo("Message1"));
                Assert.That(ex.Status.Messages[1], Is.EqualTo("Message2"));
            }
        }
コード例 #8
0
        private void PopulateWineTypesToVarietals()
        {
            if (_allWineTypes != null) {

                foreach (var wineType in _allWineTypes) {

                    string wineTypeName = wineType.Name;
                    int wineTypeId = wineType.Id;

                    if (wineTypeId == FAUX_CATEGORY_NO_SELECTION) {
                        continue;
                    }

                    var categoryMapService = new CategoryMapService();
                    categoryMapService.CategoriesFilter(CATEGORY_SHOP, wineTypeId);
                    var categoryMap = categoryMapService.Execute();

                    var matchingVarietals = (from c in categoryMap.Categories where c.Id == CATEGORY_VARIETALS select c).FirstOrDefault();
                    if (matchingVarietals != null) {
                        var varietals = new List<Refinement>();
                        varietals.Add(_noSelectionVarietals);
                        varietals.AddRange(matchingVarietals.Refinements);
                        _wineTypesToVarietals.Add(wineTypeId, varietals.ToArray());
                    }
                }
            }
        }
コード例 #9
0
        private void PopulateRegionsToAppellations()
        {
            if (_allRegions != null) {

                foreach (var region in _allRegions) {

                    string regionName = region.Name;
                    int regionId = region.Id;

                    if (regionId == FAUX_CATEGORY_NO_SELECTION) {
                        continue;
                    }

                    var categoryMapService = new CategoryMapService();
                    categoryMapService.CategoriesFilter(CATEGORY_SHOP, regionId);
                    var categoryMap = categoryMapService.Execute();

                    var matchingAppellations = (from c in categoryMap.Categories where c.Id == CATEGORY_APPELATIONS select c).FirstOrDefault();
                    if (matchingAppellations != null) {
                        var orderedMatchingAppellations = (from r in matchingAppellations.Refinements orderby r.Name select r).ToArray();
                        var appellations = new List<Refinement>();
                        appellations.Add(_noSelectionAppellations);
                        appellations.AddRange(orderedMatchingAppellations);
                        _regionsToAppellations.Add(regionId, appellations.ToArray());
                    }
                    else {
                        Refinement dummyAppellation = new Refinement();
                        dummyAppellation.Id = FAUX_CATEGORY_DUMMY_APPELLATION;
                        dummyAppellation.Name = regionName;
                        dummyAppellation.Url = string.Empty;
                        _regionsToAppellations.Add(regionId, new Refinement[] { _noSelectionAppellations, dummyAppellation });
                    }
                }

                // Now we can go ahead and build _allAppellations.

                var alphabeticalRegions = (from rr in _allRegions orderby rr.Name where rr.Id != FAUX_CATEGORY_NO_SELECTION select rr).ToArray();

                var allAppellations = new List<Refinement>();
                allAppellations.Add(_noSelectionAppellations);
                foreach (var region in alphabeticalRegions) {
                    int regionId = region.Id;
                    var appellations1 = _regionsToAppellations[regionId];
                    if (appellations1 != null) {
                        var appellations2 = (from ar in appellations1 where ar.Id != FAUX_CATEGORY_NO_SELECTION select ar).ToArray();
                        allAppellations.AddRange(appellations2);
                    }
                }
                _allAppellations = allAppellations;
            }
        }
コード例 #10
0
        private void PopulateFullLists()
        {
            var categoryMapService = new CategoryMapService();
            categoryMapService.CategoriesFilter(CATEGORY_SHOP);
            var categoryMap = categoryMapService.Execute();

            var wineTypeCategory = (from c in categoryMap.Categories where c.Id == CATEGORY_WINETYPES select c).FirstOrDefault();
            if (wineTypeCategory != null) {
                // Wine types seem to be presented in the order that they are returned in.
                var wineTypes = new List<Refinement>();
                wineTypes.Add(_noSelectionWineTypes);
                wineTypes.AddRange(wineTypeCategory.Refinements);
                _wineTypes = _allWineTypes = wineTypes;
            }

            var varietalCategory = (from c in categoryMap.Categories where c.Id == CATEGORY_VARIETALS select c).FirstOrDefault();
            if (varietalCategory != null) {
                // All Varietals seem to be ordered by Id.
                var orderedVarietals = (from r in varietalCategory.Refinements orderby r.Id select r).ToArray();
                var varietals = new List<Refinement>();
                varietals.Add(_noSelectionVarietals);
                varietals.AddRange(orderedVarietals);
                _varietals = _allVarietals = varietals;
            }

            var regionCategory = (from c in categoryMap.Categories where c.Id == CATEGORY_REGIONS select c).FirstOrDefault();
            if (regionCategory != null) {
                // All Regions seem to be presented in the order that they are returned in.
                var regions = new List<Refinement>();
                regions.Add(_noSelectionRegions);
                regions.AddRange(regionCategory.Refinements);
                _regions = _allRegions = regions;
            }

            var appellationCategory = (from c in categoryMap.Categories where c.Id == CATEGORY_APPELATIONS select c).FirstOrDefault();
            if (appellationCategory != null) {
                // All Appellations seems to be ordered as follows:
                //
                //  "All Appellations"
                //  Alphabetical list of appellations for the first region
                //  Alphabetical list of appellations for the second region
                //  ...
                //
                // where the regions are themselves ordered alphabetically.
                // An oddity is that "Greece", "Portugal" and "South Africa" are listed as appellations but "Canada" is not.
                //
                // Hence, we build _allAppellations in PopulateRegionsToAppellations() below.
            }
        }