def test_cycling_leaderboard(self):

        create_subjecttypes()
        create_subjects()
        create_variables()
        create_achievements()

        klaus = DBSession.query(Subject).filter_by(name="Klaus").first()
        clara = DBSession.query(Subject).filter_by(name="Clara").first()
        liam = DBSession.query(Subject).filter_by(name="Liam").first()

        bielefeld = DBSession.query(Subject).filter_by(
            name="Bielefeld").first()
        paderborn = DBSession.query(Subject).filter_by(
            name="Paderborn").first()
        germany = DBSession.query(Subject).filter_by(name="Germany").first()
        france = DBSession.query(Subject).filter_by(name="France").first()

        cyclist_of_the_month_achievement = DBSession.query(
            Achievement).filter_by(name="cyclist_of_the_month").first()

        def cycle(user, km, dt):
            Value.increase_value(variable_name="cycling",
                                 subject_id=user.id,
                                 value=km,
                                 key=None,
                                 at_datetime=dt)

        def ev(user, dt, context_subject):
            return Achievement.evaluate(
                compared_subject=user,
                achievement_id=cyclist_of_the_month_achievement.id,
                achievement_date=AchievementDate.compute(
                    evaluation_timezone=cyclist_of_the_month_achievement.
                    evaluation_timezone,
                    evaluation_type=cyclist_of_the_month_achievement.
                    evaluation,
                    evaluation_shift=cyclist_of_the_month_achievement.
                    evaluation_shift,
                    context_datetime=dt),
                execute_triggers=False,
                context_subject_id=context_subject.id)

        cycle(klaus, 5, default_dt())
        cycle(clara, 3, default_dt())
        cycle(liam, 10, default_dt())

        lb_bielefeld = ev(klaus, default_dt(), bielefeld)
        lb_germany = ev(klaus, default_dt(), germany)

        self.assertEqual(lb_bielefeld["leaderboard_position"], 0)
        self.assertEqual(lb_germany["leaderboard_position"], 1)

        lb_bielefeld = ev(clara, default_dt(), bielefeld)
        lb_germany = ev(clara, default_dt(), germany)
        lb_france = ev(clara, default_dt(), france)

        self.assertEqual(lb_bielefeld["leaderboard_position"], 1)
        self.assertEqual(lb_germany["leaderboard_position"], 2)
        self.assertEqual(lb_france["leaderboard_position"], None)

        # clara also cycled last month
        Subject.join_subject(subject_id=clara.id,
                             part_of_id=germany.id,
                             join_date=last_month(default_dt()))
        cycle(clara, 10, last_month(default_dt()))

        # should not effect this month
        lb_germany = ev(clara, default_dt(), germany)
        self.assertEqual(lb_germany["leaderboard_position"], 2)

        # but should effect last month
        lb_germany = ev(clara, last_month(default_dt()), germany)
        self.assertEqual(lb_germany["leaderboard_position"], 0)
