Example #1
0
def test_serialize_share(user_count, expense_count):
    share = random_shares(1)[0]
    if user_count:
        users = random_users(user_count)
        for user in users:
            share.users.add(user)
            share.save()
    if expense_count:
        random_expenses(expense_count, share=share)
    _assert_round_trip(ShareSerializer, share)
Example #2
0
def test_serialize_expense(created_at, resolved, paid_for_count):
    if created_at:
        (expense,), (share,), (user,) = random_expenses(
            1, resolved=resolved, created_at=rand_time(False))
    else:
        (expense,), (share,), (user,) = random_expenses(1, resolved=resolved)
    paid_for_diff = paid_for_count - 1
    paid_for_users = random_users(paid_for_diff)
    for paid_for_u in chain(paid_for_users, [user]):
        share.users.add(paid_for_u)
        share.save()
    paid_for = {u: (1, paid_for_count) for u in chain(paid_for_users, [user])}
    expense.generate_ratio(paid_for)
    _assert_round_trip(ExpenseSerializer, expense)
def test_update_expense(description, share, total, paid_by, paid_for,
                        resolved):
    (expense, *_), shares, users = random_expenses(5)
    orig_paid_for = {user: (1, 3) for user in users[:-2]}
    expense.generate_ratio(orig_paid_for)
    orig_uptate_time = expense.updated_at

    validated_data = {} if resolved is None else {'resolved': resolved}

    if description:
        validated_data['description'] = random_str(12) + expense.description
    if share:
        validated_data['share'] = shares[2]
    if total:
        validated_data['total'] = expense.total + uniform(0.5, 123.4)
    if paid_by:
        validated_data['paid_by'] = choice(
            tuple(set(users).difference({expense.paid_by})))
    if paid_for == 'add':
        validated_data['paid_for'] = {user: (1, 5) for user in users}
    elif paid_for == 'delete':
        validated_data['paid_for'] = {user: (1, 2) for user in users[:-3]}

    serializer = ExpenseSerializer()
    serializer.update(expense, validated_data)

    assert_update_time(orig_uptate_time, expense, False)
    assert_expense_items(validated_data, expense)
Example #4
0
def test_expense_delete():
    (expense, *_), shares, users = random_expenses(5)
    paid_for = {user: (1, 5) for user in users}
    expense.generate_ratio(paid_for)
    id_ = expense.id
    assert ExpenseRatio.objects.filter(expense=id_).count() == 5
    expense.delete()
    assert not ExpenseRatio.objects.filter(expense=id_)
Example #5
0
def test_expense_ratios():
    (expense, *_), shares, users = random_expenses(5)
    paid_for = {user: (1, 5) for user in users}
    expense.generate_ratio(paid_for)
    ratios = list(expense.paid_for)

    for ratio in ratios:
        assert ratio.expense == expense

    actual_paid_for = {
        ratio.user: (ratio.numerator, ratio.denominator)
        for ratio in ratios
    }
    assert paid_for == actual_paid_for
Example #6
0
def test_serialize_user(paid_by_count, paid_for_count, share_count):
    user = random_users(1)[0]
    if share_count:
        shares = random_shares(share_count)
        for share in shares:
            share.users.add(user)
            share.save()
    max_expense_count = max(paid_by_count, paid_for_count)
    if max_expense_count:
        expenses, *_ = random_expenses(max_expense_count)
        for i, exp in enumerate(expenses):
            if i < paid_by_count:
                exp.paid_by = user
                exp.save()
            if i < paid_for_count:
                exp.generate_ratio({user: (1, 1)})
    _assert_round_trip(UserSerializer, user)