コード例 #1
0
        public void Create_PaySlip_Generates_Expected_Results_For_An_Employee()
        {
            //Arrange
            var employee = new Employee
            {
                FirstName    = "TestFirstName",
                LastName     = "TestLastName",
                AnnualSalary = 60050,
                SuperRate    = 9,
                PayPeriod    = "01 March - 31 March"
            };

            var roundingService      = new RoundingService();
            var taxCalculatorService = new TaxCalculatorService();
            var payslipService       = new PaySlipService(roundingService, taxCalculatorService);

            //Act
            var result = payslipService.CreatePaySlip(employee);

            //Assert
            Assert.AreEqual(employee.FullName, result.EmployeeName, "Employee Name incorrect");
            Assert.AreEqual(employee.PayPeriod, result.PayPeriod, "Pay Period incorrect");
            Assert.AreEqual(5004, result.GrossIncome, "Gross Income incorrect");
            Assert.AreEqual(922, result.IncomeTax, "Income Tax incorrect");
            Assert.AreEqual(4082, result.NetIncome, "Net Income incorrect");
            Assert.AreEqual(450, result.SuperAmount, "Super incorrect");
        }
コード例 #2
0
        public void Should_Return_IncomeTax_For_AnnualSalary_Is_More_Than_Limit()
        {
            // Arrange
            var taxDataRepository = Substitute.For <ITaxDataRepository>();

            var taxDetails = new List <TaxData>()
            {
                new TaxData
                {
                    MinLimit = 0,
                    MaxLimit = 1000,
                    TaxRate  = 0,
                    BaseTax  = 0
                },
                new TaxData()
                {
                    MinLimit = 1000,
                    MaxLimit = -1,
                    TaxRate  = 0.10m,
                    BaseTax  = 200,
                }
            };

            taxDataRepository.LoadTaxData().Returns(taxDetails);

            // Act
            var taxCalculatorService = new TaxCalculatorService(taxDataRepository);
            var incomeTax            = taxCalculatorService.CalculateIncomeTax(6000);

            // Assert
            Assert.Equal(58, incomeTax);
        }
コード例 #3
0
        public void Should_Return_IncomeTax_For_AnnualSalary_Is_Negative()
        {
            // Arrange
            var taxDataRepository = Substitute.For <ITaxDataRepository>();

            var taxDetails = new List <TaxData>()
            {
                new TaxData
                {
                    MinLimit = 0,
                    MaxLimit = 1000,
                    TaxRate  = 0,
                    BaseTax  = 0
                },
                new TaxData()
                {
                    MinLimit = 1000,
                    MaxLimit = 5000,
                    TaxRate  = 0.10m,
                    BaseTax  = 200,
                }
            };

            taxDataRepository.LoadTaxData().Returns(taxDetails);

            // Act
            var taxCalculatorService = new TaxCalculatorService(taxDataRepository);
            var exception            = Assert.Throws <Exception>(() => taxCalculatorService.CalculateIncomeTax(-4000));

            Assert.Equal("Invalid annual salary", exception.Message);
        }
