示例#1
0
def test_get_overlapping_visits():
    user = aum.User.objects.create(name="x")
    importstep = arm.Importstep(user=user)
    importstep.save()
    rm1s1, rm1s2 = make_seats(importstep, "room1", 2)
    rm2s1, = make_seats(importstep, "room2", 1)
    targetvisit = make_visit(rm1s1, "p1", "03:00", "04:00")
    shorttargetvisit = make_visit(rm1s1, "p1", "03:00", "03:01")
    # --- the following other visits have _y if they are to be found, _n if not:
    otherroom_n = make_visit(rm2s1, "p2", "03:00", "04:00")
    before_n = make_visit(rm1s2, "p3", "02:00", "03:00")
    within_y = make_visit(rm1s2, "p4", "03:15", "03:45")
    across_y = make_visit(rm1s2, "p5", "02:00", "05:00")
    after_n = make_visit(rm1s2, "p3", "04:00", "05:00")
    halfbefore_y = make_visit(rm1s2, "p6", "02:30", "03:30")
    halfafter_y = make_visit(rm1s2, "p7", "03:30", "04:30")
    nearlybefore_n = make_visit(rm1s2, "p8", "02:00", "03:01")
    nearlyafter_n = make_visit(rm1s2, "p9", "03:59", "05:00")
    # --- now look which ones appear for targetvisit:
    results = set(targetvisit.get_overlapping_visits())
    result_pks = set(el.pk for el in results)
    expected = set(el.pk for el in (targetvisit, within_y, across_y,
                                    halfbefore_y, halfafter_y))
    not_expected = set(el.pk for el in (otherroom_n, before_n, after_n,
                                        nearlybefore_n, nearlyafter_n))
    print("result_pks", result_pks)
    print("expected", expected)
    print("not_expected", not_expected)
    assert result_pks.isdisjoint(not_expected)
    assert result_pks == expected
    # --- now look which ones appear for shorttargetvisit:
    assert shorttargetvisit.get_overlapping_visits().count() == 0
示例#2
0
def make_user_rooms_seats_visits(seat_last: str, visitsN: int) -> \
        tg.Tuple[tg.Sequence[arm.Room], tg.Sequence[tg.Sequence[arm.Seat]]]:
    """
    Creates 2 Rooms, each with #seats Seats from 'r1s1' to seat_last, and 
    visitsN 30-minute Visits for each Room,
    of which groups of #seats overlap and the next group is an hour later.
    #seats different people are used round-robin for these Visits.
    The first Visit is 24h ago, 
    each visit starts one minute after the previous one in the group.
    In room2, everything happens 10 minutes later than in room1.
    """
    MINUTE = dt.timedelta(minutes=1)
    user = make_datenverwalter_user()
    visitlength = dt.timedelta(minutes=30)
    importstep = arm.Importstep(user=user)
    importstep.save()
    rooms = []
    seatgroups = []
    for roomI in range(2):
        when = djut.localtime() - dt.timedelta(hours=24) + dt.timedelta(minutes=roomI * 10)
        visitI = 0
        room, seats = _mursv_seats(importstep, f"room{roomI + 1}", seat_last)
        rooms.append(room)
        seatgroups.append(seats)
        while visitI < visitsN:
            for visitorI, seat in enumerate(seats):  # make visits
                tfrom = when + visitorI * MINUTE
                _mursv_visit(seat, tfrom, tfrom + visitlength, visitorI)
                visitI += 1
                if visitI == visitsN:
                    break
            when += dt.timedelta(hours=1)  # next group one hour later
    return (rooms, seatgroups)
示例#3
0
def test_current_unique_visitorsN():
    # test can fail if run very shortly before midnight, just run it again
    user = aum.User.objects.create(name="x")
    importstep = arm.Importstep(user=user)
    importstep.save()

    def show_them():
        import datetime as dt
        now = djut.localtime()
        them = arm.Visit.objects.filter(
            seat__room=room, present_from_dt__lte=now,
            present_to_dt__gte=now).order_by('email').distinct('email')
        print([v.email for v in them])

    rm1s1, rm1s2, rm1s3 = make_seats(importstep, "room1", 3)
    rm2s1, = make_seats(importstep, "room2", 1)
    room = rm1s1.room
    person1_early = make_visit(rm1s1, "p1", "02:58", "04:00")  # noqa
    person2_ontime = make_visit(rm1s2, "p2", "03:00", "04:00")  # noqa
    person3_late = make_visit(rm1s3, "p3", "03:03", "04:00")  # noqa
    person4_otherroom = make_visit(rm2s1, "p4", "03:00", "04:00")  # noqa
    person3_changed = make_visit(rm1s1, "p3", "03:30", "04:00")  # noqa

    # --- now look at different times how many are in rm1:
    def freeze_at(ts: str):
        return freeze_time(aud.make_dt('now', ts))

    with freeze_at("02:50"):
        show_them()
        assert arm.Visit.current_unique_visitorsN(room) == 0
    with freeze_at("02:59"):
        show_them()
        assert arm.Visit.current_unique_visitorsN(room) == 1
    with freeze_at("03:01"):
        show_them()
        assert arm.Visit.current_unique_visitorsN(room) == 2
    with freeze_at("03:06"):
        show_them()
        assert arm.Visit.current_unique_visitorsN(room) == 3
    with freeze_at("03:33"):
        show_them()
        assert arm.Visit.current_unique_visitorsN(room) == 3
    with freeze_at("05:00"):
        show_them()
        assert arm.Visit.current_unique_visitorsN(room) == 0
示例#4
0
def make_organizations(descr) -> None:
    persons = ("p1", "p2", "p3", "p4")
    personindex = 0
    user = aum.User.objects.create(name="x")
    importstep = arm.Importstep(user=user)
    importstep.save()
    for orgname, orgdescr in descr.items():
        for depname, depdescr in orgdescr.items():
            for roomname, roomdescr in depdescr.items():
                numseats, numvisits = roomdescr
                seats = make_seats(importstep,
                                   roomname,
                                   numseats,
                                   organization=orgname,
                                   department=depname)
                for i in range(numvisits):
                    make_visit(seats[i % len(seats)],
                               persons[personindex % len(persons)])
                    personindex += 1
示例#5
0
def make_seats(roomname: str, numseats: int, organization="org",
               department="dep") -> tg.Tuple[arm.Seat, ...]:
    importstep = arm.Importstep.objects.first()  # reuse existing, if any
    if not importstep:
        user = aum.User.objects.first() or aum.User.objects.create(name="x")
        importstep = arm.Importstep(user=user)
        importstep.save()
    results = []
    room = arm.Room(organization=organization, department=department,
                    building="bldg",
                    room=roomname,
                    row_dist=1.3, seat_dist=0.8,
                    seat_last=arm.Seat.form_seatname(1, numseats),
                    importstep=importstep)
    room.save()
    for i in range(numseats):
        seat = arm.Seat(
            hash=arm.Seat.seathash(room, arm.Seat.form_seatname(1, i + 1)),
            rownumber=1, seatnumber=i + 1, room=room)
        seat.save()
        results.append(seat)
    return tuple(results)
示例#6
0
def _create_importstep(user: aum.User) -> arm.Importstep:
    result = arm.Importstep(user=user)
    result.save()
    return result