Example #1
0
def test_objective(client, db_instance):

    #ensure login
    user = ensure_logged_in(client, db_instance)

    #create 2 fake runs
    runs_id = ['1', '2']
    for i in runs_id:
        run = Run()
        run.runner = user
        run.strava_id = i
        run.distance = 1000
        run.average_speed = 10
        run.elapsed_time = 10000
        db_instance.session.add(run)

    db_instance.session.commit()

    #set objective
    objective_distance = 1000
    _setObjective(user, objective_distance)

    #check that the objective is correctly settend in the database
    assert db_instance.session.query(Objectives).first().distance == 1000

    #check that the objective is correctly showed in the view
    res = client.get('/')
    html = pq(res.data)

    objective_view = km2m(float(html("#obj_dist").html()))

    #check the view converting the distance to km
    assert objective_view == objective_distance
def createRun(
        db_instance,
        id_,
        runner_id,
        distance=1000,
        start_date=datetime.datetime(year=2018, month=1, day=1),
        elapsed_time=3600,
        average_speed=2):

    run_count = db_instance.session.query(Run).count()

    run = Run()
    run.id = id_
    run.name = 'test run ' + str(run.id)
    run.strava_id = None
    run.distance = distance
    run.start_date = start_date
    run.elapsed_time = elapsed_time
    run.average_speed = average_speed
    run.average_heartrate = None
    run.total_elevation_gain = 0
    run.runner_id = runner_id
    db_instance.session.add(run)

    assert db_instance.session.query(Run).count() == (run_count + 1)
Example #3
0
def _copy_run(runs, index):
    r1 = Run()
    r1.runner = runs[index].runner
    r1.strava_id = runs[index].strava_id
    r1.name = runs[index].name
    r1.average_speed = runs[index].average_speed
    r1.elapsed_time = runs[index].elapsed_time
    r1.distance = runs[index].distance
    return r1
Example #4
0
def test_delete_user(client, db_instance):
    email = '*****@*****.**'
    password = '******'

    client.post('/create_user',
                data=dict(email=email,
                          firstname='Jhon',
                          lastname='Doe',
                          password=password,
                          age='22',
                          weight='75',
                          max_hr='150',
                          rest_hr='60',
                          vo2max='10'),
                follow_redirects=True)

    user = db_instance.session.query(User).filter(User.email == email).first()
    user_id = user.get_id()

    runs_id = ['1', '2', '3']

    objective = Objectives()
    objective.distance = 1000
    objective.user = user

    for id in runs_id:
        run = Run()
        run.runner = user
        run.strava_id = id
        run.distance = 1500
        run.average_speed = 10
        run.elapsed_time = 200000
        db_instance.session.add(run)

    db_instance.session.add(objective)
    db_instance.session.commit()

    client.post('/login',
                data=dict(email=email, password=password),
                follow_redirects=True)

    client.post('/delete_user',
                data=dict(password=password),
                follow_redirects=True)

    user = db_instance.session.query(User).filter(User.email == email).first()
    assert user is None

    run = db_instance.session.query(Run).filter(
        Run.runner_id == user_id).first()
    assert run is None

    objective = db_instance.session.query(Objectives).filter(
        Objectives.user_id == user_id).first()
    assert objective is None
Example #5
0
def activity2run(user, activity):
    run = Run()
    run.runner = user
    run.strava_id = activity.id
    run.name = activity.name
    run.distance = activity.distance
    run.elapsed_time = activity.elapsed_time.total_seconds()
    run.average_speed = activity.average_speed
    run.average_heartrate = activity.average_heartrate
    run.total_elevation_gain = activity.total_elevation_gain
    run.start_date = activity.start_date
    return run
Example #6
0
def activity2run(user, activity):
    """Used by fetch_runs to convert a strava run into a DB entry."""
    run = Run()
    run.runner = user
    run.strava_id = activity.id
    run.name = activity.name
    run.distance = activity.distance
    run.elapsed_time = activity.elapsed_time.total_seconds()
    run.average_speed = activity.average_speed
    run.average_heartrate = activity.average_heartrate
    run.total_elevation_gain = activity.total_elevation_gain
    run.start_date = activity.start_date
    return run
Example #7
0
def new_predefined_run_test(user):
    run = Run()
    run.runner = user
    run.strava_id = 10  # a random number 100 - 1.000.000, we hope is unique
    run.name = "Run " + str(run.strava_id)
    run.distance = 20000.0  # 50m - 10 km
    run.elapsed_time = 200.0  # 30s - 1h
    run.average_speed = run.distance / run.elapsed_time
    run.average_heartrate = None
    run.total_elevation_gain = 3.0  # 0m - 25m
    run.start_date = datetime.now()
    db.session.add(run)
    db.session.commit()
    return run
Example #8
0
def test_objective(client, db_instance):

    KILOMETERS = 2

    # simulate login
    user = ensure_logged_in(client, db_instance)

    # generate some runs
    runs = []
    for i in ['1', '2']:
        run = Run()
        run.runner = user
        run.strava_id = i
        run.name = "Run " + i
        run.average_speed = float(i)
        #distance in meters
        run.distance = KILOMETERS * 1000
        run.elapsed_time = float(i) * float(i) * 1000
        runs.append(run)
        db_instance.session.add(run)

    db_instance.session.commit()

    #Testing total distance in "PROGRESS" field
    res = client.get("/")
    html = pq(res.data)
    total_distance = html("#tot_dist").html()

    assert float(total_distance) == float(len(runs) * KILOMETERS)

    #Testing remaining kilometers in "PROGRESS" field
    _setObjective(user, 30000)
    res = client.get("/")
    html = pq(res.data)
    rem_km = float(html("#rem_KM").html())
    objective = float(html("#obj_dist").html())
    total_distance = float(html("#tot_dist").html())

    assert (rem_km >= 0.0)
    assert rem_km == (objective - total_distance)
Example #9
0
def test_compare_stats(client, db_instance):

    # simulate login
    user = ensure_logged_in(client, db_instance)

    # generate some runs
    runs = []
    for i in ['1', '2']:
        run = Run()

        run.runner = user
        run.strava_id = i
        run.name = "Run " + i
        run.average_speed = float(i)
        run.elapsed_time = float(i) * float(i) * 1000

        runs.append(run)

        db_instance.session.add(run)
    db_instance.session.commit()

    # route to comparisons page
    res = client.post('/comparisons',
                      data={'runs': ['1', '2']},
                      follow_redirects=True)

    # parse html page
    html = pq(res.data)
    table = html("#comparison-table")
    trs = table("tr")

    # check number of rows equals given runs (2)
    assert len(trs) - 1 == 2

    # check html table shows correct results
    for i in range(1, len(trs)):
        tds = trs.eq(i)("td")
        assert runs[i - 1].average_speed == tds[1].text.strip()
        assert runs[i - 1].elapsed_time == tds[2].text.strip()
Example #10
0
def new_run(user,
            strava_id=randint(100, 100000000),
            name=None,
            distance=uniform(50.0, 10000.0),
            elapsed_time=uniform(30.0, 3600.0),
            average_heartrate=None,
            total_elevation_gain=uniform(0.0, 25.0),
            start_date=datetime.now()):
    if name is None:
        name = "Run %s" % strava_id

    run = Run()
    run.runner = user
    run.strava_id = strava_id  # a random number 100 - 1.000.000, we hope is unique
    run.name = name
    run.distance = distance  # 50m - 10 km
    run.elapsed_time = elapsed_time  # 30s - 1h
    run.average_speed = run.distance / run.elapsed_time
    run.average_heartrate = average_heartrate
    run.total_elevation_gain = total_elevation_gain  # 0m - 25m
    run.start_date = start_date
    db.session.add(run)
    db.session.commit()
Example #11
0
def test_challenge_run(client, db_instance):

    # simulate login
    user = ensure_logged_in(client, db_instance)

    # generate some runs
    runs = []
    for i in ['1', '2', '3', '4', '5']:
        # creating 5 incrementally better runs, except for the fourth one which is bad
        run = Run()

        run.runner = user
        run.strava_id = i
        run.name = "Run " + i
        if i != '4':
            run.average_speed = float(i)
            run.elapsed_time = float(i) * 1000
            run.distance = 25 + float(i)
        else:
            run.average_speed = 0
            run.elapsed_time = 1
            run.distance = 1
        runs.append(run)

    # truncate runs table
    db_instance.session.query(Run).delete()
    db_instance.session.flush()
    db_instance.session.commit()

    r1 = _copy_run(runs, 0)
    r2 = _copy_run(runs, 1)

    db_instance.session.add(r1)
    db_instance.session.add(r2)

    db_instance.session.flush()
    db_instance.session.commit()

    # route back to index page
    res = client.post('/challenge',
                      data={'runs': ['1']},
                      follow_redirects=True)

    challenged = db_instance.session.query(Challenge).filter(
        user.id == Run.runner_id).first()

    assert challenged
    assert challenged.run_id == 1

    r3 = _copy_run(runs, 2)
    r4 = _copy_run(runs, 3)
    r5 = _copy_run(runs, 4)

    db_instance.session.add(r3)
    db_instance.session.add(r4)
    db_instance.session.add(r5)

    db_instance.session.flush()
    db_instance.session.commit()

    toCompare = db_instance.session.query(Run).filter(
        user.id == Run.runner_id, Run.id > challenged.latest_run_id).all()

    assert len(toCompare) == 3

    better = 0
    worse = 0
    for run in toCompare:
        if run.average_speed > challenged.run.average_speed and run.distance > challenged.run.distance:
            better += 1
        else:
            worse += 1

    assert better == 2
    assert worse == 1
    assert better + worse == 3

    res = client.post('/challenge',
                      data={'runs': ['1']},
                      follow_redirects=True)

    challenged = db_instance.session.query(Challenge).filter(
        user.id == Run.runner_id).first()

    assert not challenged