Drop lots of legacy code. Support Python 3.7-3.11.
[librarian.git] / tests / test_picture.py
1 # This file is part of Librarian, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Wolne Lektury. See NOTICE for more information.
3 #
4 from os import path
5 import unittest
6 from librarian import picture, dcparser
7 from tests.utils import get_all_fixtures, get_fixture
8
9
10 class PictureTests(unittest.TestCase):
11     def test_wlpictureuri(self):
12         uri = picture.WLPictureURI('http://wolnelektury.pl/katalog/obraz/angelus-novus')
13
14     def check_load(self, xml_file):
15         pi = dcparser.parse(xml_file, picture.PictureInfo)
16         self.assertIsNotNone(pi)
17         self.assertIsInstance(pi, picture.PictureInfo)
18     
19     def test_load(self):
20         for fixture in get_all_fixtures('picture', '*.xml'):
21             with self.subTest(fixture=fixture):
22                 self.check_load(fixture)
23
24     def test_wlpicture(self):
25         with open(get_fixture('picture', 'angelus-novus.xml')) as f:
26             wlp = picture.WLPicture.from_file(f)
27         pi = wlp.picture_info
28
29         self.assertEqual(pi.type[0], "Image")
30         self.assertEqual(pi.mime_type, 'image/jpeg')
31         self.assertEqual(wlp.mime_type, 'image/jpeg')
32         self.assertEqual(wlp.slug, 'angelus-novus')
33         self.assertTrue(path.exists(wlp.image_path))
34     
35         f = wlp.image_file()
36         f.close()
37
38
39     def test_picture_parts(self):
40         with open(get_fixture('picture', 'angelus-novus.xml')) as f:
41             wlp = picture.WLPicture.from_file(f)
42         parts = list(wlp.partiter())
43         expect_parts = 4
44         self.assertEqual(len(parts), expect_parts, "there should be %d parts of the picture" % expect_parts)
45         motifs = set()
46         names = set()
47
48         for p in parts:
49             for m in p['themes']:
50                 motifs.add(m)
51         for p in parts:
52             if p['object']:
53                 names.add(p['object'])
54
55         self.assertEqual(motifs, {'anioł historii', 'spojrzenie'}, "missing motifs, got: %s" % motifs)
56         self.assertEqual(names, {'obraz cały', 'skrzydło'}, 'missing objects, got: %s' % names)