コード例 #4
0
        public void ReturnCorrectNetSalayry_WhenGrossSalaryIsAbove3000()
        {
            var grossSalaryMocked = 3001m;
            var countryMocked     = "Country";
            var expectedNetSalary = 3001m - 300m - 200.1m;

            var rateMocked = new Rate()
            {
                Country            = countryMocked,
                IncomeTaxRate      = 0.1m,
                SSCRate            = 0.15m,
                IncomeTaxThreshold = 1000m,
                SscThreshold       = 3000m
            };

            var ratesProvider = new Mock <IRatesProvider>();

            ratesProvider.Setup(x => x.GetRates(countryMocked)).Returns(rateMocked);

            var sut = new TaxCalculatorService(ratesProvider.Object);

            var netSalaryMocked = sut.CalculateNetSalary(grossSalaryMocked, countryMocked);

            Assert.AreEqual(expectedNetSalary, netSalaryMocked.NetSalary);
        }
        public void ComputeTaxForItemWiseTaxCalulator()
        {
            IDiscountService     discountService = new DiscountService();
            ITaxService          taxService      = new TaxCalculatorService("IO");
            IShoppingCartService scs             = new ShoppingCartService(discountService);
            var cs           = new CustomerService(scs);
            var custId       = cs.CreateCustomer("Paniraj N");
            var shoppingCart = cs.CreateShoppingCart(custId);


            var ps = new ProductService();
            var pA = ps.CreateProduct("A", "product A", 50, ProductCategory.Basic);
            var pB = ps.CreateProduct("B", "product B", 30, ProductCategory.Luxury);
            var pC = ps.CreateProduct("C", "product C", 20, ProductCategory.Luxury);

            var discountA             = discountService.CreateDiscount("Discount on A", "Discount if Purchase 3 As");
            var pAs                   = new KeyValuePair <Product, int>(pA, 3);
            var discountCombinationAs = new List <KeyValuePair <Product, int> >();

            discountCombinationAs.Add(pAs);
            var dci = new DiscountCombinationItems(discountCombinationAs,
                                                   20, 0);

            discountService.CreateDiscountCombinationItems(discountA.Id, dci);

            scs.AddItem(shoppingCart, pA, 1);
            scs.AddItem(shoppingCart, pB, 1);
            scs.AddItem(shoppingCart, pC, 1);

            shoppingCart = scs.ComputeBill(shoppingCart);
            taxService.CalculateTax(shoppingCart);
            //There is no discount applicable for this card, cart value is 100, Basic Tax is 10.
            Assert.AreEqual(15, shoppingCart.TaxAmount);
        }
コード例 #6
0
ファイル: Program.cs プロジェクト: svet0slav/J-2020-Demos
        static void Main(string[] args)
        {
            SConsole.WriteLine("Hello, enter gross amount:");
            var amountText = SConsole.ReadLine();

            if (decimal.TryParse(amountText, out decimal amount))
            {
                var service = new TaxCalculatorService(new TaxCalculatorDefinitionPoCService());
                var salary  = new Salary()
                {
                    GrossAmount = amount
                };
                service.CalculateTax(salary);
                SConsole.WriteLine($"Gross amount : {salary.GrossAmount:N2}");
                SConsole.WriteLine($"The tax is {salary.TaxAmount:N2}");
                if (salary.TaxExplanation != null)
                {
                    foreach (var item in salary.TaxExplanation)
                    {
                        SConsole.WriteLine($"includes {item.Key.Name} with tax {item.Value:N2}");
                    }
                }
                SConsole.WriteLine($"Net amount : {salary.NetAmount}");
            }

            SConsole.WriteLine("Finished. Press ENTER to exit!");
            SConsole.ReadLine();
        }
コード例 #7
0
        public void Setup()
        {
            var incomeTaxes = new List <IncomeTax>();

            incomeTaxes.Add(new IncomeTax()
            {
                MinRange = 0, MaxRange = 18200, MinPayableTax = 0, AdditionalTax = 0
            });
            incomeTaxes.Add(new IncomeTax()
            {
                MinRange = 18201, MaxRange = 37000, MinPayableTax = 0, AdditionalTax = 0.19m
            });
            incomeTaxes.Add(new IncomeTax()
            {
                MinRange = 37001, MaxRange = 80000, MinPayableTax = 3572, AdditionalTax = 0.325m
            });
            incomeTaxes.Add(new IncomeTax()
            {
                MinRange = 80001, MaxRange = 180000, MinPayableTax = 17547, AdditionalTax = 0.37m
            });
            incomeTaxes.Add(new IncomeTax()
            {
                MinRange = 180001, MaxRange = int.MaxValue, MinPayableTax = 54547, AdditionalTax = 0.45m
            });

            _taxRepositoryMock = new Mock <ITaxRepository>();
            _taxRepositoryMock.Setup(x => x.GetIncomeTaxCriteria()).Returns(incomeTaxes);
            _taxCalculatorService = new TaxCalculatorService(_taxRepositoryMock.Object);
        }
