class OutputDocument(TestCase): """Tests related to outputting documents.""" #------------------------------------------------------------------ Fixture def setUp(self): """Create a working document.""" self.work = Ontology() self.work.outputformat = 'n3' def tearDown(self): """Reset working document.""" self.work = None #------------------------------------------------------- Success Test Cases def test_out_versus_str(self): """Test to make sure the __str__ output is the same as the class.out() output. """ self.assertEqual( str(self.work), self.work.out(), "The output we got from __str__ is not the same as the output " "from the out() function.") #------------------------------------------------------- Failure test cases def test_invalid_outputformat(self): """Test to make sure we pick up on invalid output formats.""" def setout(fmt): """Wrapper around property. Not sure of a nicer way to do this.""" self.work.outputformat = fmt self.assertRaises(ValidationException, setout, 'asdf')
class DataDocumentCreation(TestCase): """These tests are related to basic document creation. This will test importing other schema documents and creation of individuals in a blank document. See methods in this class for details. """ #------------------------------------------------------------------ Fixture def setUp(self): """Create a working document and a document to use for import.""" self.work = Ontology() self.work.outputformat = 'n3' def tearDown(self): """Reset working document and import document.""" self.work = None #------------------------------------------------------- Success test cases def test_blankdocument_output(self): """Test just the basic creation of a blank document. Compares with expected n3 output. """ self.assertEqual( self.work.out(), """ @prefix owl: <http://www.w3.org/2002/07/owl#>. @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>. <#> a owl:Ontology. """, "Blank document n3 output does not look like we expect it to.")
class IndividualSlotManipulation(TestCase): """These tests are designed to test changes for Individuals on a slot level. """ #------------------------------------------------------------------ Fixture def setUp(self): """Creating a working and import document. Load and import a sample schema, and initialise some individuals. """ self.work = Ontology() self.work.outputformat = 'n3' self.work.imp(uri="data/SystemConfiguration.owl", prefix="sc") self.host = self.work.ind.new(rdfid="myhost", cls=self.work.cls.get(rdfid="Host")) self.kernel = self.work.ind.new(rdfid="Linux-2-6-22-9-91-fc7", cls=self.work.cls.get(rdfid="Kernel")) self.cpuarch = self.work.ind.new(rdfid="i686", cls=self.work.cls.get(rdfid="CpuArch")) def tearDown(self): """Reset working and import documents and their individuals.""" self.host = None self.kernel = None self.cpuarch = None self.work = None #------------------------------------------------------- Success test cases def test_set_slot(self): """Set a whole bunch of slots and test to see if the output is what we expect. """ self.cpuarch.set(self.work.prop.get('cpuarchName'), Literal("i686")) self.kernel.set(self.work.prop.get('kernelAbbrev'), Literal("Linux")) self.kernel.set(self.work.prop.get('kernelVersion'), Literal("2.6.22.9-91.fc7")) self.kernel.set(self.work.prop.get('hasCpuArch'), self.cpuarch.nsuri) self.host.set(self.work.prop.get('hostName'), Literal("myhost")) self.host.set(self.work.prop.get('hasKernel'), self.kernel.nsuri) self.assertEqual(self.work.out(), """ @prefix : <#>. @prefix owl: <http://www.w3.org/2002/07/owl#>. @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>. @prefix sc: <http://www.organictechnology.net/ontologies/SystemConfiguration.owl#>. <#> a owl:Ontology; owl:imports <http://www.organictechnology.net/ontologies/SystemConfiguration.owl#>. :myhost a sc:Host; sc:hasKernel :Linux-2-6-22-9-91-fc7; sc:hostName "myhost". :Linux-2-6-22-9-91-fc7 a sc:Kernel; sc:hasCpuArch :i686; sc:kernelAbbrev "Linux"; sc:kernelVersion "2.6.22.9-91.fc7". :i686 a sc:CpuArch; sc:cpuarchName "i686". """) #------------------------------------------------------- Failure test cases def test_set_inv_prop_slot(self): """Attempt to set a slot for a property which doesn't belong to the class in which the slot's individual belongs. """ self.assertRaises(SchemaException, self.cpuarch.set, self.work.prop.get('kernelAbbrev'), Literal("Linux")) def test_set_inv_prop_slot_union(self): """Attempt to set a slot for a property which doesn't belong to the class in which the slot's individual belongs. Specifically, test a property that uses unionOf for its domain setting. """ self.assertRaises(SchemaException, self.cpuarch.set, self.work.prop.get('hasKernel'), self.kernel.nsuri)
class IndividualCreation(TestCase): """These tests are designed to test the creation of individuals in a document. """ #------------------------------------------------------------------ Fixture def setUp(self): """Create a working document and a document for import. Load a sample schema and import it. """ self.work = Ontology() self.work.outputformat = 'n3' self.work.imp(uri="data/SystemConfiguration.owl", prefix="sc") def tearDown(self): """Reset working document.""" self.work = None #------------------------------------------------------- Success test cases def test_add_individual(self): """Add a new individual once and ensure the result matches what we expect. """ self.work.ind.new(rdfid="asdf", cls=self.work.cls.get(rdfid="Host")) # Note - there is a space at the end of the "owl:imports" line. self.assertEqual(self.work.out(), """ @prefix : <#>. @prefix owl: <http://www.w3.org/2002/07/owl#>. @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>. @prefix sc: <http://www.organictechnology.net/ontologies/SystemConfiguration.owl#>. <#> a owl:Ontology; owl:imports <http://www.organictechnology.net/ontologies/SystemConfiguration.owl>. :asdf a sc:Host. """) #------------------------------------------------------- Failure test cases def test_add_same_ind_twice(self): """Add the same individual multiple times and see if the document is still correct. We should see a DuplicateIdException. """ self.work.ind.new(rdfid="asdf", cls=self.work.cls.get(rdfid="Host")) self.assertRaises(DuplicateIdException, self.work.ind.new, rdfid="asdf", cls=self.work.cls.get(rdfid="Host")) self.assertEqual(self.work.out(), """ @prefix : <#>. @prefix owl: <http://www.w3.org/2002/07/owl#>. @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>. @prefix sc: <http://www.organictechnology.net/ontologies/SystemConfiguration.owl#>. <#> a owl:Ontology; owl:imports <http://www.organictechnology.net/ontologies/SystemConfiguration.owl>. :asdf a sc:Host. """)
class LocalImports(TestCase): """This will test importing other schema documents and creation of individuals in a blank document. """ #------------------------------------------------------------------ Fixture def setUp(self): """Creating working document and document for importing.""" self.work = Ontology() self.work.outputformat = 'n3' def tearDown(self): """Reset working document and import document.""" self.work = None #------------------------------------------------------- Success test cases def test_multiple_imports(self): """Import multiple schemas. Compares it with the expected n3 output. """ self.work.imp(uri="http://xmlns.com/foaf/0.1/", prefix="foaf") self.work.imp(uri="http://purl.org/dc/elements/1.1/", prefix="dc") self.assertEqual(self.work.out(), """ @prefix owl: <http://www.w3.org/2002/07/owl#>. @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>. <#> a owl:Ontology; owl:imports <http://purl.org/dc/elements/1.1/#>, <http://xmlns.com/foaf/0.1/#>. """) def test_single_import(self): """Using a blank document, import a schema from the local file system. Compares it with the expected n3 output. """ self.work.imp(uri="http://xmlns.com/foaf/0.1/", prefix="foaf") self.assertEqual(self.work.out(), """ @prefix owl: <http://www.w3.org/2002/07/owl#>. @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>. <#> a owl:Ontology; owl:imports <http://xmlns.com/foaf/0.1/>. """) #------------------------------------------------------- Failure test cases def test_duplicate_import(self): """Using a blank document, import a schema from the local file system. Compares it with the expected n3 output. """ self.work.imp(uri="http://xmlns.com/foaf/0.1/", prefix="foaf") self.assertRaises(DuplicateImportException, self.work.imp, uri="http://xmlns.com/foaf/0.1/", prefix="sc") self.assertEqual(self.work.out(), """ @prefix owl: <http://www.w3.org/2002/07/owl#>. @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>. <#> a owl:Ontology; owl:imports <http://xmlns.com/foaf/0.1/>. """)
class IndividualCreation(TestCase): """These tests are designed to test the creation of individuals in a document. """ #------------------------------------------------------------------ Fixture def setUp(self): """Create a working document and a document for import. Load a sample schema and import it. """ self.work = Ontology() self.work.outputformat = 'n3' self.work.imp(uri="data/SystemConfiguration.owl", prefix="sc") def tearDown(self): """Reset working document.""" self.work = None #------------------------------------------------------- Success test cases def test_add_individual(self): """Add a new individual once and ensure the result matches what we expect. """ self.work.ind.new(rdfid="asdf", cls=self.work.cls.get(rdfid="Host")) # Note - there is a space at the end of the "owl:imports" line. self.assertEqual( self.work.out(), """ @prefix : <#>. @prefix owl: <http://www.w3.org/2002/07/owl#>. @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>. @prefix sc: <http://www.organictechnology.net/ontologies/SystemConfiguration.owl#>. <#> a owl:Ontology; owl:imports <http://www.organictechnology.net/ontologies/SystemConfiguration.owl>. :asdf a sc:Host. """) #------------------------------------------------------- Failure test cases def test_add_same_ind_twice(self): """Add the same individual multiple times and see if the document is still correct. We should see a DuplicateIdException. """ self.work.ind.new(rdfid="asdf", cls=self.work.cls.get(rdfid="Host")) self.assertRaises(DuplicateIdException, self.work.ind.new, rdfid="asdf", cls=self.work.cls.get(rdfid="Host")) self.assertEqual( self.work.out(), """ @prefix : <#>. @prefix owl: <http://www.w3.org/2002/07/owl#>. @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>. @prefix sc: <http://www.organictechnology.net/ontologies/SystemConfiguration.owl#>. <#> a owl:Ontology; owl:imports <http://www.organictechnology.net/ontologies/SystemConfiguration.owl>. :asdf a sc:Host. """)
class IndividualSlotManipulation(TestCase): """These tests are designed to test changes for Individuals on a slot level. """ #------------------------------------------------------------------ Fixture def setUp(self): """Creating a working and import document. Load and import a sample schema, and initialise some individuals. """ self.work = Ontology() self.work.outputformat = 'n3' self.work.imp(uri="data/SystemConfiguration.owl", prefix="sc") self.host = self.work.ind.new(rdfid="myhost", cls=self.work.cls.get(rdfid="Host")) self.kernel = self.work.ind.new(rdfid="Linux-2-6-22-9-91-fc7", cls=self.work.cls.get(rdfid="Kernel")) self.cpuarch = self.work.ind.new( rdfid="i686", cls=self.work.cls.get(rdfid="CpuArch")) def tearDown(self): """Reset working and import documents and their individuals.""" self.host = None self.kernel = None self.cpuarch = None self.work = None #------------------------------------------------------- Success test cases def test_set_slot(self): """Set a whole bunch of slots and test to see if the output is what we expect. """ self.cpuarch.set(self.work.prop.get('cpuarchName'), Literal("i686")) self.kernel.set(self.work.prop.get('kernelAbbrev'), Literal("Linux")) self.kernel.set(self.work.prop.get('kernelVersion'), Literal("2.6.22.9-91.fc7")) self.kernel.set(self.work.prop.get('hasCpuArch'), self.cpuarch.nsuri) self.host.set(self.work.prop.get('hostName'), Literal("myhost")) self.host.set(self.work.prop.get('hasKernel'), self.kernel.nsuri) self.assertEqual( self.work.out(), """ @prefix : <#>. @prefix owl: <http://www.w3.org/2002/07/owl#>. @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>. @prefix sc: <http://www.organictechnology.net/ontologies/SystemConfiguration.owl#>. <#> a owl:Ontology; owl:imports <http://www.organictechnology.net/ontologies/SystemConfiguration.owl#>. :myhost a sc:Host; sc:hasKernel :Linux-2-6-22-9-91-fc7; sc:hostName "myhost". :Linux-2-6-22-9-91-fc7 a sc:Kernel; sc:hasCpuArch :i686; sc:kernelAbbrev "Linux"; sc:kernelVersion "2.6.22.9-91.fc7". :i686 a sc:CpuArch; sc:cpuarchName "i686". """) #------------------------------------------------------- Failure test cases def test_set_inv_prop_slot(self): """Attempt to set a slot for a property which doesn't belong to the class in which the slot's individual belongs. """ self.assertRaises(SchemaException, self.cpuarch.set, self.work.prop.get('kernelAbbrev'), Literal("Linux")) def test_set_inv_prop_slot_union(self): """Attempt to set a slot for a property which doesn't belong to the class in which the slot's individual belongs. Specifically, test a property that uses unionOf for its domain setting. """ self.assertRaises(SchemaException, self.cpuarch.set, self.work.prop.get('hasKernel'), self.kernel.nsuri)
class LocalImports(TestCase): """This will test importing other schema documents and creation of individuals in a blank document. """ #------------------------------------------------------------------ Fixture def setUp(self): """Creating working document and document for importing.""" self.work = Ontology() self.work.outputformat = 'n3' def tearDown(self): """Reset working document and import document.""" self.work = None #------------------------------------------------------- Success test cases def test_multiple_imports(self): """Import multiple schemas. Compares it with the expected n3 output. """ self.work.imp(uri="http://xmlns.com/foaf/0.1/", prefix="foaf") self.work.imp(uri="http://purl.org/dc/elements/1.1/", prefix="dc") self.assertEqual( self.work.out(), """ @prefix owl: <http://www.w3.org/2002/07/owl#>. @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>. <#> a owl:Ontology; owl:imports <http://purl.org/dc/elements/1.1/#>, <http://xmlns.com/foaf/0.1/#>. """) def test_single_import(self): """Using a blank document, import a schema from the local file system. Compares it with the expected n3 output. """ self.work.imp(uri="http://xmlns.com/foaf/0.1/", prefix="foaf") self.assertEqual( self.work.out(), """ @prefix owl: <http://www.w3.org/2002/07/owl#>. @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>. <#> a owl:Ontology; owl:imports <http://xmlns.com/foaf/0.1/>. """) #------------------------------------------------------- Failure test cases def test_duplicate_import(self): """Using a blank document, import a schema from the local file system. Compares it with the expected n3 output. """ self.work.imp(uri="http://xmlns.com/foaf/0.1/", prefix="foaf") self.assertRaises(DuplicateImportException, self.work.imp, uri="http://xmlns.com/foaf/0.1/", prefix="sc") self.assertEqual( self.work.out(), """ @prefix owl: <http://www.w3.org/2002/07/owl#>. @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>. <#> a owl:Ontology; owl:imports <http://xmlns.com/foaf/0.1/>. """)
class LoadDocument(TestCase): """Tests related to loading documents.""" # ------------------------------------------------------------------ Fixture def setUp(self): """Create a working document.""" self.work = Ontology() self.work.outputformat = "n3" def tearDown(self): """Reset working document.""" self.work = None # ------------------------------------------------------- Success Test Cases def test_automatic_uriextractions(self): """Test if a locally loaded documents nsuri is set automatically. OWLSugar has the ability to extract a documents namespace automatically if you didn't explicitly define it when you tried to load it from a local file. This will ensure this facility is working. """ # List for the file to nsuri mappings. tests = [ ("data/SystemConfiguration.owl", "http://www.organictechnology.net/ontologies" "/SystemConfiguration.owl#"), ("data/countries.owl", "http://www.bpiresearch.com/BPMO/2004/03/03/cdl/Countries#"), ("data/family.swrl.owl", "http://a.com/ontology#"), ( "data/demoHierarchyVisualization.owl", "http://www.ea3888.univ-rennes1.fr/ontology/" "demoHierarchyVisualization.owl#", ), ("data/foaf-index.owl", "http://xmlns.com/foaf/0.1/#"), ("http://usefulinc.com/ns/doap#", "http://usefulinc.com/ns/doap#"), ] for uri, namespace in tests: self.work.load(uri) self.assertEqual( Namespace(namespace), self.work.nsuri, "When loading the file (%s) the URI we extracted from it is " "not what we expected.", ) def test_loading_n3(self): """Test to see if we can load an n3 document.""" filepath = "data/SystemConfiguration.n3" self.work.inputformat = "n3" self.work.outputformat = "n3" self.work.load(filepath) filestring = open(filepath).read(8192) ourstring = self.work.out() # The message will dump a diff so we can see any variance. self.assertEqual( filestring, ourstring + "\n", "Input file (%s) and parsed output does not match.\n" "Diff output is:\n\n%s" % (filepath, "\n".join(list(Differ().compare(filestring.split("\n"), ourstring.split("\n"))))), ) # ------------------------------------------------------- Failure test cases def test_invalid_path(self): """Test invalid paths for loading. This makes sure a ValidationException is raised that picks up that the path is not a valid file. It also tests to make sure the original file was not clobbered. """ tests = ["src", "asdfasdf", "test"] # Directory # Invalid path # Soft link (on linux anyway) for i in tests: self.assertRaises(ValidationException, self.work.load, i) self.assertEqual( self.work.out(), """ @prefix owl: <http://www.w3.org/2002/07/owl#>. @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>. <#> a owl:Ontology. """, "Invalid path (%s) and ValidationException raised has " "clobbered the original file contents." % i, ) def test_load_invalid_with_xml(self): """Test to see if we get an exception when attempting to load a file that has invalid contents. Also check to make sure the existing contents are not clobbered. """ self.work.inputformat = "xml" self.assertRaises(InvalidOwlParseException, self.work.load, "data/README.txt") self.assertEqual( self.work.out(), """ @prefix owl: <http://www.w3.org/2002/07/owl#>. @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>. <#> a owl:Ontology. """, "Invalid file contents and SAXParseException raised has " "clobbered the original file contents.", ) def test_load_owlxml_with_n3(self): """Try and load an OWL/XML document when we are using 'n3' as an input format. """ filepath = "data/SystemConfiguration.owl" self.work.inputformat = "n3" self.assertRaises(InvalidOwlParseException, self.work.load, filepath) self.assertEqual( self.work.out(), """ @prefix owl: <http://www.w3.org/2002/07/owl#>. @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>. <#> a owl:Ontology. """, "Invalid file contents and SAXParseException raised has " "clobbered the original file contents.", ) def test_load_text_with_n3(self): """Try and load an OWL/XML document when we are using 'n3' as an input format. """ self.work.inputformat = "n3" self.assertRaises(InvalidOwlParseException, self.work.load, "data/README.txt") self.assertEqual( self.work.out(), """ @prefix owl: <http://www.w3.org/2002/07/owl#>. @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>. <#> a owl:Ontology. """, "Invalid file contents and SAXParseException raised has " "clobbered the original file contents.", )