-
Notifications
You must be signed in to change notification settings - Fork 2k
/
test_example_iresourcecontroller.py
137 lines (108 loc) · 5.85 KB
/
test_example_iresourcecontroller.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# encoding: utf-8
'''Tests for the ckanext.example_iauthfunctions extension.
'''
from ckan.common import config
import ckan.model as model
import ckan.plugins
import ckan.tests.factories as factories
import ckan.tests.helpers as helpers
from ckanext.example_iresourcecontroller import plugin
class TestExampleIResourceController(object):
'''Tests for the plugin that uses IResourceController.
'''
def setup(self):
# Set up the test app
self.app = helpers._get_test_app()
def teardown(self):
# Unload the plugin
ckan.plugins.unload('example_iresourcecontroller')
model.repo.rebuild_db()
def test_resource_controller_plugin_create(self):
user = factories.Sysadmin()
package = factories.Dataset(user=user)
# Set up the plugin
ckan.plugins.load('example_iresourcecontroller')
plugin = ckan.plugins.get_plugin('example_iresourcecontroller')
res = helpers.call_action('resource_create',
package_id=package['id'],
name='test-resource',
url='http://resource.create/',
apikey=user['apikey'])
assert plugin.counter['before_create'] == 1, plugin.counter
assert plugin.counter['after_create'] == 1, plugin.counter
assert plugin.counter['before_update'] == 0, plugin.counter
assert plugin.counter['after_update'] == 0, plugin.counter
assert plugin.counter['before_delete'] == 0, plugin.counter
assert plugin.counter['after_delete'] == 0, plugin.counter
def test_resource_controller_plugin_update(self):
user = factories.Sysadmin()
resource = factories.Resource(user=user)
# Set up the plugin here because we don't want the resource creation
# to affect it (because we will only check for changes to update)
ckan.plugins.load('example_iresourcecontroller')
plugin = ckan.plugins.get_plugin('example_iresourcecontroller')
res = helpers.call_action('resource_update',
id=resource['id'],
url='http://resource.updated/',
apikey=user['apikey'])
assert plugin.counter['before_create'] == 0, plugin.counter
assert plugin.counter['after_create'] == 0, plugin.counter
assert plugin.counter['before_update'] == 1, plugin.counter
assert plugin.counter['after_update'] == 1, plugin.counter
assert plugin.counter['before_delete'] == 0, plugin.counter
assert plugin.counter['after_delete'] == 0, plugin.counter
def test_resource_controller_plugin_delete(self):
user = factories.Sysadmin()
resource = factories.Resource(user=user)
# Set up the plugin here because we don't want the resource creation
# to affect it (because we will only check for changes to delete)
ckan.plugins.load('example_iresourcecontroller')
plugin = ckan.plugins.get_plugin('example_iresourcecontroller')
res = helpers.call_action('resource_delete',
id=resource['id'],
apikey=user['apikey'])
assert plugin.counter['before_create'] == 0, plugin.counter
assert plugin.counter['after_create'] == 0, plugin.counter
assert plugin.counter['before_update'] == 0, plugin.counter
assert plugin.counter['after_update'] == 0, plugin.counter
assert plugin.counter['before_delete'] == 1, plugin.counter
assert plugin.counter['after_delete'] == 1, plugin.counter
def test_resource_controller_plugin_delete_via_dataset_delete(self):
user = factories.Sysadmin()
dataset = factories.Dataset(user=user)
factories.Resource(user=user, package_id=dataset['id'])
factories.Resource(user=user, package_id=dataset['id'])
ckan.plugins.load('example_iresourcecontroller')
plugin = ckan.plugins.get_plugin('example_iresourcecontroller')
# Deleting the package deletes its resources too
res = helpers.call_action('package_delete',
id=dataset['id'],
apikey=user['apikey'])
assert plugin.counter['before_create'] == 0, plugin.counter
assert plugin.counter['after_create'] == 0, plugin.counter
assert plugin.counter['before_update'] == 0, plugin.counter
assert plugin.counter['after_update'] == 0, plugin.counter
assert plugin.counter['before_delete'] == 2, plugin.counter
assert plugin.counter['after_delete'] == 2, plugin.counter
def test_resource_controller_plugin_show(self):
"""
Before show gets called by the other methods but we test it
separately here and make sure that it doesn't call the other
methods.
"""
user = factories.Sysadmin()
package = factories.Dataset(user=user)
resource = factories.Resource(user=user, package_id=package['id'])
# Set up the plugin here because we don't want the resource creation
# to affect it (because we will only check for changes to delete)
ckan.plugins.load('example_iresourcecontroller')
plugin = ckan.plugins.get_plugin('example_iresourcecontroller')
res = helpers.call_action('package_show',
name_or_id=package['id'])
assert plugin.counter['before_create'] == 0, plugin.counter
assert plugin.counter['after_create'] == 0, plugin.counter
assert plugin.counter['before_update'] == 0, plugin.counter
assert plugin.counter['after_update'] == 0, plugin.counter
assert plugin.counter['before_delete'] == 0, plugin.counter
assert plugin.counter['after_delete'] == 0, plugin.counter
assert plugin.counter['before_show'] == 1, plugin.counter