def setUp(self):
        self.assertEqual(config.get_environment(),'unittest') # Distructive Tests must run in unittest enviornment
        db_utils = DatabaseUtilities()
        db_utils.delete_all_tables()

        instance = config.get_database_instance()
        instance.execute('create table ' + self.TEST_TABLE_NAME  + ' (myKey int, myText text, myDate date)')
        instance.commit()
        
        instance.execute('insert into ' + self.TEST_TABLE_NAME + " values (1, 'Test Item One', '2016-02-14')")
        instance.execute('insert into ' + self.TEST_TABLE_NAME + " values (2, 'Test Item Two', '2016-02-14')")
        instance.commit()
    def test_get_column_name(self):
        instance = config.get_database_instance()
        db_utils = DatabaseUtilities()
        db_utils.delete_all_tables()

        instance.execute('create table ' + self.TEST_TABLE_NAME  + ' (myKey int, myText text, myDate date)')
        instance.execute('insert into ' + self.TEST_TABLE_NAME + " values (1, 'Test Item One', '2016-02-14')")
        
        instance.execute('select * from ' + self.TEST_TABLE_NAME)
        
        columns = instance.get_column_names()
        self.assertEqual(columns[0],'myKey')
        self.assertEqual(columns[1],'myText')
        self.assertEqual(columns[2],'myDate')
    def test_get_column_name(self):
        instance = config.get_database_instance()
        db_utils = DatabaseUtilities()
        db_utils.delete_all_tables()

        instance.execute('create table ' + self.TEST_TABLE_NAME +
                         ' (myKey int, myText text, myDate date)')
        instance.execute('insert into ' + self.TEST_TABLE_NAME +
                         " values (1, 'Test Item One', '2016-02-14')")

        instance.execute('select * from ' + self.TEST_TABLE_NAME)

        columns = instance.get_column_names()
        self.assertEqual(columns[0], 'myKey')
        self.assertEqual(columns[1], 'myText')
        self.assertEqual(columns[2], 'myDate')
    def setUp(self):
        self.assertEqual(
            config.get_environment(),
            'unittest')  # Distructive Tests must run in unittest enviornment
        db_utils = DatabaseUtilities()
        db_utils.delete_all_tables()

        instance = config.get_database_instance()
        instance.execute('create table ' + self.TEST_TABLE_NAME +
                         ' (myKey int, myText text, myDate date)')
        instance.commit()

        instance.execute('insert into ' + self.TEST_TABLE_NAME +
                         " values (1, 'Test Item One', '2016-02-14')")
        instance.execute('insert into ' + self.TEST_TABLE_NAME +
                         " values (2, 'Test Item Two', '2016-02-14')")
        instance.commit()
    def test_calc_royalties(self):
        dbu = DatabaseUtilities()
        dbu.delete_all_tables()
        dbu.create_some_test_econdata()

        pr = ProcessRoyalties()
        well = DataStructure()
        royalty = DataStructure()
        calc = DataStructure()
        monthly = DataStructure()
        well_lease_link = DataStructure()
        rtp_info = DataStructure()

        monthly.Product = "OIL"
        monthly.ProdMonth = 201501
        monthly.ProdVol = 100
        monthly.ProdHours = 744
        monthly.SalesPrice = 210.0
        royalty.RoyaltyScheme = "SKProvCrownVar"
        royalty.OverrideRoyaltyClassification = None
        royalty.ValuationMethod = "ActSales"
        royalty.CrownModifier = None
        royalty.MinRoyaltyRate = None
        royalty.CrownMultiplier = 1.0
        royalty.MinRoyaltyDollar = None
        royalty.TransDeducted = None
        well.CommencementDate = date(2015, 1, 22)
        well.RoyaltyClassification = "Old Oil"
        well.Classification = "Other"
        well.SRC = 0.0
        well_lease_link.PEFNInterest = 1.0
        rtp_info.Percent = 100.0
        calc.SuppRoyaltyValue = 0.0
        calc.GorrRoyaltyValue = 0.0
        calc.TransGorrValue = 0.0

        pr.calc_royalties(well, royalty, calc, monthly, well_lease_link, rtp_info)

        royalty.RoyaltyScheme = "IOGR1995"
        pr.calc_royalties(well, royalty, calc, monthly, well_lease_link, rtp_info)

        royalty.RoyaltyScheme = "IOGR1995,GORR"
        royalty.Gorr = "fixed,0,.02"
        pr.calc_royalties(well, royalty, calc, monthly, well_lease_link, rtp_info)
    def test_process_monthly(self):
        db = config.get_database()
        dbu = DatabaseUtilities()
        dbu.delete_all_tables()
        dbu.create_some_test_well_royalty_masters()
        dbu.create_some_test_lease_royalty_masters()
        dbu.create_some_test_leases()
        dbu.create_some_test_well_lease_link()
        dbu.create_some_test_monthly()
        dbu.create_some_test_econdata()
        dbu.create_some_test_rtp_info()
        dbu.create_calc()
        pr = ProcessRoyalties()
        pr.process_one(4, 201501, "OIL")
        # Check to see if the oper record exists
        self.assertEqual(2, db.count("calc"))

        pr.process_all()
        self.assertEqual(3, db.count("calc"))
