def test_aggregate_final_state(
        activity_aggregate_schema: ActivityAggregateSchema,
        activity_events: List[Record]) -> None:
    # Initialize the starting state
    identity = 'user1'
    evaluation_context = EvaluationContext()
    evaluation_context.global_add('identity', identity)
    activity_aggregate = ActivityAggregate(activity_aggregate_schema, identity,
                                           evaluation_context)
    evaluation_context.global_add(activity_aggregate._schema.name,
                                  activity_aggregate)

    for record in activity_events:
        evaluate_event(record, activity_aggregate)

    activity_aggregate.run_finalize()

    store_state = activity_aggregate._store.get_all(identity)
    assert len(store_state) == 3
    assert store_state.get(
        Key('user1', 'activity_aggr',
            datetime(2018, 1, 1, 1, 1, 1, 0, timezone.utc))) == {
                '_identity': 'user1',
                '_start_time': datetime(2018, 1, 1, 1, 1, 1, 0,
                                        timezone.utc).isoformat(),
                '_end_time': datetime(2018, 1, 1, 1, 2, 1, 0,
                                      timezone.utc).isoformat(),
                'sum': 111,
                'count': 3
            }

    assert store_state.get(
        Key('user1', 'activity_aggr',
            datetime(2018, 1, 1, 3, 1, 1, 0, timezone.utc))) == {
                '_identity': 'user1',
                '_start_time': datetime(2018, 1, 1, 3, 1, 1, 0,
                                        timezone.utc).isoformat(),
                '_end_time': datetime(2018, 1, 1, 3, 1, 1, 0,
                                      timezone.utc).isoformat(),
                'sum': 1000,
                'count': 1
            }

    assert store_state.get(
        Key('user1', 'activity_aggr',
            datetime(2018, 1, 2, 1, 1, 1, 0, timezone.utc))) == {
                '_identity': 'user1',
                '_start_time': datetime(2018, 1, 2, 1, 1, 1, 0,
                                        timezone.utc).isoformat(),
                '_end_time': datetime(2018, 1, 2, 1, 1, 1, 0,
                                      timezone.utc).isoformat(),
                'sum': 10000,
                'count': 1
            }
def test_evaluate_no_separation(
        activity_aggregate_schema: ActivityAggregateSchema,
        activity_events: List[Record]) -> None:
    # Initialize the starting state
    identity = 'user1'
    evaluation_context = EvaluationContext()
    evaluation_context.global_add('identity', identity)
    activity_aggregate = ActivityAggregate(activity_aggregate_schema, identity,
                                           evaluation_context)
    evaluation_context.global_add(activity_aggregate._schema.name,
                                  activity_aggregate)

    store_state = activity_aggregate._store.get_all(identity)
    assert len(store_state) == 0

    evaluate_event(activity_events[0], activity_aggregate)
    activity_aggregate._persist()

    store_state = activity_aggregate._store.get_all(identity)
    assert len(store_state) == 1

    evaluate_event(activity_events[1], activity_aggregate)
    activity_aggregate._persist()

    store_state = activity_aggregate._store.get_all(identity)
    assert len(store_state) == 1
def evaluate_event(record: Record, aggregate: ActivityAggregate) -> None:
    aggregate._evaluation_context.global_add('source', record)
    aggregate._evaluation_context.global_add('time',
                                             parser.parse(record.event_time))
    aggregate.run_evaluate()