tests.test_catch.test_catch_all_1()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 30
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 30
rs 9.376
c 0
b 0
f 0
cc 1
nop 0
1
"""Test module for exception catching functionality."""
2
3
import math
4
5
import numpy as np
6
7
from hyperactive import Hyperactive
8
9
search_space = {
10
    "x1": list(np.arange(-100, 100, 1)),
11
}
12
13
14
def test_catch_1():
15
    """Test catching TypeError exceptions in objective function."""
16
17
    def objective_function(access):
18
        1 + "str"  # Intentional TypeError for testing
19
20
        return 0
21
22
    hyper = Hyperactive()
23
    hyper.add_search(
24
        objective_function,
25
        search_space,
26
        n_iter=100,
27
        catch={TypeError: np.nan},
28
    )
29
    hyper.run()
30
31
32
def test_catch_2():
33
    """Test catching ValueError exceptions in objective function."""
34
35
    def objective_function(access):
36
        math.sqrt(-10)
37
38
        return 0
39
40
    hyper = Hyperactive()
41
    hyper.add_search(
42
        objective_function,
43
        search_space,
44
        n_iter=100,
45
        catch={ValueError: np.nan},
46
    )
47
    hyper.run()
48
49
50
def test_catch_3():
51
    """Test catching ZeroDivisionError exceptions in objective function."""
52
53
    def objective_function(access):
54
        1 / 0  # Intentional ZeroDivisionError for testing
55
56
        return 0
57
58
    hyper = Hyperactive()
59
    hyper.add_search(
60
        objective_function,
61
        search_space,
62
        n_iter=100,
63
        catch={ZeroDivisionError: np.nan},
64
    )
65
    hyper.run()
66
67
68
def test_catch_all_0():
69
    """Test catching multiple exception types returning NaN values."""
70
71
    def objective_function(access):
72
        1 + "str"  # Intentional TypeError for testing
73
        math.sqrt(-10)  # Intentional ValueError for testing
74
        1 / 0  # Intentional ZeroDivisionError for testing
75
76
        return 0
77
78
    hyper = Hyperactive()
79
    hyper.add_search(
80
        objective_function,
81
        search_space,
82
        n_iter=100,
83
        catch={
84
            TypeError: np.nan,
85
            ValueError: np.nan,
86
            ZeroDivisionError: np.nan,
87
        },
88
    )
89
    hyper.run()
90
91
    nan_ = hyper.search_data(objective_function)["score"].values[0]
92
93
    assert math.isnan(nan_)
94
95
96
def test_catch_all_1():
97
    """Test catching multiple exception types returning tuple values."""
98
99
    def objective_function(access):
100
        1 + "str"  # Intentional TypeError for testing
101
        math.sqrt(-10)  # Intentional ValueError for testing
102
        1 / 0  # Intentional ZeroDivisionError for testing
103
104
        return 0, {"error": False}
105
106
    catch_return = (np.nan, {"error": True})
107
108
    hyper = Hyperactive()
109
    hyper.add_search(
110
        objective_function,
111
        search_space,
112
        n_iter=100,
113
        catch={
114
            TypeError: catch_return,
115
            ValueError: catch_return,
116
            ZeroDivisionError: catch_return,
117
        },
118
    )
119
    hyper.run()
120
121
    nan_ = hyper.search_data(objective_function)["score"].values[0]
122
    error_ = hyper.search_data(objective_function)["error"].values[0]
123
124
    assert math.isnan(nan_)
125
    assert error_
126