Coverage for ckanext/udc/tests/test_plugin.py: 100%

46 statements  

« prev     ^ index     » next       coverage.py v7.7.1, created at 2026-01-19 23:48 +0000

1""" 

2Tests for plugin.py. 

3 

4Tests are written using the pytest library (https://docs.pytest.org), and you 

5should read the testing guidelines in the CKAN docs: 

6https://docs.ckan.org/en/2.9/contributing/testing.html 

7 

8To write tests for your extension you should install the pytest-ckan package: 

9 

10 pip install pytest-ckan 

11 

12This will allow you to use CKAN specific fixtures on your tests. 

13 

14For instance, if your test involves database access you can use `clean_db` to 

15reset the database: 

16 

17 import pytest 

18 

19 from ckan.tests import factories 

20 

21 @pytest.mark.usefixtures("clean_db") 

22 def test_some_action(): 

23 

24 dataset = factories.Dataset() 

25 

26 # ... 

27 

28For functional tests that involve requests to the application, you can use the 

29`app` fixture: 

30 

31 from ckan.plugins import toolkit 

32 

33 def test_some_endpoint(app): 

34 

35 url = toolkit.url_for('myblueprint.some_endpoint') 

36 

37 response = app.get(url) 

38 

39 assert response.status_code == 200 

40 

41 

42To temporary patch the CKAN configuration for the duration of a test you can use: 

43 

44 import pytest 

45 

46 @pytest.mark.ckan_config("ckanext.myext.some_key", "some_value") 

47 def test_some_action(): 

48 pass 

49""" 

50import pytest 

51 

52import ckan.plugins.toolkit as tk 

53 

54import ckanext.udc.plugin as plugin 

55from ckanext.udc.i18n import ( 

56 udc_json_load, 

57 udc_lang_object, 

58 udc_json_dump, 

59) 

60 

61 

62@pytest.fixture() 

63def udc_plugin(): 

64 instance = plugin.UdcPlugin() 

65 # Force a clean slate for each test since the plugin stores state on the instance 

66 instance.disable_graphdb = True 

67 instance.sparql_client = None 

68 instance.all_fields = [] 

69 instance.facet_titles = {} 

70 instance.facet_titles_raw = {} 

71 instance.text_fields = [] 

72 instance.date_fields = [] 

73 instance.multiple_select_fields = [] 

74 instance.dropdown_options = {} 

75 instance.maturity_model = [] 

76 instance.mappings = {} 

77 instance.preload_ontologies = {} 

78 return instance 

79 

80 

81def _sample_config(): 

82 return { 

83 "maturity_model": [ 

84 { 

85 "title": "Level 1", 

86 "name": "lvl1", 

87 "fields": [ 

88 {"name": "text_field", "label": "Text Field", "type": "text"}, 

89 {"name": "date_field", "label": "Date Field", "type": "date"}, 

90 { 

91 "name": "multi_field", 

92 "label": "Multi Field", 

93 "type": "multiple_select", 

94 "options": [ 

95 {"value": "opt-a", "text": "Option A"}, 

96 {"value": "opt-b", "text": "Option B"}, 

97 ], 

98 }, 

99 { 

100 "name": "single_field", 

101 "label": "Single Field", 

102 "type": "single_select", 

103 "options": [ 

104 {"value": "one", "text": "One"}, 

105 ], 

106 }, 

107 ], 

108 } 

109 ], 

110 "mappings": {"foo": "bar"}, 

111 "preload_ontologies": {"example": "value"}, 

112 } 

113 

114 

115def test_reload_config_populates_field_metadata(udc_plugin): 

116 udc_plugin.reload_config(_sample_config()) 

117 

118 assert udc_plugin.all_fields == [ 

119 "text_field", 

120 "date_field", 

121 "multi_field", 

122 "single_field", 

123 ] 

124 assert udc_plugin.text_fields == ["text_field"] 

125 assert udc_plugin.date_fields == ["date_field"] 

126 assert udc_plugin.multiple_select_fields == ["multi_field"] 

127 assert udc_plugin.dropdown_options["multi_field"] == { 

128 "opt-a": "Option A", 

129 "opt-b": "Option B", 

130 } 

131 assert udc_plugin.facet_titles["single_field"] == "Single Field" 

132 assert udc_plugin.mappings == {"foo": "bar"} 

133 assert udc_plugin.preload_ontologies == {"example": "value"} 

134 

135 

136def test_modify_package_schema_applies_expected_validators(udc_plugin): 

137 udc_plugin.reload_config(_sample_config()) 

138 

139 base_schema = {} 

140 schema = udc_plugin._modify_package_schema(base_schema) 

141 

142 ignore_missing = tk.get_validator("ignore_missing") 

143 convert_to_extras = tk.get_converter("convert_to_extras") 

144 

145 text_pipeline = schema["text_field"] 

146 assert text_pipeline[0] == ignore_missing 

147 # text fields should round-trip JSON with multilingual helpers 

148 assert udc_json_load in text_pipeline 

149 assert udc_lang_object in text_pipeline 

150 assert udc_json_dump in text_pipeline 

151 assert text_pipeline[-1] == convert_to_extras 

152 

153 number_pipeline = schema["date_field"] 

154 assert number_pipeline == [ignore_missing, convert_to_extras]