def test_call_work(self): company = ce.Company(100000, id='company') bob = ce.Person('bob', 10000, id='bob') developer = ce.Developer(100, id='developer') self.myreg.bind(company, bob, bob, developer, 'PPR') self.assertEquals(1.2, self.myreg.invokeRole(company, bob, 'pay'))
def test_second_bind_roles_as_in_paper(self): company = ce.Company(100000, id='company') tax = ce.TaxDepartment(100000, id='tax') bob = ce.Person('bob', 10000, id='bob') ely = ce.Person('ely', 10000, id='ely') developer = ce.Developer(100, id='developer') accountant = ce.Accountant(100, id='accountant') freelance = ce.Freelance(100, id='freelance') taxpayer = ce.TaxPayer(id='taxpayer') self.myreg.bind(company, bob, bob, developer, 'PPR') self.myreg.bind(company, bob, bob, accountant, 'PPR') self.myreg.bind(company, ely, ely, freelance, 'PPR') self.myreg.bind(company, ely, freelance, taxpayer, 'RPR') self.myreg.bind(company, company, company, taxpayer, 'PPR') table = self.myreg.conn.execute("SELECT * FROM {} \ order by BindingLevel DESC,BindingSequence DESC" .format \ (self.myreg.name)).fetchall() table_goal = [('company', 'ely', 'freelance', 'taxpayer', 'RPR', 2, 1), ('company', 'bob', 'bob', 'accountant', 'PPR', 1, 2), ('company', 'bob', 'bob', 'developer', 'PPR', 1, 1), ('company', 'ely', 'ely', 'freelance', 'PPR', 1, 1), ('company', 'company', 'company', 'taxpayer', 'PPR', 1, 1)] self.assertEquals(table, table_goal)
def test_bind_role_to_person(self): company = ce.Company(100000, id='company') bob = ce.Person('bob', 10000, id='bob') developer = g.nspace['developer'](100, id='developer') self.myreg.bind(company, bob, bob, developer, 'PPR') line = self.myreg.conn.execute("SELECT * FROM {} ".format( self.myreg.name)).fetchall()[0] self.assertEquals(('company', 'bob', 'bob', 'developer', 'PPR', 1, 1), line)
def test_second_bind_role_to_person(self): company = ce.Company(100000, id='company') bob = ce.Person('bob', 10000, id='bob') developer = ce.Developer(100, id='developer') accountant = ce.Accountant(100, id='accountant') self.myreg.bind(company, bob, bob, developer, 'PPR') self.myreg.bind(company, bob, bob, accountant, 'PPR') line = self.myreg.conn.execute("SELECT * FROM {} WHERE CorePlayerId='{}' and PlayerId='{}' \ order by BindingLevel DESC,BindingSequence DESC" .format \ (self.myreg.name, bob.uuid, bob.uuid)).fetchall()[0] self.assertEquals(('company', 'bob', 'bob', 'accountant', 'PPR', 1, 2), line)
def test_runtime_change(self): self.new_role = '''from libs import rop import uuid class Developer(rop.Role): classtype = 'developer' def __init__(self, salary=200, id='developer', instance=None): self.salary = salary self.uuid = uuid.uuid1() if id is None else id super().__init__(self.uuid) def getPaid(self): pass def work(self): print('Typing like I could program...') def pay(self): print('I just pay for golden coffee') return 20 ''' self.monitor = monitor.start_monitor_thread('runtime_lib', self.myreg) filepath = os.path.abspath('.') + '/runtime_lib/test0.py' company = ce.Company(100000, id='company') bob = ce.Person('bob', 10000, id='bob') alice = ce.Person('alice', 10000, id='alice') self.myreg.add_role(ce.Developer) bob_developer = g.nspace['developer'](100, id='developer') self.myreg.bind(company, bob, bob, bob_developer, 'PPR') self.assertEquals(1.2, self.myreg.invokeRole(company, bob, 'pay')) # create file helpers.create_file(self.new_role, filepath) time.sleep(0.2) # alice will be bound to the "new" role, that pays 20 per coffee alice_developer = g.nspace['developer'](100, id='developer') self.myreg.bind(company, alice, alice, alice_developer, 'PPR') self.assertEquals(20, self.myreg.invokeRole(company, alice, 'pay')) self.monitor.stop()
def test_update_role_with_state(self): self.new_role = '''from libs import rop import uuid class Developer(rop.Role): classtype = 'developer' def __init__(self, salary=100, id='developer', instance=None): if instance is not None: self.uuid = instance.uuid self.salary = instance.salary + 100 print('Instance.savings: {}'.format(instance.savings)) self.savings = instance.savings else: self.salary = instance.salary self.uuid = uuid.uuid1() if id is None else id self.savings = 0 super().__init__(self.uuid) def getPaid(self): self.savings=self.savings+self.salary print('receiving money') def work(self): print('work the whole month...') def pay(self): print('I just wish myself coffee') return 20 def show_savings(self): return 'My savings_method (version 2) {}'.format(self.savings) ''' self.monitor = monitor.start_monitor_thread('runtime_lib', self.myreg) filepath = os.path.abspath('.') + '/runtime_lib/test3.py' print(' ------------ test_update_role_with_state') company = ce.Company(100000, id='company') bob = ce.Person('bob', 10000, id='bob') dev = ce.Developer(salary=1000, id='developer') self.myreg.add_role(ce.Developer) self.myreg.bind(company, bob, bob, dev, 'PPR') self.assertEquals(bob.roles['developer'].savings, 0) self.myreg.invokeRole(company, bob, 'getPaid') curr_salary = bob.roles['developer'].salary self.assertEquals(bob.roles['developer'].savings, curr_salary) # create file helpers.create_file(self.new_role, filepath) time.sleep(1) # bob still have the same money into his savings: self.assertEquals(bob.roles['developer'].savings, curr_salary) print('verificou salario') # bob was bound to the "new" role, that pays his old salary + 100 bucks self.myreg.invokeRole(company, bob, 'getPaid') self.assertEquals(bob.roles['developer'].savings, 2 * curr_salary + 100) self.monitor.stop()
#!/usr/bin/python3 from libs import reg, g from libs.monitor import start_monitor_thread import app.company_example as ce import time myreg = reg.Reg('roles_db') monitor = start_monitor_thread('runtime_lib', myreg) company = ce.Company(100000, id='company') bob = ce.Person('bob', 10000, id='bob') for role in [ce.Developer, ce.TaxPayer]: myreg.add_role(role) developer = g.nspace['developer'](100, id='developer') taxpayer = g.nspace['taxpayer'](id='taxpayer') myreg.bind(company, bob, bob, developer, 'PPR') myreg.invokeRole(company, bob, 'pay') myreg.bind(company, bob, developer, taxpayer, 'RPR') myreg.invokeRole(company, bob, 'pay') myreg.unbind('bob', 'taxpayer') myreg.invokeRole(company, bob, 'pay') while True: time.sleep(1) myreg.invokeRole(company, bob, 'work') myreg.invokeRole(company, bob, 'getPaid') print('Hi, I am {}. {}'.format(bob.name, myreg.invokeRole(company, bob, 'show_savings')))