def test_unit_price(): """Test unit price and unit price tax methods.""" # BaseEntry a_unitp = BaseEntry(quantity=2, price=100) assert a_unitp.get_price() == Decimal('200.00') assert a_unitp.get_unit_price() == Decimal('100.00') # MultiplyEntry b_unitp = MultiplyEntry(quantity=2, hour_rate='0:30') assert b_unitp.get_price(wage=Decimal('50.00')) == Decimal('50.00') assert b_unitp.get_unit_price(wage=Decimal('50.00')) == Decimal('25.00') # ConnectEntry c_unitp = ConnectEntry(quantity=3, multiplicator=2) # list creation liste = [] liste.append(b_unitp) liste.append(c_unitp) # linking liste[1].connect_entry(entry_list=liste, entry_id=liste[0].get_id()) assert c_unitp.get_price(entry_list=liste, wage=Decimal('50.00')) == Decimal('300.00') assert c_unitp.get_unit_price(entry_list=liste, wage=Decimal('50.00')) == Decimal('100.00')
def test_json_conversion_connectentry(): """Test the json conversion.""" # init the individual object a = ConnectEntry(title='Total individual', comment='Individual comment!', quantity=1.23, quantity_format='{F}:{R} min', is_time=True, multiplicator=9.99) c = BaseEntry() a.connect_entry([a, c], c.get_id()) assert c.get_id() in a.get_connected() assert a.get_id() not in a.get_connected() # make new default object b = ConnectEntry() # for now the values must not be the same assert b.get_id() != a.get_id() assert b.title != a.title assert b.comment != a.comment assert b.get_quantity() != a.get_quantity() assert b.quantity_format != a.quantity_format assert b.get_is_time() != a.get_is_time() assert b.get_multiplicator() != a.get_multiplicator() assert b.get_connected() != a.get_connected() # load a into b with json as object with same attributes b = ConnectEntry().from_json(js=a.to_json()) # now the values must be the same assert b.get_id() == a.get_id() assert b.title == a.title assert b.comment == a.comment assert b.get_quantity() == a.get_quantity() assert b.quantity_format == a.quantity_format assert b.get_is_time() == a.get_is_time() assert b.get_multiplicator() == a.get_multiplicator() assert b.get_connected() == a.get_connected() # load a into b with json as a preset (don't keep the id) b = ConnectEntry().from_json(js=a.to_json(), keep_id=False) # now the values must be the same - preset alike assert b.get_id() != a.get_id( ) # except the id, since it's "preset loading" assert b.title == a.title assert b.comment == a.comment assert b.get_quantity() == a.get_quantity() assert b.quantity_format == a.quantity_format assert b.get_is_time() == a.get_is_time() assert b.get_multiplicator() == a.get_multiplicator() assert b.get_connected() == a.get_connected()
class TestOffer(object): """Simple test offer.""" # generate some example entries b = BaseEntry(title='Base title', comment='Base comment', quantity=1, time=2.5, price=125) m = MultiplyEntry(title='Multiply title', comment='Multiply comment', quantity=0.5, hour_rate=4) c = ConnectEntry(title='Connect title', comment='Connect comment', quantity=3, is_time=True, multiplicator=2) # init the offer out = Offer(title='Testing offer') # add entries to its list out.append(b) out.append(m) out.append(c) assert len(out.get_entry_list()) == 3
class TestOfferB(object): """Simple test offer.""" # generate some example entries b = BaseEntry( title='Base title', comment='Base comment', quantity=1, time=2.5, price=125 ) m = MultiplyEntry( title='Multiply title', comment='Multiply comment', quantity=0.5, hour_rate=4 ) c = MultiplyEntry( title='Multiply title 2', comment='Multiply comment 2', quantity=4, hour_rate=3.15 ) d = ConnectEntry( title='Connect title 2', comment='Connect comment 2', quantity=9, is_time=False, multiplicator=0.25 ) # init the offer out = Offer( title='Testing offer B' ) # add entries to its list out.append(b) out.append(m) out.append(c) out.append(d) # connect 2nd entry to 4th entry out.get_entry_list()[3].connect_entry( entry_list=out.get_entry_list(), entry_id=out.get_entry_list()[1].get_id() ) # 4th entry get_price() should now return 0.5 * 4 * 9 * 0.25 * 50.00 = 225.00 wage = Decimal('50.00') p = round(Decimal(0.5 * 4 * 9 * 0.25) * wage, 2) assert out.get_entry_list()[3].get_price( entry_list=out.get_entry_list(), wage=wage ) == p assert len(out.get_entry_list()) == 4
def test_price_total(): """Test the total methods.""" off = Offer() off.append(BaseEntry(quantity=1.0, price=100.0, tax=19)) off.append(BaseEntry(quantity=1.0, price=10.0, tax=7)) off.append(ConnectEntry(quantity=1.0, tax=19, multiplicator=1)) off.get_entry_list()[2].connect_entry( entry_list=off.get_entry_list(), entry_id=off.get_entry_list()[1].get_id()) wage = Decimal('50.00') assert off.get_price_total(wage=wage) == Decimal('120') assert off.get_price_tax_total(wage=wage) == Decimal('21.6')
def test_baseentry_set_price(): """Set the price for BaseEntry.""" a = BaseEntry(quantity=1) a.set_price(3) # integer gets two decimal after comma assert a.get_price() == Decimal('3.00') # price should not changed due to wrong input a.set_price('pupsen') assert a.get_price() == Decimal('3.00')
def load_item_from_file(self, filename=None): """Load the item from the file.""" filename = str(filename) # cancel if the file does not exist if not os.path.isfile(filename): return False # try to load the file try: with open(filename, 'r') as f: dic = json.load(f) except Exception: return False # get type typ = False if 'item' in dic.keys(): if 'type' in dic['item'].keys(): typ = dic['item']['type'] # has no type, so cancel if not typ: return False # convert item json to Offer if typ == 'Offer': dic['item'] = Offer().from_json(js=dic['item']) # convert item json to Invoice elif typ == 'Invoice': dic['item'] = Invoice().from_json(js=dic['item']) # convert item json to BaseEntry elif typ == 'BaseEntry': dic['item'] = BaseEntry().from_json(js=dic['item']) # convert item json to MultiplyEntry elif typ == 'MultiplyEntry': dic['item'] = MultiplyEntry().from_json(js=dic['item']) # convert item json to ConnectEntry elif typ == 'ConnectEntry': dic['item'] = ConnectEntry().from_json(js=dic['item']) # otherwise cancel else: return False return dic
def NewBaseEntry(settings=None, global_list=None, client=None, project=None): """Return BaseEntry according to settings.""" is_settings = type(settings) is Settings is_client = type(client) is Client if not is_settings or not is_client: return BaseEntry() # get language from client lang = client.language # get replaces replace_dict = replacer( settings=settings, global_list=global_list, client=client, project=project ) title = settings.defaults[lang].baseentry_title.format_map(replace_dict) comment = settings.defaults[lang].baseentry_comment.format_map(replace_dict) # get other values quantity = settings.defaults[lang].get_baseentry_quantity() quantity_format = settings.defaults[lang].baseentry_quantity_format time = settings.defaults[lang].get_baseentry_time() price = settings.defaults[lang].get_baseentry_price() # return entry with default values from settings default return BaseEntry( title=title, comment=comment, quantity=quantity, quantity_format=quantity_format, time=time, price=price )
def test_copy_entry(): """Copy an entry.""" ac = BaseEntry() bc = ac.copy() # by default copy does keep the ID assert ac.get_id() == bc.get_id() bc = ac.copy(keep_id=False) # now bc is a copy of ac, but with its own ID assert ac.get_id() != bc.get_id()
def PresetBaseEntry( entry_preset=None, settings=None, global_list=None, client=None, project=None ): """Return BaseEntry according to settings.""" if type(entry_preset) is not BaseEntry: return NewBaseEntry(settings=settings, client=client, project=project) # get replaces replace_dict = replacer( settings=settings, global_list=global_list, client=client, project=project ) title = entry_preset.title.format_map(replace_dict) comment = entry_preset.comment.format_map(replace_dict) # get other values id = entry_preset._id quantity = entry_preset._quantity quantity_format = entry_preset.quantity_format quantity_b = entry_preset._quantity_b quantity_b_format = entry_preset.quantity_b_format time = entry_preset._time price = entry_preset._price # return entry with default values from settings default return BaseEntry( id=id, title=title, comment=comment, quantity=quantity, quantity_format=quantity_format, quantity_b=quantity_b, quantity_b_format=quantity_b_format, time=time, price=price )
def test_get_quantity_str(): """Test the get_quantity_str method.""" # init object a = BaseEntry(title='Total individual', comment='Individual comment!', quantity=61.75, quantity_format='') # test differnet quantity formats a.quantity_format = '{F}:{R}' assert a.get_quantity_str() == '61:45' a.quantity_format = '{s}' assert a.get_quantity_str() == '61.75'
def load_entry_list_from_js(self, lis=None): """Convert list to entry object list.""" entry_list = [] # cycle through the list of dicts for entry in lis: # it should have a type key if 'type' in entry.keys(): # entry is BaseEntry if entry['type'] == 'BaseEntry': # convert this dict to an offer objetc then! entry_list.append(BaseEntry().from_json(js=entry)) # entry is MultiplyEntry if entry['type'] == 'MultiplyEntry': # convert this dict to an offer objetc then! entry_list.append(MultiplyEntry().from_json(js=entry)) # entry is ConnectEntry if entry['type'] == 'ConnectEntry': # convert this dict to an offer objetc then! entry_list.append(ConnectEntry().from_json(js=entry)) return entry_list
def onStart(self): """Create all the forms and variables, which are needed.""" # get global variables for the app self.S = Settings() self.L = List(data_path=self.S.data_path) self.L.update_inactive_list(settings=self.S) self.P = Preset(data_path=self.S.data_path) self.P_what = 'offer' self.H = 'Fallback helptext is: learn by doing! (;' # set global temp variables self.tmpDefault = Default() self.tmpDefault_new = True self.tmpClient = Client() self.tmpClient_new = True self.tmpProject = Project() self.tmpProject_new = True self.tmpOffer = Offer() self.tmpOffer_new = True self.tmpOffer_index = -1 self.tmpInvoice = Invoice() self.tmpInvoice_new = True self.tmpInvoice_index = -1 self.tmpEntry = BaseEntry() self.tmpEntry_new = True self.tmpEntry_index = -1 self.tmpEntry_change_type = False self.tmpEntry_offer_invoice = 'offer' # create the forms self.addForm('MAIN', MainForm, name='Freelance') self.addForm('Help', HelpForm, name='Freelance - Help') self.addForm('Settings', SettingsForm, name='Freelance > Settings') self.addForm('Defaults', DefaultsForm, name='Freelance > Settings > Defaults') self.addForm('Defaults_general', DefaultsGeneralForm, name='Freelance > Settings > Defaults > General') self.addForm('Defaults_offer', DefaultsOfferForm, name='Freelance > Settings > Defaults > Offer') self.addForm('Defaults_invoice', DefaultsInvoiceForm, name='Freelance > Settings > Defaults > Invoice') self.addForm('Defaults_clientproject', DefaultsClientProjectForm, name='Freelance > Settings > Defaults > Client / Project') self.addForm('Defaults_entry', DefaultsEntryForm, name='Freelance > Settings > Defaults > Entry') self.addForm('Client', ClientForm, name='Freelance > Client', color='NO_EDIT') self.addForm('Project', ProjectForm, name='Freelance > Project', color='NO_EDIT') self.addForm('Inactive', InactiveForm, name='Freelance > Inactive clients and projects', color='WARNING') self.addForm('Offer', OfferForm, name='Freelance > Project > Offer', color='NO_EDIT') self.addForm('Invoice', InvoiceForm, name='Freelance > Project > Invoice', color='NO_EDIT') self.addForm('UnpaidInvoice', UnpaidInvoiceForm, name='Freelance > Unpaid invoices', color='DANGER') self.addForm('AllInvoices', AllInvoicesForm, name='Freelance > All invoices', color='WARNING') self.addForm('EntryChoose', EntryChooseForm, name='Freelance > Project > Offer > Entry') self.addForm('BaseEntry', BaseEntryForm, name='Freelance > Project > Offer > Base entry', color='NO_EDIT') self.addForm('MultiplyEntry', MultiplyEntryForm, name='Freelance > Project > Offer > Multiply entry', color='NO_EDIT') self.addForm('ConnectEntry', ConnectEntryForm, name='Freelance > Project > Offer > Connect entry', color='NO_EDIT') self.addForm('Presets', PresetForm, name='Choose a preset', color='WARNING') self.addForm('Export', ExportForm, name='Choose a template')
def test_entry_tax_set(): """Test the entry tax setter and getter.""" taxi = BaseEntry() taxi.set_tax(1.54) assert taxi.get_tax() == Decimal('0.0154') assert taxi.get_tax_percent() == Decimal('1.54') taxi.set_tax('17.3') assert taxi.get_tax() == Decimal('0.173') assert taxi.get_tax_percent() == Decimal('17.3') taxi.set_tax(0.45) assert taxi.get_tax() == Decimal('0.45') assert taxi.get_tax_percent() == Decimal('45')
def test_integrety_entry(): """Test if BaseEntry, ConnectEntry and MultiplyEntry work as they should.""" a = BaseEntry(title='Title A', comment='Comment A', quantity=1.0, time=1.0, price=50.00) b = MultiplyEntry(title='Title B', comment='Comment B', quantity=2.0, hour_rate=0.5) c = ConnectEntry(title='Title C', comment='Comment C', quantity=2.5, is_time=False, multiplicator=0.5) d = ConnectEntry(title='Title D', comment='Comment D', quantity=3.0, is_time=True, multiplicator=0.75) # init entries in list entries = [] entries.append(a) entries.append(b) entries.append(c) entries.append(d) # connect first two entries two the first ConnectEntry entries[2].connect_entry(entry_list=entries, entry_id=entries[0].get_id()) entries[2].connect_entry(entry_list=entries, entry_id=entries[1].get_id()) # connect first ConnectEntry to the second ConnectEntry entries[3].connect_entry(entry_list=entries, entry_id=entries[2].get_id()) # connect second ConnectEntry to the first ConnectEntry # and it should not work anymore entries[2].connect_entry(entry_list=entries, entry_id=entries[3].get_id()) assert entries[3].get_id() not in entries[2].get_connected() # connect MultiplyEntry to the second ConnectEntry entries[3].connect_entry(entry_list=entries, entry_id=entries[1].get_id()) # set wage to work with wage = Decimal('50.00') # check values for BaseEntry assert entries[0].title == 'Title A' assert entries[0].comment == 'Comment A' assert entries[0].get_quantity() == Decimal('1.0') assert entries[0].get_time() == QuantityTime(1.0) assert entries[0].get_price() == Decimal('50.00') # check values for MultiplyEntry assert entries[1].title == 'Title B' assert entries[1].comment == 'Comment B' assert entries[1].get_quantity() == Decimal('2.0') assert entries[1].get_hour_rate() == QuantityTime(0.5) assert entries[1].get_time() == QuantityTime(1.0) assert entries[1].get_price(wage=wage) == Decimal('50.00') # check values for first ConnectEntry assert entries[2].title == 'Title C' assert entries[2].comment == 'Comment C' assert entries[2].get_quantity() == Decimal('2.5') assert entries[2].get_is_time() is False assert entries[2].get_time(entry_list=entries) == QuantityTime(0) assert entries[2].get_price(entry_list=entries, wage=wage) == Decimal('125.00') # check values for second ConnectEntry assert entries[3].title == 'Title D' assert entries[3].comment == 'Comment D' assert entries[3].get_quantity() == Decimal('3.0') assert entries[3].get_is_time() is True assert entries[3].get_time(entry_list=entries) == QuantityTime('2:15:00') assert entries[3].get_price(entry_list=entries, wage=wage) == Decimal('112.50')
def test_json_conversion_baseentry(): """Test the json conversion.""" # init the individual object a = BaseEntry(title='Total individual', comment='Individual comment!', quantity=1.25, quantity_format='{F}:{R} min', time='1:45', price=1000) # make new default object b = BaseEntry() # for now the values must not be the same assert b.get_id() != a.get_id() assert b.title != a.title assert b.comment != a.comment assert b.get_quantity() != a.get_quantity() assert b.quantity_format != a.quantity_format assert b.get_time() != a.get_time() assert b.get_price() != a.get_price() # load a into b with json as object with same attrbutes b = BaseEntry().from_json(js=a.to_json()) # now the values must be the same assert b.get_id() == a.get_id() assert b.title == a.title assert b.comment == a.comment assert b.get_quantity() == a.get_quantity() assert b.quantity_format == a.quantity_format assert b.get_time() == a.get_time() assert b.get_price() == a.get_price() # load a into b with json as preset b = BaseEntry().from_json(js=a.to_json(), keep_id=False) # now the values must be the same assert b.get_id() != a.get_id() # not same, since "preset loading" assert b.title == a.title assert b.comment == a.comment assert b.get_quantity() == a.get_quantity() assert b.quantity_format == a.quantity_format assert b.get_time() == a.get_time() assert b.get_price() == a.get_price()