def newsstanddb_getdbfile(): try: db = ndb(':memory:') except IOError: print 'Couldn\'nt connect to DB' return db.getDBFile()
def newsstanddb_import(): parser = argparse.ArgumentParser(description='Import files into the DB.') parser.add_argument('filename', help='the file(s) to import', nargs='+') print '\n' parser.print_help() print '\n' args = parser.parse_args() try: db = ndb(newsstanddb_getdbfile()) except IOError: print 'Couldn\'nt connect to DB' files = args.filename count = len(files) try: db.multiImport(*files) except ProgrammingError as e: print "Some files could not be imported: {0}".format(e.message,) count -= len(e.message.split(',')) except: raise #TODO: Better exception handling print "\n== Import Successful (%i file(s) imported) ==\n" % (count,)
def testCreate(self): # Create database s = ndb('test.sql') # Create tables s.createDB() # Fetch all tables from DB s.cur.execute("SELECT name FROM sqlite_master WHERE type = 'table';") t = s.cur.fetchall() # print t #Debug # @unittest.skip('Temp') # Verify that the tables we = ndb('test.sql')re created self.assertIn((u'data',), t, '\'data\' table not in database') self.assertIn((u'file',), t, '\'file\' table not in database') self.assertIn((u'stats',), t, '\'stats\' table not in database') self.assertIn((u'monthProceeds',), t, '\'monthProceeds\' table not in database') self.assertIn((u'optin',), t, '\'optin\' table not in database') self.assertIn((u'product',), t, '\'product\' table not in database') for name,structure in [('data',self.__dataSQL),('file',self.__fileSQL), ('stats',self.__statsSQL), ('monthProceeds',self.__monthProceedsSQL), ('optin',self.__optinSQL), ('product',self.__productSQL)]: # Fetch each table's sql code = ''.join(["SELECT sql FROM sqlite_master WHERE type = 'table' AND name = '",name,"';",]) s.cur.execute(code) t = s.cur.fetchone()[0] # No exception handling here, assuming that table still exists # print t #Debug # And verify SQL schema is correct self.assertEqual(t, structure, ''.join(['\'',name, '\' table schema is not as expected',]))
def newsstanddb_getstats(): try: db = ndb(newsstanddb_getdbfile()) except IOError: print 'Couldn\'nt connect to DB' db.buildStats() db.printStats()
def setUp(self): self.__path = path.split(__file__)[0] self.__db = ndb(path.join(self.__path, "test.sql")) self.__db.createDB() self.__db.importData(path.join(self.__path, "test2.csv")) # Product setup self.__db.addProduct("02/01/2013", "IA1", 3.5) self.__db.addProduct("02/01/2013", "IAY", 1.4) self.__db.addProduct("10/01/2013", "IAY", 2.1)
def setUp(self): self.__path = path.split(__file__)[0] self.__db = ndb(path.join(self.__path,'test.sql')) self.__db.createDB() self.__db.importData(path.join(self.__path,'test2.csv')) #Product setup self.__db.addProduct('02/01/2013','IA1',3.5) self.__db.addProduct('02/01/2013','IAY',1.4) self.__db.addProduct('10/01/2013','IAY',2.1)
def setUp(self): self.__path = path.split(__file__)[0] self.__testsql = path.join(self.__path,'test.sql') self.__testcsv = path.join(self.__path,'test.csv') self.__test2csv = path.join(self.__path,'test2.csv') self.__testoptin = path.join(self.__path,'testOptin.csv') self.__faketest = path.join(self.__path,'faketest.csv') # self.__statsmd = path.join(self.__path,'stats.md') #Fails test self.__statsmd = 'stats.md' self.__db = ndb(self.__testsql)
def newsstanddb_addproduct(): parser = argparse.ArgumentParser(description='Add product definition to DB.') parser.add_argument('date', help='the cut-off date (when this change took effect)') parser.add_argument('product', help='the product identifier') parser.add_argument('proceeds', type=float, help='the product\'s developer proceeds') args = parser.parse_args() try: db = ndb(newsstanddb_getdbfile()) except IOError: print 'Couldn\'nt connect to DB' db.addProduct(args.date,args.product,args.proceeds)
def testProductSetup(self): # Create database s = ndb('test.sql') # Create tables s.createDB() s.addProduct('02/01/2013','IA1',3.5) s.addProduct('02/01/2013','IAY',1.4) s.addProduct('10/01/2013','IAY',2.1) s.cur.execute("SELECT * FROM product") self.assertEqual(len(s.cur.fetchall()), 3, 'The number of records in the product table are not as expected')
def newsstanddb_autoupdate(): #basedir='.',pattern=('N_D_W*','O_S_W*'),outputFile='stats.md' parser = argparse.ArgumentParser(description='Auto update the DB.') parser.add_argument('-d', '--basedir', dest='basedir', help='the directory to search for new files',default='.') parser.add_argument('-p','--pattern', dest='pattern',help='the pattern to search for', nargs='+',default=['N_D_W*','O_S_W*']) parser.add_argument('-o','--output', dest='outputFile', help='the file to write the stats out', default='stats.md') parser.add_argument('-v','--verbose', action='count', help='adjust verbosity level') args = parser.parse_args() if args.verbose: print '\n' parser.print_help() print '\n' try: db = ndb(newsstanddb_getdbfile()) except IOError: print 'Couldn\'nt connect to DB' count = 0 for pat in args.pattern: files = glob('/'.join([args.basedir,pat])) if args.verbose > 1: print files #Debug count += len(files) try: db.multiImport(*files) except ProgrammingError as e: if args.verbose > 0: print "Some files could not be imported: {0}".format(e.message,) count -= len(e.message.split(',')) except: raise #TODO: Better exception handling db.buildStats() db.writeStats() db.outputStats('/'.join([args.basedir,args.outputFile])) print "\n== Update Successful (%i files imported) ==\n" % (count,) db.printStats()
def newsstanddb_setdbfile(): parser = argparse.ArgumentParser(description='Set the DB file.') parser.add_argument('file', help='the file to set as persistent DB') # print '\n' # parser.print_help() # print '\n' args = parser.parse_args() try: db = ndb(':memory:') except IOError: print 'Couldn\'nt connect to DB' db.setDBFile(args.file) print '\n== DB File Set Successfully ==\n'
def newsstanddb_create(): parser = argparse.ArgumentParser(description='Create a DB.') parser.add_argument('filename', help='the filename of the database') # print '\n' # parser.print_help() # print '\n' args = parser.parse_args() filename = args.filename try: db = ndb(filename) db.createDB() except IOError: print 'Unable to createDB' print "\n== Database creation was successful ==\n"
def setUp(self): self.__path = path.split(__file__)[0] self.db = ndb(path.join(self.__path,'test.sql')) self.db.createDB()
def setUp(self): self.__path = path.split(__file__)[0] self.__db = ndb(path.join(self.__path,'test.sql')) self.__db.createDB() self.__db.importData(path.join(self.__path,'test2.csv'))
def test_is_ndb(self): s = ndb('test.sql') self.assertTrue(isinstance(s, ndb))
def tearDown(self): s = ndb('test.sql') s.removeDB()