12
12
13
13
14
14
@pytest .mark .parametrize (
15
- "do_args_1, do_args_2, expected_equality, warning_expected " ,
15
+ "do_args_1, do_args_2, expected_equality, wavelength_warning_expected " ,
16
16
[
17
17
# Test when __eq__ returns True and False
18
- # Identical args, expect equality
18
+ # C1: Identical args, expect equality
19
19
(
20
20
{
21
21
"name" : "same" ,
38
38
True ,
39
39
False ,
40
40
),
41
- ( # Different names, expect inequality
41
+ # Different names, expect inequality
42
+ (
42
43
{
43
44
"name" : "something" ,
44
45
"xtype" : "tth" ,
56
57
False ,
57
58
True ,
58
59
),
59
- ( # One without wavelength, expect inequality
60
+ # C2: One without wavelength, expect inequality
61
+ (
60
62
{
61
63
"wavelength" : 0.71 ,
62
64
"xtype" : "tth" ,
73
75
False ,
74
76
True ,
75
77
),
76
- ( # Different wavelengths, expect inequality
78
+ # C3: Different wavelength values, expect inequality
79
+ (
77
80
{
78
81
"wavelength" : 0.71 ,
79
82
"xtype" : "tth" ,
91
94
False ,
92
95
False ,
93
96
),
94
- ( # Different scat_quantity, expect inequality
97
+ # C4: Different scat_quantity, expect inequality
98
+ (
95
99
{
96
100
"scat_quantity" : "x-ray" ,
97
101
"xtype" : "tth" ,
109
113
False ,
110
114
True ,
111
115
),
112
- ( # Different q xarray values, expect inequality
116
+ # C5: Different q xarray values, expect inequality
117
+ (
113
118
{
114
119
"xtype" : "q" ,
115
120
"xarray" : np .array ([1.0 , 2.0 ]),
124
129
False ,
125
130
True ,
126
131
),
127
- ( # Different metadata, expect inequality
132
+ # C6: Different metadata, expect inequality
133
+ (
128
134
{
129
135
"xtype" : "q" ,
130
136
"xarray" : np .empty (0 ),
143
149
],
144
150
)
145
151
def test_diffraction_objects_equality (
146
- do_args_1 , do_args_2 , expected_equality , warning_expected , wavelength_warning_msg
152
+ do_args_1 , do_args_2 , expected_equality , wavelength_warning_expected , wavelength_warning_msg
147
153
):
148
- if warning_expected :
154
+ if wavelength_warning_expected :
149
155
with pytest .warns (UserWarning , match = re .escape (wavelength_warning_msg )):
150
156
do_1 = DiffractionObject (** do_args_1 )
151
157
do_2 = DiffractionObject (** do_args_2 )
@@ -158,9 +164,15 @@ def test_diffraction_objects_equality(
158
164
@pytest .mark .parametrize (
159
165
"xtype, expected_xarray" ,
160
166
[
167
+ # Test whether on_xtype returns the correct xarray values.
168
+ # C1: tth to tth, expect no change in xarray value
169
+ # 1. "tth" provided, expect tth
170
+ # 2. "2theta" provided, expect tth
161
171
("tth" , np .array ([30 , 60 ])),
162
172
("2theta" , np .array ([30 , 60 ])),
173
+ # C2: "q" provided, expect q converted from tth
163
174
("q" , np .array ([0.51764 , 1 ])),
175
+ # C3: "d" provided, expect d converted from tth
164
176
("d" , np .array ([12.13818 , 6.28319 ])),
165
177
],
166
178
)
@@ -185,8 +197,8 @@ def test_init_invalid_xtype():
185
197
@pytest .mark .parametrize (
186
198
"org_do_args, target_do_args, scale_inputs, expected" ,
187
199
[
188
- # Test that scale_to() scales to the correct values
189
- # C1: Same x-array and y-array, check offset
200
+ # Test whether scale_to() scales to the expected values
201
+ # C1: Same x-array and y-array with 2.1 offset, expect yarray shifted by 2.1 offset
190
202
(
191
203
{
192
204
"xarray" : np .array ([10 , 15 , 25 , 30 , 60 , 140 ]),
@@ -290,7 +302,8 @@ def test_scale_to(org_do_args, target_do_args, scale_inputs, expected):
290
302
@pytest .mark .parametrize (
291
303
"org_do_args, target_do_args, scale_inputs" ,
292
304
[
293
- # UC1: User did not specify anything
305
+ # Test expected errors produced from scale_to() with invalid inputs
306
+ # C1: none of q, tth, d, provided, expect ValueError
294
307
(
295
308
{
296
309
"xarray" : np .array ([0.1 , 0.2 , 0.3 ]),
@@ -311,7 +324,7 @@ def test_scale_to(org_do_args, target_do_args, scale_inputs, expected):
311
324
"offset" : 0 ,
312
325
},
313
326
),
314
- # UC2: User specified more than one of q, tth, and d
327
+ # C2: tth and d both provided, expect ValueErrort
315
328
(
316
329
{
317
330
"xarray" : np .array ([10 , 25 , 30.1 , 40.2 , 61 , 120 , 140 ]),
@@ -350,21 +363,84 @@ def test_scale_to_bad(org_do_args, target_do_args, scale_inputs):
350
363
351
364
352
365
@pytest .mark .parametrize (
353
- "wavelength, xarray, yarray, xtype_1, xtype_2, value , expected_index" ,
366
+ "do_args, get_array_index_inputs , expected_index" ,
354
367
[
355
- # UC1: Exact match
356
- (4 * np .pi , np .array ([30.005 , 60 ]), np .array ([1 , 2 ]), "tth" , "tth" , 30.005 , [0 ]),
357
- # UC2: Target value lies in the array, returns the (first) closest index
358
- (4 * np .pi , np .array ([30 , 60 ]), np .array ([1 , 2 ]), "tth" , "tth" , 45 , [0 ]),
359
- (4 * np .pi , np .array ([30 , 60 ]), np .array ([1 , 2 ]), "tth" , "q" , 0.25 , [0 ]),
360
- # UC3: Target value out of the range, returns the closest index
361
- (4 * np .pi , np .array ([0.25 , 0.5 , 0.71 ]), np .array ([1 , 2 , 3 ]), "q" , "q" , 0.1 , [0 ]),
362
- (4 * np .pi , np .array ([30 , 60 ]), np .array ([1 , 2 ]), "tth" , "tth" , 63 , [1 ]),
368
+ # Test get_array_index() returns the expected index given xtype and value
369
+ # C1: Target value is in the xarray and xtype is identical, expect exact index match
370
+ (
371
+ {
372
+ "wavelength" : 4 * np .pi ,
373
+ "xarray" : np .array ([30.005 , 60 ]),
374
+ "yarray" : np .array ([1 , 2 ]),
375
+ "xtype" : "tth" ,
376
+ },
377
+ {
378
+ "xtype" : "tth" ,
379
+ "value" : 30.005 ,
380
+ },
381
+ [0 ],
382
+ ),
383
+ # C2: Target value lies in the array, expect the (first) closest index
384
+ (
385
+ {
386
+ "wavelength" : 4 * np .pi ,
387
+ "xarray" : np .array ([30 , 60 ]),
388
+ "yarray" : np .array ([1 , 2 ]),
389
+ "xtype" : "tth" ,
390
+ },
391
+ {
392
+ "xtype" : "tth" ,
393
+ "value" : 45 ,
394
+ },
395
+ [0 ],
396
+ ),
397
+ (
398
+ {
399
+ "wavelength" : 4 * np .pi ,
400
+ "xarray" : np .array ([30 , 60 ]),
401
+ "yarray" : np .array ([1 , 2 ]),
402
+ "xtype" : "tth" ,
403
+ },
404
+ {
405
+ "xtype" : "q" ,
406
+ "value" : 0.25 ,
407
+ },
408
+ [0 ],
409
+ ),
410
+ # C3: Target value out of the range, expect the closest index
411
+ # 1. Test with xtype of "q"
412
+ (
413
+ {
414
+ "wavelength" : 4 * np .pi ,
415
+ "xarray" : np .array ([0.25 , 0.5 , 0.71 ]),
416
+ "yarray" : np .array ([1 , 2 , 3 ]),
417
+ "xtype" : "q" ,
418
+ },
419
+ {
420
+ "xtype" : "q" ,
421
+ "value" : 0.1 ,
422
+ },
423
+ [0 ],
424
+ ),
425
+ # 2. Test with xtype of "tth"
426
+ (
427
+ {
428
+ "wavelength" : 4 * np .pi ,
429
+ "xarray" : np .array ([30 , 60 ]),
430
+ "yarray" : np .array ([1 , 2 ]),
431
+ "xtype" : "tth" ,
432
+ },
433
+ {
434
+ "xtype" : "tth" ,
435
+ "value" : 63 ,
436
+ },
437
+ [1 ],
438
+ ),
363
439
],
364
440
)
365
- def test_get_array_index (wavelength , xarray , yarray , xtype_1 , xtype_2 , value , expected_index ):
366
- do = DiffractionObject (wavelength = wavelength , xarray = xarray , yarray = yarray , xtype = xtype_1 )
367
- actual_index = do .get_array_index (value = value , xtype = xtype_2 )
441
+ def test_get_array_index (do_args , get_array_index_inputs , expected_index ):
442
+ do = DiffractionObject (** do_args )
443
+ actual_index = do .get_array_index (get_array_index_inputs [ " value" ], get_array_index_inputs [ " xtype" ] )
368
444
assert actual_index == expected_index
369
445
370
446
@@ -411,7 +487,9 @@ def test_dump(tmp_path, mocker):
411
487
@pytest .mark .parametrize (
412
488
"do_init_args, expected_do_dict, divide_by_zero_warning_expected" ,
413
489
[
414
- ( # Instantiate just array attributes
490
+ # Test __dict__ of DiffractionObject instance initialized with valid arguments
491
+ (
492
+ # C1: Minimum arguments provided for init, expect all attributes set without None
415
493
{
416
494
"xarray" : np .array ([0.0 , 90.0 , 180.0 ]),
417
495
"yarray" : np .array ([1.0 , 2.0 , 3.0 ]),
@@ -440,7 +518,8 @@ def test_dump(tmp_path, mocker):
440
518
},
441
519
True ,
442
520
),
443
- ( # Instantiate just array attributes
521
+ # C2: Initialize with an optional scat_quantity argument, expect non-empty string for scat_quantity
522
+ (
444
523
{
445
524
"xarray" : np .array ([np .inf , 2 * np .sqrt (2 ) * np .pi , 2 * np .pi ]),
446
525
"yarray" : np .array ([1.0 , 2.0 , 3.0 ]),
@@ -487,11 +566,12 @@ def test_init_valid(do_init_args, expected_do_dict, divide_by_zero_warning_expec
487
566
@pytest .mark .parametrize (
488
567
"do_init_args, expected_error_msg" ,
489
568
[
490
- ( # C1: No arguments provided
569
+ # Test expected error messages when 3 required arguments not provided in DiffractionObject init
570
+ ( # C1: No arguments provided, expect 3 required positional arguments error
491
571
{},
492
572
"missing 3 required positional arguments: 'xarray', 'yarray', and 'xtype'" ,
493
573
),
494
- ( # C2: Only xarray and yarray provided
574
+ ( # C2: Only xarray and yarray provided, expect 1 required positional argument error
495
575
{"xarray" : np .array ([0.0 , 90.0 ]), "yarray" : np .array ([0.0 , 90.0 ])},
496
576
"missing 1 required positional argument: 'xtype'" ,
497
577
),
@@ -514,7 +594,6 @@ def test_all_array_getter(do_minimal_tth):
514
594
515
595
def test_all_array_setter (do_minimal ):
516
596
do = do_minimal
517
- # Attempt to directly modify the property
518
597
with pytest .raises (
519
598
AttributeError ,
520
599
match = "Direct modification of attribute 'all_arrays' is not allowed. "
0 commit comments