Ejemplo n.º 1
0
    def test_get_link_data(self):
        #setup stuff
        utils = Utils()
        db = config.get_database()
        dbu = DatabaseUtilities()
        db_create = DatabaseCreate()

        dbu.delete_all_tables()
        db_create.linktab()
        dbu.create_some_test_well_royalty_masters()
        dbu.create_some_test_leases()

        linktab = db.get_data_structure('LinkTab')
        linktab.TabName = 'Lease'
        linktab.AttrName = 'ID'
        linktab.LinkName = 'Lease'
        linktab.BaseTab = 1
        linktab.ShowAttrs = 'ID,Lessor'
        db.insert(linktab)

        linktab = db.get_data_structure('LinkTab')
        linktab.TabName = 'Well'
        linktab.AttrName = 'LeaseID'
        linktab.LinkName = 'Lease'
        linktab.BaseTab = 0
        linktab.ShowAttrs = ''
        db.insert(linktab)

        data = dict()
        data["TabName"] = 'Well'
        data["AttrName"] = 'LeaseID'
        data["AttrValue"] = 2

        json_from_browser = json.dumps(data)
        print('json_from_browser', json_from_browser)

        # Data should be found
        resp = self.myapp.post('/data/getLinkData.json',
                               data=json_from_browser)
        print("resp:", resp)
        self.assertEqual(resp.status_code, 200)
        data = utils.json_decode(resp)

        print('data', data)
        rows = data['BaseData']
        self.assertEqual(len(rows), 2)
        self.assertEqual(rows[0][0], 'ID')
        self.assertEqual(rows[0][1], 'Lessor')
        self.assertEqual(rows[1][0], 2)
        self.assertEqual(rows[1][1], 2346)

        rows = data['Links']
        self.assertEqual(len(rows), 2)
        self.assertEqual(rows[0][0], 'Lease')
        self.assertEqual(rows[0][1], 'ID')
        self.assertEqual(rows[1][0], 'Well')
        self.assertEqual(rows[1][1], 'LeaseID')
    def test_get_link_data(self):
        #setup stuff
        utils = Utils()
        db = config.get_database()
        dbu = DatabaseUtilities()
        db_create = DatabaseCreate()

        dbu.delete_all_tables()
        db_create.linktab()
        dbu.create_some_test_well_royalty_masters()
        dbu.create_some_test_leases()
        
        linktab = db.get_data_structure('LinkTab')
        linktab.TabName = 'Lease'
        linktab.AttrName = 'ID'
        linktab.LinkName = 'Lease'
        linktab.BaseTab = 1
        linktab.ShowAttrs = 'ID,Lessor'
        db.insert(linktab)
        
        linktab = db.get_data_structure('LinkTab')
        linktab.TabName = 'Well'
        linktab.AttrName = 'LeaseID'
        linktab.LinkName = 'Lease'
        linktab.BaseTab = 0
        linktab.ShowAttrs = ''
        db.insert(linktab)

        data = dict()
        data["TabName"] = 'Well'
        data["AttrName"] = 'LeaseID'
        data["AttrValue"] = 2
        
        json_from_browser = json.dumps(data)
        print('json_from_browser', json_from_browser)

        # Data should be found
        resp = self.myapp.post('/data/getLinkData.json',data=json_from_browser)
        print("resp:",resp)
        self.assertEqual(resp.status_code, 200)
        data = utils.json_decode(resp)
        
        print('data',data)
        rows = data['BaseData']
        self.assertEqual(len(rows),2)
        self.assertEqual(rows[0][0],'ID')
        self.assertEqual(rows[0][1],'Lessor')
        self.assertEqual(rows[1][0],2)
        self.assertEqual(rows[1][1],2346)
        
        rows = data['Links']
        self.assertEqual(len(rows),2)
        self.assertEqual(rows[0][0],'Lease')
        self.assertEqual(rows[0][1],'ID')
        self.assertEqual(rows[1][0],'Well')
        self.assertEqual(rows[1][1],'LeaseID')
    def setUp(self):
        self.assertEqual(
            config.get_environment(),
            'unittest')  # Distructive Tests must run in unittest enviornment
        self.dbi = config.get_database_instance()
        self.db = config.get_database()
        self.dbu = DatabaseUtilities()
        self.db_create = DatabaseCreate()

        self.dbu.delete_all_tables()
    def test_run(self):
        #Testing loading an Excel spreadsheet into an sqlite3 database.
#         print ("Creating temporary database %s" % self.TEST_DATABASE)

        self.assertEqual(config.get_environment(),'unittest') # Distructive Tests must run in unittest enviornment

        dbu = DatabaseUtilities()
        dbc = DatabaseCreate()
        loader = Loader()
        dbu.delete_all_tables()
        dbc.create_all()
        dbi = config.get_database_instance()
        
        
        loader.connect()
        loader.open_excel(self.TEST_SPREADSHEET)
        shower = Shower()
        shower.connect()

        #Test that the worksheet has x number of tabs
        self.assertEqual(len(loader.wb.get_sheet_names()), 2)

        #Test that each tab has x number of columns
        self.assertEqual(len(loader.wb['Well'].columns), 12)
        self.assertEqual(len(loader.wb['Royalty Master'].columns), 11)

        #Test that each tab has x number of rows
        self.assertEqual(len(loader.wb['Well'].rows), 9)
        self.assertEqual(len(loader.wb['Royalty Master'].rows), 11)

#         print(dbi.get_table_names())
#         self.assertEqual(len(dbi.get_table_names()), 0)
# 
#         #Test that we have x number of tables
        loader.load_all_sheets()
#         self.assertEqual(len(dbi.get_table_names()), 2)

        #test that each table has x number of columns
        self.assertEqual(len(shower.show_table('Well')), 8)
        self.assertEqual(len(shower.show_table('RoyaltyMaster')), 10)

        #test that each table has x number of row
        self.assertEqual(len(shower.show_columns('Well')), 12)
        self.assertEqual(len(shower.show_columns('RoyaltyMaster')), 11)

        #test column type
        self.assertEqual(shower.column_type('Well', 'ID'), 'integer')
        loader.commit()
 def setUp(self):
     self.assertEqual(config.get_environment(),'unittest') # Distructive Tests must run in unittest enviornment
     self.dbi = config.get_database_instance()
     self.db = config.get_database()
     self.dbu = DatabaseUtilities()
     self.db_create = DatabaseCreate()
     
     self.dbu.delete_all_tables()
Ejemplo n.º 6
0
    def test_link_update(self):
        db = config.get_database()
        dbu = DatabaseUtilities()
        db_create = DatabaseCreate()
        utils = Utils()

        dbu.delete_all_tables()
        db_create.linktab()

        # Test Insert
        data = dict()
        data["ID"] = '0'
        data["TabName"] = 'Lease'
        data["AttrName"] = 'ID'
        data["LinkName"] = 'Lease'
        json_to_browser = json.dumps(data)
        resp = self.myapp.post('/data/updateLinkRow.json',
                               data=json_to_browser)
        self.assertEqual(resp.status_code, 200)
        data = utils.json_decode(resp)
        self.assertEqual(data["StatusCode"], 0)

        result = db.select('LinkTab', TabName='Lease', AttrName='ID')
        self.assertEqual(len(result), 1)

        # Test Update
        data = utils.obj_to_dict(result[0])
        data['LinkName'] = 'Changed'

        json_to_browser = json.dumps(data)
        resp = self.myapp.post('/data/updateLinkRow.json',
                               data=json_to_browser)
        self.assertEqual(resp.status_code, 200)
        data = utils.json_decode(resp)
        self.assertEqual(data["StatusCode"], 0)

        result = db.select('LinkTab', TabName='Lease', AttrName='ID')
        self.assertEqual(len(result), 1)
        self.assertEqual(result[0].LinkName, 'Changed')
