Skip to content

Commit

Permalink
refined fixture count and added date generation to match fixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
artnez committed May 13, 2012
1 parent aa1b031 commit 67903fa
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
26 changes: 18 additions & 8 deletions src/faceoff/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@
"""

import os
import json
from math import ceil
from time import mktime
from datetime import datetime, timedelta
from logging import getLogger, debug
from random import choice, shuffle, randint
from jinja2.utils import generate_lorem_ipsum
import json
from faceoff.models.user import create_user, get_active_users
from faceoff.models.league import create_league, get_all_leagues, get_active_leagues
from faceoff.models.match import create_match, rebuild_rankings
Expand Down Expand Up @@ -75,7 +78,7 @@ def generate_full_db(db, truncate=False):
[rebuild_rankings(db, league['id']) for league in get_all_leagues(db)]
db.is_building = False

def generate_users(db, min_count=8, max_count=25, truncate=False):
def generate_users(db, min_count=4, max_count=12, truncate=False):
"""
Generates a random amount of users into the given database connection
object. The amount of users will fall between `min_count` and `max_count`.
Expand All @@ -85,12 +88,12 @@ def generate_users(db, min_count=8, max_count=25, truncate=False):
if truncate:
db.truncate_table('user')
users = []
for user in rand_users(min_count, max_count):
for user in rand_users(min_count=min_count, max_count=max_count):
users.append(create_user(db=db, **user))
logger().info('created %d users' % len(users))
return users

def generate_leagues(db, min_count=4, max_count=10, truncate=False):
def generate_leagues(db, min_count=2, max_count=6, truncate=False):
"""
Generates a random amount of leagues into the given database connection
object. The amount of leagues will fall between `min_count` and `max_count`.
Expand Down Expand Up @@ -122,13 +125,20 @@ def generate_matches(db, truncate=False):
leagues = get_active_leagues(db)
for league in leagues:
logger().info('creating matches for league: %s' % league['name'])
for i in range(randint(0, 50)):
matches = []
for i in range(randint(0, 25)):
shuffle(users)
winner = users[0]['id']
loser = users[1]['id']
create_match(db, league['id'], winner, loser)

def rand_users(min_count=3, max_count=10):
matches.append([league, winner, loser])
match_date = datetime.now() - timedelta(days=100)
diff_hours = round(100.0/len(matches), 2)
for match in matches:
match_date = match_date + timedelta(hours=diff_hours*24)
match_time = mktime(match_date.timetuple())
create_match(db, match[0]['id'], match[1], match[2], match_date=match_time)

def rand_users(min_count, max_count):
"""
Returns a list of random objects that map the properties of a user record.
"""
Expand Down
4 changes: 2 additions & 2 deletions src/faceoff/models/match.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ def get_match_history(db, league_id, user_id=None, start=0, count=100):
return db.select(query, params)

@use_db
def create_match(db, league_id, winner_user_id, loser_user_id, norebuild=False):
def create_match(db, league_id, winner_user_id, loser_user_id, match_date=None, norebuild=False):
match_id = db.insert(
'match',
league_id = league_id,
winner_id = winner_user_id,
winner_rank = get_user_rank(db, league_id, winner_user_id),
loser_id = loser_user_id,
loser_rank = get_user_rank(db, league_id, loser_user_id),
date_created = int(time())
date_created = int(time()) if match_date is None else match_date
)
if not norebuild:
rebuild_rankings(db, league_id)
Expand Down

0 comments on commit 67903fa

Please sign in to comment.