Skip to content

Commit 1c60bb7

Browse files
authored
Update perturbIC_test.py
Update unit tests for mule implementation. They are not fully up-to-date
1 parent 48234ab commit 1c60bb7

File tree

1 file changed

+30
-19
lines changed

1 file changed

+30
-19
lines changed

tests/perturbIC_test.py

+30-19
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import pytest
22
import sys
3-
from perturbIC import parse_args, set_seed, create_perturbation, is_end_of_file, if_perturb
3+
from perturbIC import parse_args, set_seed, create_perturbation, is_end_of_file,do_perturb
44
from unittest.mock import Mock
55
import numpy as np
66
import numpy.random as rs
@@ -42,12 +42,13 @@ def mock_metadata():
4242
list - Command line arguements
4343
"""
4444

45-
metadata_index_false = 43
45+
metadata_index_false = 24
4646
metadata_index_true = -99
4747
end_of_data = -99
4848

4949
return metadata_index_false, metadata_index_true, end_of_data
5050

51+
#Test the Imports may not be necessary
5152
def test_parse_args(monkeypatch, mock_command_line):
5253
"""
5354
This function tests the parse_args function with the fake commandline arguments
@@ -63,7 +64,16 @@ def test_parse_args(monkeypatch, mock_command_line):
6364
assert args.amplitude == 0.4
6465
assert args.seed == 23452
6566
assert args.output == "restart_dump_copy_perturb.astart"
67+
#Test checking the seed
68+
#def test_set_seed(args):
69+
#Not sure if we need test but the conditionals in a function is nice.
6670

71+
72+
#Test creating output file
73+
#def test_creating_output_file():
74+
75+
76+
#Test the random generator
6777
def test_create_perturbation(monkeypatch, mock_command_line, mock_perturbation):
6878
"""
6979
This function tests the create_perturbation function with the fake commandline arguments
@@ -95,7 +105,10 @@ def test_is_end_of_file_keep_going(mock_metadata):
95105
assert is_end_of_file(metadata_index_false, end_of_data) == False
96106
assert is_end_of_file(metadata_index_true, end_of_data) == True
97107

108+
109+
#Test that the perturbation has been applied
98110
def test_applying_perturbation(mock_perturbation):
111+
99112
"""
100113
This function tests the addition of the perturbation to the correct field
101114
This function in the perturbIC.py is written to both check the itemcode when
@@ -110,26 +123,24 @@ def test_applying_perturbation(mock_perturbation):
110123

111124
#Create random perturbation
112125
nlon, nlat = mock_perturbation
113-
perturb = 0.5 * (2.*rs.random(nlon*nlat).reshape((nlat,nlon))-1.)
114-
perturb[0] = 0
115-
perturb[-1] = 0
126+
perturbation = 0.5 * (2.*rs.random(nlon*nlat).reshape((nlat,nlon))-1.)
127+
perturbation[0] = 0
128+
perturbation[-1] = 0
129+
stash_code = 24
116130

117131
#Create a fake data array to simulate the numpy array that is
118132
#To mock the method readfld that reads the field corresponding
119133
#To the itemcode
120-
shape = (nlat, nlon) # Example shape of 3 rows and 4 columns
121-
mock_data = Mock()
122-
mock_data.readfld.return_value = np.ones(shape)
123-
metadata_index = 4
124-
surface_temp_item_code = 4
125-
endgame = 388
126-
k = 0
127-
128-
#Run the fucntion to get the outputs
129-
is_perturb,a = if_perturb(metadata_index,k,mock_data,perturb,surface_temp_item_code,endgame)
134+
135+
shape = (nlat, nlon)
136+
field_theta = Mock()
137+
field_not_theta = Mock()
130138

139+
field_theta.lbuser4 = 24
140+
field_not_theta.lbuser4 = 3
131141
#Testing if the perturb conditional works and if the resulting array is correct
132-
testing_a = np.round((a - perturb) / np.ones(shape),0)
133-
assert is_perturb == True
134-
assert a.shape == (nlat, nlon)
135-
assert testing_a.all() == 1.
142+
#testing_a = np.round((perturbed_array - perturb) / np.ones(shape),0)
143+
assert do_perturb(field_theta, stash_code) == True
144+
assert do_perturb(field_not_theta, stash_code) == False
145+
#assert perturbed_array.shape == (nlat, nlon)
146+
#assert testing_a.all() == 1.

0 commit comments

Comments
 (0)