1
1
import os
2
2
import tempfile
3
3
import unittest
4
+ from unittest .mock import patch
4
5
5
6
from pytissueoptics .rayscattering .opencl .config import CLConfig as clc
6
7
@@ -23,11 +24,11 @@ def testGivenNoConfigFile_shouldWarnAndCreateANewOne(self):
23
24
self .assertTrue (os .path .exists (clc .OPENCL_CONFIG_PATH ))
24
25
25
26
@tempConfigPath
26
- def testGivenNewConfigFile_shouldHaveDefaultValues (self ):
27
+ def testGivenNewConfigFile_shouldHaveDefaultsFromEnvironment (self ):
27
28
with self .assertWarns (UserWarning ):
28
29
config = clc .CLConfig ()
29
- self .assertEqual (None , config .N_WORK_UNITS )
30
- self .assertEqual (None , config .MAX_MEMORY_MB )
30
+ self .assertEqual (str ( os . getenv ( "PTO_N_WORK_UNITS" )), str ( config .N_WORK_UNITS ) )
31
+ self .assertEqual (str ( os . getenv ( "PTO_MAX_MEMORY_MB" )), str ( config .MAX_MEMORY_MB ) )
31
32
self .assertEqual (1000 , config .IPP_TEST_N_PHOTONS )
32
33
self .assertEqual (0.20 , config .BATCH_LOAD_FACTOR )
33
34
@@ -44,7 +45,8 @@ def testGivenMaxMemoryNotSet_whenValidate_shouldWarnAndSetMaxMemory(self):
44
45
with open (clc .OPENCL_CONFIG_PATH , "w" ) as f :
45
46
f .write ('{"DEVICE_INDEX": 0, "N_WORK_UNITS": 100, "MAX_MEMORY_MB": null, '
46
47
'"IPP_TEST_N_PHOTONS": 1000, "BATCH_LOAD_FACTOR": 0.2}' )
47
- config = clc .CLConfig ()
48
+ with patch ("os.getenv" , return_value = None ):
49
+ config = clc .CLConfig ()
48
50
with self .assertWarns (UserWarning ):
49
51
config .validate ()
50
52
self .assertIsNotNone (config .MAX_MEMORY_MB )
@@ -66,8 +68,10 @@ def testGivenFileWithAParameterBelowOrEqualToZero_whenValidate_shouldResetDefaul
66
68
with open (clc .OPENCL_CONFIG_PATH , "w" ) as f :
67
69
f .write ('{"DEVICE_INDEX": 0, "N_WORK_UNITS": 100, "MAX_MEMORY_MB": 0, '
68
70
'"IPP_TEST_N_PHOTONS": 1000, "BATCH_LOAD_FACTOR": 0.2}' )
69
- config = clc .CLConfig ()
70
- with self .assertRaises (ValueError ):
71
- config .validate ()
72
- config = clc .CLConfig ()
73
- self .assertIsNone (config .MAX_MEMORY_MB )
71
+
72
+ with patch ("os.getenv" , return_value = None ):
73
+ config = clc .CLConfig ()
74
+ with self .assertRaises (ValueError ):
75
+ config .validate ()
76
+ config = clc .CLConfig ()
77
+ self .assertIsNone (config .MAX_MEMORY_MB )
0 commit comments