def _one_of_each():
    user = User.objects.create(username="******")
    bs = Subscription(user=user, bill_id="ocd-bill/1")
    qs = Subscription(user=user, query="topic", state="ak")
    ss = Subscription(user=user,
                      sponsor=Person.objects.get(name="Amanda Adams"))
    return bs, qs, ss
def test_complex_pretty():
    cs = Subscription(
        query="topic",
        state="ak",
        chamber="upper",
        classification="bill",
        subjects=["MOOSE", "WILDLIFE"],
        status=["passed_lower"],
        sponsor=Person.objects.get(name="Amanda Adams"),
    )
    assert cs.pretty == COMPLEX_STR
def test_process_query_sub_simple(user):
    hb1 = Bill.objects.get(identifier="HB 1")
    sub = Subscription(user=user, query="moose")

    now = utcnow()
    yesterday = now - datetime.timedelta(days=1)

    # no changes since now
    assert process_query_sub(sub, now) == []

    # bill changed in last day
    assert process_query_sub(sub, yesterday) == [hb1]
def test_process_bill_sub(user):
    hb1 = Bill.objects.get(identifier="HB 1")
    sub = Subscription(user=user, bill=hb1)

    now = utcnow()
    yesterday = now - datetime.timedelta(days=1)

    # no changes since now
    assert process_bill_sub(sub, now) is None

    # bill changed in last day
    assert process_bill_sub(sub, yesterday) == hb1
def test_process_bill_sub_future(user):
    hb1 = Bill.objects.get(identifier="HB 1")
    sub = Subscription(user=user, bill=hb1)

    now = utcnow()
    yesterday = now - datetime.timedelta(days=1)
    tomorrow = now + datetime.timedelta(days=1)
    hb1.latest_action_date = tomorrow.strftime("%Y-%m-%d")
    hb1.save()

    # no changes since now
    assert process_bill_sub(sub, now) is None

    # bill changed in last day
    assert process_bill_sub(sub, yesterday) is None
    def create(self, validated_data):
        kit = validated_data.pop('kit')
        subscription_plan = kit.subscription_plan
        user = validated_data.pop('user')

        subscription = Subscription(user=user, active=True,
                                    subscription_plan=subscription_plan)
        default_billing = validated_data.get('default_billing', False)
        if default_billing and user.profile.billing_address:
            billing_data = user.profile.billing_address
            subscription.billing_address = billing_data
        else:
            billing_data = validated_data.get('billing_address')
            billing_data.update(user=user)
            billing_address = Address.objects.create(**billing_data)
            subscription.billing_address = billing_address
            if validated_data.get('save_billing', False):
                user.profile.billing_address = billing_address

        same = validated_data.get('same', False)
        default_shipping = validated_data.get('default_shipping', False)
        if same:
            if billing_data == user.profile.billing_address:
                subscription.shipping_address = billing_data
            else:
                subscription.shipping_address = billing_address
        elif default_shipping and user.profile.shipping_address:
            shipping_data = user.profile.shipping_address
            subscription.shipping_address = shipping_data
        else:
            shipping_data = validated_data.get('shipping_address')
            shipping_data.update(user=user)
            shipping_address = Address.objects.create(**billing_data)
            subscription.shipping_address = shipping_address
            if validated_data.get('save_shipping', False):
                user.profile.shipping_address = shipping_address

        subscription.related_to_model = 'Kit'
        subscription.related_to_name = kit.name
        subscription.related_to_pk = kit.pk
        subscription.save()
        user.profile.subscriptions.add(subscription)
        user.save()
        return subscription