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
4874c3d8
Commit
4874c3d8
authored
4 years ago
by
Eva Bardou
Browse files
Options
Downloads
Patches
Plain Diff
Fix some review related code snippets
parent
3ad7a80c
No related branches found
No related tags found
No related merge requests found
Pipeline
#78323
passed
4 years ago
Stage: test
Stage: build
Changes
3
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
arkindex_worker/cache.py
+24
-1
24 additions, 1 deletion
arkindex_worker/cache.py
arkindex_worker/worker.py
+6
-7
6 additions, 7 deletions
arkindex_worker/worker.py
tests/test_cache.py
+4
-4
4 additions, 4 deletions
tests/test_cache.py
with
34 additions
and
12 deletions
arkindex_worker/cache.py
+
24
−
1
View file @
4874c3d8
...
...
@@ -21,6 +21,13 @@ CachedElement = namedtuple(
)
def
convert_table_tuple
(
table
):
if
table
==
"
elements
"
:
return
CachedElement
else
:
raise
NotImplementedError
class
LocalDB
(
object
):
def
__init__
(
self
,
path
):
self
.
db
=
sqlite3
.
connect
(
path
)
...
...
@@ -43,11 +50,27 @@ class LocalDB(object):
self
.
db
.
commit
()
def
fetch
(
self
,
table
,
where
=
[]):
"""
where parameter is a list containing 3-values tuples defining an SQL WHERE condition.
e.g: where=[(
"
id
"
,
"
LIKE
"
,
"
%0000%
"
), (
"
id
"
,
"
NOT LIKE
"
,
"
%1111%
"
)]
stands for
"
WHERE id LIKE
'
%0000%
'
AND id NOT LIKE
'
%1111%
'"
in SQL.
This method only supports
'
AND
'
SQL conditions.
"""
sql
=
f
"
SELECT * FROM
{
table
}
"
if
where
:
assert
isinstance
(
where
,
list
),
"
where should be a list
"
assert
all
(
isinstance
(
condition
,
tuple
)
and
len
(
condition
)
==
3
for
condition
in
where
),
"
where conditions should be tuples of 3 values
"
sql
+=
"
WHERE
"
sql
+=
"
AND
"
.
join
(
[
f
"
{
field
}
{
operator
}
(?)
"
for
field
,
operator
,
_
in
where
]
)
self
.
cursor
.
execute
(
sql
,
[
value
for
_
,
_
,
value
in
where
])
return
self
.
cursor
.
fetchall
()
tuple_type
=
convert_table_tuple
(
table
)
return
[
tuple_type
(
**
dict
(
row
))
for
row
in
self
.
cursor
.
fetchall
()]
This diff is collapsed.
Click to expand it.
arkindex_worker/worker.py
+
6
−
7
View file @
4874c3d8
...
...
@@ -897,18 +897,17 @@ class ElementsWorker(BaseWorker):
},
"
When using the local cache, you can only filter by
'
type
'
and/or
'
worker_version
'"
conditions
=
[(
"
parent_id
"
,
"
=
"
,
convert_str_uuid_to_hex
(
element
.
id
))]
conditions
+=
[(
"
type
"
,
"
=
"
,
type
)]
if
type
else
[]
conditions
+=
(
[(
"
worker_version_id
"
,
"
=
"
,
convert_str_uuid_to_hex
(
worker_version
))]
if
worker_version
else
[]
)
if
type
:
conditions
.
append
((
"
type
"
,
"
=
"
,
type
))
if
worker_version
:
conditions
.
append
(
(
"
worker_version_id
"
,
"
=
"
,
convert_str_uuid_to_hex
(
worker_version
))
)
children
=
self
.
cache
.
fetch
(
"
elements
"
,
where
=
conditions
,
)
children
=
[
CachedElement
(
**
dict
(
child
))
for
child
in
children
]
else
:
children
=
self
.
api_client
.
paginate
(
"
ListElementChildren
"
,
id
=
element
.
id
,
**
query_params
...
...
This diff is collapsed.
Click to expand it.
tests/test_cache.py
+
4
−
4
View file @
4874c3d8
...
...
@@ -134,15 +134,15 @@ def test_fetch_all():
db_path
=
f
"
{
FIXTURES
}
/lines.sqlite
"
cache
=
LocalDB
(
db_path
)
cache
.
create_tables
()
rows
=
cache
.
fetch
(
"
elements
"
)
assert
[
CachedElement
(
**
dict
(
row
))
for
row
in
rows
]
==
ELEMENTS_TO_INSERT
children
=
cache
.
fetch
(
"
elements
"
)
assert
children
==
ELEMENTS_TO_INSERT
def
test_fetch_with_where
():
db_path
=
f
"
{
FIXTURES
}
/lines.sqlite
"
cache
=
LocalDB
(
db_path
)
cache
.
create_tables
()
rows
=
cache
.
fetch
(
children
=
cache
.
fetch
(
"
elements
"
,
where
=
[
(
...
...
@@ -153,4 +153,4 @@ def test_fetch_with_where():
(
"
id
"
,
"
LIKE
"
,
"
%1111%
"
),
],
)
assert
[
CachedElement
(
**
dict
(
row
))
for
row
in
rows
]
==
[
ELEMENTS_TO_INSERT
[
0
]]
assert
children
==
[
ELEMENTS_TO_INSERT
[
0
]]
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