1 """ Steps that run search queries.
3 Queries may either be run directly via PHP using the query script
4 or via the HTTP interface.
10 from collections import OrderedDict
11 from nose.tools import * # for assert functions
13 class SearchResponse(object):
15 def __init__(self, page, fmt='json', errorcode=200):
18 self.errorcode = errorcode
19 getattr(self, 'parse_' + fmt)()
22 self.result = json.JSONDecoder(object_pairs_hook=OrderedDict).decode(self.page)
24 def match_row(self, row):
25 if 'ID' in row.headings:
26 todo = [int(row['ID'])]
28 todo = range(len(self.result))
32 for h in row.headings:
36 assert_equal(res['osm_type'], row[h][0])
37 assert_equal(res['osm_id'], row[h][1:])
39 x, y = row[h].split(' ')
40 assert_almost_equal(float(y), float(res['lat']))
41 assert_almost_equal(float(x), float(res['lon']))
44 assert_equal(str(res[h]), str(row[h]))
47 @when(u'searching for "(?P<query>.*)"(?P<dups> with dups)?')
48 def query_cmd(context, query, dups):
49 """ Query directly via PHP script.
51 cmd = [os.path.join(context.nominatim.build_dir, 'utils', 'query.php'),
53 # add more parameters in table form
55 for h in context.table.headings:
56 value = context.table[0][h].strip()
58 cmd.extend(('--' + h, value))
61 cmd.extend(('--dedupe', '0'))
63 proc = subprocess.Popen(cmd, cwd=context.nominatim.build_dir,
64 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
65 (outp, err) = proc.communicate()
67 assert_equals (0, proc.returncode, "query.php failed with message: %s" % err)
69 context.response = SearchResponse(outp.decode('utf-8'), 'json')