Example #2
0
def create_subjects():
    germany = create_subject(name="Germany", type="Country")
    DBSession.add(germany)

    france = create_subject(name="France", type="Country")
    DBSession.add(france)

    bielefeld = create_subject(name="Bielefeld", type="City")
    DBSession.add(bielefeld)
    DBSession.flush()
    Subject.join_subject(subject_id=bielefeld.id,
                         part_of_id=germany.id,
                         join_date=default_dt())

    paderborn = create_subject(name="Paderborn", type="City")
    DBSession.add(paderborn)
    DBSession.flush()
    Subject.join_subject(subject_id=paderborn.id,
                         part_of_id=germany.id,
                         join_date=default_dt())

    lyon = create_subject(name="Lyon", type="City")
    DBSession.add(lyon)
    DBSession.flush()
    Subject.join_subject(subject_id=lyon.id,
                         part_of_id=france.id,
                         join_date=default_dt())

    junior_developer = create_subject(name="Junior Developer", type="Position")
    DBSession.add(junior_developer)

    senior_developer = create_subject(name="Senior Developer", type="Position")
    DBSession.add(senior_developer)

    project_manager = create_subject(name="Project Manager", type="Position")
    DBSession.add(project_manager)

    dev_team_bielefeld = create_subject(name="Developer Team Bielefeld",
                                        type="Team")
    DBSession.add(dev_team_bielefeld)
    DBSession.flush()
    Subject.join_subject(subject_id=dev_team_bielefeld.id,
                         part_of_id=bielefeld.id,
                         join_date=default_dt())

    dev_team_paderborn = create_subject(name="Developer Team Paderborn",
                                        type="Team")
    DBSession.add(dev_team_paderborn)
    DBSession.flush()
    Subject.join_subject(subject_id=dev_team_paderborn.id,
                         part_of_id=paderborn.id,
                         join_date=default_dt())

    dev_team_lyon = create_subject(name="Developer Team Lyon", type="Team")
    DBSession.add(dev_team_lyon)
    DBSession.flush()
    Subject.join_subject(subject_id=dev_team_lyon.id,
                         part_of_id=lyon.id,
                         join_date=default_dt())

    klaus = create_subject(name="Klaus", type="User")
    DBSession.add(klaus)
    DBSession.flush()
    Subject.join_subject(subject_id=klaus.id,
                         part_of_id=bielefeld.id,
                         join_date=default_dt())
    Subject.join_subject(subject_id=klaus.id,
                         part_of_id=junior_developer.id,
                         join_date=default_dt())
    Subject.join_subject(subject_id=klaus.id,
                         part_of_id=dev_team_bielefeld.id,
                         join_date=default_dt())
    Subject.join_subject(subject_id=klaus.id,
                         part_of_id=germany.id,
                         join_date=default_dt())

    clara = create_subject(name="Clara", type="User")
    DBSession.add(clara)
    DBSession.flush()
    Subject.join_subject(subject_id=clara.id,
                         part_of_id=bielefeld.id,
                         join_date=default_dt())
    Subject.join_subject(subject_id=clara.id,
                         part_of_id=junior_developer.id,
                         join_date=default_dt())
    Subject.join_subject(subject_id=clara.id,
                         part_of_id=dev_team_bielefeld.id,
                         join_date=default_dt())
    Subject.join_subject(subject_id=clara.id,
                         part_of_id=germany.id,
                         join_date=default_dt())

    manfred = create_subject(name="Manfred", type="User")
    DBSession.add(manfred)
    DBSession.flush()
    Subject.join_subject(subject_id=manfred.id,
                         part_of_id=bielefeld.id,
                         join_date=default_dt())
    Subject.join_subject(subject_id=manfred.id,
                         part_of_id=junior_developer.id,
                         join_date=default_dt())
    Subject.join_subject(subject_id=manfred.id,
                         part_of_id=dev_team_bielefeld.id,
                         join_date=default_dt())
    Subject.join_subject(subject_id=manfred.id,
                         part_of_id=germany.id,
                         join_date=default_dt())

    otto = create_subject(name="Otto", type="User")
    DBSession.add(otto)
    DBSession.flush()
    Subject.join_subject(subject_id=otto.id,
                         part_of_id=bielefeld.id,
                         join_date=default_dt())
    Subject.join_subject(subject_id=otto.id,
                         part_of_id=junior_developer.id,
                         join_date=default_dt())
    Subject.join_subject(subject_id=otto.id,
                         part_of_id=dev_team_bielefeld.id,
                         join_date=default_dt())
    Subject.join_subject(subject_id=otto.id,
                         part_of_id=germany.id,
                         join_date=default_dt())

    max = create_subject(name="Max", type="User")
    DBSession.add(max)
    DBSession.flush()
    Subject.join_subject(subject_id=max.id,
                         part_of_id=bielefeld.id,
                         join_date=default_dt())
    Subject.join_subject(subject_id=max.id,
                         part_of_id=senior_developer.id,
                         join_date=default_dt())
    Subject.join_subject(subject_id=max.id,
                         part_of_id=dev_team_bielefeld.id,
                         join_date=default_dt())
    Subject.join_subject(subject_id=max.id,
                         part_of_id=germany.id,
                         join_date=default_dt())

    ronny = create_subject(name="Ronny", type="User")
    DBSession.add(ronny)
    DBSession.flush()
    Subject.join_subject(subject_id=ronny.id,
                         part_of_id=bielefeld.id,
                         join_date=default_dt())
    Subject.join_subject(subject_id=ronny.id,
                         part_of_id=senior_developer.id,
                         join_date=default_dt())
    Subject.join_subject(subject_id=ronny.id,
                         part_of_id=dev_team_bielefeld.id,
                         join_date=default_dt())
    Subject.join_subject(subject_id=ronny.id,
                         part_of_id=germany.id,
                         join_date=default_dt())

    sonja = create_subject(name="Sonja", type="User")
    DBSession.add(sonja)
    DBSession.flush()
    Subject.join_subject(subject_id=sonja.id,
                         part_of_id=bielefeld.id,
                         join_date=default_dt())
    Subject.join_subject(subject_id=sonja.id,
                         part_of_id=project_manager.id,
                         join_date=default_dt())
    Subject.join_subject(subject_id=sonja.id,
                         part_of_id=dev_team_bielefeld.id,
                         join_date=default_dt())
    Subject.join_subject(subject_id=sonja.id,
                         part_of_id=germany.id,
                         join_date=default_dt())

    #Paderborn

    liam = create_subject(name="Liam", type="User")
    DBSession.add(liam)
    DBSession.flush()
    Subject.join_subject(subject_id=liam.id,
                         part_of_id=paderborn.id,
                         join_date=default_dt())
    Subject.join_subject(subject_id=liam.id,
                         part_of_id=junior_developer.id,
                         join_date=default_dt())
    Subject.join_subject(subject_id=liam.id,
                         part_of_id=dev_team_paderborn.id,
                         join_date=default_dt())
    Subject.join_subject(subject_id=liam.id,
                         part_of_id=germany.id,
                         join_date=default_dt())

    tim = create_subject(name="Tim", type="User")
    DBSession.add(tim)
    DBSession.flush()
    Subject.join_subject(subject_id=tim.id,
                         part_of_id=paderborn.id,
                         join_date=default_dt())
    Subject.join_subject(subject_id=tim.id,
                         part_of_id=junior_developer.id,
                         join_date=default_dt())
    Subject.join_subject(subject_id=tim.id,
                         part_of_id=dev_team_paderborn.id,
                         join_date=default_dt())
    Subject.join_subject(subject_id=tim.id,
                         part_of_id=germany.id,
                         join_date=default_dt())

    robin = create_subject(name="Robin", type="User")
    DBSession.add(robin)
    DBSession.flush()
    Subject.join_subject(subject_id=robin.id,
                         part_of_id=paderborn.id,
                         join_date=default_dt())
    Subject.join_subject(subject_id=robin.id,
                         part_of_id=junior_developer.id,
                         join_date=default_dt())
    Subject.join_subject(subject_id=robin.id,
                         part_of_id=dev_team_paderborn.id,
                         join_date=default_dt())
    Subject.join_subject(subject_id=robin.id,
                         part_of_id=germany.id,
                         join_date=default_dt())

    alina = create_subject(name="Alina", type="User")
    DBSession.add(alina)
    DBSession.flush()
    Subject.join_subject(subject_id=alina.id,
                         part_of_id=paderborn.id,
                         join_date=default_dt())
    Subject.join_subject(subject_id=alina.id,
                         part_of_id=junior_developer.id,
                         join_date=default_dt())
    Subject.join_subject(subject_id=alina.id,
                         part_of_id=dev_team_paderborn.id,
                         join_date=default_dt())
    Subject.join_subject(subject_id=alina.id,
                         part_of_id=germany.id,
                         join_date=default_dt())

    charlotte = create_subject(name="Charlotte", type="User")
    DBSession.add(charlotte)
    DBSession.flush()
    Subject.join_subject(subject_id=charlotte.id,
                         part_of_id=paderborn.id,
                         join_date=default_dt())
    Subject.join_subject(subject_id=charlotte.id,
                         part_of_id=senior_developer.id,
                         join_date=default_dt())
    Subject.join_subject(subject_id=charlotte.id,
                         part_of_id=dev_team_paderborn.id,
                         join_date=default_dt())
    Subject.join_subject(subject_id=charlotte.id,
                         part_of_id=germany.id,
                         join_date=default_dt())

    ida = create_subject(name="Ida", type="User")
    DBSession.add(ida)
    DBSession.flush()
    Subject.join_subject(subject_id=ida.id,
                         part_of_id=paderborn.id,
                         join_date=default_dt())
    Subject.join_subject(subject_id=ida.id,
                         part_of_id=senior_developer.id,
                         join_date=default_dt())
    Subject.join_subject(subject_id=ida.id,
                         part_of_id=dev_team_paderborn.id,
                         join_date=default_dt())
    Subject.join_subject(subject_id=ida.id,
                         part_of_id=germany.id,
                         join_date=default_dt())

    carolin = create_subject(name="Carolin", type="User")
    DBSession.add(carolin)
    DBSession.flush()
    Subject.join_subject(subject_id=carolin.id,
                         part_of_id=paderborn.id,
                         join_date=default_dt())
    Subject.join_subject(subject_id=carolin.id,
                         part_of_id=project_manager.id,
                         join_date=default_dt())
    Subject.join_subject(subject_id=carolin.id,
                         part_of_id=dev_team_paderborn.id,
                         join_date=default_dt())
    Subject.join_subject(subject_id=carolin.id,
                         part_of_id=germany.id,
                         join_date=default_dt())

    #Lyon

    lola = create_subject(name="Lola", type="User")
    DBSession.add(lola)
    DBSession.flush()
    Subject.join_subject(subject_id=lola.id,
                         part_of_id=lyon.id,
                         join_date=default_dt())
    Subject.join_subject(subject_id=lola.id,
                         part_of_id=junior_developer.id,
                         join_date=default_dt())
    Subject.join_subject(subject_id=lola.id,
                         part_of_id=dev_team_lyon.id,
                         join_date=default_dt())
    Subject.join_subject(subject_id=lola.id,
                         part_of_id=france.id,
                         join_date=default_dt())

    lina = create_subject(name="Lina", type="User")
    DBSession.add(lina)
    DBSession.flush()
    Subject.join_subject(subject_id=lina.id,
                         part_of_id=lyon.id,
                         join_date=default_dt())
    Subject.join_subject(subject_id=lina.id,
                         part_of_id=senior_developer.id,
                         join_date=default_dt())
    Subject.join_subject(subject_id=lina.id,
                         part_of_id=dev_team_lyon.id,
                         join_date=default_dt())
    Subject.join_subject(subject_id=lina.id,
                         part_of_id=france.id,
                         join_date=default_dt())

    ethan = create_subject(name="Ethan", type="User")
    DBSession.add(ethan)
    DBSession.flush()
    Subject.join_subject(subject_id=ethan.id,
                         part_of_id=lyon.id,
                         join_date=default_dt())
    Subject.join_subject(subject_id=ethan.id,
                         part_of_id=project_manager.id,
                         join_date=default_dt())
    Subject.join_subject(subject_id=ethan.id,
                         part_of_id=dev_team_lyon.id,
                         join_date=default_dt())
    Subject.join_subject(subject_id=ethan.id,
                         part_of_id=france.id,
                         join_date=default_dt())