From 82ac754cd667fe974dfc7be647773603a0f791b5 Mon Sep 17 00:00:00 2001 From: Khlopkova Olga Date: Thu, 9 Nov 2023 18:25:58 +0300 Subject: [PATCH] fixes --- include/just_gtfs/just_gtfs.h | 35 ++++----- tests/unit_tests.cpp | 134 +++++++++++++++++----------------- 2 files changed, 86 insertions(+), 83 deletions(-) diff --git a/include/just_gtfs/just_gtfs.h b/include/just_gtfs/just_gtfs.h index c8d1059..5cd3947 100644 --- a/include/just_gtfs/just_gtfs.h +++ b/include/just_gtfs/just_gtfs.h @@ -1264,35 +1264,30 @@ class Feed inline Result read_feed(); inline Result write_feed(const std::string & gtfs_path) const; - inline Result read_agencies(); inline Result write_agencies(const std::string & gtfs_path) const; inline const Agencies & get_agencies() const; inline const Agency & get_agency(const Id & agency_id) const; inline void add_agency(const Agency & agency); - inline Result read_stops(); inline Result write_stops(const std::string & gtfs_path) const; inline const Stops & get_stops() const; inline const Stop & get_stop(const Id & stop_id) const; inline void add_stop(const Stop & stop); - inline Result read_routes(); inline Result write_routes(const std::string & gtfs_path) const; inline const Routes & get_routes() const; inline const Route & get_route(const Id & route_id) const; inline void add_route(const Route & route); - inline Result read_trips(); inline Result write_trips(const std::string & gtfs_path) const; inline const Trips & get_trips() const; inline const Trip & get_trip(const Id & trip_id) const; inline void add_trip(const Trip & trip); - inline Result read_stop_times(); inline Result write_stop_times(const std::string & gtfs_path) const; inline const StopTimes & get_stop_times() const; @@ -1300,56 +1295,48 @@ class Feed inline StopTimesRange get_stop_times_for_trip(const Id & trip_id) const; inline void add_stop_time(const StopTime & stop_time); - inline Result read_calendar(); inline Result write_calendar(const std::string & gtfs_path) const; inline const Calendar & get_calendar() const; inline const CalendarItem & get_calendar_item(const Id & service_id) const; inline void add_calendar_item(const CalendarItem & calendar_item); - inline Result read_calendar_dates(); inline Result write_calendar_dates(const std::string & gtfs_path) const; inline const CalendarDates & get_calendar_dates() const; inline CalendarDatesRange get_calendar_dates(const Id & service_id) const; inline void add_calendar_date(const CalendarDate & calendar_date); - inline Result read_fare_rules(); inline Result write_fare_rules(const std::string & gtfs_path) const; inline const FareRules & get_fare_rules() const; inline FareRulesRange get_fare_rules(const Id & fare_id) const; inline void add_fare_rule(const FareRule & fare_rule); - inline Result read_fare_attributes(); inline Result write_fare_attributes(const std::string & gtfs_path) const; inline const FareAttributes & get_fare_attributes() const; inline FareAttributesRange get_fare_attributes(const Id & fare_id) const; inline void add_fare_attributes(const FareAttributesItem & fare_attributes_item); - inline Result read_shapes(); inline Result write_shapes(const std::string & gtfs_path) const; inline const Shapes & get_shapes() const; inline ShapeRange get_shape(const Id & shape_id) const; inline void add_shape(const ShapePoint & shape); - inline Result read_frequencies(); inline Result write_frequencies(const std::string & gtfs_path) const; inline const Frequencies & get_frequencies() const; inline FrequenciesRange get_frequencies(const Id & trip_id) const; inline void add_frequency(const Frequency & frequency); - inline Result read_transfers(); inline Result write_transfers(const std::string & gtfs_path) const; inline const Transfers & get_transfers() const; inline const Transfer & get_transfer(const Id & from_stop_id, const Id & to_stop_id) const; inline void add_transfer(const Transfer & transfer); - inline Result read_pathways(); inline Result write_pathways(const std::string & gtfs_path) const; inline const Pathways & get_pathways() const; @@ -1357,27 +1344,23 @@ class Feed inline Pathways get_pathways(const Id & from_stop_id, const Id & to_stop_id) const; inline void add_pathway(const Pathway & pathway); - inline Result read_levels(); inline Result write_levels(const std::string & gtfs_path) const; inline const Levels & get_levels() const; inline const Level & get_level(const Id & level_id) const; inline void add_level(const Level & level); - inline Result read_feed_info(); inline Result write_feed_info(const std::string & gtfs_path) const; inline FeedInfo get_feed_info() const; inline void set_feed_info(const FeedInfo & feed_info); - inline Result read_translations(); inline Result write_translations(const std::string & gtfs_path) const; inline const Translations & get_translations() const; inline TranslationsRange get_translations(const Text & table_name) const; inline void add_translation(const Translation & translation); - inline Result read_attributions(); inline Result write_attributions(const std::string & gtfs_path) const; inline const Attributions & get_attributions() const; @@ -1409,6 +1392,24 @@ class Feed inline Result add_translation(const ParsedCsvRow & row); inline Result add_attribution(const ParsedCsvRow & row); + inline Result read_agencies(); + inline Result read_stops(); + inline Result read_routes(); + inline Result read_trips(); + inline Result read_stop_times(); + inline Result read_calendar(); + inline Result read_calendar_dates(); + inline Result read_fare_rules(); + inline Result read_fare_attributes(); + inline Result read_shapes(); + inline Result read_frequencies(); + inline Result read_transfers(); + inline Result read_pathways(); + inline Result read_levels(); + inline Result read_feed_info(); + inline Result read_translations(); + inline Result read_attributions(); + inline void write_agencies(std::ofstream & out) const; inline void write_routes(std::ofstream & out) const; inline void write_shapes(std::ofstream & out) const; diff --git a/tests/unit_tests.cpp b/tests/unit_tests.cpp index f087f48..c67414b 100644 --- a/tests/unit_tests.cpp +++ b/tests/unit_tests.cpp @@ -6,7 +6,7 @@ #include using namespace gtfs; - +const std::string test_feed = "/Users/o.khlopkova/Documents/just_gtfs/tests/data/sample_feed"; TEST_SUITE_BEGIN("Handling time GTFS fields"); TEST_CASE("Time in H:MM:SS format") { @@ -239,14 +239,14 @@ TEST_CASE("Empty container before parsing") TEST_CASE("Non existing directory") { Feed feed("data/non_existing_dir"); - REQUIRE_EQ(feed.read_transfers(), ResultCode::ERROR_FILE_ABSENT); + REQUIRE_EQ(feed.read_feed(), ResultCode::ERROR_FILE_ABSENT); CHECK_EQ(feed.get_transfers().size(), 0); } TEST_CASE("Transfers") { - Feed feed("data/sample_feed"); - REQUIRE_EQ(feed.read_transfers(), ResultCode::OK); + Feed feed(test_feed); + REQUIRE_EQ(feed.read_feed(), ResultCode::OK); const auto & transfers = feed.get_transfers(); CHECK_EQ(transfers.size(), 4); @@ -263,8 +263,8 @@ TEST_CASE("Transfers") TEST_CASE("Calendar") { - Feed feed("data/sample_feed"); - REQUIRE_EQ(feed.read_calendar(), ResultCode::OK); + Feed feed(test_feed); + REQUIRE_EQ(feed.read_feed(), ResultCode::OK); const auto & calendar = feed.get_calendar(); REQUIRE_EQ(calendar.size(), 2); @@ -284,8 +284,8 @@ TEST_CASE("Calendar") TEST_CASE("Calendar dates") { - Feed feed("data/sample_feed"); - REQUIRE_EQ(feed.read_calendar_dates(), ResultCode::OK); + Feed feed(test_feed); + REQUIRE_EQ(feed.read_feed(), ResultCode::OK); const auto & calendar_dates = feed.get_calendar_dates(); REQUIRE_EQ(calendar_dates.size(), 1); @@ -297,7 +297,7 @@ TEST_CASE("Calendar dates") TEST_CASE("Read GTFS feed") { - Feed feed("data/sample_feed"); + Feed feed(test_feed); REQUIRE_EQ(feed.read_feed(), ResultCode::OK); CHECK_EQ(feed.get_agencies().size(), 1); @@ -321,8 +321,8 @@ TEST_CASE("Read GTFS feed") TEST_CASE("Agency") { - Feed feed("data/sample_feed"); - REQUIRE_EQ(feed.read_agencies(), ResultCode::OK); + Feed feed(test_feed); + REQUIRE_EQ(feed.read_feed(), ResultCode::OK); const auto & agencies = feed.get_agencies(); REQUIRE_EQ(agencies.size(), 1); @@ -337,14 +337,14 @@ TEST_CASE("Agency") REQUIRE_EQ(feed.write_agencies("data/output_feed"), ResultCode::OK); Feed feed_copy("data/output_feed"); - REQUIRE_EQ(feed_copy.read_agencies(), ResultCode::OK); + REQUIRE_EQ(feed_copy.read_feed(), ResultCode::OK); CHECK_EQ(agencies, feed_copy.get_agencies()); } TEST_CASE("Routes") { - Feed feed("data/sample_feed"); - REQUIRE_EQ(feed.read_routes(), ResultCode::OK); + Feed feed(test_feed); + REQUIRE_EQ(feed.read_feed(), ResultCode::OK); const auto & routes = feed.get_routes(); REQUIRE_EQ(routes.size(), 5); @@ -364,8 +364,8 @@ TEST_CASE("Routes") TEST_CASE("Trips") { - Feed feed("data/sample_feed"); - REQUIRE_EQ(feed.read_trips(), ResultCode::OK); + Feed feed(test_feed); + REQUIRE_EQ(feed.read_feed(), ResultCode::OK); const auto & trips = feed.get_trips(); REQUIRE_EQ(trips.size(), 11); @@ -384,20 +384,21 @@ TEST_CASE("Trips") TEST_CASE("Stops") { - Feed feed("data/sample_feed"); - REQUIRE_EQ(feed.read_stops(), ResultCode::OK); + Feed feed(test_feed); + REQUIRE_EQ(feed.read_feed(), ResultCode::OK); const auto & stops = feed.get_stops(); REQUIRE_EQ(stops.size(), 9); - CHECK_EQ(stops[0].stop_lat, 36.425288); - CHECK_EQ(stops[0].stop_lon, -117.133162); - CHECK(stops[0].stop_code.empty()); - CHECK(stops[0].stop_url.empty()); - CHECK_EQ(stops[0].stop_id, "FUR_CREEK_RES"); - CHECK(stops[0].stop_desc.empty()); - CHECK_EQ(stops[0].stop_name, "Furnace Creek Resort (Demo)"); - CHECK_EQ(stops[0].location_type, StopLocationType::StopOrPlatform); - CHECK(stops[0].zone_id.empty()); + size_t i = 5; + CHECK_EQ(stops[i].stop_lat, 36.425288); + CHECK_EQ(stops[i].stop_lon, -117.133162); + CHECK(stops[i].stop_code.empty()); + CHECK(stops[i].stop_url.empty()); + CHECK_EQ(stops[i].stop_id, "FUR_CREEK_RES"); + CHECK(stops[i].stop_desc.empty()); + CHECK_EQ(stops[i].stop_name, "Furnace Creek Resort (Demo)"); + CHECK_EQ(stops[i].location_type, StopLocationType::StopOrPlatform); + CHECK(stops[i].zone_id.empty()); auto const & stop = feed.get_stop("FUR_CREEK_RES"); CHECK_EQ(stop.stop_name, "Furnace Creek Resort (Demo)"); @@ -405,8 +406,8 @@ TEST_CASE("Stops") TEST_CASE("StopTimes") { - Feed feed("data/sample_feed"); - REQUIRE_EQ(feed.read_stop_times(), ResultCode::OK); + Feed feed(test_feed); + REQUIRE_EQ(feed.read_feed(), ResultCode::OK); const auto & stop_times = feed.get_stop_times(); REQUIRE_EQ(stop_times.size(), 28); @@ -429,8 +430,8 @@ TEST_CASE("StopTimes") TEST_CASE("Shapes") { - Feed feed("data/sample_feed"); - REQUIRE_EQ(feed.read_shapes(), ResultCode::OK); + Feed feed(test_feed); + REQUIRE_EQ(feed.read_feed(), ResultCode::OK); const auto & shapes = feed.get_shapes(); REQUIRE_EQ(shapes.size(), 8); @@ -446,8 +447,8 @@ TEST_CASE("Shapes") TEST_CASE("Calendar") { - Feed feed("data/sample_feed"); - REQUIRE_EQ(feed.read_calendar(), ResultCode::OK); + Feed feed(test_feed); + REQUIRE_EQ(feed.read_feed(), ResultCode::OK); const auto & calendar = feed.get_calendar(); REQUIRE_EQ(calendar.size(), 2); @@ -464,8 +465,8 @@ TEST_CASE("Calendar") TEST_CASE("Calendar dates") { - Feed feed("data/sample_feed"); - REQUIRE_EQ(feed.read_calendar_dates(), ResultCode::OK); + Feed feed(test_feed); + REQUIRE_EQ(feed.read_feed(), ResultCode::OK); const auto & calendar_dates = feed.get_calendar_dates(); REQUIRE_EQ(calendar_dates.size(), 1); @@ -479,8 +480,8 @@ TEST_CASE("Calendar dates") TEST_CASE("Frequencies") { - Feed feed("data/sample_feed"); - REQUIRE_EQ(feed.read_frequencies(), ResultCode::OK); + Feed feed(test_feed); + REQUIRE_EQ(feed.read_feed(), ResultCode::OK); const auto & frequencies = feed.get_frequencies(); REQUIRE_EQ(frequencies.size(), 11); @@ -495,8 +496,8 @@ TEST_CASE("Frequencies") TEST_CASE("Fare attributes") { - Feed feed("data/sample_feed"); - REQUIRE_EQ(feed.read_fare_attributes(), ResultCode::OK); + Feed feed(test_feed); + REQUIRE_EQ(feed.read_feed(), ResultCode::OK); const auto & attributes = feed.get_fare_attributes(); REQUIRE_EQ(attributes.size(), 3); @@ -527,46 +528,45 @@ TEST_CASE("Fare attributes") REQUIRE_EQ(feed.write_fare_attributes("data/output_feed"), ResultCode::OK); Feed feed_copy("data/output_feed"); - REQUIRE_EQ(feed_copy.read_fare_attributes(), ResultCode::OK); + REQUIRE_EQ(feed_copy.read_feed(), ResultCode::OK); CHECK_EQ(attributes, feed_copy.get_fare_attributes()); } TEST_CASE("Fare rules") { - Feed feed("data/sample_feed"); - REQUIRE_EQ(feed.read_fare_rules(), ResultCode::OK); + Feed feed(test_feed); + REQUIRE_EQ(feed.read_feed(), ResultCode::OK); const auto & fare_rules = feed.get_fare_rules(); REQUIRE_EQ(fare_rules.size(), 4); CHECK_EQ(fare_rules[0].fare_id, "p"); CHECK_EQ(fare_rules[0].route_id, "AB"); - //const auto & rules_for_id = feed.get_fare_rules("p"); - // REQUIRE_EQ(rules_for_id.size(), 3); - // CHECK_EQ(rules_for_id[1].route_id, "STBA"); + const auto & rules_for_id = feed.get_fare_rules("p"); + CHECK_EQ(std::distance(rules_for_id.first, rules_for_id.second), 3); + //CHECK_EQ(rules_for_id[1].route_id, "STBA"); } TEST_CASE("Levels") { - Feed feed("data/sample_feed"); - REQUIRE_EQ(feed.read_levels(), ResultCode::OK); + Feed feed(test_feed); + REQUIRE_EQ(feed.read_feed(), ResultCode::OK); const auto & levels = feed.get_levels(); REQUIRE_EQ(levels.size(), 3); CHECK_EQ(levels[0].level_id, "U321L1"); CHECK_EQ(levels[0].level_index, -1.5); - //const auto & level = feed.get_level("U321L2"); - //REQUIRE(level); + const auto & level = feed.get_level("U321L2"); - //CHECK_EQ(level.value().level_index, -2); - //CHECK_EQ(level.value().level_name, "Vestibul2"); + CHECK_EQ(level.level_index, -2); + CHECK_EQ(level.level_name, "Vestibul2"); } TEST_CASE("Pathways") { - Feed feed("data/sample_feed"); - REQUIRE_EQ(feed.read_pathways(), ResultCode::OK); + Feed feed(test_feed); + REQUIRE_EQ(feed.read_feed(), ResultCode::OK); const auto & pathways = feed.get_pathways(); REQUIRE_EQ(pathways.size(), 3); @@ -578,16 +578,17 @@ TEST_CASE("Pathways") CHECK_EQ(pathways[0].reversed_signposted_as, "Sign2"); CHECK_EQ(pathways[0].is_bidirectional, PathwayDirection::Bidirectional); - // const auto & pathways_by_id = feed.get_pathways("T-A01D01"); -// REQUIRE_EQ(pathways_by_id.size(), 2); - // CHECK_EQ(pathways_by_id[0].is_bidirectional, PathwayDirection::Unidirectional); - //CHECK(pathways_by_id[0].reversed_signposted_as.empty()); + const auto & pathways_by_id = feed.get_pathways("T-A01D01"); + + CHECK_EQ(std::distance(pathways_by_id.first, pathways_by_id.second), 2); + CHECK_EQ(pathways_by_id.first->is_bidirectional, PathwayDirection::Unidirectional); + CHECK(pathways_by_id.first->reversed_signposted_as.empty()); } TEST_CASE("Translations") { - Feed feed("data/sample_feed"); - REQUIRE_EQ(feed.read_translations(), ResultCode::OK); + Feed feed(test_feed); + REQUIRE_EQ(feed.read_feed(), ResultCode::OK); const auto & translations = feed.get_translations(); REQUIRE_EQ(translations.size(), 1); @@ -599,13 +600,14 @@ TEST_CASE("Translations") CHECK(translations[0].record_sub_id.empty()); CHECK(translations[0].field_value.empty()); - //CHECK_EQ(feed.get_translations("stop_times").size(), 1); + auto const & t = feed.get_translations("stop_times"); + CHECK_EQ(std::distance(t.first, t.second), 1); } TEST_CASE("Attributions") { - Feed feed("data/sample_feed"); - REQUIRE_EQ(feed.read_attributions(), ResultCode::OK); + Feed feed(test_feed); + REQUIRE_EQ(feed.read_feed(), ResultCode::OK); const auto & attributions = feed.get_attributions(); REQUIRE_EQ(attributions.size(), 1); @@ -621,8 +623,8 @@ TEST_CASE("Attributions") TEST_CASE("Feed info") { - Feed feed("data/sample_feed"); - REQUIRE_EQ(feed.read_feed_info(), ResultCode::OK); + Feed feed(test_feed); + REQUIRE_EQ(feed.read_feed(), ResultCode::OK); const auto & info = feed.get_feed_info(); @@ -659,7 +661,7 @@ TEST_CASE("Agencies create & save") REQUIRE_EQ(feed_for_writing.write_agencies("data/output_feed"), ResultCode::OK); Feed feed_for_testing("data/output_feed"); - REQUIRE_EQ(feed_for_testing.read_agencies(), ResultCode::OK); + REQUIRE_EQ(feed_for_testing.read_feed(), ResultCode::OK); CHECK_EQ(feed_for_writing.get_agencies(), feed_for_testing.get_agencies()); } @@ -674,7 +676,7 @@ TEST_CASE("Shapes create & save") REQUIRE_EQ(feed_for_writing.write_shapes("data/output_feed"), ResultCode::OK); Feed feed_for_testing("data/output_feed"); - REQUIRE_EQ(feed_for_testing.read_shapes(), ResultCode::OK); + REQUIRE_EQ(feed_for_testing.read_feed(), ResultCode::OK); CHECK_EQ(feed_for_testing.get_shapes().size(), 3); } TEST_SUITE_END();