def test_can_construct_from_id(self): team = Team.from_id(self.make_team().id) assert team.name == 'The A Team' assert team.owner == 'hannibal'
def test_can_construct_from_id(self): team = Team.from_id(self.make_team().id) assert team.name == 'The Enterprise' assert team.owner == 'picard'
def update_giving_and_teams(self): with self.db.get_cursor() as cursor: updated_giving = self.update_giving(cursor) for payment_instruction in updated_giving: Team.from_id( payment_instruction.team_id).update_receiving(cursor)
def update_giving_and_teams(self): with self.db.get_cursor() as cursor: updated_giving = self.update_giving(cursor) for payment_instruction in updated_giving: Team.from_id(payment_instruction.team_id).update_receiving(cursor)
def populate_db(db, num_participants=100, ntips=200, num_teams=5, num_transfers=5000): """Populate DB with fake data. """ print("Making Participants") make_flag_tester = num_participants > 1 participants = [] for i in xrange(num_participants - 1 if make_flag_tester else num_participants): participants.append(fake_participant(db)) if make_flag_tester: # make a participant for testing weird flags flag_tester = fake_participant(db, random_identities=False) participants.append(flag_tester) nepal = db.one("SELECT id FROM countries WHERE code='NP'") flag_tester.store_identity_info(nepal, 'nothing-enforced', {}) flag_tester.set_identity_verification(nepal, True) vatican = db.one("SELECT id FROM countries WHERE code='VA'") flag_tester.store_identity_info(vatican, 'nothing-enforced', {}) flag_tester.set_identity_verification(vatican, True) print("Making Teams") teams = [] teamowners = random.sample(participants, num_teams) for teamowner in teamowners: teams.append(fake_team(db, teamowner)) # Creating a fake Gratipay Team teamowner = random.choice(participants) teams.append(fake_team(db, teamowner, "Gratipay")) print("Making Payment Instructions") npayment_instructions = 0 payment_instructions = [] for participant in participants: for team in teams: #eliminate self-payment if participant.username != team.owner: npayment_instructions += 1 if npayment_instructions > ntips: break payment_instructions.append( fake_payment_instruction(db, participant, team)) if npayment_instructions > ntips: break print("Making Elsewheres") for p in participants: #All participants get between 1 and 3 elsewheres num_elsewheres = random.randint(1, 3) for platform_name in random.sample(PLATFORMS, num_elsewheres): fake_elsewhere(db, p, platform_name) print("Making Tips") tips = [] for i in xrange(ntips): tipper, tippee = random.sample(participants, 2) tips.append(fake_tip(db, tipper, tippee)) # Payments payments = [] paymentcount = 0 team_amounts = defaultdict(int) for payment_instruction in payment_instructions: participant = Participant.from_id( payment_instruction['participant_id']) team = Team.from_id(payment_instruction['team_id']) amount = payment_instruction['amount'] assert participant.username != team.owner paymentcount += 1 sys.stdout.write("\rMaking Payments (%i)" % (paymentcount)) sys.stdout.flush() payments.append( fake_payment(db, participant.username, team.slug, amount, 'to-team')) team_amounts[team.slug] += amount for team in teams: paymentcount += 1 sys.stdout.write("\rMaking Payments (%i)" % (paymentcount)) sys.stdout.flush() payments.append( fake_payment(db, team.owner, team.slug, team_amounts[team.slug], 'to-participant')) print("") # Transfers transfers = [] for i in xrange(num_transfers): sys.stdout.write("\rMaking Transfers (%i/%i)" % (i + 1, num_transfers)) sys.stdout.flush() tipper, tippee = random.sample(participants, 2) transfers.append(fake_transfer(db, tipper, tippee)) print("") # Paydays # First determine the boundaries - min and max date min_date = min(min(x['ctime'] for x in payment_instructions + tips), min(x['timestamp'] for x in payments + transfers)) max_date = max(max(x['ctime'] for x in payment_instructions + tips), max(x['timestamp'] for x in payments + transfers)) # iterate through min_date, max_date one week at a time payday_counter = 1 date = min_date paydays_total = (max_date - min_date).days / 7 + 1 while date < max_date: sys.stdout.write("\rMaking Paydays (%i/%i)" % (payday_counter, paydays_total)) sys.stdout.flush() payday_counter += 1 end_date = date + datetime.timedelta(days=7) week_tips = filter(lambda x: date <= x['ctime'] < end_date, tips) week_transfers = filter(lambda x: date <= x['timestamp'] < end_date, transfers) week_payment_instructions = filter( lambda x: date <= x['ctime'] < end_date, payment_instructions) week_payments = filter(lambda x: date <= x['timestamp'] < end_date, payments) week_payments_to_teams = filter(lambda x: x['direction'] == 'to-team', week_payments) week_payments_to_owners = filter( lambda x: x['direction'] == 'to-participant', week_payments) for p in participants: transfers_in = filter(lambda x: x['tippee'] == p.username, week_transfers) payments_in = filter(lambda x: x['participant'] == p.username, week_payments_to_owners) transfers_out = filter(lambda x: x['tipper'] == p.username, week_transfers) payments_out = filter(lambda x: x['participant'] == p.username, week_payments_to_teams) amount_in = sum([t['amount'] for t in transfers_in + payments_in]) amount_out = sum( [t['amount'] for t in transfers_out + payments_out]) amount = amount_out - amount_in fee = amount * D('0.02') fee = abs(fee.quantize(D('.01'))) if amount != 0: fee = amount * D('0.02') fee = abs(fee.quantize(D('.01'))) fake_exchange(db=db, participant=p, amount=amount, fee=fee, timestamp=(end_date - datetime.timedelta(seconds=1))) actives = set() tippers = set() #week_tips, week_transfers for xfers in week_tips, week_transfers: actives.update(x['tipper'] for x in xfers) actives.update(x['tippee'] for x in xfers) tippers.update(x['tipper'] for x in xfers) # week_payment_instructions actives.update(x['participant_id'] for x in week_payment_instructions) tippers.update(x['participant_id'] for x in week_payment_instructions) # week_payments actives.update(x['participant'] for x in week_payments) tippers.update(x['participant'] for x in week_payments_to_owners) payday = { 'ts_start': date, 'ts_end': end_date, 'nusers': len(actives), 'volume': sum(x['amount'] for x in week_transfers) } insert_fake_data(db, "paydays", **payday) date = end_date print("")
def populate_db(db, num_participants=100, ntips=200, num_teams=5): """Populate DB with fake data. """ print("Making Participants") make_flag_tester = num_participants > 1 participants = [] for i in xrange(num_participants - 1 if make_flag_tester else num_participants): participants.append(fake_participant(db)) if make_flag_tester: # make a participant for testing weird flags flag_tester = fake_participant(db, random_identities=False) participants.append(flag_tester) nepal = db.one("SELECT id FROM countries WHERE code='NP'") flag_tester.store_identity_info(nepal, 'nothing-enforced', {}) flag_tester.set_identity_verification(nepal, True) vatican = db.one("SELECT id FROM countries WHERE code='VA'") flag_tester.store_identity_info(vatican, 'nothing-enforced', {}) flag_tester.set_identity_verification(vatican, True) print("Making Teams") teams = [] teamowners = random.sample(participants, num_teams) for teamowner in teamowners: teams.append(fake_team(db, teamowner)) # Creating a fake Gratipay Team teamowner = random.choice(participants) teams.append(fake_team(db, teamowner, "Gratipay")) print("Making Payment Instructions") npayment_instructions = 0 payment_instructions = [] for participant in participants: for team in teams: #eliminate self-payment if participant.username != team.owner: npayment_instructions += 1 if npayment_instructions > ntips: break payment_instructions.append( fake_payment_instruction(db, participant, team)) if npayment_instructions > ntips: break print("Making Elsewheres") for p in participants: #All participants get between 1 and 3 elsewheres num_elsewheres = random.randint(1, 3) for platform_name in random.sample(PLATFORMS, num_elsewheres): fake_elsewhere(db, p, platform_name) # Paydays # First determine the boundaries - min and max date min_date = min(x['mtime'] for x in payment_instructions) max_date = max(x['mtime'] for x in payment_instructions) # iterate through min_date, max_date one week at a time payday_counter = 0 date = min_date paydays_total = (max_date - min_date).days / 7 + 1 while date < max_date: payday_counter += 1 end_date = date + datetime.timedelta(days=7) week_payment_instructions = filter(lambda x: x['mtime'] < date, payment_instructions) # Need to create the payday record before inserting payment records params = dict(ts_start=date, ts_end=end_date) with db.get_cursor() as cursor: payday_id = cursor.one( """ INSERT INTO paydays (ts_start, ts_end) VALUES (%(ts_start)s, %(ts_end)s) RETURNING id """, params) sys.stdout.write("\rMaking Paydays (%i/%i)" % (payday_id, paydays_total)) sys.stdout.flush() week_payments = [] for payment_instruction in week_payment_instructions: participant = Participant.from_id( payment_instruction['participant_id']) team = Team.from_id(payment_instruction['team_id']) amount = payment_instruction['amount'] assert participant.username != team.owner week_payments.append( fake_payment(db=db, participant=participant.username, team=team.slug, timestamp=date, amount=amount, payday=payday_id, direction='to-team')) if amount != 0: fee = amount * D('0.02') fee = abs(fee.quantize(D('.01'))) fake_exchange(db=db, participant=participant, amount=amount, fee=fee, timestamp=date + datetime.timedelta(seconds=1)) for team in teams: week_payments_to_team = filter(lambda x: x['team'] == team.slug, week_payments) pay_out = sum(t['amount'] for t in week_payments_to_team) if pay_out: week_payments.append( fake_payment(db=db, participant=team.owner, team=team.slug, timestamp=date, amount=pay_out, payday=payday_id, direction='to-participant')) actives = set() # week_payments actives.update(x['participant'] for x in week_payments) params = dict(nusers=len(actives), volume=sum(x['amount'] for x in week_payment_instructions), payday_id=payday_id) db.run( """ UPDATE paydays SET nusers=%(nusers)s, volume=%(volume)s WHERE id=%(payday_id)s """, params) date = end_date print("")
def populate_db(db, num_participants=100, ntips=200, num_teams=5): """Populate DB with fake data. """ print("Making Participants") make_flag_tester = num_participants > 1 participants = [] for i in xrange(num_participants - 1 if make_flag_tester else num_participants): participants.append(fake_participant(db)) if make_flag_tester: # make a participant for testing weird flags flag_tester = fake_participant(db, random_identities=False) participants.append(flag_tester) nepal = db.one("SELECT id FROM countries WHERE code='NP'") flag_tester.store_identity_info(nepal, 'nothing-enforced', {}) flag_tester.set_identity_verification(nepal, True) vatican = db.one("SELECT id FROM countries WHERE code='VA'") flag_tester.store_identity_info(vatican, 'nothing-enforced', {}) flag_tester.set_identity_verification(vatican, True) print("Making Teams") teams = [] teamowners = random.sample(participants, num_teams) for teamowner in teamowners: teams.append(fake_team(db, teamowner)) # Creating a fake Gratipay Team teamowner = random.choice(participants) teams.append(fake_team(db, teamowner, "Gratipay")) print("Making Payment Instructions") npayment_instructions = 0 payment_instructions = [] for participant in participants: for team in teams: #eliminate self-payment if participant.username != team.owner: npayment_instructions += 1 if npayment_instructions > ntips: break payment_instructions.append(fake_payment_instruction(db, participant, team)) if npayment_instructions > ntips: break print("Making Elsewheres") for p in participants: #All participants get between 1 and 3 elsewheres num_elsewheres = random.randint(1, 3) for platform_name in random.sample(PLATFORMS, num_elsewheres): fake_elsewhere(db, p, platform_name) # Paydays # First determine the boundaries - min and max date min_date = min(x['mtime'] for x in payment_instructions) max_date = max(x['mtime'] for x in payment_instructions) # iterate through min_date, max_date one week at a time payday_counter = 0 date = min_date paydays_total = (max_date - min_date).days/7 + 1 while date < max_date: payday_counter += 1 end_date = date + datetime.timedelta(days=7) week_payment_instructions = filter(lambda x: x['mtime'] < date, payment_instructions) # Need to create the payday record before inserting payment records params = dict(ts_start=date, ts_end=end_date) with db.get_cursor() as cursor: payday_id = cursor.one(""" INSERT INTO paydays (ts_start, ts_end) VALUES (%(ts_start)s, %(ts_end)s) RETURNING id """, params) sys.stdout.write("\rMaking Paydays (%i/%i)" % (payday_id, paydays_total)) sys.stdout.flush() week_payments = [] for payment_instruction in week_payment_instructions: participant = Participant.from_id(payment_instruction['participant_id']) team = Team.from_id(payment_instruction['team_id']) amount = payment_instruction['amount'] assert participant.username != team.owner week_payments.append(fake_payment( db=db, participant=participant.username, team=team.slug, timestamp=date, amount=amount, payday=payday_id, direction='to-team' ) ) if amount != 0: fee = amount * D('0.02') fee = abs(fee.quantize(D('.01'))) fake_exchange( db=db, participant=participant, amount=amount, fee=fee, timestamp=date + datetime.timedelta(seconds=1) ) for team in teams: week_payments_to_team = filter(lambda x: x['team'] == team.slug, week_payments) pay_out = sum(t['amount'] for t in week_payments_to_team) if pay_out: week_payments.append(fake_payment( db=db, participant=team.owner, team=team.slug, timestamp=date, amount=pay_out, payday=payday_id, direction= 'to-participant' ) ) actives=set() # week_payments actives.update(x['participant'] for x in week_payments) params = dict( nusers=len(actives) , volume=sum(x['amount'] for x in week_payment_instructions) , payday_id=payday_id ) db.run(""" UPDATE paydays SET nusers=%(nusers)s, volume=%(volume)s WHERE id=%(payday_id)s """, params) date = end_date print("")