Exemplo n.º 1
0
def test_order_creator(rf, admin_user):
    source = seed_source(admin_user)
    source.add_line(
        type=OrderLineType.PRODUCT,
        product=get_default_product(),
        supplier=get_default_supplier(),
        quantity=1,
        base_unit_price=source.create_price(10),
    )
    source.add_line(
        type=OrderLineType.OTHER,
        quantity=1,
        base_unit_price=source.create_price(10),
        require_verification=True,
    )

    creator = OrderCreator()
    order = creator.create_order(source)
    assert get_data_dict(source.billing_address) == get_data_dict(
        order.billing_address)
    assert get_data_dict(source.shipping_address) == get_data_dict(
        order.shipping_address)
    customer = source.customer
    assert customer == order.customer
    assert customer.groups.count() == 1
    assert customer.groups.first() == order.customer_groups.first()
    assert customer.tax_group is not None
    assert customer.tax_group == order.tax_group

    assert source.payment_method == order.payment_method
    assert source.shipping_method == order.shipping_method
    assert order.pk
Exemplo n.º 2
0
def test_new_mutable_address():
    address = get_address()
    new_mutable = address.to_mutable()

    # New address should be unsaved
    assert new_mutable.pk == None
    assert isinstance(new_mutable, MutableAddress)
    assert get_data_dict(address).items() == get_data_dict(new_mutable).items()
Exemplo n.º 3
0
def test_immutable_address():
    address = get_address()
    new_immutable = address.to_immutable()

    # New address should be saved
    assert new_immutable.pk != None
    assert isinstance(new_immutable, ImmutableAddress)
    assert get_data_dict(address).items() == get_data_dict(
        new_immutable).items()

    # Taking immutable for same address should return same object
    assert new_immutable == address.to_immutable()

    # Taking immutable from new_immutable should return same item
    assert new_immutable == new_immutable.to_immutable()
Exemplo n.º 4
0
def test_order_customer_groups(rf, admin_user):
    customer = create_random_person()
    default_group = get_default_customer_group()
    default_group.members.add(customer)
    source = seed_source(admin_user)
    source.customer = customer

    source.add_line(
        type=OrderLineType.PRODUCT,
        product=get_default_product(),
        supplier=get_default_supplier(),
        quantity=1,
        base_unit_price=source.create_price(10),
    )
    source.add_line(
        type=OrderLineType.OTHER,
        quantity=1,
        base_unit_price=source.create_price(10),
        require_verification=True,
    )

    creator = OrderCreator()
    order = creator.create_order(source)
    assert get_data_dict(source.billing_address) == get_data_dict(
        order.billing_address)
    assert get_data_dict(source.shipping_address) == get_data_dict(
        order.shipping_address)
    customer = source.customer
    assert customer == order.customer
    assert customer.groups.count() == 2
    assert order.customer_groups.filter(id=default_group.id).exists()
    with pytest.raises(ProtectedError):
        default_group.delete()

    assert customer.tax_group is not None
    assert customer.tax_group == order.tax_group
    with pytest.raises(ProtectedError):
        customer.tax_group.delete()

    new_group = create_random_contact_group()
    new_group.members.add(customer)

    order.phone = "911"
    order.save()
    assert order.customer_groups.filter(id=default_group.id).exists()
    assert not order.customer_groups.filter(id=new_group.id).exists()
Exemplo n.º 5
0
    def to_mutable(self):
        """
        Get a new MutableAddress from self.

        :rtype: MutableAddress
        :return: Fresh unsaved MutableAddress with same data as self.
        """
        data = get_data_dict(self)
        return MutableAddress.from_data(data)
Exemplo n.º 6
0
    def to_immutable(self):
        """
        Get or create saved ImmutableAddress from self.

        :rtype: ImmutableAddress
        :return: Saved ImmutableAddress with same data as self.
        """
        data = get_data_dict(self)
        return ImmutableAddress.from_data(data)
Exemplo n.º 7
0
 def get_form(self, form_class=None):
     order = self.get_object()
     form_group = FormGroup(**self.get_form_kwargs())
     address_form_class = cached_load("WSHOP_ADDRESS_MODEL_FORM")
     form_group.add_form_def("billing_address",
                             address_form_class,
                             kwargs={
                                 "initial":
                                 get_data_dict(order.billing_address)
                                 if order.billing_address else {}
                             })
     form_group.add_form_def("shipping_address",
                             address_form_class,
                             kwargs={
                                 "initial":
                                 get_data_dict(order.shipping_address)
                                 if order.shipping_address else {}
                             })
     return form_group
Exemplo n.º 8
0
    def from_data(cls, data):
        """
        Get or create immutable address with given data.

        :param data: data for address
        :type data: dict[str,str]
        :return: Saved immutable address
        :rtype: ImmutableAddress
        """
        # Populate all known address fields even if not originally in `data`
        data_with_all_fields = get_data_dict(cls(**data))
        address = cls.objects.filter(**data_with_all_fields).first()
        return address if address else cls.objects.create(
            **data_with_all_fields)
Exemplo n.º 9
0
def test_basic_address():
    address = get_address()
    address.full_clean()
    string_repr = str(address)
    for field, value in get_data_dict(address).items():
        if field == "country":  # We can't test this right now, it's formatted in the repr
            continue
        if not value:
            continue
        assert value in string_repr, "Field %s is not represented in %r" % (
            field, string_repr)

    assert address.is_european_union, "Dog Fort, UK is in the EU"
    assert list(address.split_name) == ["Dog",
                                        "Hello"], "Names split correctly"
    assert address.first_name == "Dog", "Names split correctly"
    assert address.last_name == "Hello", "Names split correctly"
    assert address.full_name == "Sir Dog Hello , Esq.", "Names join correctly"