コード例 #8
0
        public void TestCalculateOrderTaxes()
        {
            var service    = new TaxCalculatorService(new TaxJarClient());
            var orderTaxes = service.CalculateOrderTaxes("US", "CA", "92093", 100f, 10f);

            Assert.IsNotNull(orderTaxes);
            Assert.AreEqual(0f, orderTaxes);
        }
コード例 #9
0
        public void TestGetLocationTaxRate()
        {
            var service = new TaxCalculatorService(new TaxJarClient());
            var taxRate = service.GetLocationTaxRate("33441");

            Assert.IsNotNull(taxRate);
            Assert.AreEqual(0.07f, taxRate);
        }
コード例 #10
0
        public void Calculate_ShouldThrowException_WhenCountryCodeIsInvalid()
        {
            // Arrange
            var sut = new TaxCalculatorService();

            // Act

            // Assert
            Assert.Throws <Exception>(() => sut.Calculate("AA", 25000m));
        }
コード例 #11
0
        public void When_CalculateTax_Called_With_Given_Paratemeters_Returns_Correct_Tax_Results(int salary, decimal expectedTax)
        {
            //Arrange
            var taxCalculatorService = new TaxCalculatorService();

            //Act
            var result = taxCalculatorService.CalculateTax(salary);

            //Assert
            Assert.AreEqual(decimal.Round(expectedTax, 2, MidpointRounding.AwayFromZero), decimal.Round(result, 2, MidpointRounding.AwayFromZero));
        }
コード例 #12
0
        public void Calculate_ShouldReturnGreaterThanZero_WhenAnnualIncomeGreaterThanZero()
        {
            // Arrange
            var sut = new TaxCalculatorService();

            // Act
            var result = sut.Calculate("TR", 32000m);

            // Assert
            Assert.True(result > decimal.Zero);
        }
コード例 #13
0
        public void Calculate_ShouldNotReturnNull_WhenAnnualIncomeGreaterThanZero()
        {
            // Arrange
            var sut = new TaxCalculatorService();

            // Act
            var result = sut.Calculate("TR", 32000m);

            // Assert
            Assert.NotEqual(Decimal.Zero, result);
        }
コード例 #14
0
        public void Calculate_ShouldReturnZero_WhenAnnualIncomeZero()
        {
            // Arrange
            var sut = new TaxCalculatorService();

            // Act
            var result = sut.Calculate("BE", decimal.Zero);

            //Assert
            Assert.Equal(decimal.Zero, result);
        }
コード例 #15
0
        public void Should_Throw_Exception_When_TaxDetail_Is_Not_Defined()
        {
            var taxDataRepository = Substitute.For <ITaxDataRepository>();

            taxDataRepository.LoadTaxData().Returns(new List <TaxData>());

            var taxCalculatorService = new TaxCalculatorService(taxDataRepository);

            var exception = Assert.Throws <Exception>(() => taxCalculatorService.CalculateIncomeTax(4000));

            Assert.Equal("Tax detail is not defined", exception.Message);
        }