Ejemplo n.º 7
0
    def test_get_link_row(self):
        #setup stuff
        db = config.get_database()
        dbu = DatabaseUtilities()
        db_create = DatabaseCreate()
        utils = Utils()

        dbu.delete_all_tables()

        db_create.linktab()
        linktab = db.get_data_structure('LinkTab')
        linktab.TabName = 'Well'
        linktab.AttrName = 'ID'
        json_to_browser = json.dumps(utils.obj_to_dict(linktab))
        print('json_to_browser', json_to_browser)

        # Data should not be found but there should not be an error
        resp = self.myapp.post('/data/getLinkRow.json', data=json_to_browser)
        self.assertEqual(resp.status_code, 200)
        data = utils.json_decode(resp)
        self.assertEqual(data["LinkName"], '')

        linktab.LinkName = 'Well'
        linktab.BaseTab = 0
        linktab.ShowAttrs = 'ID,UWI'
        db.insert(linktab)

        # Data should be found
        resp = self.myapp.post('/data/getLinkRow.json', data=json_to_browser)
        data = utils.json_decode(resp)
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(data["LinkName"], 'Well')
        self.assertEqual(data["ShowAttrs"], 'ID,UWI')

        print('status:', resp.status)
        print('status code:', resp.status_code)
        print(resp)
        print(resp.data)
    def test_get_link_row(self):
        #setup stuff
        db = config.get_database()
        dbu = DatabaseUtilities()
        db_create = DatabaseCreate()
        utils = Utils()

        dbu.delete_all_tables()

        db_create.linktab()
        linktab = db.get_data_structure('LinkTab')
        linktab.TabName = 'Well'
        linktab.AttrName = 'ID'
        json_to_browser = json.dumps(utils.obj_to_dict(linktab))
        print('json_to_browser', json_to_browser)

        # Data should not be found but there should not be an error
        resp = self.myapp.post('/data/getLinkRow.json',data=json_to_browser)
        self.assertEqual(resp.status_code, 200)
        data = utils.json_decode(resp)
        self.assertEqual(data["LinkName"],'')
        
        linktab.LinkName = 'Well'
        linktab.BaseTab = 0
        linktab.ShowAttrs = 'ID,UWI'
        db.insert(linktab)
        
        # Data should be found 
        resp = self.myapp.post('/data/getLinkRow.json',data=json_to_browser)
        data = utils.json_decode(resp)
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(data["LinkName"],'Well')
        self.assertEqual(data["ShowAttrs"],'ID,UWI')
        
        print('status:',resp.status)
        print('status code:',resp.status_code)
        print(resp)
        print(resp.data)      
    def test_link_update(self):
        db = config.get_database()
        dbu = DatabaseUtilities()
        db_create = DatabaseCreate()
        utils = Utils()

        dbu.delete_all_tables()
        db_create.linktab()

        # Test Insert
        data = dict()
        data["ID"] = '0'
        data["TabName"] = 'Lease'
        data["AttrName"] = 'ID'
        data["LinkName"] = 'Lease'
        json_to_browser = json.dumps(data)
        resp = self.myapp.post('/data/updateLinkRow.json',data=json_to_browser)
        self.assertEqual(resp.status_code, 200)
        data = utils.json_decode(resp)
        self.assertEqual(data["StatusCode"],0)
        
        result = db.select('LinkTab', TabName='Lease', AttrName='ID')
        self.assertEqual(len(result), 1)
        
        # Test Update
        data = utils.obj_to_dict(result[0])
        data['LinkName'] = 'Changed' 

        json_to_browser = json.dumps(data)
        resp = self.myapp.post('/data/updateLinkRow.json',data=json_to_browser)
        self.assertEqual(resp.status_code, 200)
        data = utils.json_decode(resp)
        self.assertEqual(data["StatusCode"],0)
        
        result = db.select('LinkTab', TabName='Lease', AttrName='ID')
        self.assertEqual(len(result), 1)
        self.assertEqual(result[0].LinkName,'Changed')
