Testing utilities
Data generation
-
ckan_api_client.tests.utils.generate.generate_organization()[source]
Generate a random organization object, with:
- name, random, example: "org-abc123"
- title, random, example: "Organization abc123"
- description, random
- image, url pointing to a random-generated pic
-
ckan_api_client.tests.utils.generate.generate_group()[source]
Generate a random group object, with:
- name, random, example: "grp-abc123"
- title, random, example: "Group abc123"
- description, random
- image, url pointing to a random-generated pic
-
ckan_api_client.tests.utils.generate.generate_dataset()[source]
Generate a dataset, populated with random data.
Fields:
- name – random string, in the form dataset-{random}
- title – random string, in the form Dataset {random}
- author – random-generated name
- author_email – random-generated email address
- license_id – random license id. One of cc-by, cc-zero,
cc-by-sa or notspecified.
- maintainer – random-generated name
- maintainer_email – random-generated email address
- notes – random string, containing some markdown
- owner_org – set to None
- private – Fixed to False
- tags – random list of tags (strings)
- type – fixed string: "dataset"
- url – random url of dataset on an “external source”
- extras – dictionary containing random key / value pairs
- groups – empty list
- resources – list of random resources
- relationships – empty list
Note
The owner_org and groups fields will be blank,
as they must match with existing groups / organizations
and we don’t have access to database from here (nor
is it in the scope of this function!)
-
ckan_api_client.tests.utils.generate.generate_resource()[source]
Generate a random resource, to be put in a dataset.
Fields:
- url – resource URL on an “external source”
- resource_type – one of api or file
- name – random-generated name
- format – a random format (eg: csv, json)
- description – random generated string
-
ckan_api_client.tests.utils.generate.generate_tags(amount)[source]
Generate amount random tags.
Each tag is in the form tag-<random-int>.
Returns: | a list of tag names |
Generate a dict with amount random key/value pairs.
-
ckan_api_client.tests.utils.generate.generate_data(dataset_count=50, orgs_count=10, groups_count=15)[source]
Generate a bunch of random data.
Will also associate datasets with random organizations / groups.
Returns: | a dict with the dataset, organization and
group keys; each of them a dict of {key: object}. |
-
ckan_api_client.tests.utils.generate.generate_id(length=10)[source]
HTTP Utilities
Utilities for handling / checking HTTP responses
-
ckan_api_client.tests.utils.http.check_response_ok(response, status_code=200)[source]
-
-
ckan_api_client.tests.utils.http.check_response_error(response, status_code)[source]
-
-
ckan_api_client.tests.utils.http.check_api_v3_response(response, status_code=200)[source]
Make sure that response is a valid successful response
from API v3.
- check http status code to be in the 200-299 range
- check http status code to match status_code
- check content-type to be application/json
- check charset to be utf-8
- check content body to be valid json
- make sure response object contains the success,
result and help keys.
- check that success is True
- check that error key is not in the response
Parameters: |
- response – a requests response
- status_code – http status code to be checked
(default: 200)
|
-
ckan_api_client.tests.utils.http.check_api_v3_error(response, status_code)[source]
Make sure that response is a valid error response from
API v3.
- check http status code to match status_code
Parameters: |
- response – a requests response
- status_code – http status code to be checked
|
String-related
String generation functions.
-
ckan_api_client.tests.utils.strings.generate_password(length=20)[source]
Generate random password of the given length.
Beware that the string will be generate as random data from urandom,
and returned as headecimal string of twice the length.
-
ckan_api_client.tests.utils.strings.generate_random_alphanum(length=10)[source]
Generate a random string, made of ascii letters + digits
-
ckan_api_client.tests.utils.strings.gen_random_id(length=10)[source]
Generate a random id, made of lowercase ascii letters + digits
-
ckan_api_client.tests.utils.strings.gen_dataset_name()[source]
Generate a random dataset name
-
ckan_api_client.tests.utils.strings.gen_picture(s, size=200)[source]
Generate URL to picture from some text hash
-
ckan_api_client.tests.utils.strings.gen_gravatar(s, size=200)[source]
Return URL for gravatar of md5 of string s
-
ckan_api_client.tests.utils.strings.gen_robohash(s, size=200)[source]
Return URL for robohash pic for sha1 hash of string s
Validation
Utility functions to validate expectations
-
ckan_api_client.tests.utils.validation.check_dataset(dataset, expected)[source]
Make sure dataset matches the expected one
-
ckan_api_client.tests.utils.validation.check_group(group, expected)[source]
Make sure group matches the expected one
-
ckan_api_client.tests.utils.validation.check_organization(organization, expected)[source]
Make sure organization matches the expected one