Пример #1
0
class Test(unittest.TestCase):
    
    def setUp(self):
        # For a fresh test each time, we copy the unittest database
        # in a temporary directory and work with that temporary database.
        tmp_dir = tempfile.gettempdir()
        tmp_dir = os.path.join(tmp_dir, '.montehelper')
        try:
            os.makedirs(tmp_dir)
        except OSError as exception:
            if exception.errno != errno.EEXIST:
                raise
        tmp_database = os.path.join(tmp_dir, 'montehelper_unittest.db')
        # Copy unittest database to temporary directory
        shutil.copy('/home/oliver/montehelper_unittest.db', tmp_database)
        self.db = Database()
        self.db.dbfile = tmp_database

    def tearDown(self):
        pass
    
    def test_getadults(self):
        l = self.db.getadults()
        # Order the list
        l.sort(key=itemgetter('adult_id'))
        # This is the result we expect
        el = [
            {'adult_id':1,'firstname':'Dirk','name':'Seifert','work':'03334-288548'},
            {'adult_id':2,'firstname':'Oliver','name':'Bienert','home':'03334-3693346'},
            {'adult_id':3, 'firstname':'Jenny','name':'Ehlert'},
            {'adult_id':4, 'firstname':'Sabrina','name':'Bonk'}
        ]
        self.__comparelstdicts(l, el)

    def test_getchildren(self):
        l = self.db.getchildren()
        # Order the list
        l.sort(key=itemgetter('child_id'))
        # This is the result we expect
        el = [
            {'child_id':5,'firstname':'Deborian','name':'Brosche','birthdate':parse('2002-10-29'),'yearcalculated':3, 'yc_description':'3rd year', 'ry_description':'2nd year'},
            {'child_id':6,'firstname':'Tarek','name':'Eglin','birthdate':parse('2003-03-04'),'yearcalculated':3, 'yc_description':'3rd year', 'ry_description':None},
            {'child_id':7,'firstname':'Paul','name':'Bonk','birthdate':parse('2003-02-24'),'yearcalculated':3, 'yc_description':'3rd year', 'ry_description':None},
            {'child_id':8,'firstname':'Ida','name':'Bonk','birthdate':parse('2006-10-10'),'yearcalculated':0, 'yc_description':'Kindergarten', 'ry_description':None}
        ]
        self.__comparelstdicts(l, el)
        
    def test_getadult(self):
        d = self.db.getadult(1)
        # The expected result
        ed = {'adult_id':1,'firstname':'Dirk','name':'Seifert','address_id':1,'street':'Schicklerstr.','number':'1','postcode':'16225','city':'Eberswalde','householdsize':5}
        self.__comparedicts(d, ed)
        
    def test_getfonnumbers(self):
        l = self.db.getfonnumbers(1)
        # Order the list
        l.sort(key=itemgetter('fonnumber_id'))
        # Expected result
        el = [{'fonnumber_id':1,'number':'03334-288548','fonnumbertype_id':'work','description':'Business'}]
        self.__comparelstdicts(l, el)
        
    def test_getemail(self):
        l = self.db.getemail(1)
        # Order the list
        l.sort(key=itemgetter('email_id'))
        # Expected result
        el = [{'email_id':1,'email':'*****@*****.**','note':'Allgemeine Schuladresse'}]
        self.__comparelstdicts(l, el)
        
    def test_getincome(self):
        l = self.db.getincome(1)
        # Order the list
        l.sort(key=itemgetter('income'))
        # Expected result
        el = [
            {'incometype_id':'less','income':85,'description':'Less'},
            {'incometype_id':'salary','income':1355,'description':'Salary'}
        ]
        self.__comparelstdicts(l, el)
        
    def test_getpositions(self):
        l = self.db.getpositions(1)
        # Order the list
        l.sort(key=itemgetter(0))
        # Expected result
        el = [('organization',),('staff',)]
        self.__comparelists(l, el)
        
    def test_getchild(self):
        d = self.db.getchild(5)
        # The expected result
        ed = {'child_id':5,'firstname':'Deborian','name':'Brosche','birthdate':parse('2002-10-29'),'year':2,'benefit':185,'yearcalculated':3, 'yc_description':'3rd year', 'joindate':None, 'separationdate':None}
        self.__comparedicts(d, ed)
        
    def test_getparents(self):
        l = self.db.getparents(5)
        # Order the list
        l.sort(key=itemgetter('adult_id'))
        el = [{'adult_id': 1,'name':'Seifert','firstname':'Dirk','relation_id':'father','description':'Father','calc':True,'pay':True}]
        self.__comparelstdicts(l, el)
        
    def test_getrulings(self):
        l = self.db.getrulings(5)
        # Order the list
        l.sort(key=itemgetter('ruling_id'))
        el = [{'ruling_id':'extendeddaytime','description':'Extended day time','startdate':parse('2012-08-01'),'enddate':parse('2013-07-31')}]
        self.__comparelstdicts(l, el)
        
    def test_getadults_for_calcfee(self):
        l = self.db.getadults_for_calcfee(1)
        # Order the list
        l.sort(key=itemgetter('income'))
        el = [
            {'adult_id':1,'name':'Seifert','firstname':'Dirk','relation_id':'father','income':85,'incometype_id':'less','size':5,'flags':3},
            {'adult_id':1,'name':'Seifert','firstname':'Dirk','relation_id':'father','income':1355,'incometype_id':'salary','size':5,'flags':3}
        ]
        self.__comparelstdicts(l, el)
        
    def test_getchildren_for_calcfee(self):
        l = self.db.getchildren_for_calcfee(1, '2013-02-05')
        # Order the list
        l.sort(key=itemgetter('child_id'))
        # Expected result
        el = [{'child_id':5,'name':'Brosche','firstname':'Deborian','birthdate':parse('2002-10-29'),'benefit':185,'year_id':2,'exdt':1}]
        self.__comparelstdicts(l, el)
        
    def test_getaddress(self):
        d = self.db.getaddress(1)
        # Expected result
        ed = {'name':'Seifert','firstname':'Dirk','street':'Schicklerstr.','number':'1','postcode':'16225', 'city':'Eberswalde'}
        self.__comparedicts(d, ed)
        
    def test_getlookup(self):
        # Get the data from the test database
        d = self.db.getlookup('l_fonnumbertypes')
        # Make a ordered dictionary from the result
        d = collections.OrderedDict(sorted(d.items()))
        # This is the result we expect
        ed = {'home':'Private','work':'Business','mobile':'Mobile'}
        ed = collections.OrderedDict(sorted(ed.items()))
        self.failUnless(len(d) == len(ed), "Database query returned %d records, should be %d" % (len(d), len(ed)))
        for a, b in zip(d, ed):
            self.failIf(a != b, "Keys %s and %s doesn't match" % (a, b))
            self.failIf(d[a] != ed[b], "Values %s and %s doesn't match" % (d[a], ed[b]))
            
    def test_setadult(self):
        data = {'name':'Brosche','firstname':'Dorothee','householdsize':5,'street':'Schicklerstr.','number':'1','postcode':'16225','city':'Eberswalde'}
        positions = ('organization',)
        fonnumbers = [
            {'fonnumbertype_id':'work','number':'03334-288548'},
            {'fonnumbertype_id':'home','number':'03334-123456'}
        ]
        email = [
            {'email':'*****@*****.**','note':'Privat'}
        ]
        income = [
            {'income':85,'incometype_id':'less'},
            {'income':35,'incometype_id':'misc'},
            {'income':700,'incometype_id':'salary'}
        ]
        r = self.db.setadult(data, positions, fonnumbers, email, income)
        self.failUnless(r == True, "Database statement rolled back")
        d = self.db.getadult(9)
        # The expected result
        ed = {'adult_id':9,'firstname':'Dorothee','name':'Brosche','address_id':1,'street':'Schicklerstr.','number':'1','postcode':'16225','city':'Eberswalde','householdsize':5}
        self.__comparedicts(d, ed)
        l = self.db.gettable('addresses')
        # Order the list
        l.sort(key=itemgetter('address_id'))
        el = [
            {'address_id':1,'street':'Schicklerstr.','number':'1','postcode':'16225', 'city':'Eberswalde'},
            {'address_id':2,'street':'Ruhlaer Str.','number':'18','postcode':'16225', 'city':'Eberswalde'},
            {'address_id':3,'street':'Karlswerk','number':'18','postcode':'16248', 'city':'Niederfinow'}
        ]
        self.__comparelstdicts(l, el)
        l = self.db.gettable('positions', getdict=False)
        # Order the list
        l.sort()
        el = ((1,'organization'),(1,'staff'),(9,'organization'))
        self.__comparelists(l, el)
        l = self.db.gettable('adults_fonnumbers', getdict=False)
        # Order the list
        l.sort()
        el = ((1,1,'work'),(2,2,'home'),(9,1,'work'),(9,3,'home'))
        self.__comparelists(l, el)
        l = self.db.gettable('fonnumbers', getdict=False)
        # Order the list
        l.sort()
        el = ((1,'03334-288548'),(2,'03334-3693346'),(3,'03334-123456'))
        self.__comparelists(l, el)
        l = self.db.gettable('adults_emailaddresses', getdict=False)
        # Order the list
        l.sort()
        el = ((1,1,'Allgemeine Schuladresse'),(9,2,'Privat'))
        self.__comparelists(l, el)
        l = self.db.gettable('emailaddresses', getdict=False)
        # Order the list
        l.sort()
        el = ((1,'*****@*****.**'),(2,'*****@*****.**'))
        self.__comparelists(l, el)
        l = self.db.gettable('income', getdict=False)
        # Order the list
        l.sort()
        el = ((1,85,'less'),(1,1355,'salary'),(4,1500,'unemployment'),(9,35,'misc'),(9,85,'less'),(9,700,'salary'))
        self.__comparelists(l, el)
    
    def test_setchild(self):
        data = {'name':'Ehlert','firstname':'Annabelle','birthdate':parse('2005-12-15'),'benefit':185, 'joindate':parse('2008-08-01'), 'separationdate':None}
        parents = [
            {'adult_id':3,'relation_id':'mother','calc':True,'pay':True}
        ]
        rulings = [
            {'ruling_id':'extendeddaytime','description':'Extended day time','startdate':parse('2012-08-01'),'enddate':parse('2013-07-31')}
        ]
        r = self.db.setchild(data, parents, rulings)
        self.failUnless(r == True, "Database statement rolled back")
        d = self.db.getchild(9)
        ed = {'child_id':9,'firstname':'Annabelle','name':'Ehlert','birthdate':parse('2005-12-15'),'year':None,'benefit':185,'yearcalculated':0, 'yc_description':'Kindergarten', 'joindate':parse('2008-08-01'), 'separationdate':None}
        self.__comparedicts(d, ed)   
        l = self.db.gettable('childbenefit', getdict=False)
        # Order the list
        l.sort()
        el = ((5,185),(6,185),(9,185))
        self.__comparelists(l, el)
        l = self.db.gettable('realyear', getdict=False)
        # Order the list
        l.sort()
        el = ((5,1),)
        self.__comparelists(l, el)
        l = self.db.gettable('adults_children', getdict=False)
        # Order the list
        l.sort()
        el = ((1,5,'father',3),(3,9,'mother',3),(4,7,'mother',3),(4,8,'mother',3))
        self.__comparelists(l, el)
        l = self.db.gettable('rulings', getdict=False)
        # Order the list
        l.sort()
        el = ((5,'extendeddaytime','2012-08-01','2013-07-31'),(6,'extendeddaytime','2012-08-01','2013-07-31'),(8,'extendeddaytime','2012-08-01','2013-07-31'),(9,'extendeddaytime','2012-08-01T00:00:00','2013-07-31T00:00:00'))
        self.__comparelists(l, el)
        
    def test_getchildren_for_quarterannouncement(self):
        l = self.db.getchildren_for_quarterannouncement('2012-12-01')
        # Order the list
        l.sort(key=itemgetter('child_id'))
        # Expected results
        el = [
            {'child_id':5, 'name': 'Brosche', 'firstname': 'Deborian', 'birthdate':parse('2002-10-29'),'address':'Schicklerstr. 1','postcode':'16225','city':'Eberswalde','district':'BAR','year_id':2,'exdt':1},
            {'child_id':7, 'name': 'Bonk', 'firstname': 'Paul', 'birthdate':parse('2003-02-24'),'address':'Karlswerk 18','postcode':'16248','city':'Niederfinow','district':'BAR','year_id':3,'exdt':None},
            {'child_id':8, 'name': 'Bonk', 'firstname': 'Ida', 'birthdate':parse('2006-10-10'),'address':'Karlswerk 18','postcode':'16248','city':'Niederfinow','district':'BAR','year_id':0,'exdt':1}
        ]
        self.__comparelstdicts(l, el)


    def __comparelstdicts(self, l, el):
        self.failUnless(len(l) == len(el), "Database query returned %d records, should be %d" % (len(l), len(el)))
        for a, b in zip(l, el):
            self.__comparedicts(a, b)

    def __comparedicts(self, a, b):
        # a and b are dictionaries. Order them.
        oa = collections.OrderedDict(sorted(a.items()))
        ob = collections.OrderedDict(sorted(b.items()))
        for c, d in zip(oa, ob):
            self.failIf(c != d, "Keys %s and %s doesn't match" % (c, d))
            self.failIf(oa[c] != ob[d], "Values %s and %s doesn't match" % (oa[c], ob[d]))
            
    def __comparelists(self, l, el):
        self.failUnless(len(l) == len(el), "Database query returned %d records, should be %d" % (len(l), len(el)))
        for a, b in zip(l, el):
            self.failIf(a != b, "Values %s and %s doesn't match" % (a, b))