class SqliteDatabaseTest(unittest.TestCase):
    def setUp(self):
        self.assertEqual(
            config.get_environment(),
            'unittest')  # Distructive Tests must run in unittest enviornment
        self.dbi = config.get_database_instance()
        self.db = config.get_database()
        self.dbu = DatabaseUtilities()
        self.db_create = DatabaseCreate()

        self.dbu.delete_all_tables()

    def test_to_db_value(self):
        self.assertEqual('123', self.db.to_db_value(123))
        self.assertEqual('"asdf"', self.db.to_db_value("asdf"))
        self.assertEqual('123.45', self.db.to_db_value(123.45))
        self.assertEqual('1', self.db.to_db_value(True))
        self.assertEqual('0', self.db.to_db_value(False))
        self.assertEqual('null', self.db.to_db_value(None))

    def test_get_data_structure(self):
        ds = self.db.get_data_structure('WhatEver')
        self.assertEqual('WhatEver', ds._table_name)

    def test_select(self):
        self.dbu.create_some_test_well_royalty_masters()
        self.dbu.create_some_test_leases()

        self.assertIsNotNone(
            self.db.select('Well', RoyaltyClassification='New Oil'))
        self.assertEqual(
            len(self.db.select('Well', Prov='SK', Classification='Other')), 2)
        self.assertEqual(len(self.db.select('Well')), 4)
        self.assertRaises(AppError, self.db.select, 'WrongTable')
        self.assertRaises(AppError,
                          self.db.select,
                          'WrongTable',
                          WrongAttr='WhoCares')
        self.assertRaises(AppError, self.db.select, 'Well', Foo='bar')
        self.assertEqual(len(self.db.select('Lease')), 3)
        self.assertEqual(len(self.db.select('Lease', Prov='SK')), 3)
        self.assertEqual(len(self.db.select('Well', ID=1000)), 0)

    def test_update(self):
        self.dbu.create_some_test_well_royalty_masters()

        # change all types of attributes, read another record and then read the record again to make sure the changes were made.
        well = self.db.select('Well', ID=2)
        well[0].UWI = 'Changed'
        well[0].LeaseID = 100
        well[0].CommencementDate = '2016-02-01 00:00:00'
        well[0].WellType = None
        self.db.update(well[0])
        well = self.db.select('Well', ID=1)
        self.assertEqual(well[0].ID, 1)
        self.assertEqual(well[0].UWI, 'SKWI111062705025W300')
        well = self.db.select('Well', ID=2)
        self.assertEqual(well[0].ID, 2)
        self.assertEqual(well[0].UWI, 'Changed')
        self.assertEqual(well[0].LeaseID, 100)
        self.assertEqual(well[0].CommencementDate, datetime(2016, 2, 1, 0, 0))
        self.assertEqual(well[0].WellType, None)

        ds = DataStructure()
        self.assertRaises(AttributeError, self.db.update, ds)

        ds._table_name = 'Well'
        self.assertRaises(AttributeError, self.db.update, ds)
        ds.ID = 100
        self.assertRaises(AppError, self.db.update, ds)

    def test_insert(self):
        self.db_create.well()

        well = DataStructure()
        well.UWI = 'UWI for this well'
        # Should raise this error since we need to get the structure from the database
        self.assertRaises(TypeError, self.db.insert)

        well = self.db.get_data_structure('Well')
        well.UWI = 'UWI for this well'
        self.db.insert(well)
        self.assertEqual(well.ID, 1)

        well = self.db.get_data_structure('Well')
        well.UWI = 'Different UWI for this well'
        self.db.insert(well)
        self.assertEqual(well.ID, 2)

        well = self.db.select('Well', ID=1)
        self.assertEqual(well[0].ID, 1)
        self.assertEqual(well[0].UWI, 'UWI for this well')

        well = self.db.get_data_structure('Well')
        well.UWI = 'Next Well UWI'
        well.ID = 10
        self.db.insert(well)

        well = self.db.select('Well', ID=1)
        self.assertEqual(well[0].ID, 1)
        self.assertEqual(well[0].UWI, 'UWI for this well')

        well = self.db.select('Well', ID=10)
        self.assertEqual(well[0].ID, 10)
        self.assertEqual(well[0].UWI, 'Next Well UWI')

        well = self.db.get_data_structure('Well')
        well.UWI = 'Just One More'
        self.db.insert(well)
        self.assertEqual(well.ID, 11)

        well = self.db.get_data_structure('Well')
        well.BadAttr = 'Just another value'
        self.assertRaises(AppError, self.db.insert, well)

        # if the ID is None,Blank,or zero we shold still be able to insert a record
        well = self.db.get_data_structure('Well')
        well.ID = None
        well.UWI = 'Just One More'
        self.db.insert(well)
        self.assertEqual(well.ID, 12)
        well.ID = 0
        self.db.insert(well)
        self.assertEqual(well.ID, 13)
        well.ID = ''
        self.db.insert(well)
        self.assertEqual(well.ID, 14)

    def test_delete(self):
        self.dbu.create_some_test_well_royalty_masters()

        self.assertEqual(4, len(self.db.select('Well')))

        self.db.delete('Well', 2)
        self.assertEqual(3, len(self.db.select('Well')))
        self.assertEqual(0, len(self.db.select('Well', ID=2)))

    def test_date_format(self):
        self.dbu.create_some_test_well_royalty_masters()

        well = self.db.select('Well', ID=1)
        self.assertTrue(isinstance(well[0].CommencementDate, datetime))
 def __init__(self):
     self.db_instance = config.get_database_instance()
     self.db_create = DatabaseCreate()
