Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include tests for sample configuration file #1130

Open
pnorman opened this issue Apr 18, 2020 · 1 comment
Open

Include tests for sample configuration file #1130

pnorman opened this issue Apr 18, 2020 · 1 comment
Labels
flex Issues about the flex output

Comments

@pnorman
Copy link
Collaborator

pnorman commented Apr 18, 2020

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.

@pnorman pnorman added the flex Issues about the flex output label Apr 18, 2020
@mmd-osm
Copy link
Contributor

mmd-osm commented May 24, 2020

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"
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flex Issues about the flex output
Projects
None yet
Development

No branches or pull requests

2 participants