1
1
import atexit
2
- import os
3
2
import tempfile
4
3
import unittest
5
4
from numbers import Integral
@@ -456,67 +455,67 @@ def test_copy_all():
456
455
assert destination_group .subgroup .attrs ["info" ] == "sub attrs"
457
456
458
457
459
- # noinspection PyAttributeOutsideInit
460
- class TestCopy (unittest .TestCase ):
461
-
462
- def __init__ (self , * args , ** kwargs ):
463
- super ().__init__ (* args , ** kwargs )
464
- self .source_h5py = False
465
- self .dest_h5py = False
466
- self .new_source = group
467
- self .new_dest = group
468
-
469
- def setUp (self ):
470
- source = self .new_source ()
471
- foo = source .create_group ('foo' )
472
- foo .attrs ['experiment' ] = 'weird science'
473
- baz = foo .create_dataset ('bar/baz' , data = np .arange (100 ), chunks = (50 ,))
474
- baz .attrs ['units' ] = 'metres'
475
- if self .source_h5py :
476
- extra_kws = dict (compression = 'gzip' , compression_opts = 3 , fillvalue = 84 ,
477
- shuffle = True , fletcher32 = True )
458
+ class TestCopy :
459
+ @pytest .fixture (params = [False , True ], ids = ['zarr' , 'hdf5' ])
460
+ def source (self , request , tmpdir ):
461
+ def prep_source (source ):
462
+ foo = source .create_group ('foo' )
463
+ foo .attrs ['experiment' ] = 'weird science'
464
+ baz = foo .create_dataset ('bar/baz' , data = np .arange (100 ), chunks = (50 ,))
465
+ baz .attrs ['units' ] = 'metres'
466
+ if request .param :
467
+ extra_kws = dict (compression = 'gzip' , compression_opts = 3 , fillvalue = 84 ,
468
+ shuffle = True , fletcher32 = True )
469
+ else :
470
+ extra_kws = dict (compressor = Zlib (3 ), order = 'F' , fill_value = 42 , filters = [Adler32 ()])
471
+ source .create_dataset ('spam' , data = np .arange (100 , 200 ).reshape (20 , 5 ),
472
+ chunks = (10 , 2 ), dtype = 'i2' , ** extra_kws )
473
+ return source
474
+
475
+ if request .param :
476
+ h5py = pytest .importorskip ('h5py' )
477
+ fn = tmpdir .join ('source.h5' )
478
+ with h5py .File (str (fn ), mode = 'w' ) as h5f :
479
+ yield prep_source (h5f )
478
480
else :
479
- extra_kws = dict (compressor = Zlib (3 ), order = 'F' , fill_value = 42 ,
480
- filters = [Adler32 ()])
481
- source .create_dataset ('spam' , data = np .arange (100 , 200 ).reshape (20 , 5 ),
482
- chunks = (10 , 2 ), dtype = 'i2' , ** extra_kws )
483
- self .source = source
484
-
485
- def test_copy_array (self ):
486
- source = self .source
487
- dest = self .new_dest ()
481
+ yield prep_source (group ())
482
+
483
+ @pytest .fixture (params = [False , True ], ids = ['zarr' , 'hdf5' ])
484
+ def dest (self , request , tmpdir ):
485
+ if request .param :
486
+ h5py = pytest .importorskip ('h5py' )
487
+ fn = tmpdir .join ('dest.h5' )
488
+ with h5py .File (str (fn ), mode = 'w' ) as h5f :
489
+ yield h5f
490
+ else :
491
+ yield group ()
488
492
493
+ def test_copy_array (self , source , dest ):
489
494
# copy array with default options
490
495
copy (source ['foo/bar/baz' ], dest )
491
496
check_copied_array (source ['foo/bar/baz' ], dest ['baz' ])
492
497
copy (source ['spam' ], dest )
493
498
check_copied_array (source ['spam' ], dest ['spam' ])
494
499
495
- def test_copy_bad_dest (self ):
496
- source = self .source
497
-
500
+ def test_copy_bad_dest (self , source , dest ):
498
501
# try to copy to an array, dest must be a group
499
- dest = self . new_dest () .create_dataset ('eggs' , shape = (100 ,))
502
+ dest = dest .create_dataset ('eggs' , shape = (100 ,))
500
503
with pytest .raises (ValueError ):
501
504
copy (source ['foo/bar/baz' ], dest )
502
505
503
- def test_copy_array_name (self ):
504
- source = self .source
505
- dest = self .new_dest ()
506
-
506
+ def test_copy_array_name (self , source , dest ):
507
507
# copy array with name
508
508
copy (source ['foo/bar/baz' ], dest , name = 'qux' )
509
509
assert 'baz' not in dest
510
510
check_copied_array (source ['foo/bar/baz' ], dest ['qux' ])
511
511
512
- def test_copy_array_create_options (self ):
513
- source = self .source
514
- dest = self .new_dest ()
512
+ def test_copy_array_create_options (self , source , dest ):
513
+ dest_h5py = dest .__module__ .startswith ('h5py.' )
515
514
516
515
# copy array, provide creation options
517
516
compressor = Zlib (9 )
518
517
create_kws = dict (chunks = (10 ,))
519
- if self . dest_h5py :
518
+ if dest_h5py :
520
519
create_kws .update (compression = 'gzip' , compression_opts = 9 ,
521
520
shuffle = True , fletcher32 = True , fillvalue = 42 )
522
521
else :
@@ -526,10 +525,7 @@ def test_copy_array_create_options(self):
526
525
check_copied_array (source ['foo/bar/baz' ], dest ['baz' ],
527
526
without_attrs = True , expect_props = create_kws )
528
527
529
- def test_copy_array_exists_array (self ):
530
- source = self .source
531
- dest = self .new_dest ()
532
-
528
+ def test_copy_array_exists_array (self , source , dest ):
533
529
# copy array, dest array in the way
534
530
dest .create_dataset ('baz' , shape = (10 ,))
535
531
@@ -554,10 +550,7 @@ def test_copy_array_exists_array(self):
554
550
with pytest .raises (ValueError ):
555
551
copy (source ['foo/bar/baz' ], dest , if_exists = 'foobar' )
556
552
557
- def test_copy_array_exists_group (self ):
558
- source = self .source
559
- dest = self .new_dest ()
560
-
553
+ def test_copy_array_exists_group (self , source , dest ):
561
554
# copy array, dest group in the way
562
555
dest .create_group ('baz' )
563
556
@@ -577,13 +570,13 @@ def test_copy_array_exists_group(self):
577
570
copy (source ['foo/bar/baz' ], dest , if_exists = 'replace' )
578
571
check_copied_array (source ['foo/bar/baz' ], dest ['baz' ])
579
572
580
- def test_copy_array_skip_initialized (self ):
581
- source = self . source
582
- dest = self . new_dest ()
573
+ def test_copy_array_skip_initialized (self , source , dest ):
574
+ dest_h5py = dest . __module__ . startswith ( 'h5py.' )
575
+
583
576
dest .create_dataset ('baz' , shape = (100 ,), chunks = (10 ,), dtype = 'i8' )
584
577
assert not np .all (source ['foo/bar/baz' ][:] == dest ['baz' ][:])
585
578
586
- if self . dest_h5py :
579
+ if dest_h5py :
587
580
with pytest .raises (ValueError ):
588
581
# not available with copy to h5py
589
582
copy (source ['foo/bar/baz' ], dest , if_exists = 'skip_initialized' )
@@ -599,55 +592,37 @@ def test_copy_array_skip_initialized(self):
599
592
assert_array_equal (np .arange (100 , 200 ), dest ['baz' ][:])
600
593
assert not np .all (source ['foo/bar/baz' ][:] == dest ['baz' ][:])
601
594
602
- def test_copy_group (self ):
603
- source = self .source
604
- dest = self .new_dest ()
605
-
595
+ def test_copy_group (self , source , dest ):
606
596
# copy group, default options
607
597
copy (source ['foo' ], dest )
608
598
check_copied_group (source ['foo' ], dest ['foo' ])
609
599
610
- def test_copy_group_no_name (self ):
611
- source = self .source
612
- dest = self .new_dest ()
613
-
600
+ def test_copy_group_no_name (self , source , dest ):
614
601
with pytest .raises (TypeError ):
615
602
# need a name if copy root
616
603
copy (source , dest )
617
604
618
605
copy (source , dest , name = 'root' )
619
606
check_copied_group (source , dest ['root' ])
620
607
621
- def test_copy_group_options (self ):
622
- source = self .source
623
- dest = self .new_dest ()
624
-
608
+ def test_copy_group_options (self , source , dest ):
625
609
# copy group, non-default options
626
610
copy (source ['foo' ], dest , name = 'qux' , without_attrs = True )
627
611
assert 'foo' not in dest
628
612
check_copied_group (source ['foo' ], dest ['qux' ], without_attrs = True )
629
613
630
- def test_copy_group_shallow (self ):
631
- source = self .source
632
- dest = self .new_dest ()
633
-
614
+ def test_copy_group_shallow (self , source , dest ):
634
615
# copy group, shallow
635
616
copy (source , dest , name = 'eggs' , shallow = True )
636
617
check_copied_group (source , dest ['eggs' ], shallow = True )
637
618
638
- def test_copy_group_exists_group (self ):
639
- source = self .source
640
- dest = self .new_dest ()
641
-
619
+ def test_copy_group_exists_group (self , source , dest ):
642
620
# copy group, dest groups exist
643
621
dest .create_group ('foo/bar' )
644
622
copy (source ['foo' ], dest )
645
623
check_copied_group (source ['foo' ], dest ['foo' ])
646
624
647
- def test_copy_group_exists_array (self ):
648
- source = self .source
649
- dest = self .new_dest ()
650
-
625
+ def test_copy_group_exists_array (self , source , dest ):
651
626
# copy group, dest array in the way
652
627
dest .create_dataset ('foo/bar' , shape = (10 ,))
653
628
@@ -6
F438
67,10 +642,7 @@ def test_copy_group_exists_array(self):
667
642
copy (source ['foo' ], dest , if_exists = 'replace' )
668
643
check_copied_group (source ['foo' ], dest ['foo' ])
669
644
670
- def test_copy_group_dry_run (self ):
671
- source = self .source
672
- dest = self .new_dest ()
673
-
645
+ def test_copy_group_dry_run (self , source , dest ):
674
646
# dry run, empty destination
675
647
n_copied , n_skipped , n_bytes_copied = \
676
648
copy (source ['foo' ], dest , dry_run = True , return_stats = True )
@@ -710,67 +682,18 @@ def test_copy_group_dry_run(self):
710
682
assert 0 == n_bytes_copied
711
683
assert_array_equal (baz , dest ['foo/bar/baz' ])
712
684
713
- def test_logging (self ):
714
- source = self .source
715
- dest = self .new_dest ()
716
-
685
+ def test_logging (self , source , dest , tmpdir ):
717
686
# callable log
718
687
copy (source ['foo' ], dest , dry_run = True , log = print )
719
688
720
689
# file name
721
- fn = tempfile .mktemp ()
722
- atexit .register (os .remove , fn )
690
+ fn = str (tmpdir .join ('log_name' ))
723
691
copy (source ['foo' ], dest , dry_run = True , log = fn )
724
692
725
693
# file
726
- with tempfile . TemporaryFile (mode = 'w' ) as f :
694
+ with tmpdir . join ( 'log_file' ). open (mode = 'w' ) as f :
727
695
copy (source ['foo' ], dest , dry_run = True , log = f )
728
696
729
697
# bad option
730
698
with pytest .raises (TypeError ):
731
699
copy (source ['foo' ], dest , dry_run = True , log = True )
732
-
733
-
734
- try :
735
- import h5py
736
- except ImportError : # pragma: no cover
737
- h5py = None
738
-
739
-
740
- def temp_h5f ():
741
- h5py = pytest .importorskip ("h5py" )
742
- fn = tempfile .mktemp ()
743
- atexit .register (os .remove , fn )
744
- h5f = h5py .File (fn , mode = 'w' )
745
- atexit .register (lambda v : v .close (), h5f )
746
- return h5f
747
-
748
-
749
- class TestCopyHDF5ToZarr (TestCopy ):
750
-
751
- def __init__ (self , * args , ** kwargs ):
752
- super ().__init__ (* args , ** kwargs )
753
- self .source_h5py = True
754
- self .dest_h5py = False
755
- self .new_source = temp_h5f
756
- self .new_dest = group
757
-
758
-
759
- class TestCopyZarrToHDF5 (TestCopy ):
760
-
761
- def __init__ (self , * args , ** kwargs ):
762
- super ().__init__ (* args , ** kwargs )
763
- self .source_h5py = False
764
- self .dest_h5py = True
765
- self .new_source = group
766
- self .new_dest = temp_h5f
767
-
768
-
769
- class TestCopyHDF5ToHDF5 (TestCopy ):
770
-
771
- def __init__ (self , * args , ** kwargs ):
772
- super ().__init__ (* args , ** kwargs )
773
- self .source_h5py = True
774
- self .dest_h5py = True
775
- self .new_source = temp_h5f
776
- self .new_dest = temp_h5f
0 commit comments