class DatabaseUtilities(object):

    def __init__(self):
        self.db_instance = config.get_database_instance()
        self.db_create = DatabaseCreate()
    
    def delete_all_tables(self):
        """ Used only for unit tests. It was put here so the database itself did not have distructive code in it """
        for table in self.db_instance.get_table_names():
            self.delete_table(table)
        self.db_instance.commit()
        
    def delete_table(self,table):
        self.db_instance.execute('DROP TABLE %s' % table)

    def create_some_test_wells(self):
        if not 'Well' in self.db_instance.get_table_names():
            self.db_create.well()
            
        statement = """
            INSERT INTO Well VALUES(1,'SKWI111062705025W300','SK','Oil','OL',1,'New Oil','Heavy',0,0.25,'2014-12-01 00:00:00',1);
            INSERT INTO Well VALUES(2,'SKWI112062705025W300','SK','Oil','OL',2,'Third Tier Oil','Southwest',0,0.95,'2014-12-01 00:00:00',1);
            INSERT INTO Well VALUES(3,'SKWI113062705025W300','SK','Oil','OL',2,'Fourth Tier Oil','Other',0,1.0,NULL,NULL);
            INSERT INTO Well VALUES(4,'SKWI114062705025W300','SK','Oil','OL',3,'Old Oil','Other',0,1.0,NULL,NULL);
        """
        
        self.db_instance.execute_statement(statement)

    def create_some_test_royalties(self):
        if not 'RoyaltyMaster' in self.db_instance.get_table_names():
            self.db_create.royalty_master()

        statement = """
            INSERT INTO RoyaltyMaster VALUES(3, 'OL', 'All', 'SKProvCrownVar, GORR', 1.2, 50, 'SaskWellHead', 'Y', 'Y', 'mprod,250,2,300,3,400,4,500,5,0,6', NULL)
        """

        self.db_instance.execute_statement(statement)


    def create_some_test_monthly(self):
        if not 'Monthly' in self.db_instance.get_table_names():
            self.db_create.monthly()

        statement = """
            INSERT INTO Monthly Values(4, '2015-09-29 00:00:00', 201501, 4, 'Oil', 2, 740, 100, 2.2, 221.123456, 2.123455, 0.123455)
        """
        self.db_instance.execute_statement(statement)

    def create_calc(self):
        if not 'Calc' in self.db_instance.get_table_names():
            self.db_create.calc()

    def create_some_test_leases(self):
        if not 'Lease' in self.db_instance.get_table_names():
            self.db_create.lease()
        statement = """
            INSERT INTO Lease VALUES(1,'OL','SK',123,2345,NULL);
            INSERT INTO Lease VALUES(2,'OL','SK',123,2346,NULL);
            INSERT INTO Lease VALUES(3,'OL','SK',123,2347,NULL);
        """
        self.db_instance.execute_statement(statement)

    def create_some_test_econdata(self):
        if not 'ECONdata' in self.db_instance.get_table_names():
            self.db_create.econ_oil()
        statement = """
            INSERT INTO ECONData VALUES(39,'Jan.',201501,181,223,262,0.0934,2.34,23.12,1734,21.73,502,27.11,626,0.1085,2.71,
            26.84,2013,32.38,747,38.94,899,0.1181,2.95,29.22,2192,35.58,821,40.54,936,52.3,1207);
        """
        self.db_instance.execute_statement(statement)
 def __init__(self):
     self.db_instance = config.get_database_instance()
     self.db_create = DatabaseCreate()
