Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
MLAir
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
esde
machine-learning
MLAir
Commits
2e4838f3
Commit
2e4838f3
authored
Mar 23, 2020
by
Felix Kleinert
Browse files
Options
Downloads
Patches
Plain Diff
update tests for helpers
parent
2019194a
Branches
Branches containing commit
Tags
Tags containing commit
3 merge requests
!90
WIP: new release update
,
!89
Resolve "release branch / CI on gpu"
,
!84
Felix issue080 testing test helpers
Pipeline
#32777
passed
Mar 23, 2020
Stage: test
Stage: pages
Stage: deploy
This commit is part of merge request
!89
. Comments created here will be created in the context of that merge request.
Changes
1
Pipelines
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
test/test_helpers.py
+172
-1
172 additions, 1 deletion
test/test_helpers.py
with
172 additions
and
1 deletion
test/test_helpers.py
+
172
−
1
Edit
View file @
2e4838f3
...
...
@@ -7,6 +7,8 @@ import mock
import
numpy
as
np
import
pytest
import
re
from
src.helpers
import
*
...
...
@@ -128,7 +130,8 @@ class TestTimeTracking:
class
TestPrepareHost
:
@mock.patch
(
"
socket.gethostname
"
,
side_effect
=
[
"
linux-aa9b
"
,
"
ZAM144
"
,
"
zam347
"
,
"
jrtest
"
,
"
jwtest
"
])
@mock.patch
(
"
socket.gethostname
"
,
side_effect
=
[
"
linux-aa9b
"
,
"
ZAM144
"
,
"
zam347
"
,
"
jrtest
"
,
"
jwtest
"
,
"
runner-6HmDp9Qd-project-2411-concurrent
"
])
@mock.patch
(
"
os.getlogin
"
,
return_value
=
"
testUser
"
)
@mock.patch
(
"
os.path.exists
"
,
return_value
=
True
)
def
test_prepare_host
(
self
,
mock_host
,
mock_user
,
mock_path
):
...
...
@@ -142,6 +145,8 @@ class TestPrepareHost:
assert
path
==
"
/p/project/cjjsc42/testUser/DATA/toar_daily/
"
path
=
prepare_host
()
assert
path
==
"
/p/home/jusers/testUser/juwels/intelliaq/DATA/toar_daily/
"
path
=
prepare_host
()
assert
path
==
'
/home/testUser/machinelearningtools/data/toar_daily/
'
@mock.patch
(
"
socket.gethostname
"
,
return_value
=
"
NotExistingHostName
"
)
@mock.patch
(
"
os.getlogin
"
,
return_value
=
"
zombie21
"
)
...
...
@@ -154,6 +159,35 @@ class TestPrepareHost:
with
pytest
.
raises
(
NotADirectoryError
)
as
e
:
prepare_host
()
assert
"
does not exist for host
'
linux-aa9b
'"
in
e
.
value
.
args
[
0
]
with
pytest
.
raises
(
NotADirectoryError
)
as
e
:
prepare_host
(
False
)
assert
"
does not exist for host
'
linux-aa9b
'"
in
e
.
value
.
args
[
0
]
@mock.patch
(
"
socket.gethostname
"
,
side_effect
=
[
"
linux-aa9b
"
,
"
ZAM144
"
,
"
zam347
"
,
"
jrtest
"
,
"
jwtest
"
,
"
runner-6HmDp9Qd-project-2411-concurrent
"
])
@mock.patch
(
"
os.getlogin
"
,
side_effect
=
OSError
)
@mock.patch
(
"
os.path.exists
"
,
return_value
=
True
)
def
test_os_error
(
self
,
mock_host
,
mock_user
,
mock_path
):
path
=
prepare_host
()
assert
path
==
"
/home/default/machinelearningtools/data/toar_daily/
"
path
=
prepare_host
()
assert
path
==
"
/home/default/Data/toar_daily/
"
path
=
prepare_host
()
assert
path
==
"
/home/default/Data/toar_daily/
"
path
=
prepare_host
()
assert
path
==
"
/p/project/cjjsc42/default/DATA/toar_daily/
"
path
=
prepare_host
()
assert
path
==
"
/p/home/jusers/default/juwels/intelliaq/DATA/toar_daily/
"
path
=
prepare_host
()
assert
path
==
'
/home/default/machinelearningtools/data/toar_daily/
'
@mock.patch
(
"
socket.gethostname
"
,
side_effect
=
[
"
linux-aa9b
"
])
@mock.patch
(
"
os.getlogin
"
,
return_value
=
"
testUser
"
)
@mock.patch
(
"
os.path.exists
"
,
return_value
=
False
)
@mock.patch
(
"
os.makedirs
"
,
side_effect
=
None
)
def
test_os_path_exists
(
self
,
mock_host
,
mock_user
,
mock_path
,
mock_check
):
path
=
prepare_host
()
assert
path
==
"
/home/testUser/machinelearningtools/data/toar_daily/
"
class
TestSetExperimentName
:
...
...
@@ -170,6 +204,23 @@ class TestSetExperimentName:
exp_name
,
_
=
set_experiment_name
(
experiment_date
=
"
2019-11-14
"
)
assert
exp_name
==
"
2019-11-14_network
"
def
test_set_expperiment_hourly
(
self
):
exp_name
,
exp_path
=
set_experiment_name
(
sampling
=
"
hourly
"
)
assert
exp_name
==
"
TestExperiment_hourly
"
assert
exp_path
==
os
.
path
.
abspath
(
os
.
path
.
join
(
os
.
path
.
dirname
(
__file__
),
"
..
"
,
"
TestExperiment_hourly
"
))
class
TestSetBootstrapPath
:
def
test_bootstrap_path_is_none
(
self
):
bootstrap_path
=
set_bootstrap_path
(
None
,
'
TestDataPath/
'
,
'
daily
'
)
assert
bootstrap_path
==
'
TestDataPath/../bootstrap_daily
'
@mock.patch
(
"
os.makedirs
"
,
side_effect
=
None
)
def
test_bootstap_path_is_given
(
self
,
mock_makedir
):
bootstrap_path
=
set_bootstrap_path
(
'
Test/path/to/boots
'
,
None
,
None
)
assert
bootstrap_path
==
'
Test/path/to/boots
'
class
TestPytestRegex
:
...
...
@@ -221,3 +272,123 @@ class TestFloatRound:
assert
float_round
(
-
34.9221
,
0
,
math
.
floor
)
==
-
35.
assert
float_round
(
-
34.9221
,
2
)
==
-
34.92
assert
float_round
(
-
34.9221
,
0
)
==
-
34.
class
TestDictPop
:
@pytest.fixture
def
custom_dict
(
self
):
return
{
'
a
'
:
1
,
'
b
'
:
2
,
2
:
'
ab
'
}
def
test_dict_pop_single
(
self
,
custom_dict
):
# one out as list
d_pop
=
dict_pop
(
custom_dict
,
[
4
])
assert
d_pop
==
custom_dict
# one out as str
d_pop
=
dict_pop
(
custom_dict
,
'
4
'
)
assert
d_pop
==
custom_dict
# one in as str
d_pop
=
dict_pop
(
custom_dict
,
'
b
'
)
assert
d_pop
==
{
'
a
'
:
1
,
2
:
'
ab
'
}
# one in as list
d_pop
=
dict_pop
(
custom_dict
,
[
'
b
'
])
assert
d_pop
==
{
'
a
'
:
1
,
2
:
'
ab
'
}
def
test_dict_pop_multiple
(
self
,
custom_dict
):
# all out (list)
d_pop
=
dict_pop
(
custom_dict
,
[
4
,
'
mykey
'
])
assert
d_pop
==
custom_dict
# all in (list)
d_pop
=
dict_pop
(
custom_dict
,
[
'
a
'
,
2
])
assert
d_pop
==
{
'
b
'
:
2
}
# one in one out (list)
d_pop
=
dict_pop
(
custom_dict
,
[
2
,
'
10
'
])
assert
d_pop
==
{
'
a
'
:
1
,
'
b
'
:
2
}
def
test_dict_pop_missing_argument
(
self
,
custom_dict
):
with
pytest
.
raises
(
TypeError
)
as
e
:
dict_pop
()
assert
"
dict_pop() missing 2 required positional arguments:
'
dict_orig
'
and
'
pop_keys
'"
in
e
.
value
.
args
[
0
]
with
pytest
.
raises
(
TypeError
)
as
e
:
dict_pop
(
custom_dict
)
assert
"
dict_pop() missing 1 required positional argument:
'
pop_keys
'"
in
e
.
value
.
args
[
0
]
class
TestListPop
:
@pytest.fixture
def
custom_list
(
self
):
return
[
1
,
2
,
3
,
'
a
'
,
'
bc
'
]
def
test_list_pop_single
(
self
,
custom_list
):
l_pop
=
list_pop
(
custom_list
,
1
)
assert
l_pop
==
[
2
,
3
,
'
a
'
,
'
bc
'
]
l_pop
=
list_pop
(
custom_list
,
'
bc
'
)
assert
l_pop
==
[
1
,
2
,
3
,
'
a
'
]
l_pop
=
list_pop
(
custom_list
,
5
)
assert
l_pop
==
custom_list
def
test_list_pop_multiple
(
self
,
custom_list
):
# all in list
l_pop
=
list_pop
(
custom_list
,
[
2
,
'
a
'
])
assert
l_pop
==
[
1
,
3
,
'
bc
'
]
# one in one out
l_pop
=
list_pop
(
custom_list
,
[
'
bc
'
,
10
])
assert
l_pop
==
[
1
,
2
,
3
,
'
a
'
]
# all out
l_pop
=
list_pop
(
custom_list
,
[
10
,
'
aa
'
])
assert
l_pop
==
custom_list
def
test_list_pop_missing_argument
(
self
,
custom_list
):
with
pytest
.
raises
(
TypeError
)
as
e
:
list_pop
()
assert
"
list_pop() missing 2 required positional arguments:
'
list_full
'
and
'
pop_items
'"
in
e
.
value
.
args
[
0
]
with
pytest
.
raises
(
TypeError
)
as
e
:
list_pop
(
custom_list
)
assert
"
list_pop() missing 1 required positional argument:
'
pop_items
'"
in
e
.
value
.
args
[
0
]
class
TestLogger
:
@pytest.fixture
def
logger
(
self
):
return
Logger
()
def
test_init_default
(
self
):
log
=
Logger
()
assert
log
.
formatter
==
"
%(asctime)s - %(levelname)s: %(message)s [%(filename)s:%(funcName)s:%(lineno)s]
"
assert
log
.
log_file
==
Logger
.
setup_logging_path
()
# assert PyTestRegex(
# ".*machinelearningtools/src/\.{2}/logging/logging_\d{4}-\d{2}-\d{2}_\d{2}-\d{2}-\d{2}\.log") == log.log_file
def
test_setup_logging_path_none
(
self
):
log_file
=
Logger
.
setup_logging_path
(
None
)
assert
PyTestRegex
(
"
.*machinelearningtools/src/\.{2}/logging/logging_\d{4}-\d{2}-\d{2}_\d{2}-\d{2}-\d{2}\.log
"
)
==
log_file
@mock.patch
(
"
os.makedirs
"
,
side_effect
=
None
)
def
test_setup_logging_path_given
(
self
,
mock_makedirs
):
path
=
"
my/test/path
"
log_path
=
Logger
.
setup_logging_path
(
path
)
assert
PyTestRegex
(
"
my/test/path/logging_\d{4}-\d{2}-\d{2}_\d{2}-\d{2}-\d{2}\.log
"
)
==
log_path
def
test_logger_console_level0
(
self
,
logger
):
consol
=
logger
.
logger_console
(
0
)
assert
isinstance
(
consol
,
logging
.
StreamHandler
)
assert
consol
.
level
==
0
formatter
=
logging
.
Formatter
(
logger
.
formatter
)
assert
isinstance
(
formatter
,
logging
.
Formatter
)
def
test_logger_console_level1
(
self
,
logger
):
consol
=
logger
.
logger_console
(
1
)
assert
isinstance
(
consol
,
logging
.
StreamHandler
)
assert
consol
.
level
==
1
formatter
=
logging
.
Formatter
(
logger
.
formatter
)
assert
isinstance
(
formatter
,
logging
.
Formatter
)
def
test_logger_console_level_wrong_type
(
self
,
logger
):
with
pytest
.
raises
(
TypeError
)
as
e
:
logger
.
logger_console
(
1.5
)
assert
"
Level not an integer or a valid string: 1.5
"
==
e
.
value
.
args
[
0
]
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
sign in
to comment