Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
CADET-RDM
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
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
IBG-1
ModSim
CADET
CADET-RDM
Commits
0815f8fe
Commit
0815f8fe
authored
1 year ago
by
Ronald Jäpel
Browse files
Options
Downloads
Patches
Plain Diff
add tests for current use-cases
parent
35e855db
No related branches found
No related tags found
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
.gitignore
+2
-1
2 additions, 1 deletion
.gitignore
modsimdata/utils.py
+2
-159
2 additions, 159 deletions
modsimdata/utils.py
tests/__init__.py
+0
-0
0 additions, 0 deletions
tests/__init__.py
tests/test_git_adapter.py
+139
-0
139 additions, 0 deletions
tests/test_git_adapter.py
with
143 additions
and
160 deletions
.gitignore
+
2
−
1
View file @
0815f8fe
...
@@ -2,4 +2,5 @@
...
@@ -2,4 +2,5 @@
build
build
*.egg-info
*.egg-info
results
results
__pycache__
__pycache__
\ No newline at end of file
tests/test_repo
\ No newline at end of file
This diff is collapsed.
Click to expand it.
modsimdata/utils.py
+
2
−
159
View file @
0815f8fe
...
@@ -272,10 +272,10 @@ class ProjectRepo(BaseRepo):
...
@@ -272,10 +272,10 @@ class ProjectRepo(BaseRepo):
previous_branch
=
self
.
output_repo
.
active_branch
.
name
previous_branch
=
self
.
output_repo
.
active_branch
.
name
self
.
output_repo
.
git
.
checkout
(
branch_name
)
self
.
output_repo
.
git
.
checkout
(
branch_name
)
source_filepath
=
os
.
path
.
join
(
self
.
_
output_
folde
r
,
file_path
)
source_filepath
=
os
.
path
.
join
(
self
.
output_
repo
.
working_di
r
,
file_path
)
# target_folder = os.path.join(self._output_folder + "_cached", branch_name)
# target_folder = os.path.join(self._output_folder + "_cached", branch_name)
target_folder
=
os
.
path
.
join
(
self
.
_
output_
folde
r
,
"
cached
"
,
branch_name
)
target_folder
=
os
.
path
.
join
(
self
.
output_
repo
.
working_di
r
,
"
cached
"
,
branch_name
)
os
.
makedirs
(
target_folder
,
exist_ok
=
True
)
os
.
makedirs
(
target_folder
,
exist_ok
=
True
)
target_filepath
=
os
.
path
.
join
(
target_folder
,
file_path
)
target_filepath
=
os
.
path
.
join
(
target_folder
,
file_path
)
...
@@ -414,7 +414,6 @@ def is_tool(name):
...
@@ -414,7 +414,6 @@ def is_tool(name):
"""
Check whether `name` is on PATH and marked as executable.
"""
"""
Check whether `name` is on PATH and marked as executable.
"""
from
shutil
import
which
from
shutil
import
which
return
which
(
name
)
is
not
None
return
which
(
name
)
is
not
None
...
@@ -471,159 +470,3 @@ def initialize_git_repo(path_to_repo: str, output_repo_name: (str | bool) = "out
...
@@ -471,159 +470,3 @@ def initialize_git_repo(path_to_repo: str, output_repo_name: (str | bool) = "out
repo
.
git
.
commit
(
"
-m
"
,
"
initial commit
"
)
repo
.
git
.
commit
(
"
-m
"
,
"
initial commit
"
)
os
.
chdir
(
starting_directory
)
os
.
chdir
(
starting_directory
)
return
def
example_generate_results_array
(
seed
=
None
):
import
numpy
as
np
if
seed
is
not
None
:
np
.
random
.
seed
(
seed
)
results_array
=
np
.
random
.
random
((
500
,
3
))
np
.
savetxt
(
os
.
path
.
join
(
"
output
"
,
"
result.csv
"
),
results_array
,
delimiter
=
"
,
"
)
return
results_array
def
example_generate_results_figures
(
input_array
):
import
matplotlib.pyplot
as
plt
import
numpy
as
np
plt
.
figure
()
plt
.
scatter
(
np
.
arange
(
0
,
500
),
input_array
[:,
0
],
alpha
=
0.5
)
plt
.
scatter
(
np
.
arange
(
0
,
500
),
input_array
[:,
1
],
alpha
=
0.5
)
plt
.
scatter
(
np
.
arange
(
0
,
500
),
input_array
[:,
2
],
alpha
=
0.5
)
plt
.
savefig
(
os
.
path
.
join
(
"
output
"
,
"
fig.png
"
))
plt
.
savefig
(
os
.
path
.
join
(
"
output
"
,
"
fig.jpg
"
),
dpi
=
1000
)
plt
.
savefig
(
os
.
path
.
join
(
"
output
"
,
f
"
fig_
{
np
.
random
.
randint
(
265
)
}
_
{
random
.
randint
(
0
,
1000
)
}
.png
"
))
def
alter_code
():
# Add changes to the project code
random_number
=
random
.
randint
(
0
,
265
)
# random_number = 42
with
open
(
"
random_number.txt
"
,
"
a
"
)
as
file
:
file
.
write
(
str
(
random_number
))
return
random_number
def
example_usage
():
"""
Pretend this is a python file
"""
home_dir
=
os
.
path
.
expanduser
(
"
~
"
)
os
.
chdir
(
os
.
path
.
join
(
home_dir
,
'
ModSimData
'
))
random_number
=
alter_code
()
project_repo
=
ProjectRepo
(
"
.
"
)
project_repo
.
commit
(
message
=
"
fixed super important bug
"
,
update_packages
=
False
)
with
project_repo
.
track_results
(
results_commit_message
=
"
Add figures and array
"
):
# Generate data
print
(
"
Generating results output
"
)
results_array
=
example_generate_results_array
(
seed
=
random_number
)
example_generate_results_figures
(
results_array
)
def
example_write_array
():
"""
Pretend this is a python file
"""
home_dir
=
os
.
path
.
expanduser
(
"
~
"
)
os
.
chdir
(
os
.
path
.
join
(
home_dir
,
'
ModSimData
'
))
# Add changes to the project code
random_number
=
random
.
randint
(
0
,
265
)
# random_number = 42
with
open
(
f
"
random_number_
{
random_number
}
.txt
"
,
"
a
"
)
as
file
:
file
.
write
(
str
(
random_number
))
project_repo
=
ProjectRepo
(
"
.
"
)
project_repo
.
commit
(
"
add code that writes an array to file
"
,
update_packages
=
False
)
with
project_repo
.
track_results
(
results_commit_message
=
"
Add array
"
):
example_generate_results_array
()
branch_name
=
str
(
project_repo
.
output_repo
.
active_branch
)
return
branch_name
def
example_load
(
branch_name
):
"""
Pretend this is a python file
"""
import
numpy
as
np
"""
move into home directory
"""
home_dir
=
os
.
path
.
expanduser
(
"
~
"
)
os
.
chdir
(
os
.
path
.
join
(
home_dir
,
'
ModSimData
'
))
# Add changes to the project code
random_number
=
random
.
randint
(
0
,
265
)
# random_number = 42
with
open
(
f
"
random_number_
{
random_number
}
.txt
"
,
"
a
"
)
as
file
:
file
.
write
(
str
(
random_number
))
project_repo
=
ProjectRepo
(
"
.
"
)
project_repo
.
commit
(
"
add code that creates figures based on an array
"
,
update_packages
=
False
)
with
project_repo
.
track_results
(
results_commit_message
=
"
Add figures
"
):
cached_array_path
=
project_repo
.
cache_previous_results
(
branch_name
=
branch_name
,
file_path
=
"
result.csv
"
)
previous_array
=
np
.
loadtxt
(
cached_array_path
,
delimiter
=
"
,
"
)
# with project_repo.load_previous_result_file(branch_name=branch_name,
# file_path="result.csv") as file_handle:
# pass
example_generate_results_figures
(
previous_array
)
branch_name
=
str
(
project_repo
.
output_repo
.
active_branch
)
return
branch_name
def
example_load_large
(
branch_name1
,
branch_name2
):
"""
Pretend this is a python file
"""
import
numpy
as
np
"""
move into home directory
"""
home_dir
=
os
.
path
.
expanduser
(
"
~
"
)
os
.
chdir
(
os
.
path
.
join
(
home_dir
,
'
ModSimData
'
))
# Add changes to the project code
random_number
=
random
.
randint
(
0
,
265
)
# random_number = 42
with
open
(
f
"
random_number_
{
random_number
}
.txt
"
,
"
a
"
)
as
file
:
file
.
write
(
str
(
random_number
))
project_repo
=
ProjectRepo
(
"
.
"
)
project_repo
.
commit
(
"
add code that creates figures based on an array
"
,
update_packages
=
False
)
with
project_repo
.
track_results
(
results_commit_message
=
"
Add figures
"
):
# cached_fig_path = project_repo.cache_previous_results(branch_name=branch_name2,
# file_path="fig.jpg")
cached_array_path
=
project_repo
.
cache_previous_results
(
branch_name
=
branch_name1
,
file_path
=
"
result.csv
"
)
previous_array
=
np
.
loadtxt
(
cached_array_path
,
delimiter
=
"
,
"
)
example_generate_results_figures
(
previous_array
)
branch_name
=
str
(
project_repo
.
output_repo
.
active_branch
)
return
branch_name
def
example_two_step_process
():
branch_name
=
example_write_array
()
branch_name2
=
example_load
(
branch_name
)
example_load_large
(
branch_name
,
branch_name2
)
def
create_example_repo
():
os
.
chdir
(
os
.
path
.
expanduser
(
"
~
"
))
"""
initialize Project directory
"""
if
not
os
.
path
.
exists
(
"
ModSimData
"
):
initialize_git_repo
(
"
ModSimData
"
)
if
__name__
==
'
__main__
'
:
# pass
create_example_repo
()
example_usage
()
example_two_step_process
()
This diff is collapsed.
Click to expand it.
tests/__init__.py
0 → 100644
+
0
−
0
View file @
0815f8fe
This diff is collapsed.
Click to expand it.
tests/test_git_adapter.py
0 → 100644
+
139
−
0
View file @
0815f8fe
import
os.path
import
shutil
import
stat
import
random
import
pytest
import
git
import
numpy
as
np
from
modsimdata
import
initialize_git_repo
,
ProjectRepo
@pytest.fixture
(
scope
=
"
module
"
)
def
path_to_repo
():
# a "fixture" serves up shared, ready variables to test functions that should use the fixture as a kwarg
return
"
test_repo
"
# @pytest.fixture(scope="module", autouse=True)
# def my_fixture(path_to_repo):
# print('INITIALIZATION')
# if os.path.exists(path_to_repo):
# remove_dir(path_to_repo)
# yield "this is just here because something must yield"
# print("TEAR DOWN")
# remove_dir(path_to_repo)
def
remove_dir
(
path_to_dir
):
def
remove_readonly
(
func
,
path
,
exc_info
):
"
Clear the readonly bit and reattempt the removal
"
# ERROR_ACCESS_DENIED = 5
if
func
not
in
(
os
.
unlink
,
os
.
rmdir
)
or
exc_info
[
1
].
winerror
!=
5
:
raise
exc_info
[
1
]
os
.
chmod
(
path
,
stat
.
S_IWRITE
)
func
(
path
)
shutil
.
rmtree
(
path_to_dir
,
onerror
=
remove_readonly
)
def
modify_code
(
path_to_repo
):
# Add changes to the project code
random_number
=
random
.
randint
(
0
,
265
)
filepath
=
os
.
path
.
join
(
path_to_repo
,
f
"
print_random_number.py
"
)
with
open
(
filepath
,
"
w
"
)
as
file
:
file
.
write
(
f
"
print(
{
random_number
}
)
\n
"
)
def
count_commit_number
(
repo
):
commit_log
=
repo
.
git
.
log
(
"
--oneline
"
).
split
(
"
\n
"
)
current_commit_number
=
len
(
commit_log
)
return
current_commit_number
def
example_generate_results_array
(
path_to_repo
,
output_folder
):
results_array
=
np
.
random
.
random
((
500
,
3
))
np
.
savetxt
(
os
.
path
.
join
(
path_to_repo
,
output_folder
,
"
result.csv
"
),
results_array
,
delimiter
=
"
,
"
)
return
results_array
def
try_initialize_git_repo
(
path_to_repo
):
def
try_init_gitpython_repo
(
repo_path
):
os
.
path
.
exists
(
repo_path
)
git
.
Repo
(
repo_path
)
return
True
if
os
.
path
.
exists
(
path_to_repo
):
remove_dir
(
path_to_repo
)
initialize_git_repo
(
path_to_repo
=
path_to_repo
)
assert
try_init_gitpython_repo
(
path_to_repo
)
assert
try_init_gitpython_repo
(
os
.
path
.
join
(
path_to_repo
,
"
output
"
))
def
try_commit_code
(
path_to_repo
):
repo
=
ProjectRepo
(
path_to_repo
)
current_commit_number
=
count_commit_number
(
repo
)
modify_code
(
path_to_repo
)
repo
.
commit
(
"
add code to print random number
"
,
update_packages
=
False
)
updated_commit_number
=
count_commit_number
(
repo
)
assert
current_commit_number
+
1
==
updated_commit_number
def
try_commit_code_without_code_changes
(
path_to_repo
):
repo
=
ProjectRepo
(
path_to_repo
)
current_commit_number
=
count_commit_number
(
repo
)
repo
.
commit
(
"
This commit will not be made
"
)
updated_commit_number
=
count_commit_number
(
repo
)
assert
current_commit_number
==
updated_commit_number
def
try_commit_results_data
(
path_to_repo
):
repo
=
ProjectRepo
(
path_to_repo
)
current_commit_number
=
count_commit_number
(
repo
.
output_repo
)
with
repo
.
track_results
(
results_commit_message
=
"
Add array
"
):
example_generate_results_array
(
path_to_repo
,
output_folder
=
repo
.
_output_folder
)
updated_commit_number
=
count_commit_number
(
repo
.
output_repo
)
assert
current_commit_number
+
1
==
updated_commit_number
return
str
(
repo
.
output_repo
.
active_branch
)
def
try_commit_results_with_uncommitted_code_changes
(
path_to_repo
):
repo
=
ProjectRepo
(
path_to_repo
)
modify_code
(
path_to_repo
)
with
pytest
.
raises
(
Exception
):
with
repo
.
track_results
(
results_commit_message
=
"
Add array
"
):
example_generate_results_array
(
path_to_repo
,
output_folder
=
repo
.
_output_folder
)
repo
.
commit
(
"
add code to print random number
"
,
update_packages
=
False
)
def
try_load_previous_result
(
path_to_repo
,
branch_name
):
repo
=
ProjectRepo
(
path_to_repo
)
with
repo
.
track_results
(
results_commit_message
=
"
Load array and extend
"
):
cached_array_path
=
repo
.
cache_previous_results
(
branch_name
=
branch_name
,
file_path
=
"
result.csv
"
)
previous_array
=
np
.
loadtxt
(
cached_array_path
,
delimiter
=
"
,
"
)
extended_array
=
np
.
concatenate
([
previous_array
,
previous_array
])
extended_array_file_path
=
os
.
path
.
join
(
path_to_repo
,
repo
.
_output_folder
,
"
extended_result.csv
"
)
np
.
savetxt
(
extended_array_file_path
,
extended_array
,
delimiter
=
"
,
"
)
assert
os
.
path
.
exists
(
cached_array_path
)
assert
os
.
path
.
exists
(
extended_array_file_path
)
def
test_cadet_rdm
(
path_to_repo
):
# because these depend on one-another and there is no native support afaik for sequential tests
# these tests are called sequentially here as try_ functions.
try_initialize_git_repo
(
path_to_repo
)
try_commit_code
(
path_to_repo
)
try_commit_code_without_code_changes
(
path_to_repo
)
results_branch_name
=
try_commit_results_data
(
path_to_repo
)
try_commit_results_with_uncommitted_code_changes
(
path_to_repo
)
try_load_previous_result
(
path_to_repo
,
results_branch_name
)
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
register
or
sign in
to comment