def test_join_conflict(self): """Joining storages with same named arrays should raise an error or rename the arrays.""" even_store = FlattenedStorage(even=self.even, even_sum=self.even_sum) even2_store = FlattenedStorage(even=self.even, even_sum=self.even_sum) with self.assertRaises( ValueError, msg= "Joining should raise an error if storages share an array name" ): even_store.join(even2_store) for lsuffix, rsuffix in (("_left", ""), ("", "_suffix"), ("_left", "_right")): with self.subTest(lsuffix=lsuffix, rsuffix=rsuffix): join_store = even_store.copy().join(even2_store, lsuffix=lsuffix, rsuffix=rsuffix) self.assertTrue(join_store.has_array(f"even{lsuffix}"), "left array not present after join.") self.assertTrue(join_store.has_array(f"even{rsuffix}"), "right array not present after join.") self.assertTrue(join_store.has_array(f"even_sum{lsuffix}"), "left array not present after join.") self.assertTrue(join_store.has_array(f"even_sum{rsuffix}"), "right array not present after join.") self.assertTrue( np.array_equal(join_store[f"even{lsuffix}"], even_store["even"]), "right array not the same after join.") self.assertTrue( np.array_equal(join_store[f"even{rsuffix}"], even2_store["even"]), "left array not the same after join.")
def test_join(self): """All arrays should be present in joined storage.""" even_store = FlattenedStorage(even=self.even, even_sum=self.even_sum) odd_store = FlattenedStorage(odd=self.odd, odd_sum=self.odd_sum) both_store = even_store.copy().join(odd_store) self.assertTrue((both_store["even"] == even_store["even"]).all(), "Per element array 'even' not present after join!") self.assertTrue((both_store["odd"] == odd_store["odd"]).all(), "Per chunk array 'odd' not present after join!") self.assertTrue( (both_store["even_sum"] == even_store["even_sum"]).all(), "Per element array 'even_sum' not present after join!") self.assertTrue((both_store["odd_sum"] == odd_store["odd_sum"]).all(), "Per chunk array 'odd_sum' not present after join!")
def test_copy(self): """copy should give the same data and be a deep copy.""" store = FlattenedStorage(even=self.even, odd=self.odd, even_sum=self.even_sum, odd_sum=self.odd_sum) copy = store.copy() for k in "even", "odd", "even_sum", "odd_sum": with self.subTest(k=k): self.assertTrue((store[k] == copy[k]).all(), f"Array {k} not equal after copy!") even_before = copy["even"] even_sum_before = copy["even_sum"] store["even", 2] *= 2 store["even_sum", 2] *= 2 self.assertTrue((even_before == copy["even"]).all(), "Per element array changed in copy when original is!") self.assertTrue((even_sum_before == copy["even_sum"]).all(), "Per chunk array changed in copy when original is!")