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
97d66a04
Commit
97d66a04
authored
3 years ago
by
Eva Bardou
Browse files
Options
Downloads
Patches
Plain Diff
Use ARKINDEX_CORPUS_ID env variable in get_ml_class_id
parent
66f50605
No related branches found
No related tags found
1 merge request
!88
Use ARKINDEX_CORPUS_ID env variable in get_ml_class_id
Pipeline
#78427
passed
3 years ago
Stage: test
Stage: build
Changes
3
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
arkindex_worker/worker/classification.py
+7
-2
7 additions, 2 deletions
arkindex_worker/worker/classification.py
tests/conftest.py
+2
-0
2 additions, 0 deletions
tests/conftest.py
tests/test_elements_worker/test_classifications.py
+13
-40
13 additions, 40 deletions
tests/test_elements_worker/test_classifications.py
with
22 additions
and
42 deletions
arkindex_worker/worker/classification.py
+
7
−
2
View file @
97d66a04
# -*- coding: utf-8 -*-
import
os
from
apistar.exceptions
import
ErrorResponse
from
arkindex_worker
import
logger
...
...
@@ -19,11 +21,14 @@ class ClassificationMixin(object):
}
logger
.
info
(
f
"
Loaded
{
len
(
self
.
classes
[
corpus_id
])
}
ML classes
"
)
def
get_ml_class_id
(
self
,
corpus_id
,
ml_class
):
def
get_ml_class_id
(
self
,
ml_class
,
corpus_id
=
None
):
"""
Return the ID corresponding to the given class name on a specific corpus
This method will automatically create missing classes
"""
if
not
corpus_id
:
corpus_id
=
os
.
environ
.
get
(
"
ARKINDEX_CORPUS_ID
"
)
if
not
self
.
classes
.
get
(
corpus_id
):
self
.
load_corpus_classes
(
corpus_id
)
...
...
@@ -82,7 +87,7 @@ class ClassificationMixin(object):
"
CreateClassification
"
,
body
=
{
"
element
"
:
element
.
id
,
"
ml_class
"
:
self
.
get_ml_class_id
(
element
.
corpus
.
id
,
ml_class
),
"
ml_class
"
:
self
.
get_ml_class_id
(
ml_class
),
"
worker_version
"
:
self
.
worker_version_id
,
"
confidence
"
:
confidence
,
"
high_confidence
"
:
high_confidence
,
...
...
This diff is collapsed.
Click to expand it.
tests/conftest.py
+
2
−
0
View file @
97d66a04
...
...
@@ -165,6 +165,7 @@ def mock_user_api(responses):
def
mock_elements_worker
(
monkeypatch
,
mock_worker_version_api
):
"""
Build and configure an ElementsWorker with fixed CLI parameters to avoid issues with pytest
"""
monkeypatch
.
setattr
(
sys
,
"
argv
"
,
[
"
worker
"
])
monkeypatch
.
setenv
(
"
ARKINDEX_CORPUS_ID
"
,
"
11111111-1111-1111-1111-111111111111
"
)
worker
=
ElementsWorker
()
worker
.
configure
()
...
...
@@ -185,6 +186,7 @@ def mock_base_worker_with_cache(mocker, monkeypatch, mock_worker_version_api):
def
mock_elements_worker_with_cache
(
monkeypatch
,
mock_worker_version_api
):
"""
Build and configure an ElementsWorker using SQLite cache with fixed CLI parameters to avoid issues with pytest
"""
monkeypatch
.
setattr
(
sys
,
"
argv
"
,
[
"
worker
"
])
monkeypatch
.
setenv
(
"
ARKINDEX_CORPUS_ID
"
,
"
11111111-1111-1111-1111-111111111111
"
)
worker
=
ElementsWorker
(
use_cache
=
True
)
worker
.
configure
()
...
...
This diff is collapsed.
Click to expand it.
tests/test_elements_worker/test_classifications.py
+
13
−
40
View file @
97d66a04
...
...
@@ -27,7 +27,7 @@ def test_get_ml_class_id_load_classes(responses, mock_elements_worker):
)
assert
not
mock_elements_worker
.
classes
ml_class_id
=
mock_elements_worker
.
get_ml_class_id
(
corpus_id
,
"
good
"
)
ml_class_id
=
mock_elements_worker
.
get_ml_class_id
(
"
good
"
,
corpus_id
=
corpus_id
)
assert
len
(
responses
.
calls
)
==
3
assert
[
call
.
request
.
url
for
call
in
responses
.
calls
]
==
[
...
...
@@ -60,7 +60,7 @@ def test_get_ml_class_id_inexistant_class(mock_elements_worker, responses):
"
12341234-1234-1234-1234-123412341234
"
:
{
"
good
"
:
"
0000
"
}
}
ml_class_id
=
mock_elements_worker
.
get_ml_class_id
(
corpus_id
,
"
bad
"
)
ml_class_id
=
mock_elements_worker
.
get_ml_class_id
(
"
bad
"
,
corpus_id
=
corpus_id
)
assert
ml_class_id
==
"
new-ml-class-1234
"
# Now it's available
...
...
@@ -78,7 +78,7 @@ def test_get_ml_class_id(mock_elements_worker):
"
12341234-1234-1234-1234-123412341234
"
:
{
"
good
"
:
"
0000
"
}
}
ml_class_id
=
mock_elements_worker
.
get_ml_class_id
(
corpus_id
,
"
good
"
)
ml_class_id
=
mock_elements_worker
.
get_ml_class_id
(
"
good
"
,
corpus_id
=
corpus_id
)
assert
ml_class_id
==
"
0000
"
...
...
@@ -130,7 +130,10 @@ def test_get_ml_class_reload(responses, mock_elements_worker):
)
# Simply request class 2, it should be reloaded
assert
mock_elements_worker
.
get_ml_class_id
(
corpus_id
,
"
class2
"
)
==
"
class2_id
"
assert
(
mock_elements_worker
.
get_ml_class_id
(
"
class2
"
,
corpus_id
=
corpus_id
)
==
"
class2_id
"
)
assert
len
(
responses
.
calls
)
==
5
assert
mock_elements_worker
.
classes
==
{
...
...
@@ -172,12 +175,7 @@ def test_create_classification_wrong_element(mock_elements_worker):
def
test_create_classification_wrong_ml_class
(
mock_elements_worker
,
responses
):
elt
=
Element
(
{
"
id
"
:
"
12341234-1234-1234-1234-123412341234
"
,
"
corpus
"
:
{
"
id
"
:
"
11111111-1111-1111-1111-111111111111
"
},
}
)
elt
=
Element
({
"
id
"
:
"
12341234-1234-1234-1234-123412341234
"
})
with
pytest
.
raises
(
AssertionError
)
as
e
:
mock_elements_worker
.
create_classification
(
...
...
@@ -249,12 +247,7 @@ def test_create_classification_wrong_confidence(mock_elements_worker):
mock_elements_worker
.
classes
=
{
"
11111111-1111-1111-1111-111111111111
"
:
{
"
a_class
"
:
"
0000
"
}
}
elt
=
Element
(
{
"
id
"
:
"
12341234-1234-1234-1234-123412341234
"
,
"
corpus
"
:
{
"
id
"
:
"
11111111-1111-1111-1111-111111111111
"
},
}
)
elt
=
Element
({
"
id
"
:
"
12341234-1234-1234-1234-123412341234
"
})
with
pytest
.
raises
(
AssertionError
)
as
e
:
mock_elements_worker
.
create_classification
(
element
=
elt
,
...
...
@@ -308,12 +301,7 @@ def test_create_classification_wrong_high_confidence(mock_elements_worker):
mock_elements_worker
.
classes
=
{
"
11111111-1111-1111-1111-111111111111
"
:
{
"
a_class
"
:
"
0000
"
}
}
elt
=
Element
(
{
"
id
"
:
"
12341234-1234-1234-1234-123412341234
"
,
"
corpus
"
:
{
"
id
"
:
"
11111111-1111-1111-1111-111111111111
"
},
}
)
elt
=
Element
({
"
id
"
:
"
12341234-1234-1234-1234-123412341234
"
})
with
pytest
.
raises
(
AssertionError
)
as
e
:
mock_elements_worker
.
create_classification
(
...
...
@@ -342,12 +330,7 @@ def test_create_classification_api_error(responses, mock_elements_worker):
mock_elements_worker
.
classes
=
{
"
11111111-1111-1111-1111-111111111111
"
:
{
"
a_class
"
:
"
0000
"
}
}
elt
=
Element
(
{
"
id
"
:
"
12341234-1234-1234-1234-123412341234
"
,
"
corpus
"
:
{
"
id
"
:
"
11111111-1111-1111-1111-111111111111
"
},
}
)
elt
=
Element
({
"
id
"
:
"
12341234-1234-1234-1234-123412341234
"
})
responses
.
add
(
responses
.
POST
,
"
http://testserver/api/v1/classifications/
"
,
...
...
@@ -379,12 +362,7 @@ def test_create_classification(responses, mock_elements_worker):
mock_elements_worker
.
classes
=
{
"
11111111-1111-1111-1111-111111111111
"
:
{
"
a_class
"
:
"
0000
"
}
}
elt
=
Element
(
{
"
id
"
:
"
12341234-1234-1234-1234-123412341234
"
,
"
corpus
"
:
{
"
id
"
:
"
11111111-1111-1111-1111-111111111111
"
},
}
)
elt
=
Element
({
"
id
"
:
"
12341234-1234-1234-1234-123412341234
"
})
responses
.
add
(
responses
.
POST
,
"
http://testserver/api/v1/classifications/
"
,
...
...
@@ -423,12 +401,7 @@ def test_create_classification_duplicate(responses, mock_elements_worker):
mock_elements_worker
.
classes
=
{
"
11111111-1111-1111-1111-111111111111
"
:
{
"
a_class
"
:
"
0000
"
}
}
elt
=
Element
(
{
"
id
"
:
"
12341234-1234-1234-1234-123412341234
"
,
"
corpus
"
:
{
"
id
"
:
"
11111111-1111-1111-1111-111111111111
"
},
}
)
elt
=
Element
({
"
id
"
:
"
12341234-1234-1234-1234-123412341234
"
})
responses
.
add
(
responses
.
POST
,
"
http://testserver/api/v1/classifications/
"
,
...
...
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