Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
B
Base Worker
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
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
Workers
Base Worker
Commits
ec3904de
Commit
ec3904de
authored
1 year ago
by
Manon Blanco
Committed by
Yoann Schneider
1 year ago
Browse files
Options
Downloads
Patches
Plain Diff
Add an API helper to link two elements
parent
8dedb440
No related branches found
Branches containing commit
No related tags found
Tags containing commit
1 merge request
!474
Add an API helper to link two elements
Pipeline
#148517
passed
1 year ago
Stage: test
Stage: build
Stage: release
Stage: deploy
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
arkindex_worker/worker/element.py
+30
-1
30 additions, 1 deletion
arkindex_worker/worker/element.py
tests/test_elements_worker/test_elements.py
+88
-0
88 additions, 0 deletions
tests/test_elements_worker/test_elements.py
with
118 additions
and
1 deletion
arkindex_worker/worker/element.py
+
30
−
1
View file @
ec3904de
...
@@ -283,6 +283,35 @@ class ElementMixin:
...
@@ -283,6 +283,35 @@ class ElementMixin:
return
created_ids
return
created_ids
def
create_element_parent
(
self
,
parent
:
Element
,
child
:
Element
,
)
->
dict
[
str
,
str
]:
"""
Link an element to a parent through the API.
:param parent: Parent element.
:param child: Child element.
:returns: A dict from the ``CreateElementParent`` API endpoint.
"""
assert
parent
and
isinstance
(
parent
,
Element
),
"
parent shouldn
'
t be null and should be of type Element
"
assert
child
and
isinstance
(
child
,
Element
),
"
child shouldn
'
t be null and should be of type Element
"
if
self
.
is_read_only
:
logger
.
warning
(
"
Cannot link elements as this worker is in read-only mode
"
)
return
return
self
.
request
(
"
CreateElementParent
"
,
parent
=
parent
.
id
,
child
=
child
.
id
,
)
def
partial_update_element
(
def
partial_update_element
(
self
,
element
:
Element
|
CachedElement
,
**
kwargs
self
,
element
:
Element
|
CachedElement
,
**
kwargs
)
->
dict
:
)
->
dict
:
...
@@ -301,7 +330,7 @@ class ElementMixin:
...
@@ -301,7 +330,7 @@ class ElementMixin:
* *image* (``UUID``): Optional ID of the image of this element
* *image* (``UUID``): Optional ID of the image of this element
:returns: A dict from the ``PartialUpdateElement`` API endpoint
,
:returns: A dict from the ``PartialUpdateElement`` API endpoint
.
"""
"""
assert
element
and
isinstance
(
assert
element
and
isinstance
(
element
,
Element
|
CachedElement
element
,
Element
|
CachedElement
...
...
This diff is collapsed.
Click to expand it.
tests/test_elements_worker/test_elements.py
+
88
−
0
View file @
ec3904de
...
@@ -1241,6 +1241,94 @@ def test_create_elements_integrity_error(
...
@@ -1241,6 +1241,94 @@ def test_create_elements_integrity_error(
assert
list
(
CachedElement
.
select
())
==
[]
assert
list
(
CachedElement
.
select
())
==
[]
@pytest.mark.parametrize
(
(
"
params
"
,
"
error_message
"
),
[
(
{
"
parent
"
:
None
,
"
child
"
:
None
},
"
parent shouldn
'
t be null and should be of type Element
"
,
),
(
{
"
parent
"
:
"
not an element
"
,
"
child
"
:
None
},
"
parent shouldn
'
t be null and should be of type Element
"
,
),
(
{
"
parent
"
:
Element
(
zone
=
None
),
"
child
"
:
None
},
"
child shouldn
'
t be null and should be of type Element
"
,
),
(
{
"
parent
"
:
Element
(
zone
=
None
),
"
child
"
:
"
not an element
"
},
"
child shouldn
'
t be null and should be of type Element
"
,
),
],
)
def
test_create_element_parent_invalid_params
(
mock_elements_worker
,
params
,
error_message
):
with
pytest
.
raises
(
AssertionError
,
match
=
re
.
escape
(
error_message
)):
mock_elements_worker
.
create_element_parent
(
**
params
)
def
test_create_element_parent_api_error
(
responses
,
mock_elements_worker
):
parent
=
Element
({
"
id
"
:
"
12341234-1234-1234-1234-123412341234
"
})
child
=
Element
({
"
id
"
:
"
497f6eca-6276-4993-bfeb-53cbbbba6f08
"
})
responses
.
add
(
responses
.
POST
,
"
http://testserver/api/v1/element/497f6eca-6276-4993-bfeb-53cbbbba6f08/parent/12341234-1234-1234-1234-123412341234/
"
,
status
=
500
,
)
with
pytest
.
raises
(
ErrorResponse
):
mock_elements_worker
.
create_element_parent
(
parent
=
parent
,
child
=
child
,
)
assert
len
(
responses
.
calls
)
==
len
(
BASE_API_CALLS
)
+
5
assert
[
(
call
.
request
.
method
,
call
.
request
.
url
)
for
call
in
responses
.
calls
]
==
BASE_API_CALLS
+
[
# We retry 5 times the API call
(
"
POST
"
,
"
http://testserver/api/v1/element/497f6eca-6276-4993-bfeb-53cbbbba6f08/parent/12341234-1234-1234-1234-123412341234/
"
,
),
]
*
5
def
test_create_element_parent
(
responses
,
mock_elements_worker
):
parent
=
Element
({
"
id
"
:
"
12341234-1234-1234-1234-123412341234
"
})
child
=
Element
({
"
id
"
:
"
497f6eca-6276-4993-bfeb-53cbbbba6f08
"
})
responses
.
add
(
responses
.
POST
,
"
http://testserver/api/v1/element/497f6eca-6276-4993-bfeb-53cbbbba6f08/parent/12341234-1234-1234-1234-123412341234/
"
,
status
=
200
,
json
=
{
"
parent
"
:
"
12341234-1234-1234-1234-123412341234
"
,
"
child
"
:
"
497f6eca-6276-4993-bfeb-53cbbbba6f08
"
,
},
)
created_element_parent
=
mock_elements_worker
.
create_element_parent
(
parent
=
parent
,
child
=
child
,
)
assert
len
(
responses
.
calls
)
==
len
(
BASE_API_CALLS
)
+
1
assert
[
(
call
.
request
.
method
,
call
.
request
.
url
)
for
call
in
responses
.
calls
]
==
BASE_API_CALLS
+
[
(
"
POST
"
,
"
http://testserver/api/v1/element/497f6eca-6276-4993-bfeb-53cbbbba6f08/parent/12341234-1234-1234-1234-123412341234/
"
,
),
]
assert
created_element_parent
==
{
"
parent
"
:
"
12341234-1234-1234-1234-123412341234
"
,
"
child
"
:
"
497f6eca-6276-4993-bfeb-53cbbbba6f08
"
,
}
@pytest.mark.parametrize
(
@pytest.mark.parametrize
(
(
"
payload
"
,
"
error
"
),
(
"
payload
"
,
"
error
"
),
[
[
...
...
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