def testSqlite3(self): ''' test sqlite3 with a few records from the royal family ''' listOfRecords=Sample.getRoyals() resultList=self.checkListOfRecords(listOfRecords, 'Person', 'name',debug=True) if self.debug: print(resultList) self.assertEqual(listOfRecords,resultList)
def testIssue25(self): ''' see https://github.com/WolfgangFahl/pyLoDStorage/issues/25 ''' listOfRecords = Sample.getRoyals() df= pd.DataFrame(listOfRecords) self.assertEqual(len(df), len(listOfRecords)) self.assertEqual(len(df.columns.values), len(listOfRecords[0].keys())) averageAge= df['age'].mean() self.assertIsNotNone(averageAge) self.assertGreater(averageAge,53)
def testEntityManager(self): ''' test the entity Manager handling ''' self.debug = True for i, royals in enumerate( [Sample.getRoyals(), Sample.getRoyalsInstances()]): if self.debug: print(f"{i+1}:{royals}") sparqlConfig = StorageConfig.getSPARQL( "http://example.bitplan.com", "http://localhost:3030/example", host="localhost") # TODO use sparql Config for config in [ StorageConfig.getDefault(debug=self.debug), StorageConfig.getJSON(debug=self.debug), StorageConfig.getJsonPickle(self.debug) ]: self.configure(config) name = "royal" if i == 0 else "royalorm" clazz = None if i == 0 else Royal em = EntityManager(name=name, entityName="Royal", entityPluralName="Royals", clazz=clazz, listName="royals", config=config) em.royals = royals if i == 0: cacheFile = em.storeLoD(royals) else: cacheFile = em.store() if cacheFile is not None: self.assertTrue(os.path.isfile(cacheFile)) royalsLod = em.fromStore() self.assertTrue(isinstance(royalsLod, list)) hint = f"{i}({config.mode}):{name}" for item in royalsLod: self.assertTrue(isinstance(item, dict), f"{hint}:expecting dict") royalsList = em.getList() self.assertEqual(len(royals), len(royalsList)) for j, item in enumerate(royalsList): hint = f"{hint}/{j}" royal = royals[j] # TODO check type handling e.g. "born" self.checkItem( royal, item, ["name", "age", "numberInLine", "wikidataurl"], hint) pass
def testListOfDictInsert(self): ''' test inserting a list of Dicts and retrieving the values again using a person based example instead of https://en.wikipedia.org/wiki/FOAF_(ontology) we use an object oriented derivate of FOAF with a focus on datatypes ''' listofDicts = Sample.getRoyals() typedLiteralModes = [True, False] entityType = 'foafo:Person' primaryKey = 'name' prefixes = 'PREFIX foafo: <http://foafo.bitplan.com/foafo/0.1/>' for typedLiteralMode in typedLiteralModes: jena = self.getJena(mode='update', typedLiterals=typedLiteralMode, debug=self.debug) deleteString = """ PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX foafo: <http://foafo.bitplan.com/foafo/0.1/> DELETE WHERE { ?person a 'foafo:Person'. ?person ?p ?o. } """ jena.query(deleteString) errors = jena.insertListOfDicts(listofDicts, entityType, primaryKey, prefixes) self.checkErrors(errors) jena = self.getJena(mode="query", debug=self.debug) queryString = """ PREFIX foafo: <http://foafo.bitplan.com/foafo/0.1/> SELECT ?name ?born ?numberInLine ?wikidataurl ?age ?ofAge ?lastmodified WHERE { ?person a 'foafo:Person'. ?person foafo:Person_name ?name. ?person foafo:Person_born ?born. ?person foafo:Person_numberInLine ?numberInLine. ?person foafo:Person_wikidataurl ?wikidataurl. ?person foafo:Person_age ?age. ?person foafo:Person_ofAge ?ofAge. ?person foafo:Person_lastmodified ?lastmodified. }""" personResults = jena.query(queryString) self.assertEqual(len(listofDicts), len(personResults)) personList = jena.asListOfDicts(personResults) for index, person in enumerate(personList): if self.debug: print("%d: %s" % (index, person)) # check the correct round-trip behavior self.assertEqual(listofDicts, personList)
def testRoyals(self): ''' test conversion of royals ''' return # TODO - fix me inlod = Sample.getRoyals() csv = CSV.toCSV(inlod) if self.debug: print(csv) # https://stackoverflow.com/questions/3717785/what-is-a-convenient-way-to-store-and-retrieve-boolean-values-in-a-csv-file outlod = CSV.fromCSV(csv) if self.debug: print(outlod)
def testIssue15(self): ''' https://github.com/WolfgangFahl/pyLoDStorage/issues/15 auto create view ddl in mergeschema ''' self.sqlDB=SQLDB(debug=self.debug,errorDebug=self.debug) listOfRecords=Sample.getRoyals() entityInfo=EntityInfo(listOfRecords[:3],'Person','name',debug=self.debug) entityInfo=self.sqlDB.createTable(listOfRecords[:10],entityInfo.name,entityInfo.primaryKey) listOfRecords=[{'name': 'Royal family', 'country': 'UK', 'lastmodified':datetime.now()}] entityInfo=self.sqlDB.createTable(listOfRecords[:10],'Family','name') tableList=self.sqlDB.getTableList() viewDDL=Schema.getGeneralViewDDL(tableList,"PersonBase") if self.debug: print (viewDDL) expected="""CREATE VIEW PersonBase AS SELECT name,lastmodified FROM Person UNION SELECT name,lastmodified FROM Family""" self.assertEqual(expected,viewDDL) pass
def testEntityInfo(self): ''' test creating entityInfo from the sample record ''' listOfRecords=Sample.getRoyals() entityInfo=EntityInfo(listOfRecords[:3],'Person','name',debug=True) self.assertEqual("CREATE TABLE Person(name TEXT PRIMARY KEY,born DATE,numberInLine INTEGER,wikidataurl TEXT,age FLOAT,ofAge BOOLEAN,lastmodified TIMESTAMP)",entityInfo.createTableCmd) self.assertEqual("INSERT INTO Person (name,born,numberInLine,wikidataurl,age,ofAge,lastmodified) values (:name,:born,:numberInLine,:wikidataurl,:age,:ofAge,:lastmodified)",entityInfo.insertCmd) self.sqlDB=SQLDB(debug=self.debug,errorDebug=True) entityInfo=self.sqlDB.createTable(listOfRecords[:10],entityInfo.name,entityInfo.primaryKey) tableList=self.sqlDB.getTableList() if self.debug: print (tableList) self.assertEqual(1,len(tableList)) personTable=tableList[0] self.assertEqual("Person",personTable['name']) self.assertEqual(7,len(personTable['columns'])) uml=UML() plantUml=uml.tableListToPlantUml(tableList,packageName="Royals",withSkin=False) if self.debug: print(plantUml) expected="""package Royals { class Person << Entity >> { age : FLOAT born : DATE lastmodified : TIMESTAMP name : TEXT <<PK>> numberInLine : INTEGER ofAge : BOOLEAN wikidataurl : TEXT } } """ self.assertEqual(expected,plantUml) # testGeneralization listOfRecords=[{'name': 'Royal family', 'country': 'UK', 'lastmodified':datetime.now()}] entityInfo=self.sqlDB.createTable(listOfRecords[:10],'Family','name') tableList=self.sqlDB.getTableList() self.assertEqual(2,len(tableList)) uml=UML() plantUml=uml.tableListToPlantUml(tableList,generalizeTo="PersonBase",withSkin=False) if self.debug: print(plantUml) expected='''class PersonBase << Entity >> { lastmodified : TIMESTAMP name : TEXT <<PK>> } class Person << Entity >> { age : FLOAT born : DATE numberInLine : INTEGER ofAge : BOOLEAN wikidataurl : TEXT } class Family << Entity >> { country : TEXT } PersonBase <|-- Person PersonBase <|-- Family ''' self.assertEqual(expected,plantUml)