class DatabaseUtilities(object):

    def __init__(self):
        self.db_instance = config.get_database_instance()
        self.db_create = DatabaseCreate()
    
    def delete_all_tables(self):
        """ Used only for unit tests. It was put here so the database itself did not have distructive code in it """
        for table in self.db_instance.get_table_names():
            self.delete_table(table)
        self.db_instance.commit()
        
    def delete_table(self,table):
        self.db_instance.execute('DROP TABLE %s' % table)

    def create_some_test_wells(self):
        if not 'Well' in self.db_instance.get_table_names():
            self.db_create.well()
            
        statement = """
            INSERT INTO Well VALUES(1,'SKWI111062705025W300','SK','Oil','OL',1,'New Oil','Heavy',0,0.25,'2014-12-01 00:00:00',1);
            INSERT INTO Well VALUES(2,'SKWI112062705025W300','SK','Oil','OL',2,'Third Tier Oil','Southwest',0,0.95,'2014-12-01 00:00:00',1);
            INSERT INTO Well VALUES(3,'SKWI113062705025W300','SK','Oil','OL',2,'Fourth Tier Oil','Other',0,1.0,NULL,NULL);
            INSERT INTO Well VALUES(4,'SKWI114062705025W300','SK','Oil','OL',3,'Old Oil','Other',0,1.0,NULL,NULL);
        """
        
        self.db_instance.execute_statement(statement)

    def create_some_test_royalties(self):
        if not 'RoyaltyMaster' in self.db_instance.get_table_names():
            self.db_create.royalty_master()

        statement = """
            INSERT INTO RoyaltyMaster VALUES(3, 'OL', 'All', 'SKProvCrownVar, GORR', 1.2, 50, 'SaskWellHead', 'Y', 'Y', 'mprod,250,2,300,3,400,4,500,5,0,6', NULL)
        """

        self.db_instance.execute_statement(statement)


    def create_some_test_monthly(self):
        if not 'Monthly' in self.db_instance.get_table_names():
            self.db_create.monthly()

        statement = """
            INSERT INTO Monthly Values(4, '2015-09-29 00:00:00', 201501, 4, 'Oil', 2, 740, 100, 2.2, 221.123456, 2.123455, 0.123455)
        """
        self.db_instance.execute_statement(statement)

    def create_calc(self):
        if not 'Calc' in self.db_instance.get_table_names():
            self.db_create.calc()

    def create_some_test_leases(self):
        if not 'Lease' in self.db_instance.get_table_names():
            self.db_create.lease()
        statement = """
            INSERT INTO Lease VALUES(1,'OL','SK',123,2345,NULL);
            INSERT INTO Lease VALUES(2,'OL','SK',123,2346,NULL);
            INSERT INTO Lease VALUES(3,'OL','SK',123,2347,NULL);
        """
        self.db_instance.execute_statement(statement)

    def create_some_test_econdata(self):
        if not 'ECONdata' in self.db_instance.get_table_names():
            self.db_create.econ_data()
        statement = """
            INSERT INTO ECONData VALUES(39,'Jan.',201501,181,223,262,0.0934,2.34,23.12,1734,21.73,502,27.11,626,0.1085,2.71,
            26.84,2013,32.38,747,38.94,899,0.1181,2.95,29.22,2192,35.58,821,40.54,936,52.3,1207);
        """
        self.db_instance.execute_statement(statement)
Ejemplo n.º 15
0
def drop_create_tables():
    dbu = DatabaseUtilities()
    db_create = DatabaseCreate()

    dbu.delete_all_tables()
    db_create.create_all()
