Commit 7c4ad073 authored by Javier Vegas-Regidor's avatar Javier Vegas-Regidor
Browse files

Fixed all tests

parent 99248740
......@@ -60,7 +60,7 @@ class CDFTools(object):
@staticmethod
def _check_output_was_created(line, output):
if output:
if not os.path.exists(output):
if not os.path.isfile(output):
raise Exception('Error executing {0}\n Output file not created', ' '.join(line))
# noinspection PyShadowingBuiltins
......@@ -69,12 +69,12 @@ class CDFTools(object):
if input:
if isinstance(input, six.string_types):
line.append(input)
if not os.path.exists(input):
if not os.path.isfile(input):
raise ValueError('Error executing {0}\n Input file {1} file does not exist', command, input)
else:
for element in input:
line.append(element)
if not os.path.exists(element):
if not os.path.isfile(element):
raise ValueError('Error executing {0}\n Input file {1} file does not exist', command, element)
# noinspection PyMethodMayBeStatic
......
......@@ -43,8 +43,6 @@ class VerticalMeanMeters(Diagnostic):
self.domain = domain
self.variable = variable
self.box = box
self.required_vars = [variable]
self.generated_vars = [variable + 'vmean']
self.grid_point = grid_point
def __eq__(self, other):
......
......@@ -8,33 +8,32 @@ import mock
def mock_exists(path, access=None):
return not os.path.basename(path.startswith('bad'))
return not os.path.basename(path).startswith('bad')
class TestCDFTools(TestCase):
def setUp(self):
self.cdftools = CDFTools('/test/path')
mock.patch('os.path.join')
@mock.patch('os.path.exists', side_effect=mock_exists)
@mock.patch('os.path.isfile', side_effect=mock_exists)
@mock.patch('os.access', side_effect=mock_exists)
def test_run(self):
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'))
@mock.patch('earthdiagnostics.utils.Utils.execute_shell_command')
def test_run(self, mock_path, mock_exists, 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'))
# coding=utf-8
from unittest import TestCase
from earthdiagnostics.diagnostic import DiagnosticVariableOption
from earthdiagnostics.diagnostic import DiagnosticVariableOption, DiagnosticOptionError
from earthdiagnostics.box import Box
from earthdiagnostics.ocean.cutsection import CutSection
from mock import Mock, patch
......@@ -25,6 +25,8 @@ class TestCutSection(TestCase):
self.psi = CutSection(self.data_manager, '20000101', 1, 1, ModelingRealms.atmos, 'var', True, 0)
def fake_parse(self, value):
if not value:
raise DiagnosticOptionError
return value
@patch.object(DiagnosticVariableOption, 'parse', fake_parse)
......@@ -43,9 +45,9 @@ class TestCutSection(TestCase):
self.assertEqual(jobs[1], CutSection(self.data_manager, '20010101', 0, 1, ModelingRealms.atmos, 'var',
False, 0))
with self.assertRaises(Exception):
with self.assertRaises(DiagnosticOptionError):
CutSection.generate_jobs(self.diags, ['diagnostic'])
with self.assertRaises(Exception):
with self.assertRaises(DiagnosticOptionError):
CutSection.generate_jobs(self.diags, ['diagnostic', '0', '0', '0', '0', '0', '0', '0'])
def test_str(self):
......
# coding=utf-8
from unittest import TestCase
from earthdiagnostics.diagnostic import DiagnosticVariableOption
from earthdiagnostics.diagnostic import DiagnosticVariableOption, DiagnosticOptionError
from earthdiagnostics.box import Box
from earthdiagnostics.ocean.verticalmean import VerticalMean
from mock import Mock, patch
......@@ -23,8 +23,8 @@ class TestVerticalMean(TestCase):
self.mixed = VerticalMean(self.data_manager, '20000101', 1, 1, 'var', self.box)
def fake_parse(self, value):
if value is None:
raise Exception
if not value:
raise DiagnosticOptionError
return value
@patch.object(DiagnosticVariableOption, 'parse', fake_parse)
......@@ -46,10 +46,10 @@ class TestVerticalMean(TestCase):
self.assertEqual(jobs[0], VerticalMean(self.data_manager, '20010101', 0, 0, 'var', Box()))
self.assertEqual(jobs[1], VerticalMean(self.data_manager, '20010101', 0, 1, 'var', Box()))
with self.assertRaises(Exception):
with self.assertRaises(DiagnosticOptionError):
VerticalMean.generate_jobs(self.diags, ['diagnostic'])
with self.assertRaises(Exception):
with self.assertRaises(DiagnosticOptionError):
VerticalMean.generate_jobs(self.diags, ['diagnostic', '0', '0', '0', '0', '0', '0', '0', '0'])
def test_str(self):
......
# coding=utf-8
from unittest import TestCase
from earthdiagnostics.diagnostic import DiagnosticVariableOption
from earthdiagnostics.diagnostic import DiagnosticVariableOption, DiagnosticOptionError
from earthdiagnostics.box import Box
from earthdiagnostics.ocean.verticalmeanmeters import VerticalMeanMeters
from earthdiagnostics.modelingrealm import ModelingRealms
......@@ -24,6 +24,8 @@ class TestVerticalMeanMeters(TestCase):
self.mixed = VerticalMeanMeters(self.data_manager, '20000101', 1, 1, ModelingRealms.ocean, 'var', self.box, 'T')
def fake_parse(self, value):
if not value:
raise DiagnosticOptionError
return value
@patch.object(DiagnosticVariableOption, 'parse', fake_parse)
......@@ -39,23 +41,23 @@ class TestVerticalMeanMeters(TestCase):
box = Box(True)
box.min_depth = 0
self.assertEqual(len(jobs), 2)
self.assertEqual(jobs[0], VerticalMeanMeters(self.data_manager, '20010101', 0, 0, 'var', ModelingRealms.ocean,
self.assertEqual(jobs[0], VerticalMeanMeters(self.data_manager, '20010101', 0, 0, ModelingRealms.ocean, 'var',
box, 'T'))
self.assertEqual(jobs[1], VerticalMeanMeters(self.data_manager, '20010101', 0, 1, 'var', ModelingRealms.ocean,
self.assertEqual(jobs[1], VerticalMeanMeters(self.data_manager, '20010101', 0, 1, ModelingRealms.ocean, 'var',
box, 'T'))
jobs = VerticalMeanMeters.generate_jobs(self.diags, ['diagnostic', 'var'])
self.assertEqual(len(jobs), 2)
self.assertEqual(jobs[0], VerticalMeanMeters(self.data_manager, '20010101', 0, 0, 'var', ModelingRealms.ocean,
self.assertEqual(jobs[0], VerticalMeanMeters(self.data_manager, '20010101', 0, 0, ModelingRealms.ocean, 'var',
Box(True), 'T'))
self.assertEqual(jobs[1], VerticalMeanMeters(self.data_manager, '20010101', 0, 1, 'var', ModelingRealms.ocean,
self.assertEqual(jobs[1], VerticalMeanMeters(self.data_manager, '20010101', 0, 1, ModelingRealms.ocean, 'var',
Box(True), 'T'))
with self.assertRaises(Exception):
with self.assertRaises(DiagnosticOptionError):
VerticalMeanMeters.generate_jobs(self.diags, ['diagnostic'])
with self.assertRaises(Exception):
VerticalMeanMeters.generate_jobs(self.diags, ['diagnostic', '0', '0', '0', '0'])
with self.assertRaises(DiagnosticOptionError):
VerticalMeanMeters.generate_jobs(self.diags, ['diagnostic', '0', '0', '0', '0', '0', '0', '0'])
def test_str(self):
self.assertEquals(str(self.mixed), 'Vertical mean meters Startdate: 20000101 Member: 1 Chunk: 1 '
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment