improve test coverage of geojson indexing

This commit is contained in:
Dane Springmeyer 2015-10-20 22:56:03 -07:00
parent 9df548670c
commit 14589dabd1

View file

@ -30,6 +30,8 @@
#include <mapnik/util/fs.hpp> #include <mapnik/util/fs.hpp>
#include <cstdlib> #include <cstdlib>
#include <boost/filesystem/operations.hpp>
namespace detail { namespace detail {
mapnik::feature_ptr fetch_first_feature(std::string const& filename, bool cache_features) mapnik::feature_ptr fetch_first_feature(std::string const& filename, bool cache_features)
@ -168,17 +170,31 @@ TEST_CASE("geojson") {
SECTION("GeoJSON GeometryCollection") SECTION("GeoJSON GeometryCollection")
{ {
for (auto cache_features : {true, false}) std::string filename("./test/data/json/geometrycollection.json");
for (auto create_index : { true, false })
{ {
auto feature = detail::fetch_first_feature("./test/data/json/geometrycollection.json", cache_features); if (create_index)
// test {
auto const& geometry = feature->get_geometry(); int ret = detail::create_disk_index(filename, true);
REQUIRE(mapnik::geometry::geometry_type(geometry) == mapnik::geometry::GeometryCollection); int ret_posix = (ret >> 8) & 0x000000ff;
auto const& collection = mapnik::util::get<mapnik::geometry::geometry_collection<double> >(geometry); INFO(ret);
REQUIRE(collection.size() == 2); INFO(ret_posix);
REQUIRE(mapnik::geometry::geometry_type(collection[0]) == mapnik::geometry::Point); // index will not exist because this is not a featurecollection
REQUIRE(mapnik::geometry::geometry_type(collection[1]) == mapnik::geometry::LineString); CHECK(!mapnik::util::exists(filename + ".index"));
REQUIRE(mapnik::geometry::envelope(collection) == mapnik::box2d<double>(100,0,102,1)); }
for (auto cache_features : {true, false})
{
auto feature = detail::fetch_first_feature(filename, cache_features);
// test
auto const& geometry = feature->get_geometry();
REQUIRE(mapnik::geometry::geometry_type(geometry) == mapnik::geometry::GeometryCollection);
auto const& collection = mapnik::util::get<mapnik::geometry::geometry_collection<double> >(geometry);
REQUIRE(collection.size() == 2);
REQUIRE(mapnik::geometry::geometry_type(collection[0]) == mapnik::geometry::Point);
REQUIRE(mapnik::geometry::geometry_type(collection[1]) == mapnik::geometry::LineString);
REQUIRE(mapnik::geometry::envelope(collection) == mapnik::box2d<double>(100,0,102,1));
}
} }
} }
@ -187,22 +203,36 @@ TEST_CASE("geojson") {
// Create datasource // Create datasource
mapnik::parameters params; mapnik::parameters params;
params["type"] = "geojson"; params["type"] = "geojson";
params["file"] = "./test/data/json/feature.json"; std::string filename("./test/data/json/feature.json");
for (auto cache_features : {true, false}) params["file"] = filename;
for (auto create_index : { true, false })
{ {
params["cache-features"] = cache_features; if (create_index)
auto ds = mapnik::datasource_cache::instance().create(params);
REQUIRE(bool(ds));
auto fields = ds->get_descriptor().get_descriptors();
mapnik::query query(ds->envelope());
for (auto const& field : fields)
{ {
query.add_property_name(field.get_name()); int ret = detail::create_disk_index(filename, true);
int ret_posix = (ret >> 8) & 0x000000ff;
INFO(ret);
INFO(ret_posix);
// index will not exist because this is not a featurecollection
CHECK(!mapnik::util::exists(filename + ".index"));
}
for (auto cache_features : {true, false})
{
params["cache-features"] = cache_features;
auto ds = mapnik::datasource_cache::instance().create(params);
REQUIRE(bool(ds));
auto fields = ds->get_descriptor().get_descriptors();
mapnik::query query(ds->envelope());
for (auto const& field : fields)
{
query.add_property_name(field.get_name());
}
auto features = ds->features(query);
REQUIRE(features != nullptr);
auto feature = features->next();
REQUIRE(feature != nullptr);
} }
auto features = ds->features(query);
REQUIRE(features != nullptr);
auto feature = features->next();
REQUIRE(feature != nullptr);
} }
} }
@ -210,6 +240,12 @@ TEST_CASE("geojson") {
{ {
std::string filename("./test/data/json/featurecollection.json"); std::string filename("./test/data/json/featurecollection.json");
// cleanup in the case of a failed previous run
if (mapnik::util::exists(filename + ".index"))
{
boost::filesystem::remove(filename + ".index");
}
for (auto create_index : { true, false }) for (auto create_index : { true, false })
{ {
if (create_index) if (create_index)
@ -264,23 +300,50 @@ TEST_CASE("geojson") {
// Create datasource // Create datasource
mapnik::parameters params; mapnik::parameters params;
params["type"] = "geojson"; params["type"] = "geojson";
params["file"] = "./test/data/json/feature_collection_extra_properties.json"; std::string filename("./test/data/json/feature_collection_extra_properties.json");
for (auto cache_features : {true, false}) params["file"] = filename;
// cleanup in the case of a failed previous run
if (mapnik::util::exists(filename + ".index"))
{ {
params["cache-features"] = cache_features; boost::filesystem::remove(filename + ".index");
auto ds = mapnik::datasource_cache::instance().create(params); }
REQUIRE(bool(ds));
auto fields = ds->get_descriptor().get_descriptors(); for (auto create_index : { true, false })
mapnik::query query(ds->envelope()); {
for (auto const& field : fields) if (create_index)
{ {
query.add_property_name(field.get_name()); int ret = detail::create_disk_index(filename, true);
int ret_posix = (ret >> 8) & 0x000000ff;
INFO(ret);
INFO(ret_posix);
CHECK(mapnik::util::exists(filename + ".index"));
} }
auto features = ds->features(query);
REQUIRE(features != nullptr); for (auto cache_features : {true, false})
auto feature = features->next(); {
REQUIRE(feature != nullptr); params["cache-features"] = cache_features;
REQUIRE(feature->envelope() == mapnik::box2d<double>(123,456,123,456)); auto ds = mapnik::datasource_cache::instance().create(params);
REQUIRE(bool(ds));
auto fields = ds->get_descriptor().get_descriptors();
mapnik::query query(ds->envelope());
for (auto const& field : fields)
{
query.add_property_name(field.get_name());
}
auto features = ds->features(query);
REQUIRE(features != nullptr);
auto feature = features->next();
REQUIRE(feature != nullptr);
REQUIRE(feature->envelope() == mapnik::box2d<double>(123,456,123,456));
}
// cleanup
if (create_index && mapnik::util::exists(filename + ".index"))
{
boost::filesystem::remove(filename + ".index");
}
} }
} }
@ -288,13 +351,41 @@ TEST_CASE("geojson") {
{ {
mapnik::parameters params; mapnik::parameters params;
params["type"] = "geojson"; params["type"] = "geojson";
params["file"] = "./test/data/json/points-malformed.geojson"; // mismatched parentheses std::string filename("./test/data/json/points-malformed.geojson");
for (auto cache_features : {true, false}) params["file"] = filename; // mismatched parentheses
// cleanup in the case of a failed previous run
if (mapnik::util::exists(filename + ".index"))
{ {
params["cache-features"] = cache_features; boost::filesystem::remove(filename + ".index");
REQUIRE_THROWS(mapnik::datasource_cache::instance().create(params)); }
params["cache-features"] = true;
REQUIRE_THROWS(mapnik::datasource_cache::instance().create(params)); for (auto create_index : { true, false })
{
if (create_index)
{
CHECK(!mapnik::util::exists(filename + ".index"));
int ret = detail::create_disk_index(filename, true);
int ret_posix = (ret >> 8) & 0x000000ff;
INFO(ret);
INFO(ret_posix);
CHECK(mapnik::util::exists(filename + ".index"));
}
for (auto cache_features : {true, false})
{
std::stringstream msg;
msg << "testcase: create index " << create_index << " cache-features " << cache_features;
params["cache-features"] = cache_features;
INFO(msg.str());
CHECK_THROWS(mapnik::datasource_cache::instance().create(params));
}
// cleanup
if (create_index && mapnik::util::exists(filename + ".index"))
{
boost::filesystem::remove(filename + ".index");
}
} }
} }
} }