0001-data_source-dont-ignore-list-items-without-text-1079.patch
tests/test_datasource.py | ||
---|---|---|
152 | 152 |
assert data_sources.get_structured_items(datasource) == [ |
153 | 153 |
{'id': 1, 'text': 'foo'}, {'id': 2, 'text': 'bar'}] |
154 | 154 | |
155 |
# a json file with empty or no text values |
|
156 |
json_file = open(json_file_path, 'w') |
|
157 |
json.dump({'data': [{'id': '1', 'text': ''}, {'id': '2'}]}, json_file) |
|
158 |
json_file.close() |
|
159 |
assert data_sources.get_items(datasource) == [('1', '', '1'), ('2', '2', '2')] |
|
160 |
assert data_sources.get_structured_items(datasource) == [ |
|
161 |
{'id': '1', 'text': ''}, |
|
162 |
{'id': '2', 'text': '2'}] |
|
163 | ||
164 |
# a json file with empty or no id |
|
165 |
json_file = open(json_file_path, 'w') |
|
166 |
json.dump({'data': [{'id': '', 'text': 'foo'}, {'text': 'bar'}, {'id': None}]}, json_file) |
|
167 |
json_file.close() |
|
168 |
assert data_sources.get_items(datasource) == [] |
|
169 |
assert data_sources.get_structured_items(datasource) == [] |
|
170 | ||
171 | ||
155 | 172 |
def test_item_field_named_python_datasource(): |
156 | 173 |
NamedDataSource.wipe() |
157 | 174 |
data_source = NamedDataSource(name='foobar') |
wcs/data_sources.py | ||
---|---|---|
88 | 88 |
structured_items = get_structured_items(data_source) |
89 | 89 |
tupled_items = [] |
90 | 90 |
for item in structured_items: |
91 |
# skip malformed items |
|
92 |
if not item.get('id') or not item.get('text'): |
|
93 |
continue |
|
94 | 91 |
tupled_items.append((str(item['id']), str(item['text']), str(item.get('key', item['id'])))) |
95 | 92 |
return tupled_items |
96 | 93 | |
... | ... | |
147 | 144 |
raise ValueError('not a json dict') |
148 | 145 |
if type(entries.get('data')) is not list: |
149 | 146 |
raise ValueError('not a json dict with a data list attribute') |
150 |
return entries.get('data') |
|
147 |
items = [] |
|
148 |
for item in entries.get('data'): |
|
149 |
# skip malformed items |
|
150 |
if not item.get('id'): |
|
151 |
continue |
|
152 |
if 'text' not in item: |
|
153 |
item['text'] = item['id'] |
|
154 |
items.append(item) |
|
155 |
return items |
|
151 | 156 |
except urllib2.HTTPError as e: |
152 | 157 |
get_logger().warn('Error loading JSON data source (%s)' % str(e)) |
153 | 158 |
except urllib2.URLError as e: |
154 |
- |