test_cdftools.py 1.73 KB
Newer Older
# coding=utf-8
from unittest import TestCase
Javier Vegas-Regidor's avatar
Javier Vegas-Regidor committed

import os

from earthdiagnostics.cdftools import CDFTools
import mock


Javier Vegas-Regidor's avatar
Javier Vegas-Regidor committed
def mock_exists(path, access=None):
    return not os.path.basename(path.startswith('bad'))


class TestCDFTools(TestCase):
    def setUp(self):
Javier Vegas-Regidor's avatar
Javier Vegas-Regidor committed
        self.cdftools = CDFTools('/test/path')
        mock.patch('os.path.join')

Javier Vegas-Regidor's avatar
Javier Vegas-Regidor committed
    @mock.patch('os.path.exists', side_effect=mock_exists)
    @mock.patch('os.access', side_effect=mock_exists)
    def test_run(self):
Javier Vegas-Regidor's avatar
Javier Vegas-Regidor committed
        with mock.patch('earthdiagnostics.utils.Utils.execute_shell_command') as execute_mock:
            execute_mock.return_value = ['Command output']
            with self.assertRaises(ValueError):
                self.cdftools.run('badcommand', input='input_file', output='output_file')
            with self.assertRaises(ValueError):
                self.cdftools.run('command', input='badinput_file', output='output_file')
            with self.assertRaises(ValueError):
                self.cdftools.run('command', input=['input_file', 'badinput_file'], output='output_file')
            with self.assertRaises(ValueError):
                self.cdftools.run('command', input='input_file', output='input_file')
            with self.assertRaises(Exception):
                self.cdftools.run('command', input='input_file', output='badoutput_file')

            self.cdftools.run('command', input='input_file', output='output_file')
            self.cdftools.run('command', input='input_file')
            self.cdftools.run('command', input=None)
            self.cdftools.run('command', input=['input_file', 'input_file2'])
            self.cdftools.run('command', input='input_file', options='-o -p')
            self.cdftools.run('command', input='input_file', options=('-o', '-p'))