Пример #2
0
class Listeners(object):
    '''
    classdocs
    '''
    def __init__(self, **params):
        self.db = Database()
        pub.subscribe(self.CheckDatabase, 'main.checkdatabase') #@UndefinedVariable
        pub.subscribe(self.DialogAdultPopulate, 'dialog.adult.activated') #@UndefinedVariable
        pub.subscribe(self.StoreAdult, 'dialog.adult.senddata') #@UndefinedVariable
        pub.subscribe(self.CalculateParentsFee, 'mainframe.edit.calcfee') #@UndefinedVariable
        pub.subscribe(self.CreateFeeReport, 'dialog.calcfree.senddata') #@UndefinedVariable
        pub.subscribe(self.DialogChildPopulate, 'dialog.child.activated') #@UndefinedVariable
        pub.subscribe(self.DialogParentPopulate, 'dialog.parent.created') #@UndefinedVariable
        pub.subscribe(self.DialogChildNewParent, 'dialog.parent.objectselected') #@UndefinedVariable
        pub.subscribe(self.StoreChild, 'dialog.child.senddata') #@UndefinedVariable
        pub.subscribe(self.DialogPreferencesPopulate, 'dialog.preferences.activated') #@UndefinedVariable
        pub.subscribe(self.StorePreferences, 'dialog.preferences.save') #@UndefinedVariable
        pub.subscribe(self.DeleteAdult, 'mainframe.delete.adult') #@UndefinedVariable
        pub.subscribe(self.DeleteChild, 'mainframe.delete.child') #@UndefinedVariable
        pub.subscribe(self.DialogAdultFillCB, 'dialog.adult.created') #@UndefinedVariable
        pub.subscribe(self.DialogChildFillCB, 'dialog.child.created') #@UndefinedVariable
        pub.subscribe(self.CreateQuarterAnnouncement, 'mainframe.edit.quarterannouncement')  #@UndefinedVariable
        
    def CheckDatabase(self, init=True):
        '''Called when the main widget is ready or when preferences dialog is closed'''
        # Get location of database file
        cfg = Preferences()
        dbfile = cfg.GetProperty('sqlite', 'dbfile')
        exists = os.path.exists(dbfile)
        if exists == True:
            # Instantiate database object
            adults = self.db.getadults()
            children = self.db.getchildren()
            # Populate notebook listviews
            pub.sendMessage('main.populate.adults', adults=adults) #@UndefinedVariable
            pub.sendMessage('main.populate.children', children=children) #@UndefinedVariable
        elif exists == False and init == True:
            pub.sendMessage('main.activatepreferencesdlg') #@UndefinedVariable
        else:
            sys.exit()

    def DialogAdultPopulate(self, arg1):
        '''Called when EditAdultFrame is activated and ready to receive data'''
        # Query database and get data for selected adult
        self.data = self.db.getadult(arg1)
        self.fonnumbers = self.db.getfonnumbers(arg1)
        self.email = self.db.getemail(arg1)
        self.income = self.db.getincome(arg1)
        positions = self.db.getpositions(arg1)
        # Send data to frame
        pub.sendMessage('dialog.populate.adult', #@UndefinedVariable
                         data=self.data,
                         positions=positions,
                         fonnumbers=self.fonnumbers,
                         email=self.email,
                         income=self.income)
        
    def StoreAdult(self, data, positions, fonnumbers, email, income):
        self.db.setadult(data, positions, fonnumbers, email, income)
        # Refresh main frame listview
        adults = self.db.getadults()
        pub.sendMessage('main.populate.adults', adults=adults) #@UndefinedVariable
        
    def CalculateParentsFee(self, adult_id):
        isodate = '{:%Y-%m-%d}'.format(datetime.now())
        adults = self.db.getadults_for_calcfee(adult_id)
        if len(adults) == 0:
            logger.info('No children found for this adult: %i' % adult_id)
            return
        children = self.db.getchildren_for_calcfee(adult_id, isodate)
        fees = self.db.getfees_lookup()
        cf = CalcFee()
        data = cf.calc(adults, children, fees)
        address = self.db.getaddress(data['payer'])
        pub.sendMessage('dialog.calcfee.populate', data=data, address=address)  #@UndefinedVariable
        
    def CreateFeeReport(self, validfromdate, receiver, calcfees, income, notes):
        ch = Report()
        ch.createFeeReport(
            validfromdate=validfromdate, 
            receiver=receiver, 
            fees=calcfees,
            income=income, 
            notes=notes
        )

    def DialogChildPopulate(self, arg1):
        data = self.db.getchild(arg1)
        parents = self.db.getparents(arg1)
        rulings = self.db.getrulings(arg1)
        pub.sendMessage('dialog.child.populate', data=data, parents=parents, rulings=rulings) #@UndefinedVariable
        
    def DialogParentPopulate(self):
        self.adults = self.db.getadults()
        pub.sendMessage('dialog.parent.populate', data=self.adults) #@UndefinedVariable
        
    def DialogChildNewParent(self, data):
        pub.sendMessage('dialog.child.newparent', data=data) #@UndefinedVariable

    def StoreChild(self, data, parents, rulings):
        self.db.setchild(data, parents, rulings)
        children = self.db.getchildren()
        pub.sendMessage('main.populate.children', children=children) #@UndefinedVariable
    
    def DialogPreferencesPopulate(self):
        dct = {}
        cfg = Preferences()
        dct['dbfile'] = cfg.GetProperty('sqlite', 'dbfile')
        pub.sendMessage('dialog.preferences.populate', data=dct) #@UndefinedVariable
        
    def StorePreferences(self, data):
        cfg = Preferences()
        cfg.SetProperty('sqlite', 'dbfile', data['dbfile'])
        cfg.WriteConfig()
        
    def DeleteAdult(self, person_id):
        self.db.deleteadult(person_id)
        adults = self.db.getadults()
        pub.sendMessage('main.populate.adults', adults=adults) #@UndefinedVariable
        
    def DeleteChild(self, person_id):
        self.db.deletechild(person_id)
        children = self.db.getchildren()
        pub.sendMessage('main.populate.children', children=children) #@UndefinedVariable
        
    def DialogAdultFillCB(self):
        '''Get look-up values from database and send data to editadultframe to fill comboboxes'''
        dct = {}
        dct['fontype'] = self.db.getlookup('l_fonnumbertypes')
        dct['incometype'] = self.db.getlookup('l_incometypes')
        pub.sendMessage('editadultframe.fillcomboboxes', dct=dct) #@UndefinedVariable

    def DialogChildFillCB(self):
        '''Get look-up values from database and send data to editadultframe to fill comboboxes'''
        dct = {}
        dct['year'] = self.db.getlookup('l_year')
        dct['ruling'] = self.db.getlookup('l_rulings')
        dct['relation'] = self.db.getlookup('l_relations')
        pub.sendMessage('editchildframe.fillcomboboxes',  dct=dct) #@UndefinedVariable
        
    def CreateQuarterAnnouncement(self, date):
        setattr(self.db, 'querydate', date)
        isodate = '{:%Y-%m-%d}'.format(date)
        children = self.db.getchildren_for_quarterannouncement(isodate)
        ch = Report()
        ch.createQAReport(children, isodate)