Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Metagenomics
metagenedb
Commits
d3823222
Commit
d3823222
authored
Nov 19, 2019
by
Kenzo-Hugo Hillion
♻
Browse files
Add tests for custom retrieve function
parent
51407c65
Pipeline
#18460
passed with stages
in 2 minutes and 12 seconds
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
backend/metagenedb/api/catalog/views/test_function.py
View file @
d3823222
from
rest_framework.test
import
APITestCase
import
mock
from
metagenedb.apps.catalog.factory
import
FunctionFactory
from
metagenedb.common.utils.mocks.metagenedb
import
MetageneDBCatalogFunctionAPIMock
class
TestFunctionViewSet
(
APITestCase
):
def
setUp
(
self
):
self
.
function_api
=
MetageneDBCatalogFunctionAPIMock
(
self
.
client
)
self
.
kegg_function
=
FunctionFactory
.
create
(
source
=
'kegg'
)
self
.
eggnog_function
=
FunctionFactory
.
create
(
source
=
'eggnog'
)
def
test_retrieve
(
self
):
for
function
in
[
self
.
kegg_function
,
self
.
eggnog_function
]:
expected_function
=
{
'function_id'
:
function
.
function_id
,
'name'
:
function
.
name
,
'source'
:
function
.
source
}
self
.
assertDictEqual
(
self
.
function_api
.
get
(
function
.
function_id
),
expected_function
)
def
test_retrieve_detailed_available
(
self
):
query_params
=
{
'detailed'
:
'true'
}
class_to_mock
=
'metagenedb.api.catalog.views.function.GetFunctionExternalInfo'
detailed_kegg
=
{
'function_id'
:
self
.
kegg_function
.
function_id
,
'name'
:
self
.
kegg_function
.
name
,
'details'
:
'some details'
}
with
mock
.
patch
(
class_to_mock
)
as
MockGetFunctionExternalInfo
:
MockGetFunctionExternalInfo
.
return_value
.
get_details
.
return_value
=
detailed_kegg
tested_dict
=
self
.
function_api
.
get
(
self
.
kegg_function
.
function_id
,
query_params
=
query_params
)
self
.
assertDictEqual
(
tested_dict
,
detailed_kegg
)
def
test_retrieve_detailed_unavailable
(
self
):
"""
eggnog is not available so it is a good example and should return the DB value.
"""
query_params
=
{
'detailed'
:
'true'
}
expected_function
=
{
'function_id'
:
self
.
eggnog_function
.
function_id
,
'name'
:
self
.
eggnog_function
.
name
,
'source'
:
self
.
eggnog_function
.
source
}
tested_dict
=
self
.
function_api
.
get
(
self
.
eggnog_function
.
function_id
,
query_params
=
query_params
)
self
.
assertDictEqual
(
tested_dict
,
expected_function
)
backend/metagenedb/common/utils/mocks/metagenedb.py
View file @
d3823222
...
...
@@ -25,8 +25,8 @@ class MetageneDBAPIMock(MetageneDBCatalogGeneAPI):
return
self
.
client
.
get
(
f
"
{
url
}
?
{
query_params
}
"
).
json
()
return
self
.
client
.
get
(
f
"
{
url
}
"
).
json
()
def
get
(
self
,
entry_id
):
response
=
self
.
client
.
get
(
reverse
(
f
'
{
self
.
reverse_path
}
-detail'
,
kwargs
=
{
self
.
KEY_ID
:
entry_id
}))
def
get
(
self
,
entry_id
,
query_params
=
None
):
response
=
self
.
client
.
get
(
reverse
(
f
'
{
self
.
reverse_path
}
-detail'
,
kwargs
=
{
self
.
KEY_ID
:
entry_id
})
,
query_params
)
if
response
.
status_code
==
404
:
raise
HTTPError
return
response
.
json
()
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment