Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
C
core
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
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
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to JiHu GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Chenhao Ma
core
Commits
c24241ce
Commit
c24241ce
authored
9 years ago
by
Nathaniel Kofalt
Browse files
Options
Downloads
Patches
Plain Diff
Add some tests for rules.py
parent
b3405989
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
api/rules.py
+2
-5
2 additions, 5 deletions
api/rules.py
bin/runtests.sh
+4
-1
4 additions, 1 deletion
bin/runtests.sh
test/unit_tests/test_rules.py
+171
-0
171 additions, 0 deletions
test/unit_tests/test_rules.py
with
177 additions
and
6 deletions
api/rules.py
+
2
−
5
View file @
c24241ce
...
...
@@ -50,16 +50,13 @@ HARDCODED_RULES = [
]
def
_log_file_key_error
(
file_
,
container
,
error
):
log
.
warning
(
'
file
'
+
file_
[
'
name
'
]
+
'
in container
'
+
str
(
container
[
'
_id
'
]
)
+
'
'
+
error
)
log
.
warning
(
'
file
'
+
file_
.
get
(
'
name
'
,
'
?
'
)
+
'
in container
'
+
str
(
container
.
get
(
'
_id
'
,
'
?
'
)
)
+
'
'
+
error
)
def
eval_match
(
match_type
,
match_param
,
file_
,
container
):
"""
Given a match entry, return if the match succeeded.
"""
if
not
match_type
in
MATCH_TYPES
:
raise
Exception
(
'
Unsupported match type
'
+
match_type
)
# Match the file's type
if
match_type
==
'
file.type
'
:
try
:
...
...
@@ -75,7 +72,7 @@ def eval_match(match_type, match_param, file_, container):
# Match any of the file's measurements
elif
match_type
==
'
file.measurements
'
:
try
:
return
match_param
in
file_
[
measurements
]
return
match_param
in
file_
[
'
measurements
'
]
except
KeyError
:
_log_file_key_error
(
file_
,
container
,
'
has no measurements key
'
)
return
False
...
...
This diff is collapsed.
Click to expand it.
bin/runtests.sh
+
4
−
1
View file @
c24241ce
...
...
@@ -7,7 +7,10 @@ cd "$( dirname "${BASH_SOURCE[0]}" )/.."
(
case
"
$1
-
$2
"
in
unit-|unit---ci
)
unit-
)
PYTHONPATH
=
.
py.test
$unit_test_path
;;
unit---ci
)
PYTHONPATH
=
.
py.test
--cov
=
api
--cov-report
=
term-missing
$unit_test_path
;;
unit---watch
)
...
...
This diff is collapsed.
Click to expand it.
test/unit_tests/test_rules.py
0 → 100644
+
171
−
0
View file @
c24241ce
import
pytest
from
api
import
rules
# Statefully holds onto some construction args and can return tuples to unroll for calling rules.eval_match.
# Might indicate a need for a match tuple in rules.py.
class
rulePart
:
# Hold onto some param state
def
__init__
(
self
,
match_type
=
None
,
match_param
=
None
,
file_
=
None
,
container
=
None
):
self
.
match_type
=
match_type
self
.
match_param
=
match_param
self
.
file_
=
file_
self
.
container
=
container
# Return params as tuple, optionally with some modifications
def
gen
(
self
,
match_type
=
None
,
match_param
=
None
,
file_
=
None
,
container
=
None
):
return
(
match_type
if
match_type
else
self
.
match_type
,
match_param
if
match_param
else
self
.
match_param
,
file_
if
file_
else
self
.
file_
,
container
if
container
else
self
.
container
,
)
# DISCUSS: this basically asserts that the log helper doesn't throw, which is of non-zero but questionable value.
# Could instead be marked for pytest et. al to ignore coverage? Desirability? Compatibility?
def
test_log_file_key_error
():
rules
.
_log_file_key_error
({
'
name
'
:
'
wat
'
},
{
'
_id
'
:
0
},
'
example
'
)
def
test_eval_match_file_type
():
part
=
rulePart
(
match_type
=
'
file.type
'
,
match_param
=
'
dicom
'
)
args
=
part
.
gen
(
file_
=
{
'
type
'
:
'
dicom
'
})
result
=
rules
.
eval_match
(
*
args
)
assert
result
==
True
args
=
part
.
gen
(
file_
=
{
'
type
'
:
'
nifti
'
})
result
=
rules
.
eval_match
(
*
args
)
assert
result
==
False
# Check match returns false without raising when not given a file.type
args
=
part
.
gen
(
file_
=
{
'
a
'
:
'
b
'
},
container
=
{
'
a
'
:
'
b
'
})
result
=
rules
.
eval_match
(
*
args
)
assert
result
==
False
def
test_eval_match_file_name_match_exact
():
part
=
rulePart
(
match_type
=
'
file.name
'
,
match_param
=
'
file.txt
'
)
args
=
part
.
gen
(
file_
=
{
'
name
'
:
'
file.txt
'
})
result
=
rules
.
eval_match
(
*
args
)
assert
result
==
True
args
=
part
.
gen
(
file_
=
{
'
name
'
:
'
file2.txt
'
})
result
=
rules
.
eval_match
(
*
args
)
assert
result
==
False
def
test_eval_match_file_name_match_relative
():
part
=
rulePart
(
match_type
=
'
file.name
'
,
match_param
=
'
*.txt
'
)
args
=
part
.
gen
(
file_
=
{
'
name
'
:
'
file.txt
'
})
result
=
rules
.
eval_match
(
*
args
)
assert
result
==
True
args
=
part
.
gen
(
file_
=
{
'
name
'
:
'
file.log
'
})
result
=
rules
.
eval_match
(
*
args
)
assert
result
==
False
def
test_eval_match_file_measurements
():
part
=
rulePart
(
match_type
=
'
file.measurements
'
,
file_
=
{
'
measurements
'
:
[
'
a
'
,
'
diffusion
'
,
'
b
'
]
})
args
=
part
.
gen
(
match_param
=
'
diffusion
'
)
result
=
rules
.
eval_match
(
*
args
)
assert
result
==
True
args
=
part
.
gen
(
match_param
=
'
c
'
)
result
=
rules
.
eval_match
(
*
args
)
assert
result
==
False
# Check match returns false without raising when not given a file.type
args
=
part
.
gen
(
file_
=
{
'
a
'
:
'
b
'
},
container
=
{
'
a
'
:
'
b
'
})
result
=
rules
.
eval_match
(
*
args
)
assert
result
==
False
def
test_eval_match_container_measurement
():
part
=
rulePart
(
match_type
=
'
container.measurement
'
,
container
=
{
'
measurement
'
:
'
diffusion
'
})
args
=
part
.
gen
(
match_param
=
'
diffusion
'
)
result
=
rules
.
eval_match
(
*
args
)
assert
result
==
True
args
=
part
.
gen
(
match_param
=
'
c
'
)
result
=
rules
.
eval_match
(
*
args
)
assert
result
==
False
def
test_eval_match_container_has_type
():
part
=
rulePart
(
match_type
=
'
container.has-type
'
,
container
=
{
'
files
'
:
[
{
'
measurements
'
:
[
'
a
'
,
'
diffusion
'
,
'
b
'
]},
{
'
measurements
'
:
[
'
c
'
,
'
other
'
,
'
b
'
]},
]})
args
=
part
.
gen
(
match_param
=
'
other
'
)
result
=
rules
.
eval_match
(
*
args
)
assert
result
==
True
args
=
part
.
gen
(
match_param
=
'
d
'
)
result
=
rules
.
eval_match
(
*
args
)
assert
result
==
False
def
test_eval_match_unknown_type
():
with
pytest
.
raises
(
Exception
):
rules
.
eval_match
(
'
does-not-exist
'
,
None
,
None
,
None
)
def
test_eval_rule_any
():
container
=
{
'
a
'
:
'
b
'
}
rule
=
{
"
any
"
:
[
[
"
file.type
"
,
"
dicom
"
],
[
"
file.name
"
,
"
*.dcm
"
],
],
"
all
"
:
[],
"
alg
"
:
"
dcm2nii
"
,
}
file_
=
{
'
name
'
:
'
hello.dcm
'
,
'
type
'
:
'
a
'
}
result
=
rules
.
eval_rule
(
rule
,
file_
,
container
)
assert
result
==
True
file_
=
{
'
name
'
:
'
hello.txt
'
,
'
type
'
:
'
dicom
'
}
result
=
rules
.
eval_rule
(
rule
,
file_
,
container
)
assert
result
==
True
file_
=
{
'
name
'
:
'
hello.dcm
'
,
'
type
'
:
'
dicom
'
}
result
=
rules
.
eval_rule
(
rule
,
file_
,
container
)
assert
result
==
True
file_
=
{
'
name
'
:
'
hello.txt
'
,
'
type
'
:
'
a
'
}
result
=
rules
.
eval_rule
(
rule
,
file_
,
container
)
assert
result
==
False
def
test_eval_rule_all
():
container
=
{
'
a
'
:
'
b
'
}
rule
=
{
"
any
"
:
[
],
"
all
"
:
[
[
"
file.type
"
,
"
dicom
"
],
[
"
file.name
"
,
"
*.dcm
"
],
],
"
alg
"
:
"
dcm2nii
"
,
}
file_
=
{
'
name
'
:
'
hello.dcm
'
,
'
type
'
:
'
a
'
}
result
=
rules
.
eval_rule
(
rule
,
file_
,
container
)
assert
result
==
False
file_
=
{
'
name
'
:
'
hello.txt
'
,
'
type
'
:
'
dicom
'
}
result
=
rules
.
eval_rule
(
rule
,
file_
,
container
)
assert
result
==
False
file_
=
{
'
name
'
:
'
hello.dcm
'
,
'
type
'
:
'
dicom
'
}
result
=
rules
.
eval_rule
(
rule
,
file_
,
container
)
assert
result
==
True
file_
=
{
'
name
'
:
'
hello.txt
'
,
'
type
'
:
'
a
'
}
result
=
rules
.
eval_rule
(
rule
,
file_
,
container
)
assert
result
==
False
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