|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +############################################################################### |
| 5 | +# Copyright Kitware Inc. and Epidemico Inc. |
| 6 | +# |
| 7 | +# Licensed under the Apache License, Version 2.0 ( the "License" ); |
| 8 | +# you may not use this file except in compliance with the License. |
| 9 | +# You may obtain a copy of the License at |
| 10 | +# |
| 11 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +# |
| 13 | +# Unless required by applicable law or agreed to in writing, software |
| 14 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | +# See the License for the specific language governing permissions and |
| 17 | +# limitations under the License. |
| 18 | +############################################################################### |
| 19 | + |
| 20 | +import glob |
| 21 | +import os |
| 22 | +import unittest |
| 23 | +from datetime import date |
| 24 | +from django.test import TestCase |
| 25 | +from dataqs.udatp.udatp import UoDAirTempPrecipProcessor |
| 26 | +from mock import patch |
| 27 | + |
| 28 | +script_dir = os.path.dirname(os.path.realpath(__file__)) |
| 29 | + |
| 30 | + |
| 31 | +def mock_retrbinary_nc(self, name, writer): |
| 32 | + """ |
| 33 | + Mocks the ftplib.FTP.retrbinary method, writes test image to disk. |
| 34 | + """ |
| 35 | + with open(os.path.join(script_dir, 'resources/uodtest.nc'), 'rb') as inf: |
| 36 | + writer(inf.read()) |
| 37 | + return None |
| 38 | + |
| 39 | + |
| 40 | +def mock_retrbinary_tif(self, name, writer): |
| 41 | + """ |
| 42 | + Mocks the ftplib.FTP.retrbinary method, writes test image to disk. |
| 43 | + """ |
| 44 | + with open(os.path.join(script_dir, 'resources/uodtest.tif'), 'rb') as inf: |
| 45 | + writer(inf.read()) |
| 46 | + return None |
| 47 | + |
| 48 | + |
| 49 | +def mock_none(self, *args): |
| 50 | + """ |
| 51 | + For mocking various FTP methods that should return nothing for tests. |
| 52 | + """ |
| 53 | + return None |
| 54 | + |
| 55 | + |
| 56 | +class UoDAirTempPrecipTest(TestCase): |
| 57 | + """ |
| 58 | + Tests the dataqs.gistemp module. Since each processor is highly |
| 59 | + dependent on a running GeoNode instance for most functions, only |
| 60 | + independent functions are tested here. |
| 61 | + """ |
| 62 | + |
| 63 | + def setUp(self): |
| 64 | + self.processor = UoDAirTempPrecipProcessor() |
| 65 | + |
| 66 | + def tearDown(self): |
| 67 | + self.processor.cleanup() |
| 68 | + |
| 69 | + @patch('ftplib.FTP', autospec=True) |
| 70 | + @patch('ftplib.FTP.retrbinary', mock_retrbinary_nc) |
| 71 | + @patch('ftplib.FTP.connect', mock_none) |
| 72 | + @patch('ftplib.FTP.login', mock_none) |
| 73 | + @patch('ftplib.FTP.cwd', mock_none) |
| 74 | + def test_download(self, ftp_mock): |
| 75 | + """ |
| 76 | + Verify that a file is downloaded |
| 77 | + """ |
| 78 | + cdf_files = self.processor.download() |
| 79 | + for cdf in cdf_files: |
| 80 | + self.assertTrue(os.path.exists(cdf)) |
| 81 | + |
| 82 | + @patch('ftplib.FTP', autospec=True) |
| 83 | + @patch('ftplib.FTP.retrbinary', mock_retrbinary_nc) |
| 84 | + @patch('ftplib.FTP.connect', mock_none) |
| 85 | + @patch('ftplib.FTP.login', mock_none) |
| 86 | + @patch('ftplib.FTP.cwd', mock_none) |
| 87 | + def test_cleanup(self, ftp_mock): |
| 88 | + self.processor.download() |
| 89 | + self.assertNotEqual([], glob.glob(os.path.join( |
| 90 | + self.processor.tmp_dir, self.processor.prefix + '*'))) |
| 91 | + self.processor.cleanup() |
| 92 | + self.assertEquals([], glob.glob(os.path.join( |
| 93 | + self.processor.tmp_dir, self.processor.prefix + '*'))) |
| 94 | + |
| 95 | + def test_date(self): |
| 96 | + last_date = self.processor.get_date(1380) |
| 97 | + self.assertEquals(last_date, date(2015, 12, 1)) |
| 98 | + |
| 99 | + @patch('ftplib.FTP', autospec=True) |
| 100 | + @patch('ftplib.FTP.retrbinary', mock_retrbinary_nc) |
| 101 | + @patch('ftplib.FTP.connect', mock_none) |
| 102 | + @patch('ftplib.FTP.login', mock_none) |
| 103 | + @patch('ftplib.FTP.cwd', mock_none) |
| 104 | + def test_convert(self, ftp_mock): |
| 105 | + cdf_files = self.processor.download() |
| 106 | + for cdf in cdf_files: |
| 107 | + try: |
| 108 | + self.processor.convert(cdf) |
| 109 | + self.assertNotEqual([], glob.glob(cdf.replace( |
| 110 | + '.nc', '.classic.lng.nc'))) |
| 111 | + except OSError: |
| 112 | + # cdo and/or netcdf not installed |
| 113 | + raise unittest.SkipTest() |
| 114 | + |
| 115 | + @patch('ftplib.FTP', autospec=True) |
| 116 | + @patch('ftplib.FTP.retrbinary', mock_retrbinary_tif) |
| 117 | + @patch('ftplib.FTP.connect', mock_none) |
| 118 | + @patch('ftplib.FTP.login', mock_none) |
| 119 | + @patch('ftplib.FTP.cwd', mock_none) |
| 120 | + def test_extract_band(self, ftp_mock): |
| 121 | + cdf = self.processor.download()[0] |
| 122 | + tif = cdf.replace('.nc', '.tif') |
| 123 | + self.processor.extract_band(cdf, 1, tif) |
| 124 | + self.assertTrue(os.path.isfile(tif)) |
0 commit comments