|
| 1 | +import os |
| 2 | +import filecmp |
| 3 | +import shutil |
| 4 | + |
| 5 | +from ..codes import getMPCObsCodeFile |
| 6 | + |
| 7 | +def test_getMPCOBSCodeFile(): |
| 8 | + # Define some file paths, we expect only obscodes and obscodes_old to be affected |
| 9 | + # by any function call to getMPCObsCodeFile |
| 10 | + obscodes = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "data/ObsCodes.html")) |
| 11 | + obscodes_backup = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "data/ObsCodes_backup.html")) |
| 12 | + obscodes_test = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "data/.ObsCodes_test.html")) |
| 13 | + obscodes_old = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "data/ObsCodes.html_old")) |
| 14 | + |
| 15 | + getMPCObsCodeFile() |
| 16 | + |
| 17 | + # Lets check the file downloaded |
| 18 | + assert os.path.isfile(obscodes) |
| 19 | + |
| 20 | + # Modify the file by removing the last 10 lines |
| 21 | + codes = open(obscodes, "r") |
| 22 | + lines = codes.readlines() |
| 23 | + lines = lines[:-10] |
| 24 | + codes.close() |
| 25 | + |
| 26 | + # Write modified file to a new file |
| 27 | + testfile = open(obscodes_test, "w") |
| 28 | + testfile.writelines(lines) |
| 29 | + testfile.close() |
| 30 | + |
| 31 | + # Rename the modified file to the file name expected by getMPCObsCodeFile |
| 32 | + # See if the function recognizes the change and as a result downloads the correct file again. |
| 33 | + os.rename(obscodes, obscodes_backup) |
| 34 | + shutil.copy(obscodes_test, obscodes) |
| 35 | + |
| 36 | + getMPCObsCodeFile() |
| 37 | + |
| 38 | + # Check if newly downloaded file is the same as the unmodified one, check that that modified one has been saved as |
| 39 | + # ObsCodes_old.html |
| 40 | + # (this will only fail in the unlikely chance that between the tests the MPC updated the MPC Obs Codes file) |
| 41 | + assert filecmp.cmp(obscodes, obscodes_backup) |
| 42 | + assert filecmp.cmp(obscodes_test, obscodes_old) |
| 43 | + |
| 44 | + # Clean up files |
| 45 | + os.remove(obscodes_backup) |
| 46 | + os.remove(obscodes_old) |
| 47 | + os.remove(obscodes_test) |
| 48 | + os.remove(obscodes) |
0 commit comments