forked from openknowledge-archive/dpm-py
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test for successful publish, fix publish url
- Loading branch information
Showing
7 changed files
with
113 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .validate import validate |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# -*- coding: utf-8 -*- | ||
from __future__ import division | ||
from __future__ import print_function | ||
from __future__ import absolute_import | ||
from __future__ import unicode_literals | ||
|
||
import datapackage | ||
import sys | ||
from os.path import exists | ||
|
||
|
||
def validate(): | ||
""" | ||
Validate datapackage in the current dir. Print validation errors if found and | ||
then exit. Return datapackage if valid. | ||
:return: | ||
DataPackage -- valid DataPackage instance | ||
""" | ||
if not exists('datapackage.json'): | ||
print('Current directory is not a datapackage: datapackage.json not found.') | ||
sys.exit(1) | ||
|
||
try: | ||
dp = datapackage.DataPackage('datapackage.json') | ||
except: | ||
print('datapackage.json is malformed') | ||
sys.exit(1) | ||
|
||
try: | ||
dp.validate() | ||
except datapackage.exceptions.ValidationError: | ||
for error in dp.iter_errors(): | ||
# TODO: printing error looks very noisy on output, maybe try make it look nice. | ||
print(error) | ||
sys.exit(1) | ||
|
||
return dp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# -*- coding: utf-8 -*- | ||
from __future__ import division | ||
from __future__ import print_function | ||
from __future__ import absolute_import | ||
from __future__ import unicode_literals | ||
|
||
import datapackage | ||
import responses | ||
from mock import patch | ||
|
||
from dpm.main import cli | ||
from .base import BaseCliTestCase | ||
|
||
|
||
class PublishSuccessTest(BaseCliTestCase): | ||
""" | ||
When user publishes valid datapackage, and server accepts it, dpm should | ||
report sucess. | ||
""" | ||
|
||
def setUp(self): | ||
# GIVEN datapackage that can be treated as valid by the dpm | ||
valid_dp = datapackage.DataPackage({ | ||
"name": "some-datapackage", | ||
"resources": [ | ||
{ "name": "some-resource", "path": "./data/some_data.csv", } | ||
] | ||
}) | ||
patch('dpm.main.client', validate=lambda *a: valid_dp).start() | ||
|
||
# AND valid credentials | ||
patch('dpm.main.get_credentials', lambda *a: 'fake creds').start() | ||
|
||
def test_publish_success(self): | ||
# GIVEN the server that accepts datapackage | ||
responses.add( | ||
responses.PUT, 'https://example.com/api/package/testpub/some-datapackage', | ||
json={'message': 'OK'}, | ||
status=200) | ||
|
||
# WHEN `dpm publish` is invoked | ||
result = self.invoke(cli, ['publish', '--publisher', 'testpub']) | ||
|
||
# THEN exit code should be 0 | ||
self.assertEqual(result.exit_code, 0) | ||
# AND 'publish OK' should be printed to stdout | ||
self.assertTrue('publish ok' in result.output) |