app_test.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import os
  2. import time
  3. import pytest
  4. from app import app
  5. def createFile(name, size):
  6. # create a file of random data
  7. with open(name, 'wb') as fout:
  8. fout.write(os.urandom(size)) # replace 1024 with size_kb if not unreasonably large
  9. @pytest.fixture
  10. def client():
  11. app.config['TESTING'] = True
  12. with app.test_client() as client:
  13. yield client
  14. def test_homepage(client):
  15. """Test that the homepage can be generated"""
  16. rv = client.get('/')
  17. # assert all the controls are there
  18. assert b'<title>ArduPilot Terrain Generator</title>' in rv.data
  19. assert b'<form action="/generate" method="post">' in rv.data
  20. assert b'<input type="text" id="lat" name="lat" value="-35.363261" oninput="plotCircleCoords(true);">' in rv.data
  21. assert b'<input type="text" id="long" name="long" value="149.165230" oninput="plotCircleCoords(true);">' in rv.data
  22. assert b'<input type="range" id="radius" name="radius" value="100" min="1" max="400"\n oninput="plotCircleCoords(false);">' in rv.data
  23. assert b'<input type="submit" value="Generate" method="post">' in rv.data
  24. def test_badinput(client):
  25. """Test bad inputs"""
  26. # no input
  27. rv = client.post('/generate', data=dict(
  28. ), follow_redirects=True)
  29. assert b'<title>ArduPilot Terrain Generator</title>' in rv.data
  30. assert b'Error' in rv.data
  31. assert b'Link To Download' not in rv.data
  32. #partial input
  33. rv = client.post('/generate', data=dict(
  34. lat='-35.363261',
  35. long='149.165230',
  36. ), follow_redirects=True)
  37. assert b'<title>ArduPilot Terrain Generator</title>' in rv.data
  38. assert b'Error' in rv.data
  39. assert b'Link To Download' not in rv.data
  40. #bad lon/lat
  41. rv = client.post('/generate', data=dict(
  42. lat='I am bad data',
  43. long='echo test',
  44. radius='1',
  45. ), follow_redirects=True)
  46. assert b'<title>ArduPilot Terrain Generator</title>' in rv.data
  47. assert b'Error' in rv.data
  48. assert b'download="terrain.zip"' not in rv.data
  49. #out of bounds lon/lat
  50. rv = client.post('/generate', data=dict(
  51. lat='206.56',
  52. long='-400',
  53. radius='1',
  54. ), follow_redirects=True)
  55. assert b'<title>ArduPilot Terrain Generator</title>' in rv.data
  56. assert b'Error' in rv.data
  57. assert b'download="terrain.zip"' not in rv.data
  58. def test_simplegen(client):
  59. """Test that a small piece of terrain can be generated"""
  60. rv = client.post('/generate', data=dict(
  61. lat='-35.363261',
  62. long='149.165230',
  63. radius='1',
  64. ), follow_redirects=True)
  65. assert b'<title>ArduPilot Terrain Generator</title>' in rv.data
  66. assert b'Error' not in rv.data
  67. assert b'Tiles outside of +60 to -60 latitude were requested' not in rv.data
  68. assert b'download="terrain.zip"' in rv.data
  69. uuidkey = (rv.data.split(b"footer")[1][1:-2]).decode("utf-8")
  70. assert uuidkey != ""
  71. #file should be ready for download and around 2MB in size
  72. rdown = client.get('/terrain/' + uuidkey + ".zip", follow_redirects=True)
  73. assert b'404 Not Found' not in rdown.data
  74. assert len(rdown.data) > (1*1024*1024)
  75. def test_simplegenoutside(client):
  76. """Test that a small piece of terrain can be generated with partial outside +-60latitude"""
  77. rv = client.post('/generate', data=dict(
  78. lat='-59.363261',
  79. long='149.165230',
  80. radius='200',
  81. ), follow_redirects=True)
  82. assert b'<title>ArduPilot Terrain Generator</title>' in rv.data
  83. assert b'Error' not in rv.data
  84. assert b'Tiles outside of +60 to -60 latitude were requested' in rv.data
  85. assert b'download="terrain.zip"' in rv.data
  86. uuidkey = (rv.data.split(b"footer")[1][1:-2]).decode("utf-8")
  87. assert uuidkey != ""
  88. #file should be ready for download and around 2MB in size
  89. rdown = client.get('/terrain/' + uuidkey + ".zip", follow_redirects=True)
  90. assert b'404 Not Found' not in rdown.data
  91. assert len(rdown.data) > (0.25*1024*1024)
  92. def test_multigen(client):
  93. """Test that a a few small piece of terrains can be generated"""
  94. rva = client.post('/generate', data=dict(
  95. lat='-35.363261',
  96. long='149.165230',
  97. radius='1',
  98. ), follow_redirects=True)
  99. time.sleep(0.1)
  100. rvb = client.post('/generate', data=dict(
  101. lat='-35.363261',
  102. long='147.165230',
  103. radius='10',
  104. ), follow_redirects=True)
  105. time.sleep(0.1)
  106. rvc = client.post('/generate', data=dict(
  107. lat='-30.363261',
  108. long='137.165230',
  109. radius='100',
  110. ), follow_redirects=True)
  111. time.sleep(0.1)
  112. # Assert reponse is OK and get UUID for each ter gen
  113. allUuid = []
  114. for rv in [rva, rvb, rvc]:
  115. assert b'<title>ArduPilot Terrain Generator</title>' in rv.data
  116. assert b'Error' not in rv.data
  117. assert b'download="terrain.zip"' in rv.data
  118. uuidkey = (rv.data.split(b"footer")[1][1:-2]).decode("utf-8")
  119. assert uuidkey != ""
  120. allUuid.append(uuidkey)
  121. #files should be ready for download and around 0.7MB in size
  122. for uukey in allUuid:
  123. rdown = client.get('/terrain/' + uukey + ".zip", follow_redirects=True)
  124. assert b'404 Not Found' not in rdown.data
  125. assert len(rdown.data) > (0.7*1024*1024)