Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
S
swh-deposit
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
Model registry
Operate
Environments
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
Platform
Development
swh-deposit
Commits
78fd8929
Commit
78fd8929
authored
5 years ago
by
vlorentz
Browse files
Options
Downloads
Patches
Plain Diff
Start writing tests for the deposit upload CLI.
parent
42ed3e12
No related branches found
Branches containing commit
No related tags found
Tags containing commit
1 merge request
!338
Start writing tests for the deposit upload CLI.
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/deposit/tests/cli/test_client.py
+164
-12
164 additions, 12 deletions
swh/deposit/tests/cli/test_client.py
with
165 additions
and
12 deletions
requirements-test.txt
+
1
−
0
View file @
78fd8929
pytest
pytest-django
pytest-mock
swh.scheduler[testing]
pytest-postgresql >= 2.1.0
requests_mock
...
...
This diff is collapsed.
Click to expand it.
swh/deposit/tests/cli/test_client.py
+
164
−
12
View file @
78fd8929
...
...
@@ -3,12 +3,46 @@
# License: GNU General Public License version 3, or any later version
# See top-level LICENSE file for more information
import
pytest
import
logging
import
os
from
unittest.mock
import
MagicMock
from
click.testing
import
CliRunner
import
pytest
from
swh.deposit.client
import
PublicApiDepositClient
from
swh.deposit.cli.client
import
_url
,
_client
,
_collection
,
InputError
from
swh.deposit.cli.client
import
(
generate_slug
,
_url
,
_client
,
_collection
,
InputError
)
from
swh.deposit.cli
import
deposit
as
cli
from
..conftest
import
TEST_USER
EXAMPLE_SERVICE_DOCUMENT
=
{
'
service
'
:
{
'
workspace
'
:
{
'
collection
'
:
{
'
sword:name
'
:
'
softcol
'
,
}
}
}
}
@pytest.fixture
def
slug
():
return
generate_slug
()
@pytest.fixture
def
client_mock
(
mocker
,
slug
):
mocker
.
patch
(
'
swh.deposit.cli.client.generate_slug
'
,
return_value
=
slug
)
mock_client
=
MagicMock
()
mocker
.
patch
(
'
swh.deposit.cli.client._client
'
,
return_value
=
mock_client
)
mock_client
.
service_document
.
return_value
=
EXAMPLE_SERVICE_DOCUMENT
mock_client
.
deposit_create
.
return_value
=
'
{
"
foo
"
:
"
bar
"
}
'
return
mock_client
def
test_url
():
...
...
@@ -35,15 +69,133 @@ def test_collection_error():
def
test_collection_ok
():
mock_client
=
MagicMock
()
mock_client
.
service_document
.
return_value
=
{
'
service
'
:
{
'
workspace
'
:
{
'
collection
'
:
{
'
sword:name
'
:
'
softcol
'
,
}
}
}
}
mock_client
.
service_document
.
return_value
=
EXAMPLE_SERVICE_DOCUMENT
collection_name
=
_collection
(
mock_client
)
assert
collection_name
==
'
softcol
'
def
test_single_minimal_deposit
(
sample_archive
,
mocker
,
caplog
,
client_mock
,
slug
):
"""
from:
https://docs.softwareheritage.org/devel/swh-deposit/getting-started.html#single-deposit
"""
# noqa
runner
=
CliRunner
()
result
=
runner
.
invoke
(
cli
,
[
'
upload
'
,
'
--url
'
,
'
mock://deposit.swh/1
'
,
'
--username
'
,
TEST_USER
[
'
username
'
],
'
--password
'
,
TEST_USER
[
'
password
'
],
'
--name
'
,
'
test-project
'
,
'
--archive
'
,
sample_archive
[
'
path
'
],
])
assert
result
.
exit_code
==
0
,
result
.
output
assert
result
.
output
==
''
assert
caplog
.
record_tuples
==
[
(
'
swh.deposit.cli.client
'
,
logging
.
INFO
,
'
{
"
foo
"
:
"
bar
"
}
'
),
]
client_mock
.
deposit_create
.
assert_called_once_with
(
archive
=
sample_archive
[
'
path
'
],
collection
=
'
softcol
'
,
in_progress
=
False
,
metadata
=
None
,
slug
=
slug
)
def
test_single_deposit_slug_collection
(
sample_archive
,
mocker
,
caplog
,
client_mock
):
"""
from:
https://docs.softwareheritage.org/devel/swh-deposit/getting-started.html#single-deposit
"""
# noqa
slug
=
'
my-slug
'
collection
=
'
my-collection
'
runner
=
CliRunner
()
result
=
runner
.
invoke
(
cli
,
[
'
upload
'
,
'
--url
'
,
'
mock://deposit.swh/1
'
,
'
--username
'
,
TEST_USER
[
'
username
'
],
'
--password
'
,
TEST_USER
[
'
password
'
],
'
--name
'
,
'
test-project
'
,
'
--archive
'
,
sample_archive
[
'
path
'
],
'
--slug
'
,
slug
,
'
--collection
'
,
collection
,
])
assert
result
.
exit_code
==
0
,
result
.
output
assert
result
.
output
==
''
assert
caplog
.
record_tuples
==
[
(
'
swh.deposit.cli.client
'
,
logging
.
INFO
,
'
{
"
foo
"
:
"
bar
"
}
'
),
]
client_mock
.
deposit_create
.
assert_called_once_with
(
archive
=
sample_archive
[
'
path
'
],
collection
=
collection
,
in_progress
=
False
,
metadata
=
None
,
slug
=
slug
)
def
test_multisteps_deposit
(
sample_archive
,
atom_dataset
,
mocker
,
caplog
,
datadir
,
client_mock
,
slug
):
"""
from:
https://docs.softwareheritage.org/devel/swh-deposit/getting-started.html#multisteps-deposit
"""
# noqa
slug
=
generate_slug
()
mocker
.
patch
(
'
swh.deposit.cli.client.generate_slug
'
,
return_value
=
slug
)
# https://docs.softwareheritage.org/devel/swh-deposit/getting-started.html#create-an-incomplete-deposit
client_mock
.
deposit_create
.
return_value
=
'
{
"
deposit_id
"
:
"
42
"
}
'
runner
=
CliRunner
()
result
=
runner
.
invoke
(
cli
,
[
'
upload
'
,
'
--url
'
,
'
mock://deposit.swh/1
'
,
'
--username
'
,
TEST_USER
[
'
username
'
],
'
--password
'
,
TEST_USER
[
'
password
'
],
'
--archive
'
,
sample_archive
[
'
path
'
],
'
--partial
'
,
])
assert
result
.
exit_code
==
0
,
result
.
output
assert
result
.
output
==
''
assert
caplog
.
record_tuples
==
[
(
'
swh.deposit.cli.client
'
,
logging
.
INFO
,
'
{
"
deposit_id
"
:
"
42
"
}
'
),
]
client_mock
.
deposit_create
.
assert_called_once_with
(
archive
=
sample_archive
[
'
path
'
],
collection
=
'
softcol
'
,
in_progress
=
True
,
metadata
=
None
,
slug
=
slug
)
# Clear mocking state
caplog
.
clear
()
client_mock
.
reset_mock
()
# https://docs.softwareheritage.org/devel/swh-deposit/getting-started.html#add-content-or-metadata-to-the-deposit
metadata_path
=
os
.
path
.
join
(
datadir
,
'
atom
'
,
'
entry-data-deposit-binary.xml
'
)
result
=
runner
.
invoke
(
cli
,
[
'
upload
'
,
'
--url
'
,
'
mock://deposit.swh/1
'
,
'
--username
'
,
TEST_USER
[
'
username
'
],
'
--password
'
,
TEST_USER
[
'
password
'
],
'
--metadata
'
,
metadata_path
,
])
assert
result
.
exit_code
==
0
,
result
.
output
assert
result
.
output
==
''
assert
caplog
.
record_tuples
==
[
(
'
swh.deposit.cli.client
'
,
logging
.
INFO
,
'
{
"
deposit_id
"
:
"
42
"
}
'
),
]
client_mock
.
deposit_create
.
assert_called_once_with
(
archive
=
None
,
collection
=
'
softcol
'
,
in_progress
=
False
,
metadata
=
metadata_path
,
slug
=
slug
)
# Clear mocking state
caplog
.
clear
()
client_mock
.
reset_mock
()
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