Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
S
swh-core
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package 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
Antoine Lambert
swh-core
Commits
0d367617
Commit
0d367617
authored
5 years ago
by
vlorentz
Browse files
Options
Downloads
Patches
Plain Diff
Add tests for db_transaction and db_transaction_generator.
parent
0aaab9cf
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
requirements-test.txt
+1
-0
1 addition, 0 deletions
requirements-test.txt
swh/core/db/tests/test_db.py
+81
-0
81 additions, 0 deletions
swh/core/db/tests/test_db.py
with
82 additions
and
0 deletions
requirements-test.txt
+
1
−
0
View file @
0d367617
pytest
pytest-mock
requests-mock
hypothesis >= 3.11.0
pre-commit
...
...
This diff is collapsed.
Click to expand it.
swh/core/db/tests/test_db.py
+
81
−
0
View file @
0d367617
...
...
@@ -6,12 +6,14 @@
import
os.path
import
tempfile
import
unittest
from
unittest.mock
import
Mock
,
MagicMock
from
hypothesis
import
strategies
,
given
import
psycopg2
import
pytest
from
swh.core.db
import
BaseDb
from
swh.core.db.common
import
db_transaction
,
db_transaction_generator
from
.db_testing
import
(
SingleDbTestFixture
,
db_create
,
db_destroy
,
db_close
,
)
...
...
@@ -108,3 +110,82 @@ class TestDb(SingleDbTestFixture, unittest.TestCase):
items
=
[
dict
(
zip
([
'
i
'
,
'
txt
'
,
'
bytes
'
],
item
))
for
item
in
data
]
with
self
.
assertRaises
(
psycopg2
.
errors
.
NumericValueOutOfRange
):
self
.
db
.
copy_to
(
items
,
'
test_table
'
,
[
'
i
'
,
'
txt
'
,
'
bytes
'
])
def
test_db_transaction
(
mocker
):
expected_cur
=
object
()
called
=
False
class
Storage
:
@db_transaction
()
def
endpoint
(
self
,
cur
=
None
,
db
=
None
):
nonlocal
called
called
=
True
assert
cur
is
expected_cur
storage
=
Storage
()
# 'with storage.get_db().transaction() as cur:' should cause
# 'cur' to be 'expected_cur'
db_mock
=
Mock
()
db_mock
.
transaction
.
return_value
=
MagicMock
()
db_mock
.
transaction
.
return_value
.
__enter__
.
return_value
=
expected_cur
mocker
.
patch
.
object
(
storage
,
'
get_db
'
,
return_value
=
db_mock
,
create
=
True
)
put_db_mock
=
mocker
.
patch
.
object
(
storage
,
'
put_db
'
,
create
=
True
)
storage
.
endpoint
()
assert
called
put_db_mock
.
assert_called_once_with
(
db_mock
)
def
test_db_transaction__with_generator
():
with
pytest
.
raises
(
ValueError
,
match
=
'
generator
'
):
class
Storage
:
@db_transaction
()
def
endpoint
(
self
,
cur
=
None
,
db
=
None
):
yield
None
def
test_db_transaction_generator
(
mocker
):
expected_cur
=
object
()
called
=
False
class
Storage
:
@db_transaction_generator
()
def
endpoint
(
self
,
cur
=
None
,
db
=
None
):
nonlocal
called
called
=
True
assert
cur
is
expected_cur
yield
None
storage
=
Storage
()
# 'with storage.get_db().transaction() as cur:' should cause
# 'cur' to be 'expected_cur'
db_mock
=
Mock
()
db_mock
.
transaction
.
return_value
=
MagicMock
()
db_mock
.
transaction
.
return_value
.
__enter__
.
return_value
=
expected_cur
mocker
.
patch
.
object
(
storage
,
'
get_db
'
,
return_value
=
db_mock
,
create
=
True
)
put_db_mock
=
mocker
.
patch
.
object
(
storage
,
'
put_db
'
,
create
=
True
)
list
(
storage
.
endpoint
())
assert
called
put_db_mock
.
assert_called_once_with
(
db_mock
)
def
test_db_transaction_generator__with_nongenerator
():
with
pytest
.
raises
(
ValueError
,
match
=
'
generator
'
):
class
Storage
:
@db_transaction_generator
()
def
endpoint
(
self
,
cur
=
None
,
db
=
None
):
pass
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