-
-
Notifications
You must be signed in to change notification settings - Fork 477
/
Copy pathtest-middle.cpp
1283 lines (1039 loc) · 40.1 KB
/
test-middle.cpp
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* SPDX-License-Identifier: GPL-2.0-or-later
*
* This file is part of osm2pgsql (https://osm2pgsql.org/).
*
* Copyright (C) 2006-2025 by the osm2pgsql developer community.
* For a full list of authors see the git log.
*/
#include <catch.hpp>
#include <algorithm>
#include <array>
#include <osmium/osm/crc.hpp>
#include <osmium/osm/crc_zlib.hpp>
#include "middle-pgsql.hpp"
#include "middle-ram.hpp"
#include "osmdata.hpp"
#include "output-null.hpp"
#include "output-requirements.hpp"
#include "common-buffer.hpp"
#include "common-cleanup.hpp"
#include "common-options.hpp"
#include "common-pg.hpp"
namespace {
testing::pg::tempdb_t db;
void check_locations_are_equal(osmium::Location a, osmium::Location b)
{
CHECK(a.lat() == Approx(b.lat()));
CHECK(a.lon() == Approx(b.lon()));
}
} // anonymous namespace
struct options_slim_default
{
static options_t options(testing::pg::tempdb_t const &tmpdb)
{
return testing::opt_t().slim(tmpdb);
}
};
struct options_slim_with_lc_prefix
{
static options_t options(testing::pg::tempdb_t const &tmpdb)
{
options_t o = testing::opt_t().slim(tmpdb);
o.prefix = "pre";
return o;
}
};
struct options_slim_with_uc_prefix
{
static options_t options(testing::pg::tempdb_t const &tmpdb)
{
options_t o = testing::opt_t().slim(tmpdb);
o.prefix = "PRE";
return o;
}
};
struct options_slim_with_schema
{
static options_t options(testing::pg::tempdb_t const &tmpdb)
{
options_t o = testing::opt_t().slim(tmpdb);
o.middle_dbschema = "osm";
return o;
}
};
struct options_flat_node_cache
{
static options_t options(testing::pg::tempdb_t const &tmpdb)
{
return testing::opt_t().slim(tmpdb).flatnodes();
}
};
struct options_ram_optimized
{
static options_t options(testing::pg::tempdb_t const &)
{
return testing::opt_t();
}
};
TEMPLATE_TEST_CASE("middle import", "", options_slim_default,
options_slim_with_lc_prefix, options_slim_with_uc_prefix,
options_slim_with_schema, options_ram_optimized)
{
options_t const options = TestType::options(db);
testing::cleanup::file_t const flatnode_cleaner{options.flat_node_file};
auto conn = db.connect();
auto const num_tables =
conn.get_count("pg_catalog.pg_tables", "schemaname = 'public'");
auto const num_indexes =
conn.get_count("pg_catalog.pg_indexes", "schemaname = 'public'");
auto const num_procs =
conn.get_count("pg_catalog.pg_proc",
"pronamespace = (SELECT oid FROM "
"pg_catalog.pg_namespace WHERE nspname = 'public')");
if (options.middle_dbschema == "osm") {
conn.exec("CREATE SCHEMA IF NOT EXISTS osm;");
}
auto thread_pool = std::make_shared<thread_pool_t>(1U);
auto mid = create_middle(thread_pool, options);
mid->start();
output_requirements requirements;
requirements.full_ways = true;
requirements.full_relations = true;
mid->set_requirements(requirements);
auto const mid_q = mid->get_query_instance();
test_buffer_t buffer;
SECTION("Set and retrieve a single node")
{
auto const &node = buffer.add_node("n1234 x98.7654321 y12.3456789");
// set the node
mid->node(node);
mid->after_nodes();
mid->after_ways();
mid->after_relations();
// getting it back works only via a waylist
auto &nodes = buffer.add_way("w3 Nn1234").nodes();
// get it back
REQUIRE(mid_q->nodes_get_list(&nodes) == nodes.size());
check_locations_are_equal(nodes[0].location(), node.location());
// other nodes are not retrievable
auto &n2 = buffer.add_way("w3 Nn1,n2,n1235").nodes();
REQUIRE(mid_q->nodes_get_list(&n2) == 0);
// check the same thing again using get_node_location() function
check_locations_are_equal(mid_q->get_node_location(1234),
node.location());
CHECK_FALSE(mid_q->get_node_location(1).valid());
CHECK_FALSE(mid_q->get_node_location(2).valid());
CHECK_FALSE(mid_q->get_node_location(1235).valid());
}
SECTION("Set and retrieve a single way")
{
osmid_t const way_id = 1;
double const lon = 98.7654321;
double const lat = 12.3456789;
idlist_t nds;
// set nodes
for (osmid_t i = 1; i <= 10; ++i) {
nds.push_back(i);
auto const &node = buffer.add_node(fmt::format(
"n{} x{:.7f} y{:.7f}", i, lon - static_cast<double>(i) * 0.003,
lat + static_cast<double>(i) * 0.001));
mid->node(node);
}
mid->after_nodes();
// set the way
mid->way(buffer.add_way(way_id, nds));
mid->after_ways();
mid->after_relations();
// get it back
osmium::memory::Buffer outbuf{4096,
osmium::memory::Buffer::auto_grow::yes};
REQUIRE(mid_q->way_get(way_id, &outbuf));
auto &way = outbuf.get<osmium::Way>(0);
CHECK(way.id() == way_id);
REQUIRE(way.nodes().size() == nds.size());
REQUIRE(mid_q->nodes_get_list(&(way.nodes())) == nds.size());
for (osmid_t i = 1; i <= 10; ++i) {
auto const &nr = way.nodes()[static_cast<size_t>(i) - 1];
CHECK(nr.ref() == i);
CHECK(nr.location().lon() == Approx(lon - i * 0.003));
CHECK(nr.location().lat() == Approx(lat + i * 0.001));
}
// other ways are not retrievable
REQUIRE_FALSE(mid_q->way_get(way_id + 1, &outbuf));
}
SECTION("Set and retrieve a single relation with supporting ways")
{
std::array<idlist_t, 3> const nds = {
{{4, 5, 13, 14, 342}, {45, 90}, {30, 3, 45}}};
// set the node
mid->node(buffer.add_node("n1 x4.1 y12.8"));
mid->after_nodes();
// set the ways
osmid_t wid = 10;
for (auto const &n : nds) {
mid->way(buffer.add_way(wid, n));
++wid;
}
mid->after_ways();
// set the relation
auto const &relation =
buffer.add_relation("r123 Mw11@,w10@outer,n1@,w12@inner");
std::vector<std::pair<osmium::item_type, osmid_t>> const expected = {
{osmium::item_type::way, 11},
{osmium::item_type::way, 10},
{osmium::item_type::node, 1},
{osmium::item_type::way, 12},
};
osmium::CRC<osmium::CRC_zlib> orig_crc;
orig_crc.update(relation);
mid->relation(relation);
mid->after_relations();
// retrieve the relation
osmium::memory::Buffer outbuf{4096,
osmium::memory::Buffer::auto_grow::yes};
REQUIRE(mid_q->relation_get(123, &outbuf));
auto const &rel = outbuf.get<osmium::Relation>(0);
CHECK(rel.id() == 123);
CHECK(rel.members().size() == 4);
osmium::CRC<osmium::CRC_zlib> crc;
crc.update(rel);
CHECK(orig_crc().checksum() == crc().checksum());
// retrieve node members only
osmium::memory::Buffer memberbuf{
4096, osmium::memory::Buffer::auto_grow::yes};
REQUIRE(mid_q->rel_members_get(rel, &memberbuf,
osmium::osm_entity_bits::node) == 1);
{
auto const objects = memberbuf.select<osmium::OSMObject>();
auto it = objects.cbegin();
auto const end = objects.cend();
for (auto const &p : expected) {
if (p.first == osmium::item_type::node) {
REQUIRE(it != end);
REQUIRE(it->type() == p.first);
REQUIRE(it->id() == p.second);
++it;
}
}
}
memberbuf.clear();
// retrieve way members only
REQUIRE(mid_q->rel_members_get(rel, &memberbuf,
osmium::osm_entity_bits::way) == 3);
{
auto const objects = memberbuf.select<osmium::OSMObject>();
auto it = objects.cbegin();
auto const end = objects.cend();
for (auto const &p : expected) {
if (p.first == osmium::item_type::way) {
REQUIRE(it != end);
REQUIRE(it->type() == p.first);
REQUIRE(it->id() == p.second);
++it;
}
}
}
memberbuf.clear();
// retrieve all members
REQUIRE(mid_q->rel_members_get(rel, &memberbuf,
osmium::osm_entity_bits::node |
osmium::osm_entity_bits::way) == 4);
{
auto const objects = memberbuf.select<osmium::OSMObject>();
auto it = objects.cbegin();
auto const end = objects.cend();
for (auto const &p : expected) {
REQUIRE(it != end);
REQUIRE(it->type() == p.first);
REQUIRE(it->id() == p.second);
++it;
}
}
// other relations are not retrievable
REQUIRE_FALSE(mid_q->relation_get(999, &outbuf));
}
if (options.middle_dbschema != "public") {
REQUIRE(num_tables == conn.get_count("pg_catalog.pg_tables",
"schemaname = 'public'"));
REQUIRE(num_indexes == conn.get_count("pg_catalog.pg_indexes",
"schemaname = 'public'"));
REQUIRE(num_procs ==
conn.get_count(
"pg_catalog.pg_proc",
"pronamespace = (SELECT oid FROM "
"pg_catalog.pg_namespace WHERE nspname = 'public')"));
}
}
namespace {
/**
* Check that the node is in the mid with the right id and location.
*/
void check_node(std::shared_ptr<middle_pgsql_t> const &mid,
osmium::Node const &node)
{
test_buffer_t buffer;
auto &nodes = buffer.add_way(999, {node.id()}).nodes();
auto const mid_q = mid->get_query_instance();
REQUIRE(mid_q->nodes_get_list(&nodes) == 1);
REQUIRE(nodes[0].ref() == node.id());
REQUIRE(nodes[0].location() == node.location());
}
/// Return true if the node with the specified id is not in the mid.
bool no_node(std::shared_ptr<middle_pgsql_t> const &mid, osmid_t id)
{
test_buffer_t buffer;
auto &nodes = buffer.add_way(999, {id}).nodes();
auto const mid_q = mid->get_query_instance();
return mid_q->nodes_get_list(&nodes) == 0;
}
} // anonymous namespace
TEMPLATE_TEST_CASE("middle: add, delete and update node", "",
options_slim_default, options_flat_node_cache)
{
auto thread_pool = std::make_shared<thread_pool_t>(1U);
options_t options = TestType::options(db);
testing::cleanup::file_t const flatnode_cleaner{options.flat_node_file};
// Prepare a buffer with some nodes which we will add and change.
test_buffer_t buffer;
auto const &node10 = buffer.add_node("n10 x1.0 y0.0");
auto const &node11 = buffer.add_node("n11 x1.1 y0.0");
auto const &node12 = buffer.add_node("n12 x1.2 y0.0");
auto const &node10a = buffer.add_node("n10 x1.0 y1.0");
auto const &node5d = buffer.add_node("n5 dD");
auto const &node10d = buffer.add_node("n10 dD");
auto const &node12d = buffer.add_node("n12 dD");
auto const &node42d = buffer.add_node("n42 dD");
// Set up middle in "create" mode to get a cleanly initialized database
// and add some nodes. Does this in its own scope so that the mid is
// closed properly.
{
auto mid = std::make_shared<middle_pgsql_t>(thread_pool, &options);
mid->start();
mid->node(node10);
mid->node(node11);
mid->after_nodes();
mid->after_ways();
mid->after_relations();
check_node(mid, node10);
check_node(mid, node11);
}
// From now on use append mode to not destroy the data we just added.
options.append = true;
SECTION("Added nodes are there and no others")
{
auto mid = std::make_shared<middle_pgsql_t>(thread_pool, &options);
mid->start();
check_node(mid, node10);
check_node(mid, node11);
REQUIRE(no_node(mid, 5));
REQUIRE(no_node(mid, 42));
}
SECTION("Delete existing and non-existing node")
{
{
auto mid = std::make_shared<middle_pgsql_t>(thread_pool, &options);
mid->start();
mid->node(node5d);
mid->node(node10d);
mid->node(node42d);
mid->after_nodes();
mid->after_ways();
mid->after_relations();
REQUIRE(no_node(mid, 5));
REQUIRE(no_node(mid, 10));
check_node(mid, node11);
REQUIRE(no_node(mid, 42));
}
{
// Check with a new mid.
auto mid = std::make_shared<middle_pgsql_t>(thread_pool, &options);
mid->start();
REQUIRE(no_node(mid, 5));
REQUIRE(no_node(mid, 10));
check_node(mid, node11);
REQUIRE(no_node(mid, 42));
}
}
SECTION("Change (delete and set) existing and non-existing node")
{
{
auto mid = std::make_shared<middle_pgsql_t>(thread_pool, &options);
mid->start();
mid->node(node10d);
mid->node(node10a);
mid->node(node12d);
mid->node(node12);
mid->after_nodes();
mid->after_ways();
mid->after_relations();
check_node(mid, node10a);
check_node(mid, node11);
check_node(mid, node12);
}
{
// Check with a new mid.
auto mid = std::make_shared<middle_pgsql_t>(thread_pool, &options);
mid->start();
check_node(mid, node10a);
check_node(mid, node11);
check_node(mid, node12);
}
}
SECTION("Add new node")
{
{
auto mid = std::make_shared<middle_pgsql_t>(thread_pool, &options);
mid->start();
mid->node(node12);
mid->after_nodes();
mid->after_ways();
mid->after_relations();
REQUIRE(no_node(mid, 5));
check_node(mid, node10);
check_node(mid, node11);
check_node(mid, node12);
REQUIRE(no_node(mid, 42));
}
{
// Check with a new mid.
auto mid = std::make_shared<middle_pgsql_t>(thread_pool, &options);
mid->start();
REQUIRE(no_node(mid, 5));
check_node(mid, node10);
check_node(mid, node11);
check_node(mid, node12);
REQUIRE(no_node(mid, 42));
}
}
}
namespace {
/**
* Check that the way is in the mid with the right attributes and tags.
* Does not check node locations.
*/
void check_way(std::shared_ptr<middle_pgsql_t> const &mid,
osmium::Way const &orig_way)
{
auto const mid_q = mid->get_query_instance();
osmium::memory::Buffer outbuf{4096, osmium::memory::Buffer::auto_grow::yes};
REQUIRE(mid_q->way_get(orig_way.id(), &outbuf));
auto const &way = outbuf.get<osmium::Way>(0);
CHECK(orig_way.id() == way.id());
CHECK(orig_way.timestamp() == way.timestamp());
CHECK(orig_way.version() == way.version());
CHECK(orig_way.changeset() == way.changeset());
CHECK(orig_way.uid() == way.uid());
CHECK(std::strcmp(orig_way.user(), way.user()) == 0);
REQUIRE(orig_way.tags().size() == way.tags().size());
for (auto it1 = orig_way.tags().begin(), it2 = way.tags().begin();
it1 != orig_way.tags().end(); ++it1, ++it2) {
CHECK(*it1 == *it2);
}
REQUIRE(orig_way.nodes().size() == way.nodes().size());
for (auto it1 = orig_way.nodes().begin(), it2 = way.nodes().begin();
it1 != orig_way.nodes().end(); ++it1, ++it2) {
CHECK(*it1 == *it2);
}
osmium::CRC<osmium::CRC_zlib> orig_crc;
orig_crc.update(orig_way);
osmium::CRC<osmium::CRC_zlib> test_crc;
test_crc.update(way);
REQUIRE(orig_crc().checksum() == test_crc().checksum());
}
/**
* Check that the nodes (ids and locations) of the way with the way_id in the
* mid are identical to the nodes in the nodes vector.
*/
void check_way_nodes(std::shared_ptr<middle_pgsql_t> const &mid, osmid_t way_id,
std::vector<osmium::Node const *> const &nodes)
{
auto const mid_q = mid->get_query_instance();
osmium::memory::Buffer outbuf{4096, osmium::memory::Buffer::auto_grow::yes};
REQUIRE(mid_q->way_get(way_id, &outbuf));
auto &way = outbuf.get<osmium::Way>(0);
REQUIRE(mid_q->nodes_get_list(&way.nodes()) == way.nodes().size());
REQUIRE(way.nodes().size() == nodes.size());
REQUIRE(std::equal(way.nodes().cbegin(), way.nodes().cend(), nodes.cbegin(),
[](osmium::NodeRef const &nr, osmium::Node const *node) {
return nr.ref() == node->id() &&
nr.location() == node->location();
}));
}
/// Return true if the way with the specified id is not in the mid.
bool no_way(std::shared_ptr<middle_pgsql_t> const &mid, osmid_t id)
{
auto const mid_q = mid->get_query_instance();
osmium::memory::Buffer outbuf{4096, osmium::memory::Buffer::auto_grow::yes};
return !mid_q->way_get(id, &outbuf);
}
} // anonymous namespace
TEMPLATE_TEST_CASE("middle: add, delete and update way", "",
options_slim_default, options_flat_node_cache)
{
auto thread_pool = std::make_shared<thread_pool_t>(1U);
options_t options = TestType::options(db);
testing::cleanup::file_t const flatnode_cleaner{options.flat_node_file};
// Create some ways we'll use for the tests.
test_buffer_t buffer;
auto const &way20 =
buffer.add_way("w20 Nn10,n11 Thighway=residential,name=High_Street");
auto const &way21 = buffer.add_way("w21 Nn11,n12");
auto const &way22 = buffer.add_way("w22 Nn12,n10 Tpower=line");
auto &way20a =
buffer.add_way("w20 Nn10,n12 Thighway=primary,name=High_Street");
auto const &way5d = buffer.add_way("w5 dD");
auto &way20d = buffer.add_way("w20 dD");
auto &way22d = buffer.add_way("w22 dD");
auto &way42d = buffer.add_way("w42 dD");
// Set up middle in "create" mode to get a cleanly initialized database and
// add some ways. Does this in its own scope so that the mid is closed
// properly.
{
auto mid = std::make_shared<middle_pgsql_t>(thread_pool, &options);
mid->start();
mid->after_nodes();
mid->way(way20);
mid->way(way21);
mid->after_ways();
mid->after_relations();
check_way(mid, way20);
check_way(mid, way21);
}
// From now on use append mode to not destroy the data we just added.
options.append = true;
SECTION("Added ways are there and no others")
{
auto mid = std::make_shared<middle_pgsql_t>(thread_pool, &options);
mid->start();
REQUIRE(no_way(mid, 5));
check_way(mid, way20);
check_way(mid, way21);
REQUIRE(no_way(mid, 22));
}
SECTION("Delete existing and non-existing way")
{
{
auto mid = std::make_shared<middle_pgsql_t>(thread_pool, &options);
mid->start();
mid->after_nodes();
mid->way(way5d);
mid->way(way20d);
mid->way(way42d);
mid->after_ways();
mid->after_relations();
REQUIRE(no_way(mid, 5));
REQUIRE(no_way(mid, 20));
check_way(mid, way21);
REQUIRE(no_way(mid, 42));
}
{
// Check with a new mid.
auto mid = std::make_shared<middle_pgsql_t>(thread_pool, &options);
mid->start();
REQUIRE(no_way(mid, 5));
REQUIRE(no_way(mid, 20));
check_way(mid, way21);
REQUIRE(no_way(mid, 42));
}
}
SECTION("Change (delete and set) existing and non-existing way")
{
{
auto mid = std::make_shared<middle_pgsql_t>(thread_pool, &options);
mid->start();
mid->after_nodes();
mid->way(way20d);
mid->way(way20a);
mid->way(way22d);
mid->way(way22);
mid->after_ways();
mid->after_relations();
REQUIRE(no_way(mid, 5));
check_way(mid, way20a);
check_way(mid, way21);
check_way(mid, way22);
REQUIRE(no_way(mid, 42));
}
{
// Check with a new mid.
auto mid = std::make_shared<middle_pgsql_t>(thread_pool, &options);
mid->start();
REQUIRE(no_way(mid, 5));
check_way(mid, way20a);
check_way(mid, way21);
check_way(mid, way22);
REQUIRE(no_way(mid, 42));
}
}
SECTION("Add new way")
{
{
auto mid = std::make_shared<middle_pgsql_t>(thread_pool, &options);
mid->start();
mid->after_nodes();
mid->way(way22);
mid->after_ways();
mid->after_relations();
REQUIRE(no_way(mid, 5));
check_way(mid, way20);
check_way(mid, way21);
check_way(mid, way22);
REQUIRE(no_way(mid, 42));
}
{
// Check with a new mid.
auto mid = std::make_shared<middle_pgsql_t>(thread_pool, &options);
mid->start();
REQUIRE(no_way(mid, 5));
check_way(mid, way20);
check_way(mid, way21);
check_way(mid, way22);
REQUIRE(no_way(mid, 42));
}
}
}
TEMPLATE_TEST_CASE("middle: add way with attributes", "", options_slim_default,
options_flat_node_cache)
{
auto thread_pool = std::make_shared<thread_pool_t>(1U);
options_t options = TestType::options(db);
SECTION("With attributes") { options.extra_attributes = true; }
SECTION("No attributes") { options.extra_attributes = false; }
testing::cleanup::file_t const flatnode_cleaner{options.flat_node_file};
// Create some ways we'll use for the tests.
test_buffer_t buffer;
auto &way20 =
buffer.add_way("w20 Nn10,n11 Thighway=residential,name=High_Street");
way20.set_version(123);
way20.set_timestamp(1234567890);
way20.set_changeset(456);
way20.set_uid(789);
// The same way but with default attributes.
auto &way20_no_attr =
buffer.add_way("w20 Nn10,n11 Thighway=residential,name=High_Street");
{
auto mid = std::make_shared<middle_pgsql_t>(thread_pool, &options);
mid->start();
mid->after_nodes();
mid->way(way20);
mid->after_ways();
mid->after_relations();
check_way(mid, options.extra_attributes ? way20 : way20_no_attr);
}
// From now on use append mode to not destroy the data we just added.
options.append = true;
{
auto mid = std::make_shared<middle_pgsql_t>(thread_pool, &options);
mid->start();
check_way(mid, options.extra_attributes ? way20 : way20_no_attr);
}
}
namespace {
/**
* Check that the relation is in the mid with the right attributes, members
* and tags. Only checks the relation, does not recurse into members.
*/
void check_relation(std::shared_ptr<middle_pgsql_t> const &mid,
osmium::Relation const &orig_relation)
{
auto const mid_q = mid->get_query_instance();
osmium::memory::Buffer outbuf{4096, osmium::memory::Buffer::auto_grow::yes};
REQUIRE(mid_q->relation_get(orig_relation.id(), &outbuf));
auto const &relation = outbuf.get<osmium::Relation>(0);
CHECK(orig_relation.id() == relation.id());
CHECK(orig_relation.timestamp() == relation.timestamp());
CHECK(orig_relation.version() == relation.version());
CHECK(orig_relation.changeset() == relation.changeset());
CHECK(orig_relation.uid() == relation.uid());
CHECK(std::strcmp(orig_relation.user(), relation.user()) == 0);
REQUIRE(orig_relation.tags().size() == relation.tags().size());
for (auto it1 = orig_relation.tags().begin(), it2 = relation.tags().begin();
it1 != orig_relation.tags().end(); ++it1, ++it2) {
CHECK(*it1 == *it2);
}
REQUIRE(orig_relation.members().size() == relation.members().size());
for (auto it1 = orig_relation.members().begin(),
it2 = relation.members().begin();
it1 != orig_relation.members().end(); ++it1, ++it2) {
CHECK(it1->type() == it2->type());
CHECK(it1->ref() == it2->ref());
CHECK(std::strcmp(it1->role(), it2->role()) == 0);
}
osmium::CRC<osmium::CRC_zlib> orig_crc;
orig_crc.update(orig_relation);
osmium::CRC<osmium::CRC_zlib> test_crc;
test_crc.update(relation);
REQUIRE(orig_crc().checksum() == test_crc().checksum());
}
/// Return true if the relation with the specified id is not in the mid.
bool no_relation(std::shared_ptr<middle_pgsql_t> const &mid, osmid_t id)
{
auto const mid_q = mid->get_query_instance();
osmium::memory::Buffer outbuf{4096, osmium::memory::Buffer::auto_grow::yes};
return !mid_q->relation_get(id, &outbuf);
}
} // anonymous namespace
TEMPLATE_TEST_CASE("middle: add, delete and update relation", "",
options_slim_default, options_flat_node_cache)
{
auto thread_pool = std::make_shared<thread_pool_t>(1U);
options_t options = TestType::options(db);
testing::cleanup::file_t const flatnode_cleaner{options.flat_node_file};
// Create some relations we'll use for the tests.
test_buffer_t buffer;
auto const &relation30 = buffer.add_relation(
"r30 Mw10@outer,w11@innder Tname=Penguin_Park,type=multipolygon");
auto const &relation31 = buffer.add_relation("r31 Mn10@");
auto const &relation32 = buffer.add_relation("r32 Mr39@ Ttype=site");
auto const &relation30a = buffer.add_relation(
"r30 Mw10@outer,w11@outer Tname=Pigeon_Park,type=multipolygon");
auto const &relation5d = buffer.add_relation("r5 dD");
auto const &relation30d = buffer.add_relation("r30 dD");
auto const &relation32d = buffer.add_relation("r32 dD");
auto const &relation42d = buffer.add_relation("r42 dD");
// Set up middle in "create" mode to get a cleanly initialized database and
// add some relations. Does this in its own scope so that the mid is closed
// properly.
{
auto mid = std::make_shared<middle_pgsql_t>(thread_pool, &options);
mid->start();
mid->after_nodes();
mid->after_ways();
mid->relation(relation30);
mid->relation(relation31);
mid->after_relations();
check_relation(mid, relation30);
check_relation(mid, relation31);
}
// From now on use append mode to not destroy the data we just added.
options.append = true;
SECTION("Added relations are there and no others")
{
auto mid = std::make_shared<middle_pgsql_t>(thread_pool, &options);
mid->start();
REQUIRE(no_relation(mid, 5));
check_relation(mid, relation30);
check_relation(mid, relation31);
REQUIRE(no_relation(mid, 32));
}
SECTION("Delete existing and non-existing relation")
{
{
auto mid = std::make_shared<middle_pgsql_t>(thread_pool, &options);
mid->start();
mid->after_nodes();
mid->after_ways();
mid->relation(relation5d);
mid->relation(relation30d);
mid->relation(relation42d);
mid->after_relations();
REQUIRE(no_relation(mid, 5));
REQUIRE(no_relation(mid, 30));
check_relation(mid, relation31);
REQUIRE(no_relation(mid, 42));
}
{
// Check with a new mid.
auto mid = std::make_shared<middle_pgsql_t>(thread_pool, &options);
mid->start();
REQUIRE(no_relation(mid, 5));
REQUIRE(no_relation(mid, 30));
check_relation(mid, relation31);
REQUIRE(no_relation(mid, 42));
}
}
SECTION("Change (delete and set) existing and non-existing relation")
{
{
auto mid = std::make_shared<middle_pgsql_t>(thread_pool, &options);
mid->start();
mid->after_nodes();
mid->after_ways();
mid->relation(relation30d);
mid->relation(relation30a);
mid->relation(relation32d);
mid->relation(relation32);
mid->after_relations();
REQUIRE(no_relation(mid, 5));
check_relation(mid, relation30a);
check_relation(mid, relation31);
check_relation(mid, relation32);
REQUIRE(no_relation(mid, 42));
}
{
// Check with a new mid.
auto mid = std::make_shared<middle_pgsql_t>(thread_pool, &options);
mid->start();
REQUIRE(no_relation(mid, 5));
check_relation(mid, relation30a);
check_relation(mid, relation31);
check_relation(mid, relation32);
REQUIRE(no_relation(mid, 42));
}
}
SECTION("Add new relation")
{
{
auto mid = std::make_shared<middle_pgsql_t>(thread_pool, &options);
mid->start();
mid->after_nodes();
mid->after_ways();
mid->relation(relation32);
mid->after_relations();
REQUIRE(no_relation(mid, 5));
check_relation(mid, relation30);
check_relation(mid, relation31);
check_relation(mid, relation32);
REQUIRE(no_relation(mid, 42));
}
{
// Check with a new mid.
auto mid = std::make_shared<middle_pgsql_t>(thread_pool, &options);
mid->start();
REQUIRE(no_relation(mid, 5));
check_relation(mid, relation30);
check_relation(mid, relation31);
check_relation(mid, relation32);
REQUIRE(no_relation(mid, 42));
}
}
}
TEMPLATE_TEST_CASE("middle: add relation with attributes", "",
options_slim_default, options_flat_node_cache)
{
auto thread_pool = std::make_shared<thread_pool_t>(1U);
options_t options = TestType::options(db);
SECTION("With attributes") { options.extra_attributes = true; }
SECTION("No attributes") { options.extra_attributes = false; }
testing::cleanup::file_t const flatnode_cleaner{options.flat_node_file};
// Create some relations we'll use for the tests.
test_buffer_t buffer;
auto const &relation30 = buffer.add_relation(
"r30 v123 c456 i789 t2009-02-13T23:31:30Z Mw10@outer,w11@inner "
"Tname=Penguin_Park,type=multipolygon");
// The same relation but with default attributes.
auto const &relation30_no_attr = buffer.add_relation(
"r30 Mw10@outer,w11@inner Tname=Penguin_Park,type=multipolygon");