Passed
Push — master ( 6d9b4e...95402e )
by Simon
01:40
created

tests.test_catch.test_catch_0()   A

Complexity

Conditions 1

Size

Total Lines 14
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 11
nop 0
dl 0
loc 14
rs 9.85
c 0
b 0
f 0
1
import copy
2
import pytest
3
import math
4
import numpy as np
5
import pandas as pd
6
7
from hyperactive import Hyperactive
8
9
10
search_space = {
11
    "x1": list(np.arange(-100, 100, 1)),
12
}
13
14
15
def test_catch_1():
16
    def objective_function(access):
17
        a = 1 + "str"
18
19
        return 0
20
21
    hyper = Hyperactive()
22
    hyper.add_search(
23
        objective_function,
24
        search_space,
25
        n_iter=100,
26
        catch={TypeError: np.nan},
27
    )
28
    hyper.run()
29
30
31
def test_catch_2():
32
    def objective_function(access):
33
        math.sqrt(-10)
34
35
        return 0
36
37
    hyper = Hyperactive()
38
    hyper.add_search(
39
        objective_function,
40
        search_space,
41
        n_iter=100,
42
        catch={ValueError: np.nan},
43
    )
44
    hyper.run()
45
46
47
def test_catch_3():
48
    def objective_function(access):
49
        x = 1 / 0
50
51
        return 0
52
53
    hyper = Hyperactive()
54
    hyper.add_search(
55
        objective_function,
56
        search_space,
57
        n_iter=100,
58
        catch={ZeroDivisionError: np.nan},
59
    )
60
    hyper.run()
61
62
63
def test_catch_all_0():
64
    def objective_function(access):
65
        x = y
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable y does not seem to be defined.
Loading history...
66
        a = 1 + "str"
67
        math.sqrt(-10)
68
        x = 1 / 0
69
70
        return 0
71
72
    hyper = Hyperactive()
73
    hyper.add_search(
74
        objective_function,
75
        search_space,
76
        n_iter=100,
77
        catch={
78
            NameError: np.nan,
79
            TypeError: np.nan,
80
            ValueError: np.nan,
81
            ZeroDivisionError: np.nan,
82
        },
83
    )
84
    hyper.run()
85
86
    nan_ = hyper.search_data(objective_function)["score"].values[0]
87
88
    assert math.isnan(nan_)
89
90
91
def test_catch_all_1():
92
    def objective_function(access):
93
        x = y
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable y does not seem to be defined.
Loading history...
94
        a = 1 + "str"
95
        math.sqrt(-10)
96
        x = 1 / 0
97
98
        return 0, {"error": False}
99
100
    catch_return = (np.nan, {"error": True})
101
102
    hyper = Hyperactive()
103
    hyper.add_search(
104
        objective_function,
105
        search_space,
106
        n_iter=100,
107
        catch={
108
            NameError: catch_return,
109
            TypeError: catch_return,
110
            ValueError: catch_return,
111
            ZeroDivisionError: catch_return,
112
        },
113
    )
114
    hyper.run()
115
116
    nan_ = hyper.search_data(objective_function)["score"].values[0]
117
    error_ = hyper.search_data(objective_function)["error"].values[0]
118
119
    assert math.isnan(nan_)
120
    assert error_ == True
121