diff options
Diffstat (limited to 'test/extractor')
-rw-r--r-- | test/extractor/generic/test_constant.py | 10 | ||||
-rw-r--r-- | test/extractor/generic/test_path.py | 14 | ||||
-rw-r--r-- | test/extractor/generic/test_stat.py | 14 | ||||
-rw-r--r-- | test/extractor/image/__init__.py | 0 | ||||
-rw-r--r-- | test/extractor/image/test_colors_spatial.py | 100 | ||||
-rw-r--r-- | test/extractor/image/testimage.jpg | bin | 0 -> 349264 bytes | |||
-rw-r--r-- | test/extractor/test_base.py | 70 | ||||
-rw-r--r-- | test/extractor/test_builder.py | 103 |
8 files changed, 292 insertions, 19 deletions
diff --git a/test/extractor/generic/test_constant.py b/test/extractor/generic/test_constant.py index 9dbaced..bde3805 100644 --- a/test/extractor/generic/test_constant.py +++ b/test/extractor/generic/test_constant.py @@ -4,7 +4,7 @@ Part of the bsie test suite. A copy of the license is provided with the project. Author: Matthias Baumgartner, 2022 """ -# imports +# standard imports import unittest # bsie imports @@ -36,19 +36,19 @@ class TestConstant(unittest.TestCase): node = _node.Node(ns.bsfs.Entity, '') # Blank node p_author = ext.schema.predicate(ns.bse.author) p_comment = ext.schema.predicate(ns.bse.comment) - entity = ext.schema.node(ns.bsfs.Node).get_child(ns.bsfs.Entity) - string = ext.schema.literal(ns.bsfs.Literal).get_child(ns.xsd.string) + entity = ext.schema.node(ns.bsfs.Node).child(ns.bsfs.Entity) + string = ext.schema.literal(ns.bsfs.Literal).child(ns.xsd.string) # baseline self.assertSetEqual(set(ext.extract(node, None, (p_author, p_comment))), {(node, p_author, 'Me, myself, and I'), (node, p_comment, 'the quick brown fox jumps over the lazy dog.')}) # predicates is respected - p_foobar = ext.schema.predicate(ns.bsfs.Predicate).get_child(ns.bse.foobar, domain=entity, range=entity) + p_foobar = ext.schema.predicate(ns.bsfs.Predicate).child(ns.bse.foobar, domain=entity, range=entity) self.assertSetEqual(set(ext.extract(node, None, (p_author, p_foobar))), {(node, p_author, 'Me, myself, and I')}) self.assertSetEqual(set(ext.extract(node, None, (p_comment, p_foobar))), {(node, p_comment, 'the quick brown fox jumps over the lazy dog.')}) - p_barfoo = ext.schema.predicate(ns.bse.author).get_child(ns.bse.comment, domain=entity, range=string) + p_barfoo = ext.schema.predicate(ns.bse.author).child(ns.bse.comment, domain=entity, range=string) self.assertSetEqual(set(ext.extract(node, None, (p_foobar, p_barfoo))), set()) def test_construct(self): diff --git a/test/extractor/generic/test_path.py b/test/extractor/generic/test_path.py index 820f402..ae68686 100644 --- a/test/extractor/generic/test_path.py +++ b/test/extractor/generic/test_path.py @@ -4,11 +4,11 @@ Part of the bsie test suite. A copy of the license is provided with the project. Author: Matthias Baumgartner, 2022 """ -# imports +# standard imports import unittest # bsie imports -from bsie.base import extractor +from bsie.extractor import base from bsie.utils import bsfs, node as _node, ns # objects to test @@ -29,7 +29,7 @@ class TestPath(unittest.TestCase): def test_schema(self): self.assertEqual(Path().schema, - bsfs.schema.Schema.from_string(extractor.SCHEMA_PREAMBLE + ''' + bsfs.schema.from_string(base.SCHEMA_PREAMBLE + ''' bse:filename rdfs:subClassOf bsfs:Predicate ; rdfs:domain bsfs:File ; rdfs:range xsd:string ; @@ -41,19 +41,19 @@ class TestPath(unittest.TestCase): node = _node.Node(ns.bsfs.File, '') # Blank node content = '/tmp/foo/bar' p_filename = ext.schema.predicate(ns.bse.filename) - entity = ext.schema.node(ns.bsfs.Node).get_child(ns.bsfs.Entity) - string = ext.schema.literal(ns.bsfs.Literal).get_child(ns.xsd.string) + entity = ext.schema.node(ns.bsfs.Node).child(ns.bsfs.Entity) + string = ext.schema.literal(ns.bsfs.Literal).child(ns.xsd.string) # baseline self.assertSetEqual(set(ext.extract(node, content, (p_filename, ))), {(node, p_filename, 'bar')}) # predicates parameter is respected - p_foo = ext.schema.predicate(ns.bsfs.Predicate).get_child(ns.bse.foo, domain=entity, range=string) # unsupported predicate + p_foo = ext.schema.predicate(ns.bsfs.Predicate).child(ns.bse.foo, domain=entity, range=string) # unsupported predicate self.assertSetEqual(set(ext.extract(node, content, (p_filename, p_foo))), {(node, p_filename, 'bar')}) self.assertSetEqual(set(ext.extract(node, content, (p_foo, ))), set()) # predicates are validated - p_bar = p_foo.get_child(ns.bse.filename) # same URI but different hierarchy + p_bar = p_foo.child(ns.bse.filename) # same URI but different hierarchy self.assertSetEqual(set(ext.extract(node, content, (p_filename, p_bar))), {(node, p_filename, 'bar')}) self.assertSetEqual(set(ext.extract(node, content, (p_bar, ))), set()) diff --git a/test/extractor/generic/test_stat.py b/test/extractor/generic/test_stat.py index 3441438..e5562d1 100644 --- a/test/extractor/generic/test_stat.py +++ b/test/extractor/generic/test_stat.py @@ -4,12 +4,12 @@ Part of the bsie test suite. A copy of the license is provided with the project. Author: Matthias Baumgartner, 2022 """ -# imports +# standard imports import os import unittest # bsie imports -from bsie.base import extractor +from bsie.extractor import base from bsie.utils import bsfs, node as _node, ns # objects to test @@ -30,7 +30,7 @@ class TestStat(unittest.TestCase): def test_schema(self): self.assertEqual(Stat().schema, - bsfs.schema.Schema.from_string(extractor.SCHEMA_PREAMBLE + ''' + bsfs.schema.from_string(base.SCHEMA_PREAMBLE + ''' bse:filesize rdfs:subClassOf bsfs:Predicate ; rdfs:domain bsfs:File ; rdfs:range xsd:integer ; @@ -42,19 +42,19 @@ class TestStat(unittest.TestCase): node = _node.Node(ns.bsfs.File, '') # Blank node content = os.stat(__file__) p_filesize = ext.schema.predicate(ns.bse.filesize) - entity = ext.schema.node(ns.bsfs.Node).get_child(ns.bsfs.Entity) - string = ext.schema.literal(ns.bsfs.Literal).get_child(ns.xsd.string) + entity = ext.schema.node(ns.bsfs.Node).child(ns.bsfs.Entity) + string = ext.schema.literal(ns.bsfs.Literal).child(ns.xsd.string) # baseline self.assertSetEqual(set(ext.extract(node, content, (p_filesize, ))), {(node, p_filesize, content.st_size)}) # predicates parameter is respected - p_foo = ext.schema.predicate(ns.bsfs.Predicate).get_child(ns.bse.foo, domain=entity, range=string) # unsupported predicate + p_foo = ext.schema.predicate(ns.bsfs.Predicate).child(ns.bse.foo, domain=entity, range=string) # unsupported predicate self.assertSetEqual(set(ext.extract(node, content, (p_filesize, p_foo))), {(node, p_filesize, content.st_size)}) self.assertSetEqual(set(ext.extract(node, content, (p_foo, ))), set()) # predicates are validated - p_bar = p_foo.get_child(ns.bse.filesizse) # same URI but different hierarchy + p_bar = p_foo.child(ns.bse.filesizse) # same URI but different hierarchy self.assertSetEqual(set(ext.extract(node, content, (p_filesize, p_bar))), {(node, p_filesize, content.st_size)}) self.assertSetEqual(set(ext.extract(node, content, (p_bar, ))), set()) diff --git a/test/extractor/image/__init__.py b/test/extractor/image/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/test/extractor/image/__init__.py diff --git a/test/extractor/image/test_colors_spatial.py b/test/extractor/image/test_colors_spatial.py new file mode 100644 index 0000000..ba551f3 --- /dev/null +++ b/test/extractor/image/test_colors_spatial.py @@ -0,0 +1,100 @@ +""" + +Part of the bsie test suite. +A copy of the license is provided with the project. +Author: Matthias Baumgartner, 2022 +""" +# standard imports +import os +import unittest + +# external imports +import PIL.Image + +# bsie imports +from bsie.extractor import base +from bsie.utils import bsfs, ns, node as _node + +# objects to test +from bsie.extractor.image.colors_spatial import ColorsSpatial + + +## code ## + +class TestColorsSpatial(unittest.TestCase): + def setUp(self): + # content id with default constructors (width=32, height=32, exp=4) + self.instance_prefix = 'http://ie.bsfs.ai/schema/Feature/ColorsSpatial' + self.predicate_prefix = 'http://bsfs.ai/schema/Entity/colors_spatial' + self.uuid = 'adee8d6c43687021e1c5bffe56bcfe727f1638d792744137181304ef889dac2a' + + def test_essentials(self): + # clones are equal + self.assertEqual(ColorsSpatial(32, 32, 4), ColorsSpatial(32, 32, 4)) + self.assertEqual(hash(ColorsSpatial(32, 32, 4)), hash(ColorsSpatial(32, 32, 4))) + # equal respects type + self.assertNotEqual(ColorsSpatial(32, 32, 4), 'hello world') + self.assertNotEqual(hash(ColorsSpatial(32, 32, 4)), hash('hello world')) + # equals respects width + self.assertNotEqual(ColorsSpatial(32, 32, 4), ColorsSpatial(16, 32, 4)) + self.assertNotEqual(hash(ColorsSpatial(32, 32, 4)), hash(ColorsSpatial(16, 32, 4))) + # equals respects height + self.assertNotEqual(ColorsSpatial(32, 32, 4), ColorsSpatial(32, 16, 4)) + self.assertNotEqual(hash(ColorsSpatial(32, 32, 4)), hash(ColorsSpatial(32, 16, 4))) + # equals respects exp + self.assertNotEqual(ColorsSpatial(32, 32, 4), ColorsSpatial(32, 32, 8)) + self.assertNotEqual(hash(ColorsSpatial(32, 32, 4)), hash(ColorsSpatial(32, 32, 8))) + # string representation + self.assertEqual(str(ColorsSpatial()), 'ColorsSpatial') + self.assertEqual(repr(ColorsSpatial(64, 16, 2)), 'ColorsSpatial(64, 16, 2)') + + def test_dimension(self): + self.assertEqual(ColorsSpatial.dimension(32, 32, 4), 3 * (32*32 + 8*8 + 2*2)) + self.assertEqual(ColorsSpatial.dimension(16, 16, 8), 3 * (16*16 + 2*2)) + self.assertEqual(ColorsSpatial.dimension(64, 64, 16), 3 * (64*64 + 4*4)) + + def test_schema(self): + schema = bsfs.schema.from_string(base.SCHEMA_PREAMBLE + f''' + <{self.instance_prefix}> rdfs:subClassOf bsfs:Feature ; + # annotations + rdfs:label "Spatially dominant colors"^^xsd:string ; + schema:description "Domiant colors of subregions in an image."^^xsd:string ; + bsfs:dtype xsd:integer . + + <{self.instance_prefix}#{self.uuid}> rdfs:subClassOf <{self.instance_prefix}> ; + bsfs:dimension "3276"^^xsd:integer ; + # annotations + <{self.instance_prefix}/args#width> "32"^^xsd:integer ; + <{self.instance_prefix}/args#height> "32"^^xsd:integer ; + <{self.instance_prefix}/args#exp> "4"^^xsd:float . + + <{self.predicate_prefix}#{self.uuid}> rdfs:subClassOf bsfs:Predicate ; + rdfs:domain bsfs:File ; + rdfs:range <{self.instance_prefix}#{self.uuid}> ; + bsfs:unique "true"^^xsd:boolean . + ''') + self.assertEqual(schema, ColorsSpatial().schema) + + def test_extract(self): + ext = ColorsSpatial(2,2,2) + img = PIL.Image.open(os.path.join(os.path.dirname(__file__), 'testimage.jpg')) + node = _node.Node(ns.bsfs.Entity, bsfs.URI('http://example.com/entity#1234')) + principals = set(ext.principals) + self.assertEqual(len(principals), 1) + # valid invocation yields feature + ret = list(ext.extract(node, img, principals)) + self.assertEqual(ret[0], ( + node, + list(principals)[0], + (91, 127, 121, 94, 138, 167, 163, 134, 190, 138, 170, 156, 121, 142, 159))) + # principals is respected + self.assertListEqual(list(ext.extract(node, img, {})), []) + + + +## main ## + +if __name__ == '__main__': + unittest.main() + +## EOF ## diff --git a/test/extractor/image/testimage.jpg b/test/extractor/image/testimage.jpg Binary files differnew file mode 100644 index 0000000..c80bb48 --- /dev/null +++ b/test/extractor/image/testimage.jpg diff --git a/test/extractor/test_base.py b/test/extractor/test_base.py new file mode 100644 index 0000000..acfaf58 --- /dev/null +++ b/test/extractor/test_base.py @@ -0,0 +1,70 @@ +""" + +Part of the bsie test suite. +A copy of the license is provided with the project. +Author: Matthias Baumgartner, 2022 +""" +# standard imports +import unittest + +# bsie imports +from bsie.utils import bsfs, ns + +# objects to test +from bsie.extractor import base + + +## code ## + +class StubExtractor(base.Extractor): + def __init__(self): + super().__init__(bsfs.schema.from_string(base.SCHEMA_PREAMBLE + ''' + bse:author rdfs:subClassOf bsfs:Predicate ; + rdfs:domain bsfs:Entity ; + rdfs:range xsd:string ; + bsfs:unique "false"^^xsd:boolean . + bse:comment rdfs:subClassOf bsfs:Predicate ; + rdfs:domain bsfs:Entity ; + rdfs:range xsd:string ; + bsfs:unique "false"^^xsd:boolean . + ''')) + + def extract(self, subject, content, predicates): + raise NotImplementedError() + +class StubSub(StubExtractor): + pass + +class TestExtractor(unittest.TestCase): + def test_essentials(self): + ext = StubExtractor() + self.assertEqual(str(ext), 'StubExtractor') + self.assertEqual(repr(ext), 'StubExtractor()') + self.assertEqual(ext, StubExtractor()) + self.assertEqual(hash(ext), hash(StubExtractor())) + + sub = StubSub() + self.assertEqual(str(sub), 'StubSub') + self.assertEqual(repr(sub), 'StubSub()') + self.assertEqual(sub, StubSub()) + self.assertEqual(hash(sub), hash(StubSub())) + self.assertNotEqual(ext, sub) + self.assertNotEqual(hash(ext), hash(sub)) + + def test_principals(self): + schema = bsfs.schema.Schema() + entity = schema.node(ns.bsfs.Node).child(ns.bsfs.Entity) + string = schema.literal(ns.bsfs.Literal).child(bsfs.URI('http://www.w3.org/2001/XMLSchema#string')) + p_author = schema.predicate(ns.bsfs.Predicate).child(ns.bse.author, domain=entity, range=string) + p_comment = schema.predicate(ns.bsfs.Predicate).child(ns.bse.comment, domain=entity, range=string) + ext = StubExtractor() + self.assertSetEqual(set(ext.principals), + {p_author, p_comment} | set(schema.predicates()) - {schema.predicate(ns.bsfs.Predicate)}) + + +## main ## + +if __name__ == '__main__': + unittest.main() + +## EOF ## diff --git a/test/extractor/test_builder.py b/test/extractor/test_builder.py new file mode 100644 index 0000000..039ea53 --- /dev/null +++ b/test/extractor/test_builder.py @@ -0,0 +1,103 @@ +""" + +Part of the bsie test suite. +A copy of the license is provided with the project. +Author: Matthias Baumgartner, 2022 +""" +# standard imports +import unittest + +# bsie imports +from bsie.utils import errors + +# objects to test +from bsie.extractor import ExtractorBuilder + + +## code ## + +class TestExtractorBuilder(unittest.TestCase): + def test_iter(self): + # no specifications + self.assertListEqual(list(ExtractorBuilder([])), []) + # some specifications + builder = ExtractorBuilder([ + {'bsie.extractor.generic.path.Path': {}}, + {'bsie.extractor.generic.stat.Stat': {}}, + {'bsie.extractor.generic.path.Path': {}}, + ]) + self.assertListEqual(list(builder), [0, 1, 2]) + + def test_build(self): + # simple and repeated extractors + builder = ExtractorBuilder([ + {'bsie.extractor.generic.path.Path': {}}, + {'bsie.extractor.generic.stat.Stat': {}}, + {'bsie.extractor.generic.path.Path': {}}, + ]) + ext = [builder.build(0), builder.build(1), builder.build(2)] + import bsie.extractor.generic.path + import bsie.extractor.generic.stat + self.assertListEqual(ext, [ + bsie.extractor.generic.path.Path(), + bsie.extractor.generic.stat.Stat(), + bsie.extractor.generic.path.Path(), + ]) + # out-of-bounds raises KeyError + self.assertRaises(IndexError, builder.build, 3) + + # building with args + builder = ExtractorBuilder([ + {'bsie.extractor.generic.constant.Constant': { + 'schema': ''' + bse:author rdfs:subClassOf bsfs:Predicate ; + rdfs:domain bsfs:Entity ; + rdfs:range xsd:string ; + bsfs:unique "true"^^xsd:boolean . + bse:rating rdfs:subClassOf bsfs:Predicate ; + rdfs:domain bsfs:Entity ; + rdfs:range xsd:integer ; + bsfs:unique "true"^^xsd:boolean . + ''', + 'tuples': [ + ('http://bsfs.ai/schema/Entity#author', 'Me, myself, and I'), + ('http://bsfs.ai/schema/Entity#rating', 123), + ], + }}]) + obj = builder.build(0) + import bsie.extractor.generic.constant + self.assertEqual(obj, bsie.extractor.generic.constant.Constant(''' + bse:author rdfs:subClassOf bsfs:Predicate ; + rdfs:domain bsfs:Entity ; + rdfs:range xsd:string ; + bsfs:unique "true"^^xsd:boolean . + bse:rating rdfs:subClassOf bsfs:Predicate ; + rdfs:domain bsfs:Entity ; + rdfs:range xsd:integer ; + bsfs:unique "true"^^xsd:boolean . + ''', [ + ('http://bsfs.ai/schema/Entity#author', 'Me, myself, and I'), + ('http://bsfs.ai/schema/Entity#rating', 123), + ])) + + # building with invalid args + self.assertRaises(errors.BuilderError, ExtractorBuilder( + [{'bsie.extractor.generic.path.Path': {'foo': 123}}]).build, 0) + # non-dict build specification + self.assertRaises(TypeError, ExtractorBuilder( + [('bsie.extractor.generic.path.Path', {})]).build, 0) + # multiple keys per build specification + self.assertRaises(TypeError, ExtractorBuilder( + [{'bsie.extractor.generic.path.Path': {}, + 'bsie.extractor.generic.stat.Stat': {}}]).build, 0) + # non-dict value for kwargs + self.assertRaises(TypeError, ExtractorBuilder( + [{'bsie.extractor.generic.path.Path': 123}]).build, 0) + + +## main ## + +if __name__ == '__main__': + unittest.main() + +## EOF ## |