Ejemplo n.º 1
0
def test_log_get_events(site_users, store0):
    user = site_users["user"]
    event_log = Log()
    kwargs = dict(user=user, store=store0)
    result = sorted(
        event_log.get_events(**kwargs),
        key=(lambda ev: (ev.timestamp, ev.unit.pk)))
    expected = sorted(
        list(event_log.get_created_units(**kwargs))
        + list(event_log.get_suggestions(**kwargs))
        + list(event_log.get_submissions(**kwargs)),
        key=(lambda ev: (ev.timestamp, ev.unit.pk)))
    assert (
        [(x.timestamp, x.unit, x.action)
         for x in result]
        == [(x.timestamp, x.unit, x.action)
            for x in expected])
Ejemplo n.º 2
0
def test_log_get_suggestions(member, store0):
    suggestions = Suggestion.objects.all()
    sugg_start, sugg_end = _get_mid_times(suggestions)
    sugg_log = Log()
    sugg_events = sugg_log.get_suggestions()
    assert type(sugg_events).__name__ == "generator"
    user_time_suggestions = (
        (sugg_log.filter_user(
            sugg_log.suggestions,
            member,
            field="user_id")
         & sugg_log.filter_timestamps(
             sugg_log.suggestions,
             start=sugg_start,
             end=sugg_end))
        | (sugg_log.filter_user(
            sugg_log.suggestions,
            member,
            field="reviewer_id")
           & sugg_log.filter_timestamps(
               sugg_log.suggestions,
               start=sugg_start,
               end=sugg_end,
               field="review_time")))
    assert user_time_suggestions
    pending = suggestions.filter(
        creation_time__gte=sugg_start,
        creation_time__lt=sugg_end,
        state__name="pending").first()
    review.get(Suggestion)([pending], member).accept()
    pending = suggestions.filter(
        creation_time__gte=sugg_start,
        creation_time__lt=sugg_end,
        state__name="pending").first()
    review.get(Suggestion)([pending], member).reject()
    pending.review_time = sugg_start
    pending.save()
    expected = {}
    for suggestion in user_time_suggestions.all():
        add_event = (
            (suggestion.creation_time >= sugg_start)
            and (suggestion.creation_time < sugg_end)
            and (suggestion.user == member))
        review_event = (
            (suggestion.review_time >= sugg_start)
            and (suggestion.review_time < sugg_end)
            and (suggestion.reviewer == member))
        expected[suggestion.id] = {}
        if add_event:
            expected[suggestion.id]["suggestion_created"] = (
                sugg_log.event(
                    suggestion.unit,
                    suggestion.user,
                    suggestion.creation_time,
                    "suggestion_created",
                    suggestion))
        if review_event:
            event_name = (
                "suggestion_accepted"
                if suggestion.state.name == "accepted"
                else "suggestion_rejected")
            expected[suggestion.id][event_name] = (
                sugg_log.event(
                    suggestion.unit,
                    suggestion.reviewer,
                    suggestion.review_time,
                    event_name,
                    suggestion))
    result = sugg_log.get_suggestions(
        start=sugg_start, end=sugg_end, user=member)
    for event in result:
        assert isinstance(event, sugg_log.event)
        sugg_review = expected[event.value.pk][event.action]
        assert event.unit == sugg_review.unit
        assert event.action in [
            "suggestion_created", "suggestion_accepted", "suggestion_rejected"]
        assert event.user == (
            sugg_review.value.user
            if event.action == "suggestion_created"
            else sugg_review.value.reviewer)
        assert event.timestamp == (
            sugg_review.value.creation_time
            if event.action == "suggestion_created"
            else sugg_review.value.review_time)
        assert event.value == sugg_review.value