Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* initial commit to do test * modify test.ini and test_download.py in models * move test.ini from root to ckanext/feedback/tests * delete sections that is not needed * delete test.init and modify config/test.ini * add DownloadSummary model test * modify indents in download model test * add download controller test * add test about download service * delete test about download model * commit to checkout * change to use the mock of database access * commit to checkout * commit to checkout * commit to checkout * rewrite the test to use test DB * run isort, black, pflake8 * rename views to services * delete DownloadController.increment_resource_download * delete app_context * sepalate the test of extended_download * modify static initializaer to setup_class * add setup function and move resource and app into it * change setup to setup_method
- Loading branch information
Showing
4 changed files
with
129 additions
and
5 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
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,59 @@ | ||
from unittest.mock import patch | ||
|
||
import pytest | ||
from ckan import model | ||
from ckan.tests import factories | ||
from flask import Flask | ||
|
||
from ckanext.feedback.command.feedback import ( | ||
create_download_tables, | ||
create_resource_tables, | ||
create_utilization_tables, | ||
get_engine, | ||
) | ||
from ckanext.feedback.controllers.download import DownloadController | ||
from ckanext.feedback.models.download import DownloadSummary | ||
from ckanext.feedback.models.session import session | ||
|
||
|
||
def get_downloads(resource_id): | ||
count = ( | ||
session.query(DownloadSummary.download) | ||
.filter(DownloadSummary.resource_id == resource_id) | ||
.scalar() | ||
) | ||
return count | ||
|
||
|
||
@pytest.mark.usefixtures('clean_db', 'with_plugins', 'with_request_context') | ||
class TestDownloadController: | ||
@classmethod | ||
def setup_class(cls): | ||
model.repo.init_db() | ||
engine = get_engine('db', '5432', 'ckan_test', 'ckan', 'ckan') | ||
create_utilization_tables(engine) | ||
create_resource_tables(engine) | ||
create_download_tables(engine) | ||
|
||
def setup_method(self, method): | ||
self.app = Flask(__name__) | ||
|
||
@patch('ckanext.feedback.controllers.download.download') | ||
def test_extended_download(self, download): | ||
resource = factories.Resource() | ||
with self.app.test_request_context(headers={'Sec-Fetch-Dest': 'document'}): | ||
DownloadController.extended_download( | ||
'package_type', resource['package_id'], resource['id'], None | ||
) | ||
assert get_downloads(resource['id']) == 1 | ||
assert download | ||
|
||
@patch('ckanext.feedback.controllers.download.download') | ||
def test_extended_download_with_preview(self, download): | ||
resource = factories.Resource() | ||
with self.app.test_request_context(headers={'Sec-Fetch-Dest': 'image'}): | ||
DownloadController.extended_download( | ||
'package_type', resource['package_id'], resource['id'], None | ||
) | ||
assert get_downloads(resource['id']) is None | ||
assert download |
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,70 @@ | ||
import pytest | ||
from ckan import model | ||
from ckan.tests import factories | ||
|
||
from ckanext.feedback.command.feedback import ( | ||
create_download_tables, | ||
create_resource_tables, | ||
create_utilization_tables, | ||
get_engine, | ||
) | ||
from ckanext.feedback.models.download import DownloadSummary | ||
from ckanext.feedback.models.session import session | ||
from ckanext.feedback.services.download.summary import ( | ||
get_package_downloads, | ||
get_resource_downloads, | ||
increment_resource_downloads, | ||
) | ||
|
||
|
||
def get_downloads(resource_id): | ||
count = ( | ||
session.query(DownloadSummary.download) | ||
.filter(DownloadSummary.resource_id == resource_id) | ||
.scalar() | ||
) | ||
return count | ||
|
||
|
||
@pytest.mark.usefixtures('clean_db', 'with_plugins', 'with_request_context') | ||
class TestDownloadServices: | ||
@classmethod | ||
def setup_class(cls): | ||
model.repo.init_db() | ||
engine = get_engine('db', '5432', 'ckan_test', 'ckan', 'ckan') | ||
create_utilization_tables(engine) | ||
create_resource_tables(engine) | ||
create_download_tables(engine) | ||
|
||
def test_increment_resource_downloads(self): | ||
resource = factories.Resource() | ||
increment_resource_downloads(resource['id']) | ||
assert get_downloads(resource['id']) == 1 | ||
increment_resource_downloads(resource['id']) | ||
assert get_downloads(resource['id']) == 2 | ||
|
||
def test_get_package_download(self): | ||
resource = factories.Resource() | ||
assert get_package_downloads(resource['package_id']) == 0 | ||
download_summary = DownloadSummary( | ||
id=str('test_id'), | ||
resource_id=resource['id'], | ||
download=1, | ||
created='2023-03-31 01:23:45.123456', | ||
updated='2023-03-31 01:23:45.123456', | ||
) | ||
session.add(download_summary) | ||
assert get_package_downloads(resource['package_id']) == 1 | ||
|
||
def test_get_resource_download(self): | ||
resource = factories.Resource() | ||
assert get_resource_downloads(resource['id']) == 0 | ||
download_summary = DownloadSummary( | ||
id=str('test_id'), | ||
resource_id=resource['id'], | ||
download=1, | ||
created='2023-03-31 01:23:45.123456', | ||
updated='2023-03-31 01:23:45.123456', | ||
) | ||
session.add(download_summary) | ||
assert get_resource_downloads(resource['id']) == 1 |