def drop_create_tables():
    dbu = DatabaseUtilities()
    dbu.delete_all_tables()
    create_tables()
Beispiel #8
0
def drop_create_tables():
    from tests.database.sqlite_utilities_test import DatabaseUtilities
    dbu = DatabaseUtilities()
    dbu.delete_all_tables()
    create_tables()
class SqliteDatabaseTest(unittest.TestCase):

    def setUp(self):
        self.assertEqual('unittest', config.get_environment())  # Destructive Tests must run in unittest environment
        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('WellRoyaltyMaster', RoyaltyClassification='New Oil'))
        self.assertEqual(len(self.db.select('WellRoyaltyMaster', Prov='SK', Classification='Other')), 2)
        self.assertEqual(len(self.db.select('WellRoyaltyMaster')), 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')), 4)
        self.assertEqual(len(self.db.select('Lease', Prov='SK')), 4)
        self.assertEqual(len(self.db.select('WellRoyaltyMaster', ID=1000)), 0)

    def test_select_date_effective(self):

        self.dbu.create_some_test_well_royalty_masters()
        self.assertEqual(4, len(self.db.select('WellRoyaltyMaster')))
        self.assertEqual(1, len(self.db.select('WellRoyaltyMaster', Date=datetime(2009, 1, 1, 12, 0, 0))))
        self.assertEqual(2, len(self.db.select('WellRoyaltyMaster', Date=datetime(2010, 9, 1, 12, 0, 0))))
        self.assertEqual(3, len(self.db.select('WellRoyaltyMaster', Date=datetime(2012, 1, 1, 12, 0, 0))))
        self.assertEqual(3, len(self.db.select('WellRoyaltyMaster', Date=datetime(2017, 1, 1, 12, 0, 0))))

        # WRITE TEST FOR SELECT1
        
    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('WellRoyaltyMaster', ID=2)
        well[0].WellEvent = '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('WellRoyaltyMaster', ID=1)
        self.assertEqual(well[0].ID, 1)
        self.assertEqual(well[0].WellEvent, 'SKWI111062705025W300')
        well = self.db.select('WellRoyaltyMaster', ID=2)
        self.assertEqual(well[0].ID, 2)
        self.assertEqual(well[0].WellEvent, 'Changed')
        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 = 'WellRoyaltyMaster'
        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_royalty_master()
        
        well = DataStructure()
        well.WellEvent = 'WellEvent 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('WellRoyaltyMaster')
        well.WellEvent = 'WellEvent for this well'
        self.db.insert(well)
        self.assertEqual(well.ID, 1)
        
        well = self.db.get_data_structure('WellRoyaltyMaster')
        well.WellEvent = 'Different WellEvent for this well'
        self.db.insert(well)
        self.assertEqual(well.ID, 2)
        
        well = self.db.select('WellRoyaltyMaster', ID=1)
        self.assertEqual(well[0].ID, 1)
        self.assertEqual(well[0].WellEvent, 'WellEvent for this well')
        
        well = self.db.get_data_structure('WellRoyaltyMaster')
        well.WellEvent = 'Next Well WellEvent'
        well.ID = 10
        self.db.insert(well)
        
        well = self.db.select('WellRoyaltyMaster', ID=1)
        self.assertEqual(well[0].ID, 1)
        self.assertEqual(well[0].WellEvent, 'WellEvent for this well')
        
        well = self.db.select('WellRoyaltyMaster', ID=10)
        self.assertEqual(well[0].ID, 10)
        self.assertEqual(well[0].WellEvent, 'Next Well WellEvent')

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

        well = self.db.get_data_structure('WellRoyaltyMaster')
        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('WellRoyaltyMaster')
        well.ID = None
        well.WellEvent = '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('WellRoyaltyMaster')))
                         
        self.db.delete('WellRoyaltyMaster', 2)
        self.assertEqual(3, len(self.db.select('WellRoyaltyMaster')))
        self.assertEqual(0, len(self.db.select('WellRoyaltyMaster', ID=2)))

    def test_count(self):
        self.dbu.create_some_test_well_royalty_masters()
        self.assertEqual(1, self.db.count('WellRoyaltyMaster', ID=1))
        self.assertEqual(4, self.db.count('WellRoyaltyMaster'))

    def test_date_format(self):
        self.dbu.create_some_test_well_royalty_masters()
        
        well = self.db.select('WellRoyaltyMaster', ID=1)
        self.assertTrue(isinstance(well[0].CommencementDate, datetime))
class SqliteDatabaseTest(unittest.TestCase):
    def setUp(self):
        self.assertEqual("unittest", config.get_environment())  # Destructive Tests must run in unittest environment
        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("WellRoyaltyMaster", RoyaltyClassification="New Oil"))
        self.assertEqual(len(self.db.select("WellRoyaltyMaster", Prov="SK", Classification="Other")), 2)
        self.assertEqual(len(self.db.select("WellRoyaltyMaster")), 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")), 4)
        self.assertEqual(len(self.db.select("Lease", Prov="SK")), 4)
        self.assertEqual(len(self.db.select("WellRoyaltyMaster", ID=1000)), 0)

    def test_select_date_effective(self):

        self.dbu.create_some_test_well_royalty_masters()
        self.assertEqual(4, len(self.db.select("WellRoyaltyMaster")))
        self.assertEqual(1, len(self.db.select("WellRoyaltyMaster", Date=datetime(2009, 1, 1, 12, 0, 0))))
        self.assertEqual(2, len(self.db.select("WellRoyaltyMaster", Date=datetime(2010, 9, 1, 12, 0, 0))))
        self.assertEqual(3, len(self.db.select("WellRoyaltyMaster", Date=datetime(2012, 1, 1, 12, 0, 0))))
        self.assertEqual(3, len(self.db.select("WellRoyaltyMaster", Date=datetime(2017, 1, 1, 12, 0, 0))))

        # WRITE TEST FOR SELECT1

    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("WellRoyaltyMaster", ID=2)
        well[0].WellEvent = "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("WellRoyaltyMaster", ID=1)
        self.assertEqual(well[0].ID, 1)
        self.assertEqual(well[0].WellEvent, "SKWI111062705025W300")
        well = self.db.select("WellRoyaltyMaster", ID=2)
        self.assertEqual(well[0].ID, 2)
        self.assertEqual(well[0].WellEvent, "Changed")
        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 = "WellRoyaltyMaster"
        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_royalty_master()

        well = DataStructure()
        well.WellEvent = "WellEvent 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("WellRoyaltyMaster")
        well.WellEvent = "WellEvent for this well"
        self.db.insert(well)
        self.assertEqual(well.ID, 1)

        well = self.db.get_data_structure("WellRoyaltyMaster")
        well.WellEvent = "Different WellEvent for this well"
        self.db.insert(well)
        self.assertEqual(well.ID, 2)

        well = self.db.select("WellRoyaltyMaster", ID=1)
        self.assertEqual(well[0].ID, 1)
        self.assertEqual(well[0].WellEvent, "WellEvent for this well")

        well = self.db.get_data_structure("WellRoyaltyMaster")
        well.WellEvent = "Next Well WellEvent"
        well.ID = 10
        self.db.insert(well)

        well = self.db.select("WellRoyaltyMaster", ID=1)
        self.assertEqual(well[0].ID, 1)
        self.assertEqual(well[0].WellEvent, "WellEvent for this well")

        well = self.db.select("WellRoyaltyMaster", ID=10)
        self.assertEqual(well[0].ID, 10)
        self.assertEqual(well[0].WellEvent, "Next Well WellEvent")

        well = self.db.get_data_structure("WellRoyaltyMaster")
        well.WellEvent = "Just One More"
        self.db.insert(well)
        self.assertEqual(well.ID, 11)

        well = self.db.get_data_structure("WellRoyaltyMaster")
        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("WellRoyaltyMaster")
        well.ID = None
        well.WellEvent = "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("WellRoyaltyMaster")))

        self.db.delete("WellRoyaltyMaster", 2)
        self.assertEqual(3, len(self.db.select("WellRoyaltyMaster")))
        self.assertEqual(0, len(self.db.select("WellRoyaltyMaster", ID=2)))

    def test_count(self):
        self.dbu.create_some_test_well_royalty_masters()
        self.assertEqual(1, self.db.count("WellRoyaltyMaster", ID=1))
        self.assertEqual(4, self.db.count("WellRoyaltyMaster"))

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

        well = self.db.select("WellRoyaltyMaster", ID=1)
        self.assertTrue(isinstance(well[0].CommencementDate, datetime))
Beispiel #11
0
    def test_all(self):
        dbu = DatabaseUtilities()
        dbc = DatabaseCreate()

        dbu.delete_all_tables()
        dbc.create_all()