コード例 #16
0
        public TaxCalculatorServiceApiTests()
        {
            var config = new ConfigurationBuilder()
                         .SetBasePath(Directory.GetCurrentDirectory())
                         .AddJsonFile("appsettings.json")
                         .Build();

            var mockLogger = new Mock <ILogger <TaxCalculatorService> >();
            ILogger <TaxCalculatorService> logger = mockLogger.Object;

            var TaxJar = new TaxCalculator(config["TaxCalculators:TaxJar:BaseUrl"], config["TaxCalculators:TaxJar:Authorization"]);

            _taxService = new TaxCalculatorService(TaxJar, logger);
        }
        public void ComputeTaxForPreDiscountTaxCalulator()
        {
            IDiscountService discountService = new DiscountService();
            //FL uses PreDiscount Tax Method
            ITaxService          taxService = new TaxCalculatorService("FL");
            IShoppingCartService scs        = new ShoppingCartService(discountService);
            var cs           = new CustomerService(scs);
            var custId       = cs.CreateCustomer("Sachin Sharma");
            var shoppingCart = cs.CreateShoppingCart(custId);


            var ps = new ProductService();
            var pA = ps.CreateProduct("A", "product A", 50, ProductCategory.Basic);
            var pB = ps.CreateProduct("B", "product B", 30, ProductCategory.Luxury);
            var pC = ps.CreateProduct("C", "product C", 20, ProductCategory.Luxury);
            var pD = ps.CreateProduct("D", "product D", 15, ProductCategory.Premium);

            var discountA             = discountService.CreateDiscount("Discount on A", "Discount if Purchase 3 As");
            var pAs                   = new KeyValuePair <Product, int>(pA, 3);
            var discountCombinationAs = new List <KeyValuePair <Product, int> >();

            discountCombinationAs.Add(pAs);
            var dci = new DiscountCombinationItems(discountCombinationAs,
                                                   20, 0);

            discountService.CreateDiscountCombinationItems(discountA.Id, dci);

            //Discount B
            var discountB             = discountService.CreateDiscount("Discount on B", "Discount if Purchase 2 Bs");
            var pBs                   = new KeyValuePair <Product, int>(pB, 2);
            var discountCombinationBs = new List <KeyValuePair <Product, int> >();

            discountCombinationBs.Add(pBs);
            var dc2 = new DiscountCombinationItems(discountCombinationBs,
                                                   15, 0);

            discountService.CreateDiscountCombinationItems(discountB.Id, dc2);

            scs.AddItem(shoppingCart, pA, 5);
            scs.AddItem(shoppingCart, pB, 5);
            scs.AddItem(shoppingCart, pC, 1);

            shoppingCart = scs.ComputeBill(shoppingCart);

            taxService.CalculateTax(shoppingCart);
            //There is $50 discount applicable for this cart, cart value is 420 (370 after discount ).
            //Pre Discount Tax is 42.
            Assert.AreEqual(42, shoppingCart.TaxAmount);
        }
コード例 #18
0
        public void Test_Check_If_Super_Pay_Is_Calculated_Properly_From_Gross_Pay()
        {
            /// Arrange
            var expectedEmployeeTax = DataHelper.GetTaxForEmployeeB();
            var expectedEmployee    = DataHelper.GetEmployeeB();

            this._incomeTaxCalculationRepository.Setup(p => p.CalculateIncomeTax(expectedEmployee.AnnualSalary, Constants.TaxYear)).Returns(expectedEmployeeTax.IncomeTax);

            /// Act
            var taxCalculatorService = new TaxCalculatorService(this._incomeTaxCalculationRepository.Object);
            var actual = taxCalculatorService.Calculate(expectedEmployee, Constants.TaxYear);

            /// Assert
            Assert.AreEqual(expectedEmployeeTax.SuperAmount, actual.SuperAmount);
        }
コード例 #19
0
        public void Test_Check_If_Pay_Period_String_Is_Formed_Properly()
        {
            /// Arrange
            var expectedEmployeeTax = DataHelper.GetTaxForEmployeeB();
            var expectedEmployee    = DataHelper.GetEmployeeB();

            expectedEmployee.PaymentStartDate = new System.DateTime(2012, 12, 15);
            this._incomeTaxCalculationRepository.Setup(p => p.CalculateIncomeTax(expectedEmployee.AnnualSalary, Constants.TaxYear)).Returns(expectedEmployeeTax.IncomeTax);

            /// Act
            var taxCalculatorService = new TaxCalculatorService(this._incomeTaxCalculationRepository.Object);
            var actual = taxCalculatorService.Calculate(expectedEmployee, Constants.TaxYear);

            /// Assert
            Assert.AreEqual("15 December - 31 December", actual.PayPeriod);
        }
