Ejemplo n.º 1
0
        public void SearchTest()
        {
            APIKey      apiKey  = new APIKey("AIzaSyC3evyffluu_gsQxMJq0ljpCrsFdfldLoM"); //TODO Use using here for efficiency
            CityFinder  finder  = new CityFinder(apiKey);
            ICityResult results = (CityResult)finder.Search("mid");

            ICollection <string> nextLetters = new List <string>();
            ICollection <string> nextCities  = new List <string>();

            nextCities.Add("Midland");
            nextCities.Add("Midrand");
            nextCities.Add("Midlothian");
            nextCities.Add("Middlesbrough");
            nextCities.Add("Middletown");

            nextLetters.Add("l");
            nextLetters.Add("r");
            nextLetters.Add("l");
            nextLetters.Add("d");
            nextLetters.Add("d");

            ICityResult testResults = new CityResult(nextLetters, nextCities);

            for (int i = 0; i > nextCities.Count; i++)
            {
                Assert.AreEqual(testResults.NextCities.ElementAt(i), results.NextCities.ElementAt(i));
            }

            for (int i = 0; i > nextLetters.Count; i++)
            {
                Assert.AreEqual(testResults.NextLetters.ElementAt(i), results.NextLetters.ElementAt(i));
            }
        }
Ejemplo n.º 2
0
        public void TestCitiesGivenEmptyString()
        {
            //arrange
            string      searchTerm = "";
            ICityResult city       = new CityResult();

            for (int i = 0; i < 1000000; i++)
            {
                city.NextCities.Add(i.ToString());
            }

            city.NextCities.Add("BANDUNG");
            city.NextCities.Add("BANGUI");
            city.NextCities.Add("BANGKOK");
            city.NextCities.Add("BANGALORE");
            city.NextCities.Add("LA PAZ");
            city.NextCities.Add("LA PLATA");
            city.NextCities.Add("LAGOS");
            city.NextCities.Add("LEEDS");
            city.NextCities.Add("ZARIA");
            city.NextCities.Add("ZHUGHAI");
            city.NextCities.Add("ZIBO");

            ICityFinder finder = new CityFinder();

            //act
            var result = finder.Search(searchTerm);

            //assert
            CollectionAssert.AreEqual(city.NextCities.ToList(), result.NextCities.ToList());
        }
Ejemplo n.º 3
0
        /// <summary>
        /// function to add city to result
        /// </summary>
        /// <param name="source"></param>
        /// <param name="desCity"></param>
        protected void AddPathToResult(City source, City desCity)
        {
            var result = new CityResult(source, desCity);

            this.solverResult.CityQueue.Enqueue(result);
            this.solverResult.TotalDistance += this.GetDistanceBetweenCity(source, desCity);
        }
Ejemplo n.º 4
0
        public async Task InvalidCasing_MatchesAreNotCaseSensitive_ReturnsMatches()
        {
            // Given
            var mockDataReadModel = Substitute.For <IDataReadModel>();

            mockDataReadModel.GetData().Returns(new List <string>
            {
                "London", "Londonderry"
            });

            var citySearch = new CitySearch(mockDataReadModel, new CityCharacterValidator(), new Trie());
            await citySearch.Initialise();

            var expectedResult = new CityResult
            {
                NextCities  = new[] { "London", "Londonderry" },
                NextLetters = new[] { "d" }
            };

            // When
            var results = citySearch.Search("lon");

            // Then
            results.ShouldBeEquivalentTo(expectedResult);
        }
Ejemplo n.º 5
0
        public async Task InvalidCharactersInSet_OnlyCitiesWithValidCharactersReturned()
        {
            //Given
            var mockDataReadModel = Substitute.For <IDataReadModel>();

            mockDataReadModel.GetData().Returns(new List <string>
            {
                "Valid", "In%Valid", "In3Valid", "Val*id"
            });

            var citySearch = new CitySearch(mockDataReadModel, new CityCharacterValidator(), new Trie());
            await citySearch.Initialise();

            var expectedResult = new CityResult
            {
                NextCities  = new[] { "Valid" },
                NextLetters = new[] { "a" }
            };

            //When
            var results = citySearch.Search("V");

            //Then
            results.ShouldBeEquivalentTo(expectedResult);
        }
        public async Task GivenZE_ResultsShouldMatchExpectations()
        {
            //Given
            var mockDataReadModel = Substitute.For <IDataReadModel>();

            mockDataReadModel.GetData().Returns(new List <string>
            {
                "ZARIA", "ZHUGHAI", "ZIBO"
            });

            var citySearch = new CitySearch(mockDataReadModel, new CityCharacterValidator(), new Trie());
            await citySearch.Initialise();

            var expectedResult = new CityResult
            {
                NextCities  = new string[0],
                NextLetters = new string[0]
            };

            //When
            var results = citySearch.Search("ZE");

            //Then
            results.ShouldBeEquivalentTo(expectedResult);
        }
        public async Task GivenLA_ResultsShouldMatchExpectations()
        {
            //Given
            var mockDataReadModel = Substitute.For <IDataReadModel>();

            mockDataReadModel.GetData().Returns(new List <string>
            {
                "LA PAZ", "LA PLATA", "LAGOS", "LEEDS"
            });

            var citySearch = new CitySearch(mockDataReadModel, new CityCharacterValidator(), new Trie());
            await citySearch.Initialise();

            var expectedResult = new CityResult
            {
                NextCities  = new[] { "LA PAZ", "LA PLATA", "LAGOS" },
                NextLetters = new[] { " ", "G" }
            };

            //When
            var results = citySearch.Search("LA");

            //Then
            results.ShouldBeEquivalentTo(expectedResult);
        }
Ejemplo n.º 8
0
        public async Task NextLetter_HyphenReturned_AsValidNextLetter()
        {
            // Given
            var mockDataReadModel = Substitute.For <IDataReadModel>();

            mockDataReadModel.GetData().Returns(new List <string>
            {
                "Stockton-on-Tees"
            });

            var citySearch = new CitySearch(mockDataReadModel, new CityCharacterValidator(), new Trie());
            await citySearch.Initialise();

            var expectedResult = new CityResult
            {
                NextCities  = new[] { "Stockton-on-Tees" },
                NextLetters = new[] { "-" }
            };

            // When
            var results = citySearch.Search("Stockton");

            // Then
            results.ShouldBeEquivalentTo(expectedResult);
        }
Ejemplo n.º 9
0
        public void TestQueryRetrieve()
        {
            InitDatabase db     = new InitDatabase();
            CityResult   result = new CityResult();

            result = (CityResult)db.Search("Bang");

            Assert.IsTrue(result.NextCities.Count > 0);
            Assert.IsTrue(result.NextLetters.Count > 0);
        }
Ejemplo n.º 10
0
 public void Setup()
 {
     cities = new List <string> {
         "Manchester", "Leeds", "Newcastle"
     };
     letters = new List <string> {
         "a", "e", "u"
     };
     this.cityResult = new CityResult(letters, cities);
 }
Ejemplo n.º 11
0
        public HashSet <CityResult> ProcessAttendants(HashSet <Attende> attendes)
        {
            //the date provided by the attende is in the valid range. otherwise, ignore
            Parallel.ForEach(attendes, (attende) => {
                attende.Dates.RemoveAll(t => !(t.CompareTo(_startDate) >= 0 && t.CompareTo(_endDate) <= 0));
            });

            //Object to be retrieved
            var result  = new HashSet <CityResult>();
            var grouped = attendes.GroupBy(t => t.City);

            //I have grouped cities. Every loop contains the info of that city summarized
            Parallel.ForEach(grouped, (city) =>
            {
                var cityResult = new HashSet <CityResult>();

                //Select all the possible dates for that city
                var dates = city.SelectMany(t => t.Dates).Distinct().ToHashSet();
                foreach (var date in dates)
                {
                    //I need to assess every possible day.
                    var record = new CityResult()
                    {
                        City       = city.Key,
                        StartDate  = date,
                        EndDate    = date.AddDays(1),
                        Attendants = new List <string>()
                    };

                    // the possible attendants for every consecutive day
                    record.Attendants.AddRange(
                        city.Where(t => t.Dates.Contains(record.StartDate) || t.Dates.Contains(record.EndDate))
                        .Select(t => t.Email));
                    //if there are attendants that can come to BOTH days, they should count twice in the final count.
                    record.Score = record.Attendants.Count + city.Where(t => t.Dates.Contains(record.StartDate) && t.Dates.Contains(record.EndDate)).Count();

                    //At this point, I have summarized all the possible attendes for a specific city in a specific day
                    cityResult.Add(record);
                }

                //At this point, I have summarized all the possible attendes for a specific city in every possible date.
                //So I will choose the date range with the biggest amount of attendants/score
                //NOTE: I am considering that only one event per city is allowed. If more than one event per city is required, the following line should be removed.
                result.Add(cityResult.OrderByDescending(t => t.Attendants.Count).ThenByDescending(t => t.Score).FirstOrDefault());
            });

            //At this point, I have choosen the best date for the event in every city. Now I need to select the top 5 cities.
            return(result.OrderByDescending(t => t.Attendants.Count).ThenByDescending(t => t.Score).Take(5).ToHashSet());
        }
        public void WithSearchStringBANGK_ShouldReturn_A_O(string searchString)
        {
            ICollection <string> expectedCities = new Collection <string>();

            ICollection <string> expectedLetters = new Collection <string> {
                "A",
                "O",
            };

            ICityResult expect = new CityResult(expectedCities, expectedLetters);

            ICityFinder cityFinder = new CityFinder();
            ICityResult actual     = cityFinder.Search(searchString);

            Assert.IsTrue(expect.NextLetters.SequenceEqual(actual.NextLetters));
        }
Ejemplo n.º 13
0
        public void TestCitiesZe()
        {
            //arrange
            string      searchTerm = "ze";
            ICityResult city       = new CityResult();

            city.NextCities = new Collection <string>()
            {
            };
            ICityFinder finder = new CityFinder();

            //act
            var result = finder.Search(searchTerm);

            //assert
            CollectionAssert.AreEqual(city.NextCities.ToList(), result.NextCities.ToList());
        }
Ejemplo n.º 14
0
        public void TestLettersGivenFullCity()
        {
            //arrange
            string      searchTerm = "zhughai";
            ICityResult city       = new CityResult();

            city.NextLetters = new Collection <string>()
            {
            };
            ICityFinder finder = new CityFinder();

            //act
            var result = finder.Search(searchTerm);

            //assert
            CollectionAssert.AreEqual(city.NextLetters.ToList(), result.NextLetters.ToList());
        }
Ejemplo n.º 15
0
        public void TestCitiesBang()
        {
            //arrange
            string      searchTerm = "bang";
            ICityResult city       = new CityResult();

            city.NextCities = new Collection <string>()
            {
                "BANGUI", "BANGKOK", "BANGALORE"
            };
            ICityFinder finder = new CityFinder();

            //act
            var result = finder.Search(searchTerm);

            //assert
            CollectionAssert.AreEqual(city.NextCities.ToList(), result.NextCities.ToList());
        }
Ejemplo n.º 16
0
        public void TestLettersGivenEmptyString()
        {
            //arrange
            string      searchTerm = "";
            ICityResult city       = new CityResult();

            city.NextLetters = new Collection <string>()
            {
                "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "B", "L", "Z"
            };
            ICityFinder finder = new CityFinder();

            //act
            var result = finder.Search(searchTerm);

            //assert
            CollectionAssert.AreEqual(city.NextLetters.ToList(), result.NextLetters.ToList());
        }
Ejemplo n.º 17
0
        public void TestLettersBang()
        {
            //arrange
            string      searchTerm = "bang";
            ICityResult city       = new CityResult();

            city.NextLetters = new Collection <string>()
            {
                "U", "K", "A"
            };
            ICityFinder finder = new CityFinder();

            //act
            var result = finder.Search(searchTerm);

            //assert
            CollectionAssert.AreEqual(city.NextLetters.ToList(), result.NextLetters.ToList());
        }
        public string GetSpeciesListAsync([FromBody] CityResult res)//获取单一城市植物物种
        {
            List <allpoint> citySpecies = new List <allpoint>();

            citySpecies = _context.allpoint.Where(a => a.站点 == res.city).ToList();
            IEnumerable <IGrouping <string, allpoint> > plant = citySpecies.GroupBy(u => u.物名);
            List <SpieciesResult> species = new List <SpieciesResult>();

            foreach (IGrouping <string, allpoint> item in plant)
            {
                List <allpoint> plantlist   = item.ToList();
                SpieciesResult  speciesname = new SpieciesResult();
                speciesname.species = plantlist[0].物名;
                species.Add(speciesname);
            }
            string citySpeciesList = Newtonsoft.Json.JsonConvert.SerializeObject(species);

            return(citySpeciesList);
        }
        public string GetYearListAsync([FromBody] SpieciesResult spi)
        {
            List <allpoint> citySpecies = new List <allpoint>();

            citySpecies = _context.allpoint.Where(a => a.物名 == spi.species).ToList();
            IEnumerable <IGrouping <string, allpoint> > plant = citySpecies.GroupBy(u => u.年份);
            List <CityResult> year = new List <CityResult>();

            foreach (IGrouping <string, allpoint> item in plant)
            {
                List <allpoint> plantlist = item.ToList();
                CityResult      yearname  = new CityResult();
                yearname.city = plantlist[0].年份;
                year.Add(yearname);
            }
            var    yearo        = year.OrderByDescending(u => u.city);
            string cityYearList = Newtonsoft.Json.JsonConvert.SerializeObject(yearo);

            return(cityYearList);
        }
        public async Task GivenBang_ResultsShouldMatchExpectations()
        {
            var mockDataReadModel = Substitute.For <IDataReadModel>();

            mockDataReadModel.GetData().Returns(new List <string>
            {
                "BANDUNG", "BANGUI", "BANGKOK", "BANGALORE"
            });

            var citySearch = new CitySearch(mockDataReadModel, new CityCharacterValidator(), new Trie());
            await citySearch.Initialise();

            var expectedResult = new CityResult
            {
                NextCities  = new[] { "BANGUI", "BANGKOK", "BANGALORE" },
                NextLetters = new[] { "U", "K", "A" }
            };

            var results = citySearch.Search("BANG");

            results.ShouldBeEquivalentTo(expectedResult);
        }
        public async Task <IActionResult> stationsAnalysis(string species, string year)
        {
            var username = _httpContextAccessor.HttpContext.User.FindFirst(ClaimTypes.Name).Value;

            ViewData["UserName"] = username;

            var Stationslist = await GetStationLocation(species);

            ViewBag.stationsLocation = Newtonsoft.Json.JsonConvert.SerializeObject(Stationslist);

            List <allpoint> citySpecies = new List <allpoint>();

            citySpecies = _context.allpoint.Where(a => a.物名 == species).ToList();
            IEnumerable <IGrouping <string, allpoint> > plantl = citySpecies.GroupBy(u => u.年份);
            List <CityResult> yearl = new List <CityResult>();

            foreach (IGrouping <string, allpoint> item in plantl)
            {
                List <allpoint> plantlist = item.ToList();
                CityResult      yearname  = new CityResult();
                yearname.city = plantlist[0].年份;
                yearl.Add(yearname);
            }
            var yearo = yearl.OrderByDescending(u => u.city);

            ViewBag.Species     = species;
            ViewBag.yearSpecies = Newtonsoft.Json.JsonConvert.SerializeObject(yearo);
            ViewBag.year        = year;

            var onlyOnePlant = await GetYearDistAsync(species, year);

            if (onlyOnePlant != null)
            {
                List <plantResult> plantsData = new List <plantResult>();
                foreach (var item in onlyOnePlant)
                {
                    plantResult plant = new plantResult();
                    plant.datax1  = convertDate(item.叶芽开始膨大期);
                    plant.datax2  = convertDate(item.叶芽开放期);
                    plant.datax3  = convertDate(item.花芽开始膨大期);
                    plant.datax4  = convertDate(item.花芽开放期);
                    plant.datax5  = convertDate(item.开始展叶期);
                    plant.datax6  = convertDate(item.展叶盛期);
                    plant.datax7  = convertDate(item.花序或花蕾出现期);
                    plant.datax8  = convertDate(item.开花始期);
                    plant.datax9  = convertDate(item.开花盛期);
                    plant.datax10 = convertDate(item.开花末期);
                    plant.datax11 = convertDate(item.第二次开花期);
                    plant.datax12 = convertDate(item.果实成熟期);
                    plant.datax13 = convertDate(item.果实脱落开始期);
                    plant.datax14 = convertDate(item.果实脱落末期);
                    plant.datax15 = convertDate(item.叶开始变色期);
                    plant.datax16 = convertDate(item.叶全部变色期);
                    plant.datax17 = convertDate(item.开始落叶期);
                    plant.datax18 = convertDate(item.落叶末期);
                    plant.datay   = item.年份 + '年';
                    plant.species = species;
                    plant.city    = item.站点;
                    plantsData.Add(plant);
                }
                ViewBag.onlyPlant = Newtonsoft.Json.JsonConvert.SerializeObject(plantsData);
            }
            return(View());
        }