class SqliteDatabaseTest(unittest.TestCase):

    def setUp(self):
        self.assertEqual(config.get_environment(),'unittest') # Distructive Tests must run in unittest enviornment
        self.dbi = config.get_database_instance()
        self.db = config.get_database()
        self.dbu = DatabaseUtilities()
        self.db_create = DatabaseCreate()
        
        self.dbu.delete_all_tables()
        
    def test_to_db_value(self):
        self.assertEqual('123', self.db.to_db_value(123))
        self.assertEqual('"asdf"', self.db.to_db_value("asdf"))
        self.assertEqual('123.45', self.db.to_db_value(123.45))
        self.assertEqual('1', self.db.to_db_value(True))
        self.assertEqual('0', self.db.to_db_value(False))
        self.assertEqual('null', self.db.to_db_value(None))
        
    def test_get_data_structure(self):
        ds = self.db.get_data_structure('WhatEver')
        self.assertEqual('WhatEver',ds._table_name)

    def test_select(self):
        self.dbu.create_some_test_well_royalty_masters()
        self.dbu.create_some_test_leases()
        
        self.assertIsNotNone(self.db.select('Well', RoyaltyClassification='New Oil'))
        self.assertEqual(len(self.db.select('Well', Prov='SK', Classification='Other')), 2)
        self.assertEqual(len(self.db.select('Well')), 4)
        self.assertRaises(AppError, self.db.select, 'WrongTable')
        self.assertRaises(AppError, self.db.select, 'WrongTable',WrongAttr='WhoCares')
        self.assertRaises(AppError, self.db.select, 'Well', Foo='bar')
        self.assertEqual(len(self.db.select('Lease')), 3)
        self.assertEqual(len(self.db.select('Lease', Prov='SK')), 3)
        self.assertEqual(len(self.db.select('Well', ID=1000)),0)
        
    def test_update(self):
        self.dbu.create_some_test_well_royalty_masters()

        # change all types of attributes, read another record and then read the record again to make sure the changes were made.
        well = self.db.select('Well', ID=2)
        well[0].UWI = 'Changed'
        well[0].LeaseID = 100
        well[0].CommencementDate = '2016-02-01 00:00:00'
        well[0].WellType = None
        self.db.update(well[0])
        well = self.db.select('Well', ID=1)
        self.assertEqual(well[0].ID, 1)
        self.assertEqual(well[0].UWI, 'SKWI111062705025W300')
        well = self.db.select('Well', ID=2)
        self.assertEqual(well[0].ID, 2)
        self.assertEqual(well[0].UWI, 'Changed')
        self.assertEqual(well[0].LeaseID, 100)
        self.assertEqual(well[0].CommencementDate, datetime(2016,2,1,0,0))
        self.assertEqual(well[0].WellType, None)

        ds = DataStructure()
        self.assertRaises(AttributeError, self.db.update, ds)
        
        ds._table_name = 'Well'
        self.assertRaises(AttributeError, self.db.update, ds)
        ds.ID = 100
        self.assertRaises(AppError, self.db.update, ds)

    def test_insert(self):
        self.db_create.well()
        
        well = DataStructure()
        well.UWI = 'UWI for this well'
        # Should raise this error since we need to get the structure from the database
        self.assertRaises(TypeError, self.db.insert)

        well = self.db.get_data_structure('Well')
        well.UWI = 'UWI for this well'
        self.db.insert(well)
        self.assertEqual(well.ID, 1)
        
        well = self.db.get_data_structure('Well')
        well.UWI = 'Different UWI for this well'
        self.db.insert(well)
        self.assertEqual(well.ID, 2)
        
        well = self.db.select('Well', ID=1)
        self.assertEqual(well[0].ID, 1)
        self.assertEqual(well[0].UWI, 'UWI for this well')
        
        well = self.db.get_data_structure('Well')
        well.UWI = 'Next Well UWI'
        well.ID = 10
        self.db.insert(well)
        
        well = self.db.select('Well', ID=1)
        self.assertEqual(well[0].ID, 1)
        self.assertEqual(well[0].UWI, 'UWI for this well')
        
        well = self.db.select('Well', ID=10)
        self.assertEqual(well[0].ID, 10)
        self.assertEqual(well[0].UWI, 'Next Well UWI')

        well = self.db.get_data_structure('Well')
        well.UWI = 'Just One More'
        self.db.insert(well)
        self.assertEqual(well.ID, 11)
        

        well = self.db.get_data_structure('Well')
        well.BadAttr = 'Just another value'
        self.assertRaises(AppError, self.db.insert,well)
        
        # if the ID is None,Blank,or zero we shold still be able to insert a record
        well = self.db.get_data_structure('Well')
        well.ID = None
        well.UWI = 'Just One More'
        self.db.insert(well)
        self.assertEqual(well.ID, 12)
        well.ID = 0
        self.db.insert(well)
        self.assertEqual(well.ID, 13)
        well.ID = ''
        self.db.insert(well)
        self.assertEqual(well.ID, 14)
        
        
    def test_delete(self):
        self.dbu.create_some_test_well_royalty_masters()

        self.assertEqual(4,len(self.db.select('Well')))
                         
        self.db.delete('Well', 2)
        self.assertEqual(3,len(self.db.select('Well')))
        self.assertEqual(0, len(self.db.select('Well', ID=2)))
        
    def test_date_format(self):
        self.dbu.create_some_test_well_royalty_masters()
        
        well = self.db.select('Well', ID=1)
        self.assertTrue(isinstance(well[0].CommencementDate,datetime))
    def test_all(self):
        dbu = DatabaseUtilities()
        dbc = DatabaseCreate()

        dbu.delete_all_tables()
        dbc.create_all()
Ejemplo n.º 18
0
def drop_create_tables():
    dbu = DatabaseUtilities()
    db_create = DatabaseCreate()

    dbu.delete_all_tables()
    db_create.create_all()
    def test_all(self):
        dbu = DatabaseUtilities()
        dbc = DatabaseCreate()

        dbu.delete_all_tables()
        dbc.create_all()