Skip to content
Snippets Groups Projects
Commit e6761063 authored by Jean-Baptiste Bayle's avatar Jean-Baptiste Bayle
Browse files

Create test_containers.py

parent 9fdc5db2
No related branches found
No related tags found
1 merge request!16Fix `left_mosa` and `right_mosa`
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# pylint: disable=missing-module-docstring
import os
import numpy as np
from pytest import raises
from h5py import File
from lisainstrument.containers import ForEachObject, ForEachSC, ForEachMOSA
class ForEachAB(ForEachObject):
"""Concrete implementation of ForEachObject for tests."""
@classmethod
def indices(cls):
return ['A', 'B']
def test_init_dictionary():
"""Test that one can initialize a ForEachObject with a dictionary."""
my_object = ForEachAB({'A': 1, 'B': 2})
assert my_object['A'] == 1
assert my_object['B'] == 2
with raises(KeyError):
ForEachAB({'A': 1})
def test_init_callable():
"""Test that one can initialize a ForEachObject with a function."""
my_object = ForEachAB(lambda index: index.lower())
assert my_object['A'] == 'a'
assert my_object['B'] == 'b'
def test_init_dataset():
"""Test that one can initialize a ForEachObject with an HDF5 dataset."""
try:
with File('test-temp.h5', 'x') as h5file:
dtype = np.dtype({'names': ['A', 'B'], 'formats': [np.float64, np.float64]})
h5file.create_dataset('test-dataset', (1, ), dtype=dtype)
h5file['test-dataset']['A'] = 1
h5file['test-dataset']['B'] = 2
with File('test-temp.h5', 'r') as h5file:
my_object = ForEachAB(h5file['test-dataset'])
assert my_object['A'] == [1]
assert my_object['B'] == [2]
finally:
os.remove('test-temp.h5')
try:
with File('test-temp.h5', 'x') as h5file:
dtype = np.dtype({'names': ['A'], 'formats': [np.float64]})
h5file.create_dataset('test-dataset', (1, ), dtype=dtype)
h5file['test-dataset']['A'] = 1
with File('test-temp.h5', 'r') as h5file:
with raises(ValueError):
my_object = ForEachAB(h5file['test-dataset'])
finally:
os.remove('test-temp.h5')
def test_init_scalar():
"""Test that one can initialize a ForEachObject with a scalar."""
my_object = ForEachAB(42)
assert my_object['A'] == 42
assert my_object['B'] == 42
def test_access_item():
"""Test that one can get and set items."""
my_object = ForEachAB(0)
my_object['A'] = 1
assert my_object['A'] == 1
assert my_object['B'] == 0
my_object['B'] = 42
assert my_object['A'] == 1
assert my_object['B'] == 42
def test_len():
"""Test that one can retreive the length of values."""
my_object = ForEachAB(0)
assert len(my_object) == 1
my_object = ForEachAB({
'A': range(10),
'B': range(42),
})
assert len(my_object) == 42
my_object = ForEachAB({
'A': range(42),
'B': 1,
})
assert len(my_object) == 42
def test_equality():
"""Check that ForEachObject instances are equal when containing identical values."""
object_1 = ForEachAB({'A': 1, 'B': 2})
object_2 = ForEachAB({'A': 1, 'B': 2})
assert object_1 == object_2
object_2['A'] = 3
assert object_1 != object_2
object_1['A'] = 3
assert object_1 == object_2
def test_transformed():
"""Check that we transformation is correctly applied to ForEachObject instances."""
my_object = ForEachAB({'A': 1, 'B': 2})
my_object = my_object.transformed(lambda index, x: 2 * x)
assert my_object['A'] == 2
assert my_object['B'] == 4
my_object = my_object.transformed(lambda index, x: index.lower())
assert my_object['A'] == 'a'
assert my_object['B'] == 'b'
def test_write():
"""Check that we can write a ForEachObject instance."""
my_object = ForEachAB({'A': 1, 'B': 2})
try:
with File('test-temp.h5', 'x') as h5file:
my_object.write(h5file, 'test_dataset')
with File('test-temp.h5', 'r') as h5file:
assert h5file['test_dataset']['A'] == [1]
assert h5file['test_dataset']['B'] == [2]
finally:
os.remove('test-temp.h5')
def test_sc_indices():
"""Test spacecraft indices."""
assert ForEachSC.indices() == ['1', '2', '3']
assert ForEachSC.distant_left('1') == '2'
assert ForEachSC.distant_left('2') == '3'
assert ForEachSC.distant_left('3') == '1'
assert ForEachSC.distant_right('1') == '3'
assert ForEachSC.distant_right('2') == '1'
assert ForEachSC.distant_right('3') == '2'
assert ForEachSC.left_mosa('1') == '12'
assert ForEachSC.left_mosa('2') == '23'
assert ForEachSC.left_mosa('3') == '31'
assert ForEachSC.right_mosa('1') == '13'
assert ForEachSC.right_mosa('2') == '21'
assert ForEachSC.right_mosa('3') == '32'
def test_mosa_indices():
"""Test MOSA indices."""
assert ForEachMOSA.indices() == ['12', '23', '31', '13', '32', '21']
assert ForEachMOSA.sc('12') == '1'
assert ForEachMOSA.sc('23') == '2'
assert ForEachMOSA.sc('31') == '3'
assert ForEachMOSA.sc('13') == '1'
assert ForEachMOSA.sc('32') == '3'
assert ForEachMOSA.sc('21') == '2'
assert ForEachMOSA.distant('12') == '21'
assert ForEachMOSA.distant('23') == '32'
assert ForEachMOSA.distant('31') == '13'
assert ForEachMOSA.distant('13') == '31'
assert ForEachMOSA.distant('32') == '23'
assert ForEachMOSA.distant('21') == '12'
assert ForEachMOSA.adjacent('12') == '13'
assert ForEachMOSA.adjacent('23') == '21'
assert ForEachMOSA.adjacent('31') == '32'
assert ForEachMOSA.adjacent('13') == '12'
assert ForEachMOSA.adjacent('32') == '31'
assert ForEachMOSA.adjacent('21') == '23'
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment