示例#1
0
def test_recorder():
    try:
        from pyavatax.models import AvaTaxRecord
    except ImportError:  # no django
        pytest.mark.xfail('This can only be run inside a django environment')
        return
    random_doc_code = uuid.uuid4().hex  # you can't post/cancel the same doc code over and over
    api = get_api()
    doc = Document.new_sales_invoice(DocCode=random_doc_code, DocDate=datetime.date.today(), CustomerCode='*****@*****.**')
    to_address = Address(Line1="435 Ericksen Avenue Northeast", Line2="#250", PostalCode="98110")
    from_address = Address(Line1="100 Ravine Lane NE", Line2="#220", PostalCode="98110")
    doc.add_from_address(from_address)
    doc.add_to_address(to_address)
    line = Line(Amount=10.00)
    doc.add_line(line)
    setattr(doc, '_testing_ignore_validate', True)  # passthrough I put in to allow this test, never actually use this
    orig_doc_type = doc.DocType
    doc.DocType = 'DoesntExist'  # forcing error
    tax = api.post_tax(doc)
    assert tax.is_success == False
    assert 1 == AvaTaxRecord.failures.filter(doc_code=random_doc_code).count()
    assert 0 == AvaTaxRecord.successes.filter(doc_code=random_doc_code).count()
    doc.DocType = orig_doc_type
    tax = api.post_tax(doc, commit=True)
    assert tax.is_success == True
    assert 0 == AvaTaxRecord.failures.filter(doc_code=random_doc_code).count()
    assert 1 == AvaTaxRecord.successes.filter(doc_code=random_doc_code).count()
    tax = api.post_tax(doc, commit=True)
    assert tax.is_success == False
    assert 1 == AvaTaxRecord.failures.filter(doc_code=random_doc_code).count()
    assert 1 == AvaTaxRecord.successes.filter(doc_code=random_doc_code).count()
示例#2
0
def test_override_failure():
    try:
        from pyavatax.models import AvaTaxRecord
    except ImportError:  # no django
        pytest.mark.xfail('This can only be run inside a django environment')
        return
    random_doc_code = uuid.uuid4(
    ).hex  # you can't post/cancel the same doc code over and over
    api = get_api()
    doc = Document.new_sales_invoice(DocCode=random_doc_code,
                                     DocDate=datetime.date.today(),
                                     CustomerCode='*****@*****.**')
    to_address = Address(Line1="435 Ericksen Avenue Northeast",
                         Line2="#250",
                         PostalCode="98110")
    from_address = Address(Line1="100 Ravine Lane NE",
                           Line2="#220",
                           PostalCode="98110")
    doc.add_from_address(from_address)
    doc.add_to_address(to_address)
    line = Line(Amount=10.00)
    doc.add_line(line)
    doc.DocType = 'FooBar'  # forcing error
    tax_date = datetime.date.today() - datetime.timedelta(days=5)
    doc.add_override(
        TaxOverrideType=TaxOverride.OVERRIDE_DATE,
        TaxDate=tax_date,
        Reason="Tax Date change",
    )
    tax = api.post_tax(doc)
    assert tax.is_success == False
    assert 1 == AvaTaxRecord.failures.filter(doc_code=random_doc_code).count()
    assert 0 == AvaTaxRecord.successes.filter(doc_code=random_doc_code).count()
示例#3
0
def test_return():
    random_doc_code = uuid.uuid4().hex  # you can't post/cancel the same doc code over and over
    api = get_api()
    doc = Document.new_sales_invoice(DocCode=random_doc_code, DocDate=datetime.date.today(), CustomerCode='*****@*****.**')
    to_address = Address(Line1="435 Ericksen Avenue Northeast", Line2="#250", PostalCode="98110")
    from_address = Address(Line1="100 Ravine Lane NE", Line2="#220", PostalCode="98110")
    doc.add_from_address(from_address)
    doc.add_to_address(to_address)
    line = Line(Amount=10.00)
    doc.add_line(line)
    tax = api.post_tax(doc)
    assert tax.is_success
    assert tax.total_tax > 0
    # and return invoice
    doc = Document.new_return_invoice(DocCode=random_doc_code, DocDate=datetime.date.today(), CustomerCode='*****@*****.**')
    to_address = Address(Line1="435 Ericksen Avenue Northeast", Line2="#250", PostalCode="98110")
    from_address = Address(Line1="100 Ravine Lane NE", Line2="#220", PostalCode="98110")
    doc.add_from_address(from_address)
    doc.add_to_address(to_address)
    line = Line(Amount=-10.00)
    doc.add_line(line)
    tax_date = datetime.date.today() - datetime.timedelta(days=5)
    doc.add_override(TaxOverrideType=TaxOverride.OVERRIDE_DATE, TaxDate=tax_date, Reason="Tax Date change",)
    tax = api.post_tax(doc)
    assert tax.is_success
    assert float(tax.total_tax) < 0
示例#4
0
def test_posttax_commit_cancel():
    random_doc_code = uuid.uuid4(
    ).hex  # you can't post/cancel the same doc code over and over
    api = get_api()
    doc = Document.new_sales_order(DocCode=random_doc_code,
                                   DocDate=datetime.date.today(),
                                   CustomerCode='*****@*****.**')
    to_address = Address(Line1="435 Ericksen Avenue Northeast",
                         Line2="#250",
                         PostalCode="98110")
    from_address = Address(Line1="100 Ravine Lane NE",
                           Line2="#220",
                           PostalCode="98110")
    doc.add_from_address(from_address)
    doc.add_to_address(to_address)
    line = Line(Amount=10.00, Qty=2)
    doc.add_line(line)
    tax = api.post_tax(doc, commit=True)
    assert doc.Commit
    assert doc.DocType == Document.DOC_TYPE_SALE_INVOICE  # make sure the doc type changes with commit
    assert tax.is_success is True
    assert tax.TotalTax > 0
    assert tax.total_tax == tax.TotalTax
    assert len(tax.TaxAddresses) == 2
    assert len(tax.TaxLines) == 1
    assert len(tax.TaxLines[0].TaxDetails) > 0
    cancel = api.cancel_tax(doc)
    assert cancel.is_success is True
    assert cancel.CancelTaxResult
示例#5
0
def test_posttax_commit_exempt():
    random_doc_code = uuid.uuid4(
    ).hex  # you can't post/cancel the same doc code over and over
    api = get_api()
    # G = resale, which means exempt from tax
    doc = Document.new_sales_order(DocCode=random_doc_code,
                                   DocDate=datetime.date.today(),
                                   CustomerCode='*****@*****.**',
                                   CustomerUsageType='G')
    to_address = Address(Line1="435 Ericksen Avenue Northeast",
                         Line2="#250",
                         PostalCode="98110")
    from_address = Address(Line1="100 Ravine Lane NE",
                           Line2="#220",
                           PostalCode="98110")
    doc.add_from_address(from_address)
    doc.add_to_address(to_address)
    line = Line(Amount=10.00, Qty=2)
    doc.add_line(line)
    doc.add_line(TaxCode='FR', Amount='12.00')
    tax = api.post_tax(doc, commit=True)
    assert doc.Commit
    assert doc.DocType == Document.DOC_TYPE_SALE_INVOICE  # make sure the doc type changes with commit
    assert tax.is_success is True
    assert tax.TotalTax == '0'
    assert len(tax.TaxAddresses) == 4
    assert len(tax.TaxLines) == 2
示例#6
0
def test_validate_address():
    api = get_api()
    address = Address(Line1="435 Ericksen Avenue Northeast", Line2="#250", PostalCode="98110")
    validate = api.validate_address(address)
    assert validate.is_success is True
    assert validate.Address.Region == 'WA'

    # this is the right zip code
    address = Address(Line1="11 Endicott Ave", Line2="Apt 1", PostalCode="02144")
    validate = api.validate_address(address)
    assert validate.is_success is True
    assert validate.Address.Region == 'MA'
示例#7
0
def test_validation():
    with pytest.raises(AvalaraValidationException) as e:
        doc = Document(DocDate='foo')  # testing date
    assert e.value.code == AvalaraException.CODE_BAD_DATE
    with pytest.raises(AvalaraValidationException) as e:
        line = Line(Qty='foo')  # testing int
    assert e.value.code == AvalaraException.CODE_BAD_FLOAT
    with pytest.raises(AvalaraValidationException) as e:
        line = Line(Amount='foo')  # testing float
    assert e.value.code == AvalaraException.CODE_BAD_FLOAT
    with pytest.raises(AvalaraValidationException) as e:
        line = Line(
            ItemCode=
            'this string is longer than fifty characters and should be stopped'
        )  # testing length
    assert e.value.code == AvalaraException.CODE_TOO_LONG
    doc = Document.new_sales_order(DocCode='1001',
                                   DocDate=datetime.date.today(),
                                   CustomerCode='*****@*****.**')
    with pytest.raises(AvalaraValidationException) as e:
        doc.validate()
    assert e.value.code == AvalaraException.CODE_BAD_ADDRESS
    from_address = Address(Line1="435 Ericksen Avenue Northeast",
                           Line2="#250",
                           PostalCode="98110")
    to_address = Address(Line1="435 Ericksen Avenue Northeast",
                         Line2="#250",
                         PostalCode="98110")
    doc.add_from_address(from_address)
    doc.add_to_address(to_address)
    with pytest.raises(AvalaraValidationException) as e:
        doc.validate()
    assert e.value.code == AvalaraException.CODE_BAD_LINE
    with pytest.raises(AvalaraException) as e:
        doc.add_from_address(from_address)
    assert e.value.code == AvalaraException.CODE_HAS_FROM
    with pytest.raises(AvalaraException) as e:
        doc.add_to_address(to_address)
    assert e.value.code == AvalaraException.CODE_HAS_TO
    line = Line(Amount=10.00)
    doc.add_line(line)
    doc.validate()
    api = get_api()
    lat = 47.627935
    lng = -122.51702
    with pytest.raises(AvalaraTypeException) as e:
        api.get_tax(lat, lng, 'foo', None)
    assert e.value.code == AvalaraException.CODE_BAD_DOC
    with pytest.raises(AvalaraException) as e:
        api.get_tax(lat, lng, None, None)
    assert e.value.code == AvalaraException.CODE_BAD_ARGS
示例#8
0
def test_failure_validate_address():
    #this is the wrong zip code
    api = get_api()
    address = Address(Line1="11 Endicott Ave", Line2="Apt 1", PostalCode="02139")
    validate = api.validate_address(address)
    assert validate.is_success is False
    assert len(validate.Messages) == 1
    assert len(validate.error) == 1
示例#9
0
 def validate_address(self, address):
     """Performs a HTTP GET to address/validate/"""
     if isinstance(address, dict):
         address = Address.from_data(address)
     elif not isinstance(address, Address):
         raise AvalaraTypeException(AvalaraException.CODE_BAD_ADDRESS, 'Please pass an address or a dictionary to create an Address')
     stem = '/'.join([self.VERSION, 'address', 'validate'])
     resp = self._get(stem, address.todict())
     self.logger.info('"GET", %s%s with: %s' % (self.url, stem, address.todict()))
     return ValidateAddressResponse(resp)
示例#10
0
文件: api.py 项目: lionheart/pyavatax
 def validate_address(self, address):
     """Performs a HTTP GET to address/validate/"""
     if isinstance(address, dict):
         address = Address.from_data(address)
     elif not isinstance(address, Address):
         raise AvalaraTypeException(
             AvalaraException.CODE_BAD_ADDRESS,
             'Please pass an address or a dictionary to create an Address')
     stem = '/'.join([self.VERSION, 'address', 'validate'])
     resp = self._get(stem, address.todict())
     self.logger.info('"GET", %s%s' % (self.url, stem))
     return ValidateAddressResponse(resp)