public void ModifyInvoice_ReplacesInvoiceLines()
        {
            // arrange
            var context = new CBAContext(dboptions);
            var seeder  = new CBASeeder(context, cryptography);

            seeder.Seed();

            var service         = new InvoiceService(context, options, mapper, pdf, logger);
            var originalInvoice = context.Invoice.Include("InvoiceLine")
                                  .SingleOrDefault(t => t.InvoiceNumber == "ABNZ000420");

            var modifiedInvoice = new InvoiceForUpdateDto()
            {
                // all fields should be the same except InvoiceLine
                ClientName          = originalInvoice.ClientName,
                ClientContact       = originalInvoice.ClientContact,
                ClientContactPerson = originalInvoice.ClientContactPerson,
                DateDue             = originalInvoice.DateDue,
                Email = originalInvoice.Email,
                PurchaseOrderNumber = originalInvoice.PurchaseOrderNumber,

                InvoiceLine = new List <InvoiceLineDto>()
                {
                    new InvoiceLineDto()
                    {
                        Description = "New Dinner",
                        Amount      = 50
                    },
                    new InvoiceLineDto()
                    {
                        Description = "New Cookie",
                        Amount      = 10
                    }
                }
            };

            var expectedLineCount = context.InvoiceLine.Count()
                                    - originalInvoice.InvoiceLine.Count()
                                    + modifiedInvoice.InvoiceLine.Count();

            // act
            service.ModifyInvoice("ABNZ000420", modifiedInvoice);
            var lineCount = context.InvoiceLine.Count();

            // assert
            Assert.AreEqual(expectedLineCount, lineCount);
        }
        public void DeleteInvoice_DeletesDraftInvoice()
        {
            // arrange
            var context = new CBAContext(dboptions);
            var seeder  = new CBASeeder(context, cryptography);

            seeder.Seed();

            var service = new InvoiceService(context, options, mapper, pdf, logger);

            // act
            bool result = service.DeleteInvoice("ABNZ000420");

            // assert
            Assert.AreEqual(true, result);
        }
        public void ModifyInvoice_UpdatesInvoiceLines_WhenOnlyInvoiceLinesModified()
        {
            // arrange
            var context = new CBAContext(dboptions);
            var seeder  = new CBASeeder(context, cryptography);

            seeder.Seed();

            var     service         = new InvoiceService(context, options, mapper, pdf, logger);
            Invoice invoiceToUpdate = context.Invoice.Include("InvoiceLine")
                                      .SingleOrDefault(t => t.InvoiceNumber == "ABNZ000420");

            InvoiceForUpdateDto invoice = new InvoiceForUpdateDto()
            {
                // all fields should be the same except InvoiceLine
                ClientName          = invoiceToUpdate.ClientName,
                ClientContact       = invoiceToUpdate.ClientContact,
                ClientContactPerson = invoiceToUpdate.ClientContactPerson,
                DateDue             = invoiceToUpdate.DateDue,
                Email = invoiceToUpdate.Email,
                PurchaseOrderNumber = invoiceToUpdate.PurchaseOrderNumber
            };

            invoice.InvoiceLine = new List <InvoiceLineDto>()
            {
                new InvoiceLineDto()
                {
                    Description = "New Dinner",
                    Amount      = 50
                },
                new InvoiceLineDto()
                {
                    Description = "New Cookie",
                    Amount      = 10
                }
            };

            // act
            service.ModifyInvoice("ABNZ000420", invoice);

            // assert
            var cleancontext = new CBAContext(dboptions);

            Assert.AreEqual(2, cleancontext.Invoice.Include("InvoiceLine")
                            .SingleOrDefault(t => t.InvoiceNumber == "ABNZ000420").InvoiceLine.Count());
        }
        public void DeleteInvoice_RejectsNonDraftInvoice()
        {
            // arrange
            var context = new CBAContext(dboptions);
            var seeder  = new CBASeeder(context, cryptography);

            seeder.Seed();

            var service = new InvoiceService(context, options, mapper, pdf, logger);

            // act
            bool result = false;

            try {
                service.DeleteInvoice("ABNZ000421");
            }
            catch (ArgumentException) { result = true; }

            // assert
            Assert.AreEqual(true, result);
        }