Skip to content

Commit 21982e0

Browse files
authored
Merge pull request #1122 from joto/middle-refactorings-and-tests2-ways
Test adding/changing/deleting ways in middle
2 parents a1ff1ac + 16cf8f0 commit 21982e0

1 file changed

Lines changed: 214 additions & 62 deletions

File tree

tests/test-middle.cpp

Lines changed: 214 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ static pg::tempdb_t db;
1414

1515
namespace {
1616

17-
/// Simple osmium buffer to store object with some convenience.
17+
/**
18+
* Wrapper around an Osmium buffer to create test objects in with some
19+
* convenience.
20+
*/
1821
class test_buffer_t
1922
{
2023
public:
@@ -24,10 +27,13 @@ class test_buffer_t
2427
return osmium::builder::add_node(buf, _id(id), _location(lon, lat));
2528
}
2629

27-
size_t add_way(osmid_t wid, idlist_t const &ids)
30+
size_t
31+
add_way(osmid_t wid, idlist_t const &ids,
32+
std::initializer_list<std::pair<char const *, char const *>> tags)
2833
{
2934
using namespace osmium::builder::attr;
30-
return osmium::builder::add_way(buf, _id(wid), _nodes(ids));
35+
return osmium::builder::add_way(buf, _id(wid), _nodes(ids),
36+
_tags(tags));
3137
}
3238

3339
size_t add_relation(
@@ -62,9 +68,11 @@ class test_buffer_t
6268
return get<osmium::Node>(add_node(id, lon, lat));
6369
}
6470

65-
osmium::Way &add_way_and_get(osmid_t wid, idlist_t const &ids)
71+
osmium::Way &add_way_and_get(
72+
osmid_t wid, idlist_t const &ids,
73+
std::initializer_list<std::pair<char const *, char const *>> tags = {})
6674
{
67-
return get<osmium::Way>(add_way(wid, ids));
75+
return get<osmium::Way>(add_way(wid, ids, tags));
6876
}
6977

7078
osmium::Relation &add_relation_and_get(
@@ -464,6 +472,36 @@ TEMPLATE_TEST_CASE("middle add, delete and update node", "",
464472
}
465473
}
466474

475+
/**
476+
* Check that the way is in the mid with the right attributes and tags.
477+
* Does not check node locations.
478+
*/
479+
static void check_way(std::shared_ptr<middle_pgsql_t> const &mid,
480+
osmium::Way const &orig_way)
481+
{
482+
auto const mid_q = mid->get_query_instance();
483+
484+
osmium::memory::Buffer outbuf{4096, osmium::memory::Buffer::auto_grow::yes};
485+
REQUIRE(mid_q->ways_get(orig_way.id(), outbuf));
486+
auto const &way = outbuf.get<osmium::Way>(0);
487+
488+
osmium::CRC<osmium::CRC_zlib> orig_way_crc;
489+
orig_way_crc.update(orig_way);
490+
491+
osmium::CRC<osmium::CRC_zlib> test_way_crc;
492+
test_way_crc.update(way);
493+
494+
REQUIRE(orig_way_crc().checksum() == test_way_crc().checksum());
495+
}
496+
497+
/// Return true if the way with the specified id is not in the mid
498+
static bool no_way(std::shared_ptr<middle_pgsql_t> const &mid, osmid_t id)
499+
{
500+
auto const mid_q = mid->get_query_instance();
501+
osmium::memory::Buffer outbuf{4096, osmium::memory::Buffer::auto_grow::yes};
502+
return !mid_q->ways_get(id, outbuf);
503+
}
504+
467505
TEMPLATE_TEST_CASE("middle: add, delete and update way", "",
468506
options_slim_default, options_slim_dense_cache,
469507
options_flat_node_cache)
@@ -473,96 +511,210 @@ TEMPLATE_TEST_CASE("middle: add, delete and update way", "",
473511
testing::cleanup::file_t flatnode_cleaner{
474512
options.flat_node_file.get_value_or("")};
475513

476-
osmid_t const way_id = 20;
477-
478-
// create some nodes we'll use for the ways
514+
// create some ways we'll use for the tests
479515
test_buffer_t buffer;
480-
auto const& node10 = buffer.add_node_and_get(10, 1.0, 0.0);
481-
auto const& node11 = buffer.add_node_and_get(11, 1.1, 0.0);
482-
auto const& node12 = buffer.add_node_and_get(12, 1.2, 0.0);
516+
auto const &way20 = buffer.add_way_and_get(
517+
20, {10, 11}, {{"highway", "residential"}, {"name", "High Street"}});
483518

484-
// Set up middle in "create" mode to get a cleanly initialized database
485-
// and add some nodes.
519+
auto const &way21 = buffer.add_way_and_get(21, {11, 12});
520+
521+
auto const &way22 =
522+
buffer.add_way_and_get(22, {12, 10}, {{"power", "line"}});
523+
524+
auto const &way20a = buffer.add_way_and_get(
525+
20, {10, 12}, {{"highway", "primary"}, {"name", "High Street"}});
526+
527+
// Set up middle in "create" mode to get a cleanly initialized database and
528+
// add some ways. Does this in its own scope so that the mid is closed
529+
// properly.
486530
{
487531
auto mid = std::make_shared<middle_pgsql_t>(&options);
488532
mid->start();
489533

490-
mid->nodes_set(node10);
491-
mid->nodes_set(node11);
492-
mid->nodes_set(node12);
534+
mid->ways_set(way20);
535+
mid->ways_set(way21);
493536
mid->flush();
494537

495-
// create way
496-
idlist_t nds{10, 11};
497-
mid->ways_set(buffer.add_way_and_get(way_id, nds));
498-
mid->flush();
499-
500-
osmium::memory::Buffer outbuf{4096,
501-
osmium::memory::Buffer::auto_grow::yes};
538+
check_way(mid, way20);
539+
check_way(mid, way21);
502540

503-
auto const mid_q = mid->get_query_instance();
504-
REQUIRE(mid_q->ways_get(way_id, outbuf));
541+
mid->commit();
542+
}
505543

506-
auto &way = outbuf.get<osmium::Way>(0);
544+
// From now on use append mode to not destroy the data we just added.
545+
options.append = true;
507546

508-
REQUIRE(way.id() == way_id);
509-
REQUIRE(way.nodes().size() == nds.size());
510-
REQUIRE(way.nodes()[0].ref() == 10);
511-
REQUIRE(way.nodes()[1].ref() == 11);
547+
SECTION("Check that added ways are there and no others")
548+
{
549+
auto mid = std::make_shared<middle_pgsql_t>(&options);
550+
mid->start();
512551

513-
REQUIRE(mid_q->nodes_get_list(&(way.nodes())) == nds.size());
514-
REQUIRE(way.nodes()[0].location() == osmium::Location{1.0, 0.0});
515-
REQUIRE(way.nodes()[1].location() == osmium::Location{1.1, 0.0});
552+
REQUIRE(no_way(mid, 5));
553+
check_way(mid, way20);
554+
check_way(mid, way21);
555+
REQUIRE(no_way(mid, 22));
516556

517557
mid->commit();
518558
}
519559

520-
// Set up middle again, this time in "append" mode so we can change things.
560+
SECTION("Delete existing and non-existing way")
521561
{
522-
options.append = true;
523-
auto mid = std::make_shared<middle_pgsql_t>(&options);
524-
mid->start();
562+
{
563+
auto mid = std::make_shared<middle_pgsql_t>(&options);
564+
mid->start();
525565

526-
// delete way
527-
mid->ways_delete(way_id);
528-
mid->flush();
566+
mid->ways_delete(5);
567+
mid->ways_delete(20);
568+
mid->ways_delete(42);
569+
mid->flush();
529570

530-
osmium::memory::Buffer outbuf{4096,
531-
osmium::memory::Buffer::auto_grow::yes};
571+
REQUIRE(no_way(mid, 5));
572+
REQUIRE(no_way(mid, 20));
573+
check_way(mid, way21);
574+
REQUIRE(no_way(mid, 42));
532575

533-
auto const mid_q = mid->get_query_instance();
534-
REQUIRE_FALSE(mid_q->ways_get(way_id, outbuf));
576+
mid->commit();
577+
}
578+
{
579+
// Check with a new mid
580+
auto mid = std::make_shared<middle_pgsql_t>(&options);
581+
mid->start();
535582

536-
mid->commit();
583+
REQUIRE(no_way(mid, 5));
584+
REQUIRE(no_way(mid, 20));
585+
check_way(mid, way21);
586+
REQUIRE(no_way(mid, 42));
587+
588+
mid->commit();
589+
}
537590
}
538591

539-
// Set up middle again, still in "append" mode.
592+
SECTION("Change (delete and set) existing way and non-existing way")
593+
{
594+
{
595+
auto mid = std::make_shared<middle_pgsql_t>(&options);
596+
mid->start();
597+
598+
mid->ways_delete(20);
599+
mid->ways_set(way20a);
600+
mid->ways_delete(22);
601+
mid->ways_set(way22);
602+
mid->flush();
603+
604+
REQUIRE(no_way(mid, 5));
605+
check_way(mid, way20a);
606+
check_way(mid, way21);
607+
check_way(mid, way22);
608+
REQUIRE(no_way(mid, 42));
609+
610+
mid->commit();
611+
}
612+
{
613+
// Check with a new mid
614+
auto mid = std::make_shared<middle_pgsql_t>(&options);
615+
mid->start();
616+
617+
REQUIRE(no_way(mid, 5));
618+
check_way(mid, way20a);
619+
check_way(mid, way21);
620+
check_way(mid, way22);
621+
REQUIRE(no_way(mid, 42));
622+
623+
mid->commit();
624+
}
625+
}
626+
627+
SECTION("Add new way")
628+
{
629+
{
630+
auto mid = std::make_shared<middle_pgsql_t>(&options);
631+
mid->start();
632+
633+
mid->ways_set(way22);
634+
mid->flush();
635+
636+
REQUIRE(no_way(mid, 5));
637+
check_way(mid, way20);
638+
check_way(mid, way21);
639+
check_way(mid, way22);
640+
REQUIRE(no_way(mid, 42));
641+
642+
mid->commit();
643+
}
644+
{
645+
// Check with a new mid
646+
auto mid = std::make_shared<middle_pgsql_t>(&options);
647+
mid->start();
648+
649+
REQUIRE(no_way(mid, 5));
650+
check_way(mid, way20);
651+
check_way(mid, way21);
652+
check_way(mid, way22);
653+
REQUIRE(no_way(mid, 42));
654+
655+
mid->commit();
656+
}
657+
}
658+
}
659+
660+
TEMPLATE_TEST_CASE("middle: add way with attributes", "", options_slim_default,
661+
options_slim_dense_cache, options_flat_node_cache)
662+
{
663+
options_t options = TestType::options(db);
664+
665+
SECTION("with attributes") { options.extra_attributes = true; }
666+
SECTION("no attributes") { options.extra_attributes = false; }
667+
668+
testing::cleanup::file_t flatnode_cleaner{
669+
options.flat_node_file.get_value_or("")};
670+
671+
// create some ways we'll use for the tests
672+
test_buffer_t buffer;
673+
auto &way20 = buffer.add_way_and_get(
674+
20, {10, 11}, {{"highway", "residential"}, {"name", "High Street"}});
675+
way20.set_version(123);
676+
way20.set_timestamp(1234567890);
677+
way20.set_changeset(456);
678+
way20.set_uid(789);
679+
680+
// the same way but with default attributes
681+
auto &way20_no_attr = buffer.add_way_and_get(
682+
20, {10, 11}, {{"highway", "residential"}, {"name", "High Street"}});
683+
684+
// the same way but with attributes in tags
685+
// the order of the tags is important here
686+
auto &way20_attr_tags =
687+
buffer.add_way_and_get(20, {10, 11},
688+
{{"highway", "residential"},
689+
{"name", "High Street"},
690+
{"osm_user", ""},
691+
{"osm_uid", "789"},
692+
{"osm_version", "123"},
693+
{"osm_timestamp", "2009-02-13T23:31:30Z"},
694+
{"osm_changeset", "456"}});
695+
540696
{
541697
auto mid = std::make_shared<middle_pgsql_t>(&options);
542698
mid->start();
543699

544-
idlist_t nds{11, 12};
545-
546-
// create new version of the way
547-
mid->ways_set(buffer.add_way_and_get(way_id, nds));
700+
mid->ways_set(way20);
548701
mid->flush();
549702

550-
osmium::memory::Buffer outbuf{4096,
551-
osmium::memory::Buffer::auto_grow::yes};
703+
check_way(mid,
704+
options.extra_attributes ? way20_attr_tags : way20_no_attr);
552705

553-
auto const mid_q = mid->get_query_instance();
554-
REQUIRE(mid_q->ways_get(way_id, outbuf));
706+
mid->commit();
707+
}
555708

556-
auto &way = outbuf.get<osmium::Way>(0);
709+
// From now on use append mode to not destroy the data we just added.
710+
options.append = true;
557711

558-
REQUIRE(way.id() == way_id);
559-
REQUIRE(way.nodes().size() == nds.size());
560-
REQUIRE(way.nodes()[0].ref() == 11);
561-
REQUIRE(way.nodes()[1].ref() == 12);
712+
{
713+
auto mid = std::make_shared<middle_pgsql_t>(&options);
714+
mid->start();
562715

563-
REQUIRE(mid_q->nodes_get_list(&(way.nodes())) == nds.size());
564-
REQUIRE(way.nodes()[0].location() == osmium::Location{1.1, 0.0});
565-
REQUIRE(way.nodes()[1].location() == osmium::Location{1.2, 0.0});
716+
check_way(mid,
717+
options.extra_attributes ? way20_attr_tags : way20_no_attr);
566718

567719
mid->commit();
568720
}

0 commit comments

Comments
 (0)