Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
compendium-v2
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
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
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
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
geant-swd
compendium-v2
Commits
83232b85
Commit
83232b85
authored
4 months ago
by
Bjarke Madsen
Browse files
Options
Downloads
Patches
Plain Diff
Add test for data-download API
parent
45c3c321
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
test/test_data_download.py
+69
-0
69 additions, 0 deletions
test/test_data_download.py
with
69 additions
and
0 deletions
test/test_data_download.py
0 → 100644
+
69
−
0
View file @
83232b85
import
json
import
pathlib
from
sqlalchemy
import
select
from
compendium_v2.routes.api
import
models
from
compendium_v2.db
import
db
from
compendium_v2.db.presentation_models
import
NREN
,
NRENService
,
Service
TEST_DATA_DIR
=
pathlib
.
Path
(
__file__
).
parent
/
'
data
'
/
'
presentation_models_data
'
def
_get_data
(
model
):
filename
=
TEST_DATA_DIR
/
f
'
{
model
.
__name__
}
.json
'
if
not
filename
.
exists
():
raise
FileNotFoundError
(
f
'
Data file for
{
model
.
__name__
}
not found at
{
filename
}
. Run test_data_dump.py to generate it.
'
)
with
open
(
filename
,
'
r
'
)
as
f
:
return
json
.
load
(
f
)
def
setup
(
model
,
nrens
):
if
model
==
NREN
:
return
if
model
==
NRENService
:
services
=
{
s
.
name_key
:
s
for
s
in
db
.
session
.
scalars
(
select
(
Service
)).
all
()}
def
_create_instance
(
model
,
data
):
nren
=
nrens
[
data
[
'
nren_id
'
]]
if
model
==
NRENService
:
service
=
services
[
data
[
'
service_key
'
]]
return
model
(
nren
=
nren
,
service
=
service
,
**
data
)
return
model
(
nren
=
nren
,
**
data
)
data
=
_get_data
(
model
)
instances
=
list
(
filter
(
lambda
i
:
i
is
not
None
,
[
_create_instance
(
model
,
instance
)
for
instance
in
data
]))
db
.
session
.
add_all
(
instances
)
db
.
session
.
commit
()
def
test_data_download
(
app
,
client
,
mocked_admin_user
,
nren_services
):
nren_services
(
app
)
with
app
.
app_context
():
# set up the presentation model data for NREN first, as the instances are needed for other models
nren_data
=
_get_data
(
NREN
)
nrens
=
{
nren
[
'
id
'
]:
NREN
(
**
nren
)
for
nren
in
nren_data
}
db
.
session
.
add_all
(
nrens
.
values
())
db
.
session
.
commit
()
response
=
client
.
get
(
'
/api/data-download/
'
,
headers
=
{
'
Accept
'
:
[
'
application/json
'
]})
assert
response
.
status_code
==
200
json
=
response
.
get_json
()
assert
not
any
([
d
.
get
(
'
data
'
)
for
d
in
json
])
for
model
,
*
_
in
models
:
setup
(
model
,
nrens
)
response
=
client
.
get
(
'
/api/data-download/
'
,
headers
=
{
'
Accept
'
:
[
'
application/json
'
]})
assert
response
.
status_code
==
200
json
=
response
.
get_json
()
data_counts
=
[
len
(
d
[
'
data
'
])
for
d
in
json
]
assert
sum
(
data_counts
)
>
0
assert
all
([
len
(
d
[
'
data
'
])
>
0
for
d
in
json
if
d
[
'
name
'
]
!=
'
Survey Comments
'
])
assert
len
([
d
for
d
in
json
if
len
(
d
[
'
data
'
])
==
0
])
==
1
# only no data for survey comments
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