int main(int argc, char *argv[]) {
    boost::scoped_ptr<pg::tempdb> db;

    try {
        db.reset(new pg::tempdb);
    } catch (const std::exception &e) {
        std::cerr << "Unable to setup database: " << e.what() << "\n";
        return 77; // <-- code to skip this test.
    }

    try {
        boost::shared_ptr<middle_pgsql_t> mid_pgsql(new middle_pgsql_t());
        options_t options;
        options.conninfo = db->conninfo().c_str();
        options.num_procs = 1;
        options.prefix = "osm2pgsql_test";
        options.tblsslim_index = "tablespacetest";
        options.tblsslim_data = "tablespacetest";
        options.slim = 1;

        boost::shared_ptr<geometry_processor> processor = geometry_processor::create("polygon", &options);

        export_list columns;
        { taginfo info; info.name = "building"; info.type = "text"; columns.add(OSMTYPE_WAY, info); }

        boost::shared_ptr<output_multi_t> out_test(new output_multi_t("foobar_buildings", processor, columns, mid_pgsql.get(), options));

        osmdata_t osmdata(mid_pgsql, out_test);

        boost::scoped_ptr<parse_delegate_t> parser(new parse_delegate_t(options.extra_attributes, options.bbox, options.projection));

        osmdata.start();

        if (parser->streamFile("pbf", "tests/liechtenstein-2013-08-03.osm.pbf", options.sanitize, &osmdata) != 0) {
            throw std::runtime_error("Unable to read input file `tests/liechtenstein-2013-08-03.osm.pbf'.");
        }

        parser.reset(NULL);

        osmdata.stop();

        // start a new connection to run tests on
        pg::conn_ptr test_conn = pg::conn::connect(db->conninfo());

        check_count(test_conn, 1, "select count(*) from pg_catalog.pg_class where relname = 'osm2pgsql_test_foobar_buildings'");
        check_count(test_conn, 0, "select count(*) from osm2pgsql_test_foobar_buildings where building is null");
        check_count(test_conn, 3723, "select count(*) from osm2pgsql_test_foobar_buildings");

        //check that we have the right spread
        check_count(test_conn, 1, "select count(*) from osm2pgsql_test_foobar_buildings where building='barn'");
        check_count(test_conn, 1, "select count(*) from osm2pgsql_test_foobar_buildings where building='chapel'");
        check_count(test_conn, 5, "select count(*) from osm2pgsql_test_foobar_buildings where building='church'");
        check_count(test_conn, 3, "select count(*) from osm2pgsql_test_foobar_buildings where building='commercial'");
        check_count(test_conn, 6, "select count(*) from osm2pgsql_test_foobar_buildings where building='farm'");
        check_count(test_conn, 1, "select count(*) from osm2pgsql_test_foobar_buildings where building='garage'");
        check_count(test_conn, 2, "select count(*) from osm2pgsql_test_foobar_buildings where building='glasshouse'");
        check_count(test_conn, 1, "select count(*) from osm2pgsql_test_foobar_buildings where building='greenhouse'");
        check_count(test_conn, 153, "select count(*) from osm2pgsql_test_foobar_buildings where building='house'");
        check_count(test_conn, 4, "select count(*) from osm2pgsql_test_foobar_buildings where building='hut'");
        check_count(test_conn, 8, "select count(*) from osm2pgsql_test_foobar_buildings where building='industrial'");
        check_count(test_conn, 200, "select count(*) from osm2pgsql_test_foobar_buildings where building='residential'");
        check_count(test_conn, 6, "select count(*) from osm2pgsql_test_foobar_buildings where building='roof'");
        check_count(test_conn, 4, "select count(*) from osm2pgsql_test_foobar_buildings where building='school'");
        check_count(test_conn, 2, "select count(*) from osm2pgsql_test_foobar_buildings where building='station'");
        check_count(test_conn, 3, "select count(*) from osm2pgsql_test_foobar_buildings where building='warehouse'");
        check_count(test_conn, 3323, "select count(*) from osm2pgsql_test_foobar_buildings where building='yes'");
        return 0;

    } catch (const std::exception &e) {
        std::cerr << "ERROR: " << e.what() << std::endl;

    } catch (...) {
        std::cerr << "UNKNOWN ERROR" << std::endl;
    }

    return 1;
}
int main(int argc, char *argv[]) {
    std::unique_ptr<pg::tempdb> db;

    try {
        db.reset(new pg::tempdb);
    } catch (const std::exception &e) {
        std::cerr << "Unable to setup database: " << e.what() << "\n";
        return 77; // <-- code to skip this test.
    }

    try {
        std::shared_ptr<middle_pgsql_t> mid_pgsql(new middle_pgsql_t());
        options_t options;
        options.database_options = db->database_options;
        options.num_procs = 1;
        options.prefix = "osm2pgsql_test";
        options.style="tests/hstore-match-only.style";
        options.hstore_match_only=1;
        options.hstore_mode = HSTORE_NORM;
        options.slim = 1;

        auto out_test = std::make_shared<output_pgsql_t>(mid_pgsql.get(), options);

        osmdata_t osmdata(mid_pgsql, out_test);

        std::unique_ptr<parse_delegate_t> parser(new parse_delegate_t(options.extra_attributes, options.bbox, options.projection, false));

        osmdata.start();

        parser->stream_file("libxml2", "tests/hstore-match-only.osm", &osmdata);

        parser.reset(nullptr);

        osmdata.stop();

        // tables should not contain any tag columns
        db->check_count(4, "select count(column_name) from information_schema.columns where table_name='osm2pgsql_test_point'");
        db->check_count(5, "select count(column_name) from information_schema.columns where table_name='osm2pgsql_test_polygon'");
        db->check_count(5, "select count(column_name) from information_schema.columns where table_name='osm2pgsql_test_line'");
        db->check_count(5, "select count(column_name) from information_schema.columns where table_name='osm2pgsql_test_roads'");
        
        // the testfile contains 19 tagged ways and 7 tagged nodes
        // out of them 18 ways and 6 nodes are interesting as specified by hstore-match-only.style
        // as there is also one relation we should end up getting a database which contains:
        // 6 objects in osm2pgsql_test_point
        // 7 objects in osm2pgsql_test_polygon
        // 12 objects in osm2pgsql_test_line
        // 3 objects in osm2pgsql_test_roads
        
        db->check_count(6, "select count(*) from osm2pgsql_test_point");
        db->check_count(7, "select count(*) from osm2pgsql_test_polygon");
        db->check_count(12, "select count(*) from osm2pgsql_test_line");
        db->check_count(3, "select count(*) from osm2pgsql_test_roads");
        
        return 0;

    } catch (const std::exception &e) {
        std::cerr << "ERROR: " << e.what() << std::endl;

    } catch (...) {
        std::cerr << "UNKNOWN ERROR" << std::endl;
    }

    return 1;
}
int main(int argc, char *argv[]) {
    boost::scoped_ptr<pg::tempdb> db;

    try {
        db.reset(new pg::tempdb);
    } catch (const std::exception &e) {
        std::cerr << "Unable to setup database: " << e.what() << "\n";
        return 77; // <-- code to skip this test.
    }

    try {
        boost::shared_ptr<middle_pgsql_t> mid_pgsql(new middle_pgsql_t());
        options_t options;
        options.conninfo = db->conninfo().c_str();
        options.num_procs = 1;
        options.prefix = "osm2pgsql_test";
        options.tblsslim_index = "tablespacetest";
        options.tblsslim_data = "tablespacetest";
        options.slim = 1;

        boost::shared_ptr<geometry_processor> processor =
            geometry_processor::create("point", &options);

        export_list columns;
        { taginfo info; info.name = "amenity"; info.type = "text"; columns.add(OSMTYPE_NODE, info); }

        boost::shared_ptr<output_multi_t> out_test(new output_multi_t("foobar_amenities", processor, columns, mid_pgsql.get(), options));

        osmdata_t osmdata(mid_pgsql, out_test);

        boost::scoped_ptr<parse_delegate_t> parser(new parse_delegate_t(options.extra_attributes, options.bbox, options.projection));

        osmdata.start();

        if (parser->streamFile("pbf", "tests/liechtenstein-2013-08-03.osm.pbf", options.sanitize, &osmdata) != 0) {
            throw std::runtime_error("Unable to read input file `tests/liechtenstein-2013-08-03.osm.pbf'.");
        }

        parser.reset(NULL);

        osmdata.stop();

        // start a new connection to run tests on
        pg::conn_ptr test_conn = pg::conn::connect(db->conninfo());

        check_count(test_conn, 1,
                    "select count(*) from pg_catalog.pg_class "
                    "where relname = 'osm2pgsql_test_foobar_amenities'");

        check_count(test_conn, 244,
                    "select count(*) from osm2pgsql_test_foobar_amenities");

        check_count(test_conn, 36,
                    "select count(*) from osm2pgsql_test_foobar_amenities where amenity='parking'");

        check_count(test_conn, 34,
                    "select count(*) from osm2pgsql_test_foobar_amenities where amenity='bench'");

        check_count(test_conn, 1,
                    "select count(*) from osm2pgsql_test_foobar_amenities where amenity='vending_machine'");

        return 0;

    } catch (const std::exception &e) {
        std::cerr << "ERROR: " << e.what() << std::endl;

    } catch (...) {
        std::cerr << "UNKNOWN ERROR" << std::endl;
    }

    return 1;
}
int main(int argc, char *argv[]) {
    boost::scoped_ptr<pg::tempdb> db;

    try {
        db.reset(new pg::tempdb);
    } catch (const std::exception &e) {
        std::cerr << "Unable to setup database: " << e.what() << "\n";
        return 77; // <-- code to skip this test.
    }

    try {
        boost::shared_ptr<middle_pgsql_t> mid_pgsql(new middle_pgsql_t());
        options_t options;
        options.conninfo = db->conninfo().c_str();
        options.num_procs = 1;
        options.prefix = "osm2pgsql_test";
        options.tblsslim_index = "tablespacetest";
        options.tblsslim_data = "tablespacetest";
        options.slim = 1;

        boost::shared_ptr<geometry_processor> processor =
            geometry_processor::create("line", &options);

        export_list columns;
        { taginfo info; info.name = "highway"; info.type = "text"; columns.add(OSMTYPE_WAY, info); }

        boost::shared_ptr<output_multi_t> out_test(new output_multi_t("foobar_highways", processor, columns, mid_pgsql.get(), options));

        osmdata_t osmdata(mid_pgsql, out_test);

        boost::scoped_ptr<parse_delegate_t> parser(new parse_delegate_t(options.extra_attributes, options.bbox, options.projection));

        osmdata.start();

        if (parser->streamFile("pbf", "tests/liechtenstein-2013-08-03.osm.pbf", options.sanitize, &osmdata) != 0) {
            throw std::runtime_error("Unable to read input file `tests/liechtenstein-2013-08-03.osm.pbf'.");
        }

        parser.reset(NULL);

        osmdata.stop();

        // start a new connection to run tests on
        pg::conn_ptr test_conn = pg::conn::connect(db->conninfo());

        check_count(test_conn, 1, "select count(*) from pg_catalog.pg_class where relname = 'osm2pgsql_test_foobar_highways'");
        check_count(test_conn, 2753, "select count(*) from osm2pgsql_test_foobar_highways");

        //check that we have the right spread
        check_count(test_conn, 13, "select count(*) from osm2pgsql_test_foobar_highways where highway='bridleway'");
        check_count(test_conn, 3, "select count(*) from osm2pgsql_test_foobar_highways where highway='construction'");
        check_count(test_conn, 96, "select count(*) from osm2pgsql_test_foobar_highways where highway='cycleway'");
        check_count(test_conn, 249, "select count(*) from osm2pgsql_test_foobar_highways where highway='footway'");
        check_count(test_conn, 18, "select count(*) from osm2pgsql_test_foobar_highways where highway='living_street'");
        check_count(test_conn, 171, "select count(*) from osm2pgsql_test_foobar_highways where highway='path'");
        check_count(test_conn, 6, "select count(*) from osm2pgsql_test_foobar_highways where highway='pedestrian'");
        check_count(test_conn, 81, "select count(*) from osm2pgsql_test_foobar_highways where highway='primary'");
        check_count(test_conn, 842, "select count(*) from osm2pgsql_test_foobar_highways where highway='residential'");
        check_count(test_conn, 3, "select count(*) from osm2pgsql_test_foobar_highways where highway='road'");
        check_count(test_conn, 90, "select count(*) from osm2pgsql_test_foobar_highways where highway='secondary'");
        check_count(test_conn, 1, "select count(*) from osm2pgsql_test_foobar_highways where highway='secondary_link'");
        check_count(test_conn, 352, "select count(*) from osm2pgsql_test_foobar_highways where highway='service'");
        check_count(test_conn, 34, "select count(*) from osm2pgsql_test_foobar_highways where highway='steps'");
        check_count(test_conn, 33, "select count(*) from osm2pgsql_test_foobar_highways where highway='tertiary'");
        check_count(test_conn, 597, "select count(*) from osm2pgsql_test_foobar_highways where highway='track'");
        check_count(test_conn, 164, "select count(*) from osm2pgsql_test_foobar_highways where highway='unclassified'");
        return 0;

    } catch (const std::exception &e) {
        std::cerr << "ERROR: " << e.what() << std::endl;

    } catch (...) {
        std::cerr << "UNKNOWN ERROR" << std::endl;
    }

    return 1;
}
int main(int argc, char *argv[]) {
    std::unique_ptr<pg::tempdb> db;

    try {
        db.reset(new pg::tempdb);
    } catch (const std::exception &e) {
        std::cerr << "Unable to setup database: " << e.what() << "\n";
        return 77; // <-- code to skip this test.
    }

    try {
        std::shared_ptr<middle_pgsql_t> mid_pgsql(new middle_pgsql_t());
        options_t options;
        options.database_options = db->database_options;
        options.num_procs = 1;
        options.prefix = "osm2pgsql_test";
        options.slim = true;

        export_list columns;
        { taginfo info; info.name = "amenity"; info.type = "text"; columns.add(OSMTYPE_NODE, info); }

        std::vector<std::shared_ptr<output_t> > outputs;

        // let's make lots of tables!
        for (int i = 0; i < 10; ++i) {
            std::string name = (boost::format("foobar_%d") % i).str();

            std::shared_ptr<geometry_processor> processor =
                geometry_processor::create("point", &options);

            auto out_test = std::make_shared<output_multi_t>(name, processor, columns, mid_pgsql.get(), options);

            outputs.push_back(out_test);
        }

        osmdata_t osmdata(mid_pgsql, outputs);

        std::unique_ptr<parse_delegate_t> parser(new parse_delegate_t(options.extra_attributes, options.bbox, options.projection, options.append));

        osmdata.start();

        parser->stream_file("pbf", "tests/liechtenstein-2013-08-03.osm.pbf", &osmdata);

        parser.reset(nullptr);

        osmdata.stop();

        for (int i = 0; i < 10; ++i) {
            std::string name = (boost::format("foobar_%d") % i).str();

            db->check_count(1,
                        (boost::format("select count(*) from pg_catalog.pg_class "
                                       "where relname = 'foobar_%d'")
                         % i).str());

            db->check_count(244,
                        (boost::format("select count(*) from foobar_%d")
                         % i).str());

            db->check_count(36,
                        (boost::format("select count(*) from foobar_%d "
                                       "where amenity='parking'")
                         % i).str());

            db->check_count(34,
                        (boost::format("select count(*) from foobar_%d "
                                       "where amenity='bench'")
                         % i).str());

            db->check_count(1,
                        (boost::format("select count(*) from foobar_%d "
                                       "where amenity='vending_machine'")
                         % i).str());
        }

        return 0;

    } catch (const std::exception &e) {
        std::cerr << "ERROR: " << e.what() << std::endl;

    } catch (...) {
        std::cerr << "UNKNOWN ERROR" << std::endl;
    }

    return 1;
}
int main(int argc, char *argv[]) {
    std::unique_ptr<pg::tempdb> db;

    try {
        db.reset(new pg::tempdb);
    } catch (const std::exception &e) {
        std::cerr << "Unable to setup database: " << e.what() << "\n";
        return 77; // <-- code to skip this test.
    }

    try {
        std::shared_ptr<middle_pgsql_t> mid_pgsql(new middle_pgsql_t());
        options_t options;
        options.database_options = db->database_options;
        options.num_procs = 1;
        options.slim = true;

        std::shared_ptr<geometry_processor> processor =
            geometry_processor::create("line", &options);

        export_list columns;
        { taginfo info; info.name = "highway"; info.type = "text"; columns.add(osmium::item_type::way, info); }

        // This actually uses the multi-backend with C transforms, not Lua transforms. This is unusual and doesn't reflect real practice
        auto out_test = std::make_shared<output_multi_t>("foobar_highways", processor, columns, mid_pgsql.get(), options);

        osmdata_t osmdata(mid_pgsql, out_test, options.projection);

        testing::parse("tests/liechtenstein-2013-08-03.osm.pbf", "pbf",
                       options, &osmdata);

        // start a new connection to run tests on
        db->check_count(1, "select count(*) from pg_catalog.pg_class where relname = 'foobar_highways'");
        db->check_count(2752, "select count(*) from foobar_highways");

        //check that we have the right spread
        db->check_count(13, "select count(*) from foobar_highways where highway='bridleway'");
        db->check_count(3, "select count(*) from foobar_highways where highway='construction'");
        db->check_count(96, "select count(*) from foobar_highways where highway='cycleway'");
        db->check_count(249, "select count(*) from foobar_highways where highway='footway'");
        db->check_count(18, "select count(*) from foobar_highways where highway='living_street'");
        db->check_count(171, "select count(*) from foobar_highways where highway='path'");
        db->check_count(5, "select count(*) from foobar_highways where highway='pedestrian'");
        db->check_count(81, "select count(*) from foobar_highways where highway='primary'");
        db->check_count(842, "select count(*) from foobar_highways where highway='residential'");
        db->check_count(3, "select count(*) from foobar_highways where highway='road'");
        db->check_count(90, "select count(*) from foobar_highways where highway='secondary'");
        db->check_count(1, "select count(*) from foobar_highways where highway='secondary_link'");
        db->check_count(352, "select count(*) from foobar_highways where highway='service'");
        db->check_count(34, "select count(*) from foobar_highways where highway='steps'");
        db->check_count(33, "select count(*) from foobar_highways where highway='tertiary'");
        db->check_count(597, "select count(*) from foobar_highways where highway='track'");
        db->check_count(164, "select count(*) from foobar_highways where highway='unclassified'");
        return 0;

    } catch (const std::exception &e) {
        std::cerr << "ERROR: " << e.what() << std::endl;

    } catch (...) {
        std::cerr << "UNKNOWN ERROR" << std::endl;
    }

    return 1;
}