osm2pgsql-dev / osm2pgsql

OpenStreetMap data to PostgreSQL converter
https://osm2pgsql.org
GNU General Public License v2.0
1.51k stars 474 forks source link

Include tests for sample configuration file #1130

Open pnorman opened 4 years ago

pnorman commented 4 years ago

Any complex lua transform should have tests to check expected behavior against actual behavior. For the pgsql backend an example is OpenStreetMap Carto's tests. For the multi-backend, an example is the extensive set of tests in the test-* files of cleartables.

Because flex includes complications like callbacks into osm2pgsql code, we should supply tests along with the examples.

mmd-osm commented 4 years ago

Maybe we could enhance the test framework a bit to provide some very basic mock object for the osm2pgsql functionality provided by osm2pgsql. I believe this isn't supported by tests.lua as of now.

(file osm2pgsql-mock.lua)

local osm2pgsql = {}

-- osm2pgsql.version = '1.2'
-- osm2pgsql.srid = 
-- osm2pgsql.mode = 'create'
-- osm2pgsql.stage = 1

local inspect = require('inspect')

result = {}    -- todo needs a better place

function osm2pgsql.define_table(o)

   local mock_table = {}

--   print(inspect(o))

   mock_table.name = o.name
   mock_table.ids = o.ids
   mock_table.columns = o.columns

   function mock_table.add_row(x, y)
--      print(inspect(x))
--      print(inspect(y))
      result = y

   end

   function mock_table.mark_way(x, id)

   end

   function mock_table.mark_relation(x, id)

   end

   return mock_table
end

function osm2pgsql.get_mock_result()
  return result
end

return osm2pgsql

File test.lua:

osm2pgsql = require 'osm2pgsql-mock'
-- require 'init'    -- include src/init.lua for additional osm2pgsql functions
require 'openstreetmap-carto'

print("Running tests...")

x = {}
x.tags = {}
x.tags["highway"] = "bus_stop"
x.tags["layer"] = "1"

-- Simulate callback by osm2pgsql
osm2pgsql.process_node(x)

-- Fetch results in mock object
r = osm2pgsql.get_mock_result()

assert(r["highway"] == 'bus_stop')

print("Done...")

Executing test from command line:

lua test.lua 
Running tests...
Done...

Even stuff like grab_tag can mocked (simplified version below doesn't remove tag yet):

osm2pgsql = require 'osm2pgsql-mock'
require 'init'
require 'data-types'

print("Running tests...")

x = {}
x.tags = {}
x.tags["odbl"] = "remove_me"
x.tags["highway"] = "motorway"
x.tags["name"] = "demo name"
x.tags["maxspeed"] = "60 mph"
x.tags["oneway"] = "yes"
x.nodes = { 10, 20, 30, 40, 50 }

x.grab_tag = function(x, y) return x.tags[y] end

osm2pgsql.process_way(x)

print("Done...")
{
  maxspeed = 96,
  name = "demo name",
  nodes = "{10,20,30,40,50}",
  oneway = "yes",
  tags = {
    highway = "motorway",
    maxspeed = "60 mph",
    name = "demo name",
    oneway = "yes"
  },
  type = "motorway"
}