Fixtures

Documentation of the available fixtures for tests.

Fixture functions

ckan_api_client.tests.conftest.ckan_env(request)[source]

Example usage:

def test_example(ckan_env):
    instance = ckan_env.create_instance()

    with instance.serve() as server:
        # Now make the client connect to ``server.url``

        # If you need an API key:
        api_key = ckan_env.get_sysadmin_api_key()
Returns:A configured Ckan environment object, ready for use
Return type:CkanEnvironment
ckan_api_client.tests.conftest.ckan_instance(request, ckan_env)[source]
ckan_api_client.tests.conftest.ckan_url(request, ckan_instance)[source]

Create & launch a Ckan instance; return a URL at which it is accessible.

Note this would only work for read-only access, as there is no way to get an authentication key too..

Returns:URL to access the instance
Return type:basestring
ckan_api_client.tests.conftest.data_dir()[source]

Return path to the data directory.

Return type:py.path.local
ckan_api_client.tests.conftest.ckan_client_ll(request, ckan_instance)[source]
Returns:A low-level client attached to a running Ckan
Return type:ckan_api_client.low_level.CkanLowlevelClient
ckan_api_client.tests.conftest.ckan_client_hl(request, ckan_instance)[source]
Returns:A high-level client attached to a running Ckan
Return type:ckan_api_client.high_level.CkanHighlevelClient
ckan_api_client.tests.conftest.ckan_client_sync(request, ckan_instance)[source]
Returns:A synchronization client attached to a running Ckan
Return type:ckan_api_client.syncing.SynchronizationClient

Utility objects

class ckan_api_client.tests.conftest.CkanEnvironment(venv_root, pgsql_admin_url, solr_url)[source]

Class providing functionality to manage a Ckan installation.

This manages:

  • the virtualenv
  • ckan instance creation
  • database and solr operations
classmethod from_environment()[source]

Alternate constructor: initializes configuration by reading environment variables.

  • CKAN_VIRTUALENV will be used as the virtualenv in which Ckan is installed (may differ from current virtualenv!).
  • CKAN_POSTGRES_ADMIN url with administrative credentials to be used to access PostgreSQL. Example: postgresql://postgres:pass@localhost/postgres
  • SOLR_URL url of the solr index to use.
create_instance()[source]
get_command(name)[source]

Return the full path of a command inside the virtualenv

run_paster(*args)[source]

Run a paster command in the virtualenv.

Arguments will be passed to the paster command invocation.

Executed command will be something like:

<venv>/bin/python <venv>/bin/paster --plugin=ckan [<args> ..]
get_postgres_admin_credentials()[source]
make_postgres_url(user, password, host, port, database)[source]
get_postgres_admin_connection()[source]
Returns:administrative connection to database
Return type:psycopg2.connect()
get_postgres_connection(username, password, database)[source]
Returns:“user” connection to database
Return type:psycopg2.connect()
create_db_user(username, password)[source]

Create PostgreSQL user, for use by Ckan

create_db(name, owner)[source]

Create PostgreSQL database, for use by Ckan

drop_db_user(username)[source]

Delete previously created PostgreSQL user, to cleanup after running tests.

drop_db(name)[source]

Delete previously created PostgreSQL database, to cleanup after running tests.

flush_solr_index(solr_url=None, site_id=None)[source]

Completely flush the configured Solr index.

Note

since Ckan supports sharing the same index between installations, we don’t actually delete everything from the index, but instead issued a delete on query: +site_id:"<ckan-site-id>"

create_configuration_file(file_name, extra=None)[source]

Create a configuration file for Ckan.

Returns:CkanConfFileWrapper associated to conf file
teardown()[source]
class ckan_api_client.tests.conftest.CkanInstance(virtualenv, conf)[source]

Wrapper for a Ckan instance.

Based on:

  • a virtualenv
  • a Ckan configuration file
venv_root[source]
server_host[source]
server_port[source]
server_url[source]
configuration_file[source]
database_url[source]
database_name[source]
database_username[source]
database_password[source]
solr_url[source]
site_id[source]
storage_path[source]
run_paster(*a, **kw)[source]
get_command(*a, **kw)[source]
run_paster_with_conf(command, *args)[source]

Run a paster command in the virtualenv, adding –config=ckan.ini

Parameters:
  • command – the paster command to be run
  • args – other arguments will be passed to the command

Executed command will be something like:

<venv>/bin/python <venv>/bin/paster --plugin=ckan \
    <command> --config=<venv>/etc/ckan/ckan.ini [<args> ..]
serve()[source]

Start the Ckan server, using paster serve command.

Return type:CkanServerWrapper
get_postgres_connection()[source]
paster_db_init()[source]

Initialize database, by calling paster command

paster_search_index_rebuild()[source]

Rebuild search index, by calling paster command

paster_user_add(name, **kwargs)[source]

Create Ckan user, by calling paster command

paster_user_remove(name)[source]

Remove Ckan user, by calling paster command

paster_sysadmin_add(name)[source]

Grant sysadmin privileges to a Ckan user, by calling paster command.

paster_sysadmin_remove(name)[source]

Revoke sysadmin privileges from a Ckan user, by calling paster command.

get_sysadmin_api_key()[source]

Create a sysadmin user (with random name / password) and return its API key.

teardown()[source]
class ckan_api_client.tests.conftest.ConfFileWrapper(filename)[source]

Wrapper for Ckan configuration files, providing some additional functionality.

get_conf_parser()[source]

Get a RawConfigParser instance, with configuration loaded from the wrapped configuration file.

Not caching this as we want to reload any changes that should have occurred on the file..

set(section, option, value)[source]

Set a configuration option

get(section, option)[source]

Get a configuration option

delete(section, option)[source]

Delete a configuration option

update(data)[source]

Update configuration.

Parameters:data (dict) – dict of dicts: {section: {option: value}}
class ckan_api_client.tests.conftest.ProcessWrapper(args, waitstart=None)[source]
start()[source]
stop()[source]

Utility functions

Functions used by fixtures.

ckan_api_client.tests.conftest.check_tcp_port(host, port, timeout=3)[source]

Check whether a given TCP port is reachable

ckan_api_client.tests.conftest.wait_net_service(host, port, timeout)[source]

Wait for network service to appear

Based on: http://code.activestate.com/recipes/576655/

Parameters:timeout – in seconds
Returns:True of False, if timeout is None may return only True or throw unhandled network exception
ckan_api_client.tests.conftest.discover_available_port(minport=5000, maxport=9000)[source]

Find an available TCP port in the specified range