@@ -373,15 +373,16 @@ def test_dump(tmp_path, mocker):
373
373
x , y = np .linspace (0 , 5 , 6 ), np .linspace (0 , 5 , 6 )
374
374
directory = Path (tmp_path )
375
375
file = directory / "testfile"
376
- do = DiffractionObject (
377
- wavelength = 1.54 ,
378
- name = "test" ,
379
- scat_quantity = "x-ray" ,
380
- xarray = np .array (x ),
381
- yarray = np .array (y ),
382
- xtype = "q" ,
383
- metadata = {"thing1" : 1 , "thing2" : "thing2" , "package_info" : {"package2" : "3.4.5" }},
384
- )
376
+ with pytest .warns (RuntimeWarning , match = "divide by zero encountered in divide" ):
377
+ do = DiffractionObject (
378
+ wavelength = 1.54 ,
379
+ name = "test" ,
380
+ scat_quantity = "x-ray" ,
381
+ xarray = np .array (x ),
382
+ yarray = np .array (y ),
383
+ xtype = "q" ,
384
+ metadata = {"thing1" : 1 , "thing2" : "thing2" , "package_info" : {"package2" : "3.4.5" }},
385
+ )
385
386
mocker .patch ("importlib.metadata.version" , return_value = "3.3.0" )
386
387
with freeze_time ("2012-01-14" ):
387
388
do .dump (file , "q" )
@@ -402,114 +403,107 @@ def test_dump(tmp_path, mocker):
402
403
assert actual == expected
403
404
404
405
405
- test_init_valid_params = [
406
- ( # instantiate just array attributes
407
- {
408
- "xarray" : np .array ([0.0 , 90.0 , 180.0 ]),
409
- "yarray" : np .array ([1.0 , 2.0 , 3.0 ]),
410
- "xtype" : "tth" ,
411
- "wavelength" : 4.0 * np .pi ,
412
- },
413
- {
414
- "_all_arrays" : np .array (
415
- [
416
- [1.0 , 0.0 , 0.0 , np .float64 (np .inf )],
417
- [2.0 , 1.0 / np .sqrt (2 ), 90.0 , np .sqrt (2 ) * 2 * np .pi ],
418
- [3.0 , 1.0 , 180.0 , 1.0 * 2 * np .pi ],
419
- ]
420
- ),
421
- "metadata" : {},
422
- "_input_xtype" : "tth" ,
423
- "name" : "" ,
424
- "scat_quantity" : "" ,
425
- "qmin" : np .float64 (0.0 ),
426
- "qmax" : np .float64 (1.0 ),
427
- "tthmin" : np .float64 (0.0 ),
428
- "tthmax" : np .float64 (180.0 ),
429
- "dmin" : np .float64 (2 * np .pi ),
430
- "dmax" : np .float64 (np .inf ),
431
- "wavelength" : 4.0 * np .pi ,
432
- },
433
- ),
434
- ( # instantiate just array attributes
435
- {
436
- "xarray" : np .array ([np .inf , 2 * np .sqrt (2 ) * np .pi , 2 * np .pi ]),
437
- "yarray" : np .array ([1.0 , 2.0 , 3.0 ]),
438
- "xtype" : "d" ,
439
- "wavelength" : 4.0 * np .pi ,
440
- "scat_quantity" : "x-ray" ,
441
- },
442
- {
443
- "_all_arrays" : np .array (
444
- [
445
- [1.0 , 0.0 , 0.0 , np .float64 (np .inf )],
446
- [2.0 , 1.0 / np .sqrt (2 ), 90.0 , np .sqrt (2 ) * 2 * np .pi ],
447
- [3.0 , 1.0 , 180.0 , 1.0 * 2 * np .pi ],
448
- ]
449
- ),
450
- "metadata" : {},
451
- "_input_xtype" : "d" ,
452
- "name" : "" ,
453
- "scat_quantity" : "x-ray" ,
454
- "qmin" : np .float64 (0.0 ),
455
- "qmax" : np .float64 (1.0 ),
456
- "tthmin" : np .float64 (0.0 ),
457
- "tthmax" : np .float64 (180.0 ),
458
- "dmin" : np .float64 (2 * np .pi ),
459
- "dmax" : np .float64 (np .inf ),
460
- "wavelength" : 4.0 * np .pi ,
461
- },
462
- ),
463
- ]
464
-
465
-
466
406
@pytest .mark .parametrize (
467
- "init_args, expected_do_dict" ,
468
- test_init_valid_params ,
407
+ "do_init_args, expected_do_dict, divide_by_zero_warning_expected" ,
408
+ [
409
+ ( # instantiate just array attributes
410
+ {
411
+ "xarray" : np .array ([0.0 , 90.0 , 180.0 ]),
412
+ "yarray" : np .array ([1.0 , 2.0 , 3.0 ]),
413
+ "xtype" : "tth" ,
414
+ "wavelength" : 4.0 * np .pi ,
415
+ },
416
+ {
417
+ "_all_arrays" : np .array (
418
+ [
419
+ [1.0 , 0.0 , 0.0 , np .float64 (np .inf )],
420
+ [2.0 , 1.0 / np .sqrt (2 ), 90.0 , np .sqrt (2 ) * 2 * np .pi ],
421
+ [3.0 , 1.0 , 180.0 , 1.0 * 2 * np .pi ],
422
+ ]
423
+ ),
424
+ "metadata" : {},
425
+ "_input_xtype" : "tth" ,
426
+ "name" : "" ,
427
+ "scat_quantity" : "" ,
428
+ "qmin" : np .float64 (0.0 ),
429
+ "qmax" : np .float64 (1.0 ),
430
+ "tthmin" : np .float64 (0.0 ),
431
+ "tthmax" : np .float64 (180.0 ),
432
+ "dmin" : np .float64 (2 * np .pi ),
433
+ "dmax" : np .float64 (np .inf ),
434
+ "wavelength" : 4.0 * np .pi ,
435
+ },
436
+ True ,
437
+ ),
438
+ ( # instantiate just array attributes
439
+ {
440
+ "xarray" : np .array ([np .inf , 2 * np .sqrt (2 ) * np .pi , 2 * np .pi ]),
441
+ "yarray" : np .array ([1.0 , 2.0 , 3.0 ]),
442
+ "xtype" : "d" ,
443
+ "wavelength" : 4.0 * np .pi ,
444
+ "scat_quantity" : "x-ray" ,
445
+ },
446
+ {
447
+ "_all_arrays" : np .array (
448
+ [
449
+ [1.0 , 0.0 , 0.0 , np .float64 (np .inf )],
450
+ [2.0 , 1.0 / np .sqrt (2 ), 90.0 , np .sqrt (2 ) * 2 * np .pi ],
451
+ [3.0 , 1.0 , 180.0 , 1.0 * 2 * np .pi ],
452
+ ]
453
+ ),
454
+ "metadata" : {},
455
+ "_input_xtype" : "d" ,
456
+ "name" : "" ,
457
+ "scat_quantity" : "x-ray" ,
458
+ "qmin" : np .float64 (0.0 ),
459
+ "qmax" : np .float64 (1.0 ),
460
+ "tthmin" : np .float64 (0.0 ),
461
+ "tthmax" : np .float64 (180.0 ),
462
+ "dmin" : np .float64 (2 * np .pi ),
463
+ "dmax" : np .float64 (np .inf ),
464
+ "wavelength" : 4.0 * np .pi ,
465
+ },
466
+ False ,
467
+ ),
468
+ ],
469
469
)
470
- def test_init_valid (init_args , expected_do_dict ):
471
- actual_do_dict = DiffractionObject (** init_args ).__dict__
470
+ def test_init_valid (do_init_args , expected_do_dict , divide_by_zero_warning_expected ):
471
+ if divide_by_zero_warning_expected :
472
+ with pytest .warns (RuntimeWarning , match = "divide by zero encountered in divide" ):
473
+ actual_do_dict = DiffractionObject (** do_init_args ).__dict__
474
+ else :
475
+ actual_do_dict = DiffractionObject (** do_init_args ).__dict__
472
476
diff = DeepDiff (
473
477
actual_do_dict , expected_do_dict , ignore_order = True , significant_digits = 13 , exclude_paths = "root['_id']"
474
478
)
475
479
assert diff == {}
476
480
477
481
478
- test_init_invalid_params = [
479
- ( # UC1: no arguments provided
480
- {},
481
- "missing 3 required positional arguments: 'xarray', 'yarray', and 'xtype'" ,
482
- ) ,
483
- ( # UC2: only xarray and yarray provided
484
- { "xarray" : np . array ([ 0.0 , 90.0 ]), "yarray" : np . array ([ 0.0 , 90.0 ])} ,
485
- "missing 1 required positional argument: 'xtype'" ,
486
- ) ,
487
- ]
488
-
489
-
490
- @ pytest . mark . parametrize ( "init_args, expected_error_msg" , test_init_invalid_params )
482
+ @ pytest . mark . parametrize (
483
+ "do_init_args, expected_error_msg" ,
484
+ [
485
+ ( # Case 1: no arguments provided
486
+ {} ,
487
+ "missing 3 required positional arguments: 'xarray', ' yarray', and 'xtype'" ,
488
+ ) ,
489
+ ( # Case 2: only xarray and yarray provided
490
+ { "xarray" : np . array ([ 0.0 , 90.0 ]), "yarray" : np . array ([ 0.0 , 90.0 ])} ,
491
+ "missing 1 required positional argument: 'xtype'" ,
492
+ ),
493
+ ],
494
+ )
491
495
def test_init_invalid_args (
492
- init_args ,
496
+ do_init_args ,
493
497
expected_error_msg ,
494
498
):
495
499
with pytest .raises (TypeError , match = expected_error_msg ):
496
- DiffractionObject (** init_args )
500
+ DiffractionObject (** do_init_args )
497
501
498
502
499
- def test_all_array_getter ():
500
- actual_do = DiffractionObject (
501
- xarray = np .array ([0.0 , 90.0 , 180.0 ]),
502
- yarray = np .array ([1.0 , 2.0 , 3.0 ]),
503
- xtype = "tth" ,
504
- wavelength = 4.0 * np .pi ,
505
- )
506
- expected_all_arrays = np .array (
507
- [
508
- [1.0 , 0.0 , 0.0 , np .float64 (np .inf )],
509
- [2.0 , 1.0 / np .sqrt (2 ), 90.0 , np .sqrt (2 ) * 2 * np .pi ],
510
- [3.0 , 1.0 , 180.0 , 1.0 * 2 * np .pi ],
511
- ]
512
- )
503
+ def test_all_array_getter (do_minimal_tth ):
504
+ actual_do = do_minimal_tth
505
+ print (actual_do .all_arrays )
506
+ expected_all_arrays = [[1 , 0.51763809 , 30 , 12.13818192 ], [2 , 1 , 60 , 6.28318531 ]]
513
507
assert np .allclose (actual_do .all_arrays , expected_all_arrays )
514
508
515
509
@@ -574,14 +568,8 @@ def test_input_xtype_setter_error(do_minimal):
574
568
do .input_xtype = "q"
575
569
576
570
577
- def test_copy_object ():
578
- do = DiffractionObject (
579
- name = "test" ,
580
- wavelength = 4.0 * np .pi ,
581
- xarray = np .array ([0.0 , 90.0 , 180.0 ]),
582
- yarray = np .array ([1.0 , 2.0 , 3.0 ]),
583
- xtype = "tth" ,
584
- )
571
+ def test_copy_object (do_minimal ):
572
+ do = do_minimal
585
573
do_copy = do .copy ()
586
574
assert do == do_copy
587
575
assert id (do ) != id (do_copy )
0 commit comments