Exemplo n.º 1
0
class TestAccount(unittest.TestCase):
    '''Test cases for the Account class'''

    def setUp(self):        
        self.acc = Account("me")
        
    def tearDown(self):
        pass
    
    def testCreate(self):
        assert self.acc._accountholder is not None

    def testDebitAndCredit(self):
        self.acc.credit(70)
        assert self.acc.getBalance() == 70
        self.acc.debit(70)
        assert self.acc.getBalance() == 0
        self.acc.credit(40)
        self.acc.credit(60)
        assert self.acc.getBalance() == 100

    def testExtractTooMuch(self):
        print 'Trying to extract too much'
        self.failUnlessRaises(InsufficientFunds, self.acc.debit, 70)
Exemplo n.º 2
0
 def setUp(self):        
     self.acc = Account("me")
Exemplo n.º 3
0
class LqnTrust(Process):
    """
    The trust managing the LQN; there is only one trust
    """

    def __init__(self, name, simInstance):
        Process.__init__(self, simInstance)
        self.name = name
        self.account = Account(name)
        logger.debug("%s created.", self.name)

    def create_initial_quids(self):
        # number or sponsors:
        sponsor_count = len(network.sponsors)
        # initial quids for members to start the sim
        total_member_quids = (num_of_members + num_of_employees) * initial_quids_per_account
        # initial quids for businesses to start the sim
        total_business_quids = initial_quids_per_business * num_of_businesses
        # initial quids for sponsors to start the sim
        total_sponsors_quids = initial_quids_per_account * sponsorship_factor * sponsor_count
        # total initial quids to start the sim
        total_initial_quids = total_member_quids + total_sponsors_quids + total_business_quids
        # initial quid creation; crediting directly
        # is only allowed for the trust
        self.account.credit(total_initial_quids)
        #       self.inject_quids(total_initial_quids)
        # now credit every member account with a first transaction
        # with initial quids
        self.__do_transactions(network.members, initial_quids_per_account)
        self.__do_transactions(network.employees, initial_quids_per_account)
        self.__do_transactions(network.sponsors, initial_quids_per_account * sponsorship_factor)
        self.__do_transactions(network.businesses, initial_quids_per_business)

    def __do_transactions(self, account_list, amount):
        for i in account_list:
            credit_account = i.get_account()
            timestamp = datetime.now()
            ta = Transaction(credit_account, self.account, amount, timestamp)
            ta.commit()
            network.ta_recorder.add_transaction(ta)

    def inject(self, account_list, amount):
        # first credit the trust account with the sufficient amount of quids
        count = len(account_list)
        total_amount = count * amount
        self.account.credit(total_amount)
        #        self.inject_quids(total_amount)
        self.__do_transactions(account_list, amount)
        logger.info("Injected %d quids into %d accounts. Total: %d quids.", amount, count, total_amount)

    #    def inject_quids(self, amount):
    #        self.account.credit(amount)
    #        yield put,self,total_quids_in_network,amount

    def apply_policy(self, data_accessor, list, policy):
        """
            Only the trust is allowed to apply the policy,
            so it needs to be done in this object, not in 
            scenario or elsewhere.
        """
        timestamp = datetime.now()
        quids_level = 0
        base = "Policy applied to all accounts."
        ending = ""

        for i in list:
            for member in i:
                quids_level += policy.apply(self.account, member.get_account(), data_accessor, timestamp)
        if quids_level > 0:
            ending = "Added %d quids in total.", quids_level
        elif quids_level < 0:
            ending = "Removed %d quids in total.", quids_level
        else:
            ending = "No changes in total quids."
        msg = base, ending
        logger.info(msg)
Exemplo n.º 4
0
 def __init__(self, name, simInstance):
     Process.__init__(self, simInstance)
     self.name = name
     self.account = Account(name)
     logger.debug("%s created.", self.name)