Skip to content

Commit

Permalink
adds tests case for #18 and improves dict
Browse files Browse the repository at this point in the history
  • Loading branch information
WolfgangFahl committed Jan 28, 2021
1 parent 1cfd2d6 commit 4ec654e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 15 deletions.
12 changes: 8 additions & 4 deletions lodstorage/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def execute(self,ddlCmd):
'''
self.c.execute(ddlCmd)

def createTable(self,listOfRecords,entityName,primaryKey=None,withDrop=False,sampleRecordCount=1,failIfToFew=True):
def createTable(self,listOfRecords,entityName,primaryKey=None,withDrop=False,sampleRecordCount=1,failIfTooFew=True):
'''
derive Data Definition Language CREATE TABLE command from list of Records by examining first recorda
as defining sample record and execute DDL command
Expand All @@ -91,14 +91,14 @@ def createTable(self,listOfRecords,entityName,primaryKey=None,withDrop=False,sam
primaryKey(string): the key/column to use as a primary key
withDrop(boolean): true if the existing Table should be dropped
sampleRecords(int): number of sampleRecords expected and to be inspected
failIftoFew(boolean): raise an Exception if to few sampleRecords else warn only
failIfTooFew(boolean): raise an Exception if to few sampleRecords else warn only
Returns:
EntityInfo: meta data information for the created table
'''
l= len(listOfRecords)
if l<sampleRecordCount:
msg="only %d/%d of needed sample records to createTable available" % (l,sampleRecordCount)
if failIfToFew:
if failIfTooFew:
raise Exception(msg)
else:
if self.debug:
Expand Down Expand Up @@ -239,10 +239,14 @@ def getTableDict(self):
get the schema information from this database as a dict
Returns:
dict: Lookup map of tables
dict: Lookup map of tables with columns also being converted to dict
'''
tableDict={}
for table in self.getTableList():
colDict={}
for col in table["columns"]:
colDict[col['name']]=col
table["columns"]=colDict
tableDict[table['name']]=table
return tableDict

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

setup(
name='pylodstorage',
version='0.0.22',
version='0.0.23',

packages=['lodstorage',],
author='Wolfgang Fahl',
Expand Down
35 changes: 25 additions & 10 deletions tests/testSqlite3.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,26 +309,41 @@ def testCopy(self):
# https://stackoverflow.com/a/44707371/1497139
copyDB.execute("pragma user_version=0")

def testIssue16(self):
'''
https://github.com/WolfgangFahl/pyLoDStorage/issues/16
allow to only warn if samplerecordcount is higher than number of available records
'''
def getSampleTableDB(self,withDrop=False,debug=False,failIfTooFew=False):
listOfRecords=Sample.getSample(1000)
sqlDB=SQLDB()
entityName="sample"
primaryKey='pKey'
withDrop=False
sampleRecordCount=10000
sqlDB.debug=True
sqlDB.createTable(listOfRecords, entityName, primaryKey, withDrop, sampleRecordCount,failIfToFew=False)
withDrop=True
sqlDB.debug=debug
sqlDB.createTable(listOfRecords, entityName, primaryKey, withDrop, sampleRecordCount,failIfTooFew=failIfTooFew)
return sqlDB

def testIssue16(self):
'''
https://github.com/WolfgangFahl/pyLoDStorage/issues/16
allow to only warn if samplerecordcount is higher than number of available records
'''
self.getSampleTableDB(withDrop=False, debug=True,failIfTooFew=False)
try:
sqlDB.createTable(listOfRecords, entityName, primaryKey, withDrop, sampleRecordCount)
self.getSampleTableDB(withDrop=True, debug=True,failIfTooFew=True)
self.fail("There should be an exception that too few sample records where provided")
except Exception as ex:
self.assertTrue("only 1000/10000 of needed sample records to createTable available" in str(ex))

def testIssue18(self):
'''
https://github.com/WolfgangFahl/pyLoDStorage/issues/18
'''
sqlDB=self.getSampleTableDB()
tableDict=sqlDB.getTableDict()
if self.debug:
print (tableDict)
self.assertTrue("sample" in tableDict)
cols=tableDict["sample"]["columns"]
self.assertTrue("pkey" in cols)



if __name__ == "__main__":
#import sys;sys.argv = ['', 'Test.testSqllit3']
Expand Down

0 comments on commit 4ec654e

Please sign in to comment.