Commits

Bertrand Le Roy  committed 56a1a7e

More tax tests, fixed tax token.

  • Participants
  • Parent commits 0ef7198

Comments (0)

Files changed (4)

File Models/ShoppingCart.cs

             return (
                 from tax in taxes
                 let name = tax.Name
-                let amount = tax.ComputeTax(GetProducts(), Subtotal(), shippingPrice, Country, ZipCode) where amount > 0
+                let amount = tax.ComputeTax(GetProducts(), Subtotal(), shippingPrice, Country, ZipCode)
+                where amount > 0
                 select new TaxAmount {Name = name, Amount = amount}
                 ).FirstOrDefault();
         }
                 if (ShippingOption == null) return Subtotal();
                 return Subtotal() + ShippingOption.Price;
             }
-            if (ShippingOption == null) return Subtotal() + Taxes().Amount;
-            return Subtotal() + Taxes().Amount + ShippingOption.Price;
+            if (ShippingOption == null) return Subtotal() + taxes.Amount;
+            return Subtotal() + taxes.Amount + ShippingOption.Price;
         }
 
         public double ItemCount() {

File Nwazet.Commerce.Tests/TaxTests.cs

             Assert.That(cart.Taxes().Amount, Is.EqualTo(subtotal * anyCountryTax.Rate));
         }
 
+        [Test]
+        public void NoApplicableTaxesYieldsNoTax() {
+            var frenchTax = GetFrenchTax();
+            var britishTax = GetBritishTax();
+            var washingtonTax = GetWashingtonTax();
+            var oregonTax = GetOregonTax();
+            var taxProvider = new FakeTaxProvider(new[] { washingtonTax, britishTax, frenchTax, oregonTax });
+            var cart = ShoppingCartHelpers.PrepareCart(null, new[] { taxProvider });
+
+            cart.Country = "Kazakhstan";
+            Assert.That(cart.Taxes(), Is.Null);
+        }
+
+        [Test]
+        public void NoNegativeTax()
+        {
+            var frenchTax = GetFrenchTax();
+            frenchTax.Rate = -0.5;
+            var taxProvider = new FakeTaxProvider(new[] { frenchTax });
+            var cart = ShoppingCartHelpers.PrepareCart(null, new[] { taxProvider });
+
+            cart.Country = frenchTax.Country;
+            Assert.That(cart.Taxes(), Is.Null);
+        }
+
         private static StateOrCountryTaxPart GetAnyStateTax()
         {
             var anyStateTax = new StateOrCountryTaxPart();

File Services/StateOrCountryTaxProvider.cs

             return _contentManager
                 .Query<StateOrCountryTaxPart, StateOrCountryTaxPartRecord>()
                 .ForVersion(VersionOptions.Published)
-                .OrderByDescending(t => t.Priority)
                 .List();
         }
     }

File Tokens/CartTokens.cs

                 .Token("Shipping", c => (c ?? _shoppingCart).ShippingOption.ToString())
                 .Chain("Shipping", "ShippingOption", c => (c ?? _shoppingCart).ShippingOption)
                 .Token("Subtotal", c => (c ?? _shoppingCart).Subtotal().ToString("C"))
-                .Token("Taxes", c => (c ?? _shoppingCart).Taxes().Amount.ToString("C"))
-                .Chain("Taxes", "TaxAmount", c => (c ?? _shoppingCart).Taxes())
+                .Token("Taxes", c => {
+                    var taxes = (c ?? _shoppingCart).Taxes();
+                    return taxes == null ? 0.ToString("C") : taxes.Amount.ToString("C");
+                })
+                .Chain("Taxes", "TaxAmount", c => (c ?? _shoppingCart).Taxes() ?? new TaxAmount {Amount = 0, Name = ""})
                 .Token("Total", c => (c ?? _shoppingCart).Total().ToString("C"))
                 .Token("Count", c => (c ?? _shoppingCart).ItemCount());