diff options
author | Matthias Baumgartner <dev@igsor.net> | 2023-04-17 18:47:58 +0200 |
---|---|---|
committer | Matthias Baumgartner <dev@igsor.net> | 2023-04-17 18:47:58 +0200 |
commit | be6027859c815e18b08a49ca1a45df3fc0aac301 (patch) | |
tree | e978249655fcab58f9ee1479c268ca8b06af7e8d /test | |
parent | af81318ae9311fd0b0e16949cef3cfaf7996970b (diff) | |
parent | aefd0cb4fa1a949beabc51e88a5c46843043a439 (diff) | |
download | bsie-be6027859c815e18b08a49ca1a45df3fc0aac301.tar.gz bsie-be6027859c815e18b08a49ca1a45df3fc0aac301.tar.bz2 bsie-be6027859c815e18b08a49ca1a45df3fc0aac301.zip |
Merge branch 'mb/iptc' into develop
Diffstat (limited to 'test')
-rw-r--r-- | test/extractor/image/test_iptc.py | 69 | ||||
-rw-r--r-- | test/lib/test_naming_policy.py | 41 | ||||
-rw-r--r-- | test/reader/test_exif.py | 22 | ||||
-rw-r--r-- | test/reader/testimage_exif.jpg | bin | 719 -> 777 bytes | |||
-rw-r--r-- | test/utils/test_filewalker.py | 125 |
5 files changed, 249 insertions, 8 deletions
diff --git a/test/extractor/image/test_iptc.py b/test/extractor/image/test_iptc.py new file mode 100644 index 0000000..5fa763d --- /dev/null +++ b/test/extractor/image/test_iptc.py @@ -0,0 +1,69 @@ + +# standard imports +import unittest + +# bsie imports +from bsie.extractor import base +from bsie.utils import bsfs, node as _node, ns + +# objects to test +from bsie.extractor.image.iptc import Iptc + + +## code ## + +class TestIptc(unittest.TestCase): + + def test_eq(self): + # identical instances are equal + self.assertEqual(Iptc(), Iptc()) + self.assertEqual(hash(Iptc()), hash(Iptc())) + # comparison respects type + class Foo(): pass + self.assertNotEqual(Iptc(), Foo()) + self.assertNotEqual(hash(Iptc()), hash(Foo())) + self.assertNotEqual(Iptc(), 1234) + self.assertNotEqual(hash(Iptc()), hash(1234)) + self.assertNotEqual(Iptc(), None) + self.assertNotEqual(hash(Iptc()), hash(None)) + + def test_schema(self): + self.assertSetEqual({pred.uri for pred in Iptc().schema.predicates()}, { + ns.bsfs.Predicate, + ns.bse.tag, + ns.bst.label, + }) + + def test_extract(self): + ext = Iptc() + node = _node.Node(ns.bsfs.File, '') # Blank node + content = { + 'Iptc.Application2.Keywords': ['hello', 'world'], + 'Iptc.Application2.RecordVersion': '4', + } + # target tags + t_hello = _node.Node(ns.bsn.Tag, label='hello') + t_world = _node.Node(ns.bsn.Tag, label='world') + + # invalid principals are ignored + self.assertSetEqual(set(ext.extract(node, content, {ns.bse.filename})), set()) + # extract finds all relevant information + self.assertSetEqual(set(ext.extract(node, content, {ext.schema.predicate(ns.bse.tag)})), { + (node, ext.schema.predicate(ns.bse.tag), t_hello), + (node, ext.schema.predicate(ns.bse.tag), t_world), + (t_hello, ext.schema.predicate(ns.bst.label), 'hello'), + (t_world, ext.schema.predicate(ns.bst.label), 'world'), + }) + + # empty content is acceptable + self.assertSetEqual(set(ext.extract(node, {}, set(ext.principals))), set()) + # no principals is acceptable + self.assertSetEqual(set(ext.extract(node, content, set())), set()) + + +## main ## + +if __name__ == '__main__': + unittest.main() + +## EOF ## diff --git a/test/lib/test_naming_policy.py b/test/lib/test_naming_policy.py index c9b0cd2..09fd6f6 100644 --- a/test/lib/test_naming_policy.py +++ b/test/lib/test_naming_policy.py @@ -23,27 +23,31 @@ class TestDefaultNamingPolicy(unittest.TestCase): self.assertEqual(policy.handle_node( Node(ns.bsn.Invalid, uri='http://example.com/you/foo#bar')).uri, URI('http://example.com/you/foo#bar')) - # processes bsfs:File + # processes bsn:Entity self.assertEqual(policy.handle_node( Node(ns.bsn.Entity, ucid='abc123cba')).uri, URI('http://example.com/me/file#abc123cba')) - # processes bsfs:Preview + # processes bsn:Preview self.assertEqual(policy.handle_node( Node(ns.bsn.Preview, ucid='abc123cba', size=123)).uri, URI('http://example.com/me/preview#abc123cba_s123')) + # processes bsn:Tag + self.assertEqual(policy.handle_node( + Node(ns.bsn.Tag, label='hello')).uri, + URI('http://example.com/me/tag#hello')) # raises an exception on unknown types self.assertRaises(errors.ProgrammingError, policy.handle_node, Node(ns.bsn.Invalid, ucid='abc123cba', size=123)) - def test_name_file(self): + def test_name_entity(self): # setup policy = DefaultNamingPolicy('http://example.com', 'me') - # name_file uses ucid - self.assertEqual(policy.name_file( + # name_entity uses ucid + self.assertEqual(policy.name_entity( Node(ns.bsn.Entity, ucid='123abc321')).uri, URI('http://example.com/me/file#123abc321')) - # name_file falls back to a random guid - self.assertTrue(policy.name_file( + # name_entity falls back to a random guid + self.assertTrue(policy.name_entity( Node(ns.bsn.Entity)).uri.startswith('http://example.com/me/file#')) def test_name_preview(self): @@ -71,6 +75,29 @@ class TestDefaultNamingPolicy(unittest.TestCase): self.assertTrue(policy.name_preview( Node(ns.bsn.Preview, size=200)).uri.endswith('_s200')) + def test_name_tag(self): + # setup + policy = DefaultNamingPolicy('http://example.com', 'me') + # name_tag uses label + self.assertEqual(policy.name_tag( + Node(ns.bsn.Tag, label='hello')).uri, + URI('http://example.com/me/tag#hello')) + # name_tag matches the label + self.assertEqual( + policy.name_tag(Node(ns.bsn.Tag, label='world')), + policy.name_tag(Node(ns.bsn.Tag, label='world')), + ) + self.assertNotEqual( + policy.name_tag(Node(ns.bsn.Tag, label='hello')), + policy.name_tag(Node(ns.bsn.Tag, label='world')), + ) + # label can include characters that are not valid for an uri + self.assertEqual(policy.name_tag( + Node(ns.bsn.Preview, label='hello world { foo bar ] ')).uri, + URI('http://example.com/me/tag#hello%20world%20%7B%20foo%20bar%20%5D%20')) + # name_tag falls back to a random guid + self.assertTrue(policy.name_tag( + Node(ns.bsn.Tag,)).uri.startswith('http://example.com/me/tag#')) class TestNamingPolicyIterator(unittest.TestCase): diff --git a/test/reader/test_exif.py b/test/reader/test_exif.py index de6e801..1767f12 100644 --- a/test/reader/test_exif.py +++ b/test/reader/test_exif.py @@ -10,7 +10,7 @@ import pyexiv2 from bsie.utils import errors # objects to test -from bsie.reader.exif import Exif +from bsie.reader.exif import Exif, Iptc ## code ## @@ -44,6 +44,26 @@ class TestExif(unittest.TestCase): }) +class TestIptc(unittest.TestCase): + def test_call(self): + rdr = Iptc() + # discards non-image files + self.assertRaises(errors.UnsupportedFileFormatError, rdr, + os.path.join(os.path.dirname(__file__), 'invalid.doc')) + # raises on invalid image files + self.assertRaises(errors.UnsupportedFileFormatError, rdr, + os.path.join(os.path.dirname(__file__), 'invalid.jpg')) + # raises on invalid image files + pyexiv2.set_log_level(3) # suppress log message + self.assertRaises(errors.ReaderError, rdr, + os.path.join(os.path.dirname(__file__), 'testimage_exif_corrupted.jpg')) + # returns dict with exif info + self.assertDictEqual(rdr(os.path.join(os.path.dirname(__file__), 'testimage_exif.jpg')), { + 'Iptc.Application2.Keywords': ['hello', 'world'], + 'Iptc.Application2.RecordVersion': '4', + }) + + ## main ## if __name__ == '__main__': diff --git a/test/reader/testimage_exif.jpg b/test/reader/testimage_exif.jpg Binary files differindex a774bc2..bc331ac 100644 --- a/test/reader/testimage_exif.jpg +++ b/test/reader/testimage_exif.jpg diff --git a/test/utils/test_filewalker.py b/test/utils/test_filewalker.py new file mode 100644 index 0000000..4aaba65 --- /dev/null +++ b/test/utils/test_filewalker.py @@ -0,0 +1,125 @@ + +# standard imports +import os +import shutil +import tempfile +import unittest + +# objects to test +from bsie.utils.filewalker import list_files + + +## code ## + +def touch(path, text='<test content>'): + # create folders + os.makedirs(os.path.dirname(path), exist_ok=True) + # create file + with open(path, 'wt') as ofile: + ofile.write(text) + +class TestListFiles(unittest.TestCase): + def setUp(self): + # set up directory structure + # <root> + # - zero* + # - foo + # - hello* + # - remote -> foobar/xyz + # - bar + # - world* + # - xyz* + # - bar + # - fst + # - abc* + # - zyx* + # - snd + # - cba* + # - xyz* + # - foobar + # - xyz* + # - hello* + # - world -> bar/snd + self.testdir = tempfile.mkdtemp(prefix='bsie-test-') + touch(os.path.join(self.testdir, 'zero')) + touch(os.path.join(self.testdir, 'foo', 'hello')) + touch(os.path.join(self.testdir, 'foo', 'bar', 'world')) + touch(os.path.join(self.testdir, 'foo', 'bar', 'xyz')) + touch(os.path.join(self.testdir, 'bar', 'fst', 'abc')) + touch(os.path.join(self.testdir, 'bar', 'fst', 'zyx')) + touch(os.path.join(self.testdir, 'bar', 'snd', 'cba')) + touch(os.path.join(self.testdir, 'bar', 'snd', 'xyz')) + touch(os.path.join(self.testdir, 'foobar', 'xyz')) + touch(os.path.join(self.testdir, 'foobar', 'hello')) + os.symlink( + os.path.join(self.testdir, 'bar', 'snd'), + os.path.join(self.testdir, 'foobar', 'world')) + os.symlink( + os.path.join(self.testdir, 'foobar', 'xyz'), + os.path.join(self.testdir, 'foo', 'remote')) + + def tearDown(self): + # remove testing dirs + shutil.rmtree(self.testdir, ignore_errors=True) + + def test_list_files(self): + # list_files lists all files beneath root + roots = [ + os.path.join(self.testdir, 'foo'), + os.path.join(self.testdir, 'bar'), + os.path.join(self.testdir, 'foobar'), + os.path.join(self.testdir, 'zero'), + ] + self.assertSetEqual(set(list_files(roots, recursive=True, follow_symlinks=True)), { + os.path.join(self.testdir, 'bar', 'fst', 'abc'), + os.path.join(self.testdir, 'bar', 'fst', 'zyx'), + os.path.join(self.testdir, 'bar', 'snd', 'cba'), + os.path.join(self.testdir, 'bar', 'snd', 'xyz'), + os.path.join(self.testdir, 'foo', 'bar', 'world'), + os.path.join(self.testdir, 'foo', 'bar', 'xyz'), + os.path.join(self.testdir, 'foo', 'hello'), + os.path.join(self.testdir, 'foo', 'remote'), + os.path.join(self.testdir, 'foobar', 'hello'), + os.path.join(self.testdir, 'foobar', 'world', 'cba'), + os.path.join(self.testdir, 'foobar', 'world', 'xyz'), + os.path.join(self.testdir, 'foobar', 'xyz'), + os.path.join(self.testdir, 'zero'), + }) + + # list_files lists respects root + self.assertSetEqual(set(list_files( + roots=[os.path.join(self.testdir, 'foo')], recursive=True, follow_symlinks=True)), { + os.path.join(self.testdir, 'foo', 'bar', 'world'), + os.path.join(self.testdir, 'foo', 'bar', 'xyz'), + os.path.join(self.testdir, 'foo', 'hello'), + os.path.join(self.testdir, 'foo', 'remote'), + }) + + # list_files lists respects recursive flag (lists only files in root!) + self.assertSetEqual(set(list_files(roots, recursive=False, follow_symlinks=True)), { + os.path.join(self.testdir, 'zero'), + }) + + # list_files lists respects symlink flag + # lists symlinked files but does not dive into symlinked folders + self.assertSetEqual(set(list_files(roots, recursive=True, follow_symlinks=False)), { + os.path.join(self.testdir, 'bar', 'fst', 'abc'), + os.path.join(self.testdir, 'bar', 'fst', 'zyx'), + os.path.join(self.testdir, 'bar', 'snd', 'cba'), + os.path.join(self.testdir, 'bar', 'snd', 'xyz'), + os.path.join(self.testdir, 'foo', 'bar', 'world'), + os.path.join(self.testdir, 'foo', 'bar', 'xyz'), + os.path.join(self.testdir, 'foo', 'hello'), + os.path.join(self.testdir, 'foo', 'remote'), + os.path.join(self.testdir, 'foobar', 'hello'), + os.path.join(self.testdir, 'foobar', 'xyz'), + os.path.join(self.testdir, 'zero'), + }) + + +## main ## + +if __name__ == '__main__': + unittest.main() + +## EOF ## |