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
0d85fcac
Commit
0d85fcac
authored
2 years ago
by
Yoann Schneider
Committed by
Bastien Abadie
2 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Create missing types option when checking required types existence
parent
94358166
No related branches found
No related tags found
1 merge request
!204
Create missing types option when checking required types existence
Pipeline
#79523
passed
2 years 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
+39
-8
39 additions, 8 deletions
arkindex_worker/worker/element.py
tests/test_elements_worker/test_elements.py
+51
-6
51 additions, 6 deletions
tests/test_elements_worker/test_elements.py
with
90 additions
and
14 deletions
arkindex_worker/worker/element.py
+
39
−
8
View file @
0d85fcac
...
...
@@ -4,13 +4,15 @@ ElementsWorker methods for elements and element types.
"""
import
uuid
from
typing
import
Dict
,
Iterable
,
List
,
Optional
,
Union
from
typing
import
Dict
,
Iterable
,
List
,
NamedTuple
,
Optional
,
Union
from
peewee
import
IntegrityError
from
arkindex_worker
import
logger
from
arkindex_worker.cache
import
CachedElement
,
CachedImage
from
arkindex_worker.models
import
Element
from
arkindex_worker.models
import
Corpus
,
Element
ElementType
=
NamedTuple
(
"
ElementType
"
,
name
=
str
,
slug
=
str
,
is_folder
=
bool
)
class
MissingTypeError
(
Exception
):
...
...
@@ -24,13 +26,34 @@ class ElementMixin(object):
Mixin for the :class:`ElementsWorker` to provide ``Element`` helpers.
"""
def
check_required_types
(
self
,
corpus_id
:
str
,
*
type_slugs
:
str
)
->
bool
:
def
create_required_types
(
self
,
corpus
:
Corpus
,
element_types
:
List
[
ElementType
]):
"""
Creates given element types in the corpus.
:param Corpus corpus: The corpus to create types on.
:param List[ElementType] element_types: The missing element types to create.
"""
for
element_type
in
element_types
:
self
.
request
(
"
CreateElementType
"
,
body
=
{
"
slug
"
:
element_type
.
slug
,
"
display_name
"
:
element_type
.
name
,
"
folder
"
:
element_type
.
is_folder
,
"
corpus
"
:
corpus
.
id
,
},
)
logger
.
info
(
f
"
Created a new element type with slug
{
element_type
.
slug
}
"
)
def
check_required_types
(
self
,
corpus_id
:
str
,
*
type_slugs
:
str
,
create_missing
:
bool
=
False
)
->
bool
:
"""
Check that a corpus has a list of required element types,
and raise an exception if any of them are missing.
:param str corpus_id: ID of the corpus to check types on.
:param str
\\
*type_slugs: Type slugs to look for.
:param bool create_missing: Whether missing types should be created.
:returns bool: True if all of the specified type slugs have been found.
:raises MissingTypeError: If any of the specified type slugs were not found.
"""
...
...
@@ -42,14 +65,22 @@ class ElementMixin(object):
isinstance
(
slug
,
str
)
for
slug
in
type_slugs
),
"
Element type slugs must be strings.
"
corpus
=
self
.
request
(
"
RetrieveCorpus
"
,
id
=
corpus_id
)
available_slugs
=
{
element_type
[
"
slug
"
]
for
element_type
in
corpus
[
"
types
"
]
}
corpus
=
Corpus
(
self
.
request
(
"
RetrieveCorpus
"
,
id
=
corpus_id
)
)
available_slugs
=
{
element_type
.
slug
for
element_type
in
corpus
.
types
}
missing_slugs
=
set
(
type_slugs
)
-
available_slugs
if
missing_slugs
:
raise
MissingTypeError
(
f
'
Element type(s)
{
"
,
"
.
join
(
sorted
(
missing_slugs
))
}
were not found in the
{
corpus
[
"
name
"
]
}
corpus (
{
corpus
[
"
id
"
]
}
).
'
)
if
create_missing
:
self
.
create_required_types
(
corpus
,
element_types
=
[
ElementType
(
slug
,
slug
,
False
)
for
slug
in
missing_slugs
],
)
else
:
raise
MissingTypeError
(
f
'
Element type(s)
{
"
,
"
.
join
(
sorted
(
missing_slugs
))
}
were not found in the
{
corpus
.
name
}
corpus (
{
corpus
.
id
}
).
'
)
return
True
...
...
This diff is collapsed.
Click to expand it.
tests/test_elements_worker/test_elements.py
+
51
−
6
View file @
0d85fcac
...
...
@@ -5,6 +5,7 @@ from uuid import UUID
import
pytest
from
apistar.exceptions
import
ErrorResponse
from
responses
import
matchers
from
arkindex_worker.cache
import
(
SQL_VERSION
,
...
...
@@ -33,11 +34,7 @@ def test_check_required_types_argument_types(mock_elements_worker):
assert
str
(
e
.
value
)
==
"
Element type slugs must be strings.
"
def
test_check_required_types
(
monkeypatch
,
tmp_path
,
mock_elements_worker
,
responses
):
elements_path
=
tmp_path
/
"
elements.json
"
elements_path
.
write_text
(
"
[]
"
)
monkeypatch
.
setenv
(
"
TASK_ELEMENTS
"
,
str
(
elements_path
))
def
test_check_required_types
(
responses
):
corpus_id
=
"
12341234-1234-1234-1234-123412341234
"
responses
.
add
(
responses
.
GET
,
...
...
@@ -49,7 +46,7 @@ def test_check_required_types(monkeypatch, tmp_path, mock_elements_worker, respo
},
)
worker
=
ElementsWorker
()
worker
.
configure
()
worker
.
setup_api_client
()
assert
worker
.
check_required_types
(
corpus_id
,
"
page
"
)
assert
worker
.
check_required_types
(
corpus_id
,
"
page
"
,
"
folder
"
)
...
...
@@ -62,6 +59,54 @@ def test_check_required_types(monkeypatch, tmp_path, mock_elements_worker, respo
)
def
test_create_missing_types
(
responses
):
corpus_id
=
"
12341234-1234-1234-1234-123412341234
"
responses
.
add
(
responses
.
GET
,
f
"
http://testserver/api/v1/corpus/
{
corpus_id
}
/
"
,
json
=
{
"
id
"
:
corpus_id
,
"
name
"
:
"
Some Corpus
"
,
"
types
"
:
[{
"
slug
"
:
"
folder
"
},
{
"
slug
"
:
"
page
"
}],
},
)
responses
.
add
(
responses
.
POST
,
"
http://testserver/api/v1/elements/type/
"
,
match
=
[
matchers
.
json_params_matcher
(
{
"
slug
"
:
"
text_line
"
,
"
display_name
"
:
"
text_line
"
,
"
folder
"
:
False
,
"
corpus
"
:
corpus_id
,
}
)
],
)
responses
.
add
(
responses
.
POST
,
"
http://testserver/api/v1/elements/type/
"
,
match
=
[
matchers
.
json_params_matcher
(
{
"
slug
"
:
"
act
"
,
"
display_name
"
:
"
act
"
,
"
folder
"
:
False
,
"
corpus
"
:
corpus_id
,
}
)
],
)
worker
=
ElementsWorker
()
worker
.
setup_api_client
()
assert
worker
.
check_required_types
(
corpus_id
,
"
page
"
,
"
text_line
"
,
"
act
"
,
create_missing
=
True
)
def
test_list_elements_elements_list_arg_wrong_type
(
monkeypatch
,
tmp_path
,
mock_elements_worker
):
...
...
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