1
1
import pytest
2
2
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
4
4
from unittest .mock import Mock
5
5
import numpy as np
6
6
import numpy .random as rs
@@ -42,12 +42,13 @@ def mock_metadata():
42
42
list - Command line arguements
43
43
"""
44
44
45
- metadata_index_false = 43
45
+ metadata_index_false = 24
46
46
metadata_index_true = - 99
47
47
end_of_data = - 99
48
48
49
49
return metadata_index_false , metadata_index_true , end_of_data
50
50
51
+ #Test the Imports may not be necessary
51
52
def test_parse_args (monkeypatch , mock_command_line ):
52
53
"""
53
54
This function tests the parse_args function with the fake commandline arguments
@@ -63,7 +64,16 @@ def test_parse_args(monkeypatch, mock_command_line):
63
64
assert args .amplitude == 0.4
64
65
assert args .seed == 23452
65
66
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.
66
70
71
+
72
+ #Test creating output file
73
+ #def test_creating_output_file():
74
+
75
+
76
+ #Test the random generator
67
77
def test_create_perturbation (monkeypatch , mock_command_line , mock_perturbation ):
68
78
"""
69
79
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):
95
105
assert is_end_of_file (metadata_index_false , end_of_data ) == False
96
106
assert is_end_of_file (metadata_index_true , end_of_data ) == True
97
107
108
+
109
+ #Test that the perturbation has been applied
98
110
def test_applying_perturbation (mock_perturbation ):
111
+
99
112
"""
100
113
This function tests the addition of the perturbation to the correct field
101
114
This function in the perturbIC.py is written to both check the itemcode when
@@ -110,26 +123,24 @@ def test_applying_perturbation(mock_perturbation):
110
123
111
124
#Create random perturbation
112
125
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
116
130
117
131
#Create a fake data array to simulate the numpy array that is
118
132
#To mock the method readfld that reads the field corresponding
119
133
#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 ()
130
138
139
+ field_theta .lbuser4 = 24
140
+ field_not_theta .lbuser4 = 3
131
141
#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