aboutsummaryrefslogtreecommitdiff
path: root/libs/map/map_tests/countries_names_tests.cpp
blob: c7aac62556e0f79c0ec18f17d87960241146b840 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include "testing/testing.hpp"

#include "map/framework.hpp"

#include "search/categories_cache.hpp"
#include "search/downloader_search_callback.hpp"
#include "search/mwm_context.hpp"

#include "storage/storage.hpp"

#include "indexer/data_source.hpp"
#include "indexer/ftypes_matcher.hpp"
#include "indexer/mwm_set.hpp"
#include "indexer/utils.hpp"

#include "coding/string_utf8_multilang.hpp"

#include "base/cancellable.hpp"
#include "base/checked_cast.hpp"

#include <set>
#include <string>

UNIT_TEST(CountriesNamesTest)
{
  using namespace platform;
  using namespace storage;
  using namespace std;

  Framework f(FrameworkParams(false /* m_enableDiffs */));
  auto & storage = f.GetStorage();
  auto const & synonyms = storage.GetCountryNameSynonyms();

  auto handle = indexer::FindWorld(f.GetDataSource());
  TEST(handle.IsAlive(), ());

  FeaturesLoaderGuard g(f.GetDataSource(), handle.GetId());

  auto & value = *handle.GetValue();
  TEST(value.HasSearchIndex(), ());
  search::MwmContext const mwmContext(std::move(handle));
  base::Cancellable const cancellable;
  search::CategoriesCache cache(ftypes::IsLocalityChecker::Instance(), cancellable);

  int8_t const langIndices[] = {StringUtf8Multilang::kEnglishCode, StringUtf8Multilang::kDefaultCode,
                                StringUtf8Multilang::kInternationalCode};

  set<string> const kIgnoreList = {
      "Northern Cyprus",
      "Transnistria",
      "Nagorno-Karabakh Republic",
      "Republic of Artsakh",
      "Saint Helena, Ascension and Tristan da Cunha",
      "Somaliland",
  };

  auto const features = cache.Get(mwmContext);
  features.ForEach([&](uint64_t fid)
  {
    auto ft = g.GetFeatureByIndex(base::asserted_cast<uint32_t>(fid));
    TEST(ft, ());

    ftypes::LocalityType const type = ftypes::IsLocalityChecker::Instance().GetType(*ft);
    if (type != ftypes::LocalityType::Country)
      return;

    bool found = false;
    for (auto const lang : langIndices)
    {
      std::string const name(ft->GetName(lang));
      if (!name.empty())
      {
        auto const it = synonyms.find(name);
        if (it == synonyms.end())
          found = storage.IsNode(name) || kIgnoreList.count(name) != 0;
        else
          found = storage.IsNode(it->second);

        if (found)
          break;
      }
    }

    // If this test fails, most likely somebody added fake place=country object into OSM.
    TEST(found, ("Cannot find countries.json record for country feature:", ft->DebugString()));
  });
}