コード例 #20
0
        public void Test_Check_If_Employee_Name_Is_Formed_Properly()
        {
            /// Arrange
            var expectedEmployeeTax = DataHelper.GetTaxForEmployeeB();
            var expectedEmployee    = DataHelper.GetEmployeeB();

            expectedEmployee.FirstName = "Valid";
            expectedEmployee.LastName  = "User";
            this._incomeTaxCalculationRepository.Setup(p => p.CalculateIncomeTax(expectedEmployee.AnnualSalary, Constants.TaxYear)).Returns(expectedEmployeeTax.IncomeTax);

            /// Act
            var taxCalculatorService = new TaxCalculatorService(this._incomeTaxCalculationRepository.Object);
            var actual = taxCalculatorService.Calculate(expectedEmployee, Constants.TaxYear);

            /// Assert
            Assert.AreEqual(string.Format("{0} {1}", "Valid", "User"), actual.EmployeeName);
        }
コード例 #21
0
        public async Task <ActionResult <TaxRateOrder> > GetTaxRateByOrderId(string id)
        {
            try
            {
                var taxCalculator = new TaxCalculator(_config["TaxCalculators:TaxJar:BaseUrl"], _config["TaxCalculators:TaxJar:Authorization"]);
                //var userId = Request.Headers["UserId"];
                var taxService   = new TaxCalculatorService(taxCalculator, _logger);
                var taxRateOrder = await taxService.GetRateByOrder(id);

                return(Ok(taxRateOrder));
            }
            catch (Exception ex)
            {
                _logger.LogInformation(ex.Message);
                return(BadRequest());
            }
        }
コード例 #22
0
 public TaxCalculatorServiceUnitTests()
 {
     _service     = new TaxCalculatorService(_appSettingsMock.Object);
     _appSettings = new AppSettings
     {
         TaxFreeThreshold = new TaxThreshold {
             Income = 18200, FixedRate = 0, VariableRate = 0.19M
         },
         SecondTaxThreshold = new TaxThreshold {
             Income = 37000, FixedRate = 3572, VariableRate = 0.325M
         },
         ThirdTaxThreshold = new TaxThreshold {
             Income = 80000, FixedRate = 17547, VariableRate = 0.37M
         },
         FourthTaxThreshold = new TaxThreshold {
             Income = 180000, FixedRate = 54547, VariableRate = 0.45M
         }
     };
 }
コード例 #23
0
            public void GeneratePayslips_TwoEmployeesWithUngenerateddPayslips_PayslipsGenerated()
            {
                EmployeeRepository   Repository = new EmployeeRepository(new Context(new FileContext(this.Fixture.InputFilePath, this.Fixture.OutputFilePath, new FileSystem(), Log.Logger)));
                TaxCalculatorService Manager    = new TaxCalculatorService(Repository, this.Fixture.Calculator, this.Fixture.Generator, Log.Logger);
                PaySlip p1  = new PaySlip(new DateTime(DateTime.Today.Year, 3, 1), new DateTime(DateTime.Today.Year, 3, 31), 5004, 922, 4082, 450),
                        p2  = new PaySlip(new DateTime(DateTime.Today.Year, 3, 1), new DateTime(DateTime.Today.Year, 3, 31), 10000, 2669, 7331, 1000);
                Employee e1 = new Employee("David", "Rudd", 60050, 0.09, new List <PaySlip> {
                    p1
                }),
                         e2 = new Employee("Ryan", "Chen", 120000, 0.1, new List <PaySlip> {
                    p2
                });
                IEnumerable <Employee> expected = new List <Employee>()
                {
                    e1, e2
                };

                Assert.NotEqual(expected, Repository.GetAll());
                Manager.GeneratePayslips();
                Assert.Equal(expected, Repository.GetAll());
            }
コード例 #24
0
            public void StoreData_AddNewEmployee_FileUpdatedSuccessfuly()
            {
                EmployeeRepository   Repository = new EmployeeRepository(new Context(new FileContext(this.Fixture.InputFilePath, this.Fixture.OutputFilePath, new FileSystem(), Log.Logger)));
                TaxCalculatorService Manager    = new TaxCalculatorService(Repository, this.Fixture.Calculator, this.Fixture.Generator, Log.Logger);
                PaySlip  p = new PaySlip(new DateTime(DateTime.Today.Year, 3, 1), new DateTime(DateTime.Today.Year, 3, 31));
                Employee e = new Employee("Artur", "Chaves", 90050, 0.11, new List <PaySlip> {
                    p
                });
                var outputFileContent = "David Rudd,01 March - 31 March,5004,922,4082,450\nRyan Chen,01 March - 31 March,10000,2669,7331,1000";
                var expected          = new string[] { "David Rudd,01 March - 31 March,5004,922,4082,450",
                                                       "Ryan Chen,01 March - 31 March,10000,2669,7331,1000",
                                                       "Artur Chaves,01 March - 31 March,7504,1746,5758,825" };

                Repository.GetAll();
                Repository.Add(e);
                Manager.GeneratePayslips();
                Repository.Persist();

                int n = 0;

                using (var reader = new StreamReader((new FileSystem()).File.OpenRead(this.Fixture.OutputFilePath)))
                {
                    while (!reader.EndOfStream)
                    {
                        var line = reader.ReadLine();
                        Assert.Equal(expected[n++], line);
                    }
                }

                // Restore output file content
                using (var writer = new StreamWriter(this.Fixture.OutputFilePath))
                {
                    writer.Write(outputFileContent);
                    writer.Flush();
                }

                // Restore cache to its original state
                Repository.Remove(e);
            }
コード例 #25
0
            public void GeneratePayslips_TwoMockedEmployeesWithUngenerateddPayslips_PayslipsGenerated()
            {
                PaySlip p1  = new PaySlip(new DateTime(DateTime.Today.Year, 3, 1), new DateTime(DateTime.Today.Year, 3, 31)),
                        p2  = new PaySlip(new DateTime(DateTime.Today.Year, 3, 1), new DateTime(DateTime.Today.Year, 3, 31));
                Employee e1 = new Employee("David", "Rudd", 60050, 0.09, new List <PaySlip> {
                    p1
                }),
                         e2 = new Employee("Ryan", "Chen", 120000, 0.1, new List <PaySlip> {
                    p2
                });
                Mock <IContext> context = new Mock <IContext>();

                context.Setup(x => x.Employees).Returns(new List <Employee>()
                {
                    e1, e2
                });
                EmployeeRepository repository = new EmployeeRepository(context.Object);

                TaxCalculatorService Manager = new TaxCalculatorService(repository, this.Fixture.Calculator, this.Fixture.Generator, Log.Logger);
                PaySlip pp1  = new PaySlip(new DateTime(DateTime.Today.Year, 3, 1), new DateTime(DateTime.Today.Year, 3, 31), 5004, 922, 4082, 450),
                        pp2  = new PaySlip(new DateTime(DateTime.Today.Year, 3, 1), new DateTime(DateTime.Today.Year, 3, 31), 10000, 2669, 7331, 1000);
                Employee ee1 = new Employee("David", "Rudd", 60050, 0.09, new List <PaySlip> {
                    pp1
                }),
                         ee2 = new Employee("Ryan", "Chen", 120000, 0.1, new List <PaySlip> {
                    pp2
                });
                IEnumerable <Employee> expected = new List <Employee>()
                {
                    ee1, ee2
                };

                Assert.NotEqual(expected, repository.GetAll());
                Manager.GeneratePayslips();
                Assert.Equal(expected, repository.GetAll());
            }