From 28f2c2f469f47f59079155d180e085d228cbb9e3 Mon Sep 17 00:00:00 2001 From: Sergey Yershov Date: Mon, 18 Apr 2016 16:34:08 +0300 Subject: [PATCH 1/2] [downloader] Add GetGroupNodePathToRoot and test for it --- storage/storage.cpp | 31 +++++++++++++++++++++++++ storage/storage.hpp | 5 ++++ storage/storage_tests/storage_tests.cpp | 25 ++++++++++++++++++++ 3 files changed, 61 insertions(+) diff --git a/storage/storage.cpp b/storage/storage.cpp index 433ac09f9e..f21727d634 100644 --- a/storage/storage.cpp +++ b/storage/storage.cpp @@ -1562,4 +1562,35 @@ void Storage::GetQueuedChildren(TCountryId const & parent, TCountriesVec & queue queuedChildren.push_back(child.Value().Name()); }); } + +void Storage::GetGroupNodePathToRoot(TCountryId const & groupNode, TCountriesVec & path) const +{ + path.clear(); + + vector nodes; + m_countries.Find(groupNode, nodes); + if (nodes.empty()) + { + LOG(LWARNING, ("TCountryId =", groupNode, "not found in m_countries.")); + return; + } + + if (nodes.size() != 1) + { + LOG(LWARNING, (groupNode, "Group node can't have more then one parent.")); + return; + } + + if (nodes[0]->ChildrenCount() == 0) + { + LOG(LWARNING, (nodes[0]->Value().Name(), "is a leaf node.")); + return; + } + + ForEachAncestorExceptForTheRoot(nodes, [&path](TCountryId const & id, TCountryTreeNode const &) + { + path.push_back(id); + }); +} + } // namespace storage diff --git a/storage/storage.hpp b/storage/storage.hpp index 91cd9c89a8..428f9f566b 100644 --- a/storage/storage.hpp +++ b/storage/storage.hpp @@ -337,6 +337,11 @@ public: /// and will be added to |queuedChildren|. void GetQueuedChildren(TCountryId const & parent, TCountriesVec & queuedChildren) const; + /// \brief Fills |path| with list of TCountryId corresponding with path to the root of hierachy. + /// \param groupNode is start of path, can't be a leaf node. + /// \param path is resulting array of TCountryId. + void GetGroupNodePathToRoot(TCountryId const & groupNode, TCountriesVec & path) const; + /// \brief Returns current version for mwms which are used by storage. inline int64_t GetCurrentDataVersion() const { return m_currentVersion; } diff --git a/storage/storage_tests/storage_tests.cpp b/storage/storage_tests/storage_tests.cpp index e9fb1bd3ef..76e1fdf607 100644 --- a/storage/storage_tests/storage_tests.cpp +++ b/storage/storage_tests/storage_tests.cpp @@ -1644,4 +1644,29 @@ UNIT_TEST(StorageTest_GetQueuedChildrenSmokeTest) storage.GetQueuedChildren("Country1", queuedChildren); TEST(queuedChildren.empty(), ()); } + +UNIT_TEST(StorageTest_GetGroupNodePathToRootTest) +{ + Storage storage; + + TCountriesVec path; + + storage.GetGroupNodePathToRoot("France_Auvergne_Allier", path); + TEST(path.empty(), ()); + + storage.GetGroupNodePathToRoot("France_Auvergne", path); + TEST_EQUAL(path.size(), 1, (path)); + + storage.GetGroupNodePathToRoot("France", path); + TEST(path.empty(), ()); + + storage.GetGroupNodePathToRoot("US_Florida_Miami", path); + TEST(path.empty(), ()); + + storage.GetGroupNodePathToRoot("Florida", path); + TEST_EQUAL(path.size(), 1, (path)); + + storage.GetGroupNodePathToRoot("Country1", path); + TEST(path.empty(), ()); +} } // namespace storage From 884939fc74acd3987a011e6ca45480bf08156a61 Mon Sep 17 00:00:00 2001 From: Sergey Yershov Date: Mon, 18 Apr 2016 17:42:12 +0300 Subject: [PATCH 2/2] [downloader] Review fixes --- storage/storage.cpp | 3 ++- storage/storage_tests/storage_tests.cpp | 11 ++++++++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/storage/storage.cpp b/storage/storage.cpp index f21727d634..b1ee070bef 100644 --- a/storage/storage.cpp +++ b/storage/storage.cpp @@ -1577,7 +1577,7 @@ void Storage::GetGroupNodePathToRoot(TCountryId const & groupNode, TCountriesVec if (nodes.size() != 1) { - LOG(LWARNING, (groupNode, "Group node can't have more then one parent.")); + LOG(LWARNING, (groupNode, "Group node can't have more than one parent.")); return; } @@ -1591,6 +1591,7 @@ void Storage::GetGroupNodePathToRoot(TCountryId const & groupNode, TCountriesVec { path.push_back(id); }); + path.push_back(m_countries.GetRoot().Value().Name()); } } // namespace storage diff --git a/storage/storage_tests/storage_tests.cpp b/storage/storage_tests/storage_tests.cpp index 76e1fdf607..13f6a54c96 100644 --- a/storage/storage_tests/storage_tests.cpp +++ b/storage/storage_tests/storage_tests.cpp @@ -1655,16 +1655,21 @@ UNIT_TEST(StorageTest_GetGroupNodePathToRootTest) TEST(path.empty(), ()); storage.GetGroupNodePathToRoot("France_Auvergne", path); - TEST_EQUAL(path.size(), 1, (path)); + TEST_EQUAL(path.size(), 2, (path)); + TEST_EQUAL(path[0], "France", ()); + TEST_EQUAL(path[1], "Countries", ()); storage.GetGroupNodePathToRoot("France", path); - TEST(path.empty(), ()); + TEST_EQUAL(path.size(), 1, (path)); + TEST_EQUAL(path[0], "Countries", ()); storage.GetGroupNodePathToRoot("US_Florida_Miami", path); TEST(path.empty(), ()); storage.GetGroupNodePathToRoot("Florida", path); - TEST_EQUAL(path.size(), 1, (path)); + TEST_EQUAL(path.size(), 2, (path)); + TEST_EQUAL(path[0], "United States of America", ()); + TEST_EQUAL(path[1], "Countries", ()); storage.GetGroupNodePathToRoot("Country1", path); TEST(path.empty(), ());