mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-08 10:44:09 -04:00
Improved tests for link maps. Includes a fix for a problem the test found.
This commit is contained in:
parent
d95d7f4048
commit
26d88d1d65
@ -2405,6 +2405,9 @@ class Cache:
|
|||||||
table = self.fields[field].table
|
table = self.fields[field].table
|
||||||
if not hasattr(table, 'link_map'):
|
if not hasattr(table, 'link_map'):
|
||||||
raise ValueError(f"Lookup name {field} doesn't have a link map")
|
raise ValueError(f"Lookup name {field} doesn't have a link map")
|
||||||
|
# Clear the links for book cache as we don't know what will be affected
|
||||||
|
self.link_maps_cache = {}
|
||||||
|
|
||||||
fids = {k: self.get_item_id(field, k) for k in value_to_link_map.keys()}
|
fids = {k: self.get_item_id(field, k) for k in value_to_link_map.keys()}
|
||||||
id_to_link_map = {fid:value_to_link_map[k] for k, fid in fids.items() if fid is not None}
|
id_to_link_map = {fid:value_to_link_map[k] for k, fid in fids.items() if fid is not None}
|
||||||
result_map = table.set_links(id_to_link_map, self.backend)
|
result_map = table.set_links(id_to_link_map, self.backend)
|
||||||
|
@ -922,11 +922,13 @@ class WritingTest(BaseTest):
|
|||||||
def test_link_maps(self):
|
def test_link_maps(self):
|
||||||
cache = self.init_cache()
|
cache = self.init_cache()
|
||||||
|
|
||||||
|
# Add two tags
|
||||||
cache.set_field('tags', {1:'foo'})
|
cache.set_field('tags', {1:'foo'})
|
||||||
self.assertEqual(('foo',), cache.field_for('tags', 1), 'Setting tag foo failed')
|
self.assertEqual(('foo',), cache.field_for('tags', 1), 'Setting tag foo failed')
|
||||||
cache.set_field('tags', {1:'foo, bar'})
|
cache.set_field('tags', {1:'foo, bar'})
|
||||||
self.assertEqual(('foo', 'bar'), cache.field_for('tags', 1), 'Adding second tag failed')
|
self.assertEqual(('foo', 'bar'), cache.field_for('tags', 1), 'Adding second tag failed')
|
||||||
|
|
||||||
|
# Check adding a link
|
||||||
links = cache.get_link_map('tags')
|
links = cache.get_link_map('tags')
|
||||||
self.assertDictEqual(links, {}, 'Initial tags link dict is not empty')
|
self.assertDictEqual(links, {}, 'Initial tags link dict is not empty')
|
||||||
links['foo'] = 'url'
|
links['foo'] = 'url'
|
||||||
@ -934,13 +936,26 @@ class WritingTest(BaseTest):
|
|||||||
links2 = cache.get_link_map('tags')
|
links2 = cache.get_link_map('tags')
|
||||||
self.assertDictEqual(links2, links, 'tags link dict mismatch')
|
self.assertDictEqual(links2, links, 'tags link dict mismatch')
|
||||||
|
|
||||||
|
# Check getting links for a book and that links are correct
|
||||||
cache.set_field('publisher', {1:'random'})
|
cache.set_field('publisher', {1:'random'})
|
||||||
cache.set_link_map('publisher', {'random': 'url2'})
|
cache.set_link_map('publisher', {'random': 'url2'})
|
||||||
|
|
||||||
links = cache.get_all_link_maps_for_book(1)
|
links = cache.get_all_link_maps_for_book(1)
|
||||||
self.assert_('foo' in links['tags'], 'foo not there')
|
self.assertSetEqual({v for v in links.keys()}, {'tags', 'publisher'}, 'Wrong link keys')
|
||||||
self.assert_('bar' not in links['tags'], 'bar is there')
|
self.assertSetEqual({v for v in links['tags'].keys()}, {'foo', }, 'Should be "foo"')
|
||||||
self.assert_('random' in links['publisher'], 'random is not there')
|
self.assertSetEqual({v for v in links['publisher'].keys()}, {'random', }, 'Should be "random"')
|
||||||
self.assertSetEqual({'tags', 'publisher'}, set(links.keys()), 'Link map has extra stuff')
|
self.assertEqual('url', links['tags']['foo'], 'link for tag foo is wrong')
|
||||||
|
self.assertEqual('url2', links['publisher']['random'], 'link for publisher random is wrong')
|
||||||
|
|
||||||
|
# Now test deleting the links.
|
||||||
|
links = cache.get_link_map('tags')
|
||||||
|
to_del = {l:'' for l in links.keys()}
|
||||||
|
cache.set_link_map('tags', to_del)
|
||||||
|
self.assertEqual({}, cache.get_link_map('tags'), 'links on tags were not deleted')
|
||||||
|
links = cache.get_link_map('publisher')
|
||||||
|
to_del = {l:'' for l in links.keys()}
|
||||||
|
cache.set_link_map('publisher', to_del)
|
||||||
|
self.assertEqual({}, cache.get_link_map('publisher'), 'links on publisher were not deleted')
|
||||||
|
self.assertEqual({}, cache.get_all_link_maps_for_book(1), 'Not all links for book were deleted')
|
||||||
|
|
||||||
|
|
||||||
# }}}
|
# }}}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user