Skip to content

Commit

Permalink
fix test and texts
Browse files Browse the repository at this point in the history
  • Loading branch information
avdata99 committed Dec 11, 2024
1 parent c8b3f30 commit 58080cd
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 16 deletions.
2 changes: 1 addition & 1 deletion ckanext/api_tracking/blueprints/csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def most_accessed_resource_with_token_csv():
'most_accessed_resource_with_token_csv',
most_accessed_resource_with_token_data,
{'limit': 10},
'most-accessed-dataset-with-token.csv',
'most-accessed-resoure-with-token.csv',
)


Expand Down
4 changes: 2 additions & 2 deletions ckanext/api_tracking/queries/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

def get_most_accessed_resource_with_token(limit=10):
"""
Get most accessed datasets with token
Returns a query result with the most accessed datasets with token
Get most accessed resources with token
Returns a query result with the most accessed resources with token
"""
query = model.Session.query(
Expand Down
40 changes: 27 additions & 13 deletions ckanext/api_tracking/tests/test_csv_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,46 +37,60 @@ def base_data():
@pytest.mark.usefixtures('clean_db', 'api_tracking_migrate')
class TestTrackingCSVView:
""" Test basic tracking from requests """
def test_dataset_with_token_csv_no_user(self, app):
def test_resource_with_token_csv_no_user(self, app):
""" Test the endpoint is closed for anonymous users """
url = url_for('tracking_csv.most_accessed_resource_with_token_csv')
with pytest.raises(toolkit.NotAuthorized):
app.get(url)

def test_dataset_with_token_csv_no_auth(self, app, base_data):
def test_resource_with_token_csv_no_auth(self, app, base_data):
""" Test the endpoint is closed for regular users """
url = url_for('tracking_csv.most_accessed_resource_with_token_csv')
auth = {"Authorization": base_data.user1['token']}
with pytest.raises(toolkit.NotAuthorized):
app.get(url, extra_environ=auth)

def test_dataset_with_token_csv(self, app, base_data):
def test_resource_with_token_csv(self, app, base_data):
url = url_for('tracking_csv.most_accessed_resource_with_token_csv')
# download the CSV
auth = {"Authorization": base_data.sysadmin['token']}
response = app.get(url, extra_environ=auth)
assert response.status_code == 200
# save the response locally
full_response = response.body
with open('most-accessed-dataset-with-token.csv', 'w') as f:
with open('most-accessed-resource-with-token.csv', 'w') as f:
f.write(full_response)

# check the CSV content
lines = full_response.splitlines()
header = lines[0].split(',')
assert header == ['dataset_id', 'dataset_title', 'dataset_url', 'total']
assert header == [
'resource_id', 'resource_title', 'resource_url', 'package_id',
'package_title', 'package_url', 'organization_title', 'organization_url',
'organization_id', 'total',
]
rows = lines[1:]
# They are just two datasets
assert len(rows) == 2
for row in rows:
fields = row.split(',')
if fields[0] == base_data.dataset1['id']:
assert fields[1] == base_data.dataset1['title']
assert fields[2] == url_for('dataset.read', id=base_data.dataset1['name'], qualified=True)
assert fields[3] == '6'
elif fields[0] == base_data.dataset2['id']:
assert fields[1] == base_data.dataset2['title']
assert fields[2] == url_for('dataset.read', id=base_data.dataset2['name'], qualified=True)
assert fields[3] == '3'
if fields[0] == base_data.resource11['id']:
assert fields[4] == base_data.dataset1['title']
assert fields[2] == url_for(
'dataset_resource.read',
id=base_data.dataset1['name'],
resource_id=base_data.resource11['id'],
qualified=True
)
assert fields[9] == '6'
elif fields[0] == base_data.resource12['id']:
assert fields[4] == base_data.dataset1['title']
assert fields[2] == url_for(
'dataset_resource.read',
id=base_data.dataset1['name'],
resource_id=base_data.resource12['id'],
qualified=True
)
assert fields[9] == '6'
else:
assert False, f"Unexpected dataset id: {fields[0]}"

0 comments on commit 58080cd

Please sign in to comment.