Skip to content

Commit

Permalink
Update tests for the travis env
Browse files Browse the repository at this point in the history
  • Loading branch information
elpaso committed Aug 8, 2020
1 parent 9ed62a6 commit facfeb6
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 55 deletions.
55 changes: 30 additions & 25 deletions tests/src/python/test_qgsserver_api.py
Expand Up @@ -191,6 +191,34 @@ def assertLinesEqual(self, actual, expected, reference_file):
self.assertEqual(actual_lines[i], expected_lines[i], "File: %s\nLine: %s\nActual : %s\nExpected: %s" % (
reference_file, i, actual_lines[i], expected_lines[i]))

def normalize_json(self, content):
"""Normalize a json string"""

reference_content = content.split('\n')
j = ''.join(reference_content[reference_content.index('') + 1:])
# Do not test timeStamp
j = json.loads(j)
try:
j['timeStamp'] = '2019-07-05T12:27:07Z'
except:
pass
# Fix coordinate precision differences in Travis
try:
bbox = j['extent']['spatial']['bbox'][0]
bbox = [round(c, 4) for c in bbox]
j['extent']['spatial']['bbox'][0] = bbox
except:
pass
json_content = json.dumps(j, indent=4)
# Rounding errors
json_content = re.sub(r'(\d{5})\d+\.\d+', r'\1', json_content)
json_content = re.sub(r'(\d+\.\d{4})\d+', r'\1', json_content)
# Poject hash
json_content = re.sub(r'[a-f0-9]{32}', r'FFFFFFFFFFFFFFFFFFFFFFFFFFFFFF', json_content)
headers_content = '\n'.join(
reference_content[:reference_content.index('') + 1])
return headers_content + '\n' + json_content

def compareApi(self, request, project, reference_file, subdir='api'):
response = QgsBufferServerResponse()
# Add json to accept it reference_file is JSON
Expand All @@ -217,33 +245,10 @@ def compareApi(self, request, project, reference_file, subdir='api'):
f.close()
print("Reference file %s regenerated!" % path.encode('utf8'))

def __normalize_json(content):
reference_content = content.split('\n')
j = ''.join(reference_content[reference_content.index('') + 1:])
# Do not test timeStamp
j = json.loads(j)
try:
j['timeStamp'] = '2019-07-05T12:27:07Z'
except:
pass
# Fix coordinate precision differences in Travis
try:
bbox = j['extent']['spatial']['bbox'][0]
bbox = [round(c, 4) for c in bbox]
j['extent']['spatial']['bbox'][0] = bbox
except:
pass
json_content = json.dumps(j, indent=4)
# Rounding errors
json_content = re.sub(r'(\d+\.\d{6})\d+', r'\1', json_content)
headers_content = '\n'.join(
reference_content[:reference_content.index('') + 1])
return headers_content + '\n' + json_content

with open(path.encode('utf8'), 'r', encoding='utf8') as f:
if reference_file.endswith('json'):
self.assertLinesEqual(__normalize_json(
result), __normalize_json(f.read()), path.encode('utf8'))
self.assertLinesEqual(self.normalize_json(
result), self.normalize_json(f.read()), path.encode('utf8'))
else:
self.assertEqual(f.read(), result)

Expand Down
82 changes: 52 additions & 30 deletions tests/src/python/test_qgsserver_landingpage.py
Expand Up @@ -54,10 +54,9 @@ class QgsServerLandingPageTest(QgsServerAPITestBase):
@classmethod
def setUpClass(cls):
super().setUpClass()
os.environ['QGIS_SERVER_PROJECTS_DIRECTORIES'] = os.path.join(unitTestDataPath('qgis_server'), 'landingpage', 'projects') \
+ '||' \
+ os.path.join(unitTestDataPath('qgis_server'),
'landingpage', 'projects2')
directories = [os.path.join(unitTestDataPath('qgis_server'), 'landingpage', 'projects')]
directories.append(os.path.join(unitTestDataPath('qgis_server'), 'landingpage', 'projects2'))
os.environ['QGIS_SERVER_PROJECTS_DIRECTORIES'] = '||'.join(directories)

if not os.environ.get('TRAVIS', False):
os.environ['QGIS_SERVER_PROJECTS_PG_CONNECTIONS'] = "postgresql://localhost:5432?sslmode=disable&dbname=landing_page_test&schema=public"
Expand All @@ -83,43 +82,66 @@ def test_landing_page_redirects(self):
self.assertEqual(response.headers()[
'Location'], 'http://server.qgis.org/index.html')

def compareProjects(self, actual, expected, expected_path):
"""Order-agnostic project comparison"""

actual_raw = self.normalize_json(actual).split('\n')
expected_raw = self.normalize_json(expected).split('\n')
actual_raw = '\n'.join(actual_raw[actual_raw.index('') + 1:])
expected_raw = '\n'.join(expected_raw[expected_raw.index('') + 1:])
actual_j = json.loads(actual_raw)
expected_j = json.loads(expected_raw)
actual_projects = {p['title']: p for p in actual_j['projects']}
expected_projects = {p['title']: p for p in expected_j['projects']}

if self.regeregenerate_api_reference:
# Try to change timestamp
try:
content = actual.split('\n')
j = ''.join(content[content.index('') + 1:])
j = json.loads(j)
j['timeStamp'] = '2019-07-05T12:27:07Z'
actual = '\n'.join(content[:2]) + '\n' + \
json.dumps(j, ensure_ascii=False, indent=2)
except:
pass
f = open(expected_path.encode('utf8'), 'w+', encoding='utf8')
f.write(actual)
f.close()
print("Reference file %s regenerated!" % expected_path.encode('utf8'))

for title in expected_projects.keys():
self.assertEqual(actual_projects[title], expected_projects[title])

def test_landing_page_json(self):
"""Test landing page in JSON format"""

request = QgsBufferServerRequest('http://server.qgis.org/index.json')
if os.environ.get('TRAVIS', False):
self.compareApi(
request, None, 'test_landing_page_index.json', subdir='landingpage')
response = QgsBufferServerResponse()
self.server.handleRequest(request, response)
j_actual = 'Content-Type: application/json\n\n'
j_actual += bytes(response.body()).decode('utf8)')

if not os.environ.get('TRAVIS', False):
expected_path = os.path.join(unitTestDataPath('qgis_server'), 'landingpage', 'test_landing_page_with_pg_index.json')
else:
self.compareApi(
request, None, 'test_landing_page_with_pg_index.json', subdir='landingpage')
expected_path = os.path.join(unitTestDataPath('qgis_server'), 'landingpage', 'test_landing_page_index.json')

j_expected = open(expected_path).read()
self.compareProjects(j_actual, j_expected, expected_path)

def test_project_json(self):
"""Test landing page project call in JSON format"""

test_projects = {
'de8d9e4e8b448f0bf0a0eb1e0806b763': 'Project1.qgs',
'471b5bb5dbe79f72149529f700d948a3': 'Project2.qgz',
'9124ec69db7f0687f760504aa10e3362': 'test_project_wms_grouped_nested_layers.qgs',
'3b4a3e5d9025926cb673805b914646fa': 'project3.qgz',
}

for identifier, name in test_projects.items():
request = QgsBufferServerRequest(
'http://server.qgis.org/map/' + identifier)
request.setHeader('Accept', 'application/json')
self.compareApi(
request, None, 'test_project_{}.json'.format(name.replace('.', '_')), subdir='landingpage')
# Get hashes for test projects
request = QgsBufferServerRequest('http://server.qgis.org/index.json')
request.setHeader('Accept', 'application/json')
response = QgsBufferServerResponse()
self.server.handleRequest(request, response)

@unittest.skipIf(os.environ.get('TRAVIS'), False)
def test_pg_project_json(self):
"""Test landing page PG project call in JSON format"""
j = json.loads(bytes(response.body()))
test_projects = {p['id']: p['title'].replace(' ', '_') for p in j['projects']}

test_projects = {
'056e0bc472fc60eb32a223acf0d9d897': 'PGProject1',
'c404d3c20fdd5e81bf1c8c2ef895901e': 'PGProject2',
'e152c153073a7e6d3ad669448f85e552': 'my as areas project'
}
for identifier, name in test_projects.items():
request = QgsBufferServerRequest(
'http://server.qgis.org/map/' + identifier)
Expand Down
Binary file not shown.

0 comments on commit facfeb6

Please sign in to comment.