Coverage for tests/test_model_objects.py: 100%

1 from attrs.exceptions import FrozenInstanceError

2 from hypothesis import given, strategies as st

3 import numpy as np

4 import pytest

5

6 from flexfrac1d.lib.constants import PI_2

7 import flexfrac1d.model.model as md

8

9 from .conftest import physical_strategies

10

11

12 class TestWave:

13 @staticmethod

14 @given(

15 amplitude=physical_strategies["wave"]["amplitude"],

16 period=physical_strategies["wave"]["period"],

17 phase=(physical_strategies["wave"]["phase"]),

18 )

19 def test_basic(amplitude, period, phase):

20 wave_default, wave_phase = md.Wave(amplitude, period), md.Wave(

21 amplitude, period, phase

22 )

23 for wave in wave_default, wave_phase:

24 assert wave.amplitude == amplitude

25 assert wave.period == period

26 assert wave_default.phase == 0

27 assert wave_phase.phase == phase

28

29 @staticmethod

30 @given(

31 amplitude=physical_strategies["wave"]["amplitude"],

32 frequency=physical_strategies["wave"]["frequency"],

33 phase=(physical_strategies["wave"]["phase"]),

34 )

35 def test_frequency(amplitude, frequency, phase):

36 wave_default, wave_phase = md.Wave.from_frequency(

37 amplitude, frequency

38 ), md.Wave.from_frequency(amplitude, frequency, phase)

39 for wave in wave_default, wave_phase:

40 assert wave.amplitude == amplitude

41 assert np.isclose(wave.period - 1 / frequency, 0)

42 assert wave_default.phase == 0

43 assert wave_phase.phase == phase

44

45 @staticmethod

46 @given(

47 amplitude=physical_strategies["wave"]["amplitude"],

48 period=physical_strategies["wave"]["period"],

49 )

50 def test_cached_properties(amplitude, period):

51 wave = md.Wave(amplitude, period=period)

52 assert np.isclose(wave.frequency - 1 / period, 0)

53 assert np.isclose(wave.period - PI_2 / wave.angular_frequency, 0)

54 assert wave._angular_frequency_pow2 == wave.angular_frequency**2

55

56 @staticmethod

57 @given(

58 amplitude=physical_strategies["wave"]["amplitude"],

59 period=physical_strategies["wave"]["period"],

60 )

61 def test_immutable(amplitude, period):

62 wave = md.Wave(amplitude, period=period)

63 with pytest.raises(FrozenInstanceError):

64 wave.amplitude = 1

65 with pytest.raises(FrozenInstanceError):

66 wave.period = 1

67 with pytest.raises(FrozenInstanceError):

68 wave.phase = 1

69

70 @staticmethod

71 @given(

72 amplitude=physical_strategies["wave"]["amplitude"],

73 period=physical_strategies["wave"]["period"],

74 alt_phase=st.floats(

75 min_value=-100, max_value=100, allow_nan=False, allow_infinity=False

76 ),

77 )

78 def test_bounded_phase(amplitude, period, alt_phase):

79 wave = md.Wave(amplitude, period=period, phase=alt_phase)

80 assert np.allclose(wave.phase - alt_phase % PI_2, 0)

81

82

83 class TestSubdomain:

84 @staticmethod

85 def test_ordering():

86 left_edge, length = 0, 100

87 sd = md._Subdomain(left_edge, length)

88

89 left_edge_further_right = left_edge + length + 1

90 assert left_edge_further_right > sd

91 sd_right = md._Subdomain(left_edge_further_right, length)

92 assert sd_right > sd

93

94 left_edge_further_left = -200

95 assert left_edge_further_left < sd

96 sd_left = md._Subdomain(left_edge_further_left, length)

97 assert sd_left < sd

98

99 assert sd == left_edge

100 sd_same = md._Subdomain(left_edge, length)

101 assert sd_same == sd

102 sd_same_but_different_length = md._Subdomain(left_edge, 2 * length)

103 assert sd_same_but_different_length == sd

104

105 with pytest.raises(TypeError):

106 sd > [] # noqa: B015

107

108 @staticmethod

109 @pytest.mark.parametrize("left_edge", (-100, 0, 0.8, 45, 34.2))

110 @pytest.mark.parametrize("length", (10, 13.8, 101.9))

111 def test_right_edge(left_edge, length):

112 sd = md._Subdomain(left_edge, length)

113 assert sd.right_edge == left_edge + length