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, randomimage, 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, randomimage, 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 formdataset-{random}title– random string, in the formDataset {random}author– random-generated nameauthor_email– random-generated email addresslicense_id– random license id. One ofcc-by,cc-zero,cc-by-saornotspecified.maintainer– random-generated namemaintainer_email– random-generated email addressnotes– random string, containing some markdownowner_org– set to Noneprivate– Fixed toFalsetags– random list of tags (strings)type– fixed string:"dataset"url– random url of dataset on an “external source”extras– dictionary containing random key / value pairsgroups– empty listresources– list of random resourcesrelationships– empty list
Note
The
owner_organdgroupsfields 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 ofapiorfilename– random-generated nameformat– a random format (eg:csv,json)description– random generated string
Generate
amountrandom tags. Each tag is in the formtag-<random-int>.Returns: a list of tag names
-
ckan_api_client.tests.utils.generate.generate_extras(amount)[source]¶ Generate a dict with
amountrandom 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,organizationandgroupkeys; each of them a dict of{key: object}.
HTTP Utilities¶
Utilities for handling / checking HTTP responses
-
ckan_api_client.tests.utils.http.check_response_ok(response, status_code=200)[source]¶ Warning
deprecated function. Use
check_api_v3_response().
-
ckan_api_client.tests.utils.http.check_response_error(response, status_code)[source]¶ Warning
deprecated function. Use
check_api_v3_error().
-
ckan_api_client.tests.utils.http.check_api_v3_response(response, status_code=200)[source]¶ Make sure that
responseis 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,resultandhelpkeys. - check that
successis True - check that
errorkey is not in the response
Parameters: - response – a
requestsresponse - status_code – http status code to be checked (default: 200)
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_picture(s, size=200)[source]¶ Generate URL to picture from some text hash