From 661ccf18951ac4f9404afcfccc54f38f8b92e74b Mon Sep 17 00:00:00 2001 From: Richard Randak Date: Fri, 8 May 2020 19:42:37 +0200 Subject: [PATCH] Add test coverage --- .../GeneratedTests/ExpenseTests.cs | 59 ++++++++++++++-- .../GeneratedTests/InvoiceTests.cs | 62 ++++++++++++++-- .../GeneratedTests/LockedPeriodTests.cs | 2 + .../GeneratedTests/OfferTests.cs | 70 +++++++++++++++++++ .../GeneratedTests/OrderTests.cs | 70 +++++++++++++++++++ FortnoxAPILibrary.Tests/MyAssert.cs | 16 +++++ FortnoxAPILibrary/EntityConnector.cs | 2 +- .../Generated/Entities/Invoices/Labels.cs | 17 ----- .../Entities/Locked Period/LockedPeriod.cs | 6 -- FortnoxAPILibrary/RequestInfo.cs | 3 +- 10 files changed, 270 insertions(+), 37 deletions(-) delete mode 100644 FortnoxAPILibrary/Generated/Entities/Invoices/Labels.cs diff --git a/FortnoxAPILibrary.Tests/GeneratedTests/ExpenseTests.cs b/FortnoxAPILibrary.Tests/GeneratedTests/ExpenseTests.cs index c6a55079..c330aed7 100644 --- a/FortnoxAPILibrary.Tests/GeneratedTests/ExpenseTests.cs +++ b/FortnoxAPILibrary.Tests/GeneratedTests/ExpenseTests.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Linq; using FortnoxAPILibrary.Connectors; using FortnoxAPILibrary.Entities; using FortnoxAPILibrary.Tests; @@ -19,21 +20,23 @@ public void Init() ConnectionCredentials.ClientSecret = TestCredentials.Client_Secret; } - [Ignore("CAN NOT UPDATE OR DELETE")] [TestMethod] public void Test_Expense_CRUD() { #region Arrange - new AccountConnector().Create(new Account() {Number = 0123, Description = "TmpAccount"}); + var ac = new AccountConnector(); + if (ac.Get(0123) == null) //account 123 does not exist + ac.Create(new Account() {Number = 0123, Description = "TmpAccount"}); #endregion Arrange IExpenseConnector connector = new ExpenseConnector(); #region CREATE + var newExpense = new Expense() { Text = "TestExpense", - Code = "TST", + Code = TestUtils.RandomString(6), Account = 0123 }; @@ -44,7 +47,9 @@ public void Test_Expense_CRUD() #endregion CREATE #region UPDATE + //Not supported + #endregion UPDATE #region READ / GET @@ -56,13 +61,55 @@ public void Test_Expense_CRUD() #endregion READ / GET #region DELETE + //Not supported + #endregion DELETE #region Delete arranged resources - var accConnector = new AccountConnector(); - accConnector.Delete(0123); - MyAssert.HasNoError(accConnector); + ac.Delete(0123); + MyAssert.HasNoError(ac); + + #endregion Delete arranged resources + } + + [TestMethod] + public void Test_Expense_Find() + { + #region Arrange + var ac = new AccountConnector(); + if (ac.Get(0123) == null) //account 123 does not exist + ac.Create(new Account() { Number = 0123, Description = "TmpAccount" }); + #endregion Arrange + + var timeStamp = DateTime.Now; + var remark = TestUtils.RandomString(); + + var newExpense = new Expense() + { + Text = remark, + Account = 0123 + }; + + IExpenseConnector connector = new ExpenseConnector(); + for (var i = 0; i < 2; i++) + { + newExpense.Code = TestUtils.RandomString(6); + connector.Create(newExpense); + MyAssert.HasNoError(connector); + } + + connector.LastModified = timeStamp; //does not seem to work + var expensesCollection = connector.Find(); + + var filteredExpenses = expensesCollection.Entities.Where(x => x.Text == remark).ToList(); + MyAssert.HasNoError(connector); + Assert.AreEqual(2, filteredExpenses.Count); + Assert.IsNotNull(filteredExpenses.First().Url); + + #region Delete arranged resources + ac.Delete(0123); + MyAssert.HasNoError(ac); #endregion Delete arranged resources } } diff --git a/FortnoxAPILibrary.Tests/GeneratedTests/InvoiceTests.cs b/FortnoxAPILibrary.Tests/GeneratedTests/InvoiceTests.cs index 6e19349e..2e051208 100644 --- a/FortnoxAPILibrary.Tests/GeneratedTests/InvoiceTests.cs +++ b/FortnoxAPILibrary.Tests/GeneratedTests/InvoiceTests.cs @@ -144,7 +144,7 @@ public void Test_Find() } [TestMethod] - public void Invoice_DueDate() + public void Test_DueDate() { #region Arrange var tmpCustomer = new CustomerConnector().Create(new Customer() { Name = "TmpCustomer", CountryCode = "SE", City = "Testopolis" }); @@ -189,11 +189,15 @@ public void Invoice_DueDate() } [TestMethod] - public void Invoice_Print() + public void Test_Print() { #region Arrange - var tmpCustomer = new CustomerConnector().Create(new Customer() { Name = "TmpCustomer", CountryCode = "SE", City = "Testopolis" }); - var tmpArticle = new ArticleConnector().Create(new Article() { Description = "TmpArticle", Type = ArticleType.STOCK, PurchasePrice = 100 }); + var cc = new CustomerConnector(); + var ac = new ArticleConnector(); + var tmpCustomer = cc.Create(new Customer() { Name = "TmpCustomer", CountryCode = "SE", City = "Testopolis" }); + var tmpArticle = ac.Create(new Article() { Description = "TmpArticle", Type = ArticleType.STOCK, PurchasePrice = 100 }); + MyAssert.HasNoError(cc); + MyAssert.HasNoError(ac); #endregion Arrange IInvoiceConnector connector = new InvoiceConnector(); @@ -219,7 +223,55 @@ public void Invoice_Print() var fileData = connector.Print(createdInvoice.DocumentNumber); MyAssert.HasNoError(connector); - Assert.IsTrue(fileData.Length > 10000); + MyAssert.IsPDF(fileData); + + #region Delete arranged resources + new CustomerConnector().Delete(tmpCustomer.CustomerNumber); + new ArticleConnector().Delete(tmpArticle.ArticleNumber); + #endregion Delete arranged resources + } + + [TestMethod] + public void Test_Email() + { + #region Arrange + var cc = new CustomerConnector(); + var ac = new ArticleConnector(); + var tmpCustomer = cc.Create(new Customer() { Name = "TmpCustomer", CountryCode = "SE", City = "Testopolis", Email = "richard.randak@softwerk.se" }); + var tmpArticle = ac.Create(new Article() { Description = "TmpArticle", Type = ArticleType.STOCK, PurchasePrice = 100 }); + MyAssert.HasNoError(cc); + MyAssert.HasNoError(ac); + #endregion Arrange + + IInvoiceConnector connector = new InvoiceConnector(); + + var newInvoice = new Invoice() + { + CustomerNumber = tmpCustomer.CustomerNumber, + InvoiceDate = new DateTime(2019, 1, 20), //"2019-01-20", + DueDate = new DateTime(2019, 2, 20), //"2019-02-20", + InvoiceType = InvoiceType.CASHINVOICE, + PaymentWay = PaymentWay.CASH, + Comments = "Testing invoice email feature", + InvoiceRows = new List() + { + new InvoiceRow(){ ArticleNumber = tmpArticle.ArticleNumber, DeliveredQuantity = 10, Price = 100}, + new InvoiceRow(){ ArticleNumber = tmpArticle.ArticleNumber, DeliveredQuantity = 20, Price = 100}, + new InvoiceRow(){ ArticleNumber = tmpArticle.ArticleNumber, DeliveredQuantity = 15, Price = 100} + } + }; + + var createdInvoice = connector.Create(newInvoice); + MyAssert.HasNoError(connector); + + var emailedInvoice = connector.Email(createdInvoice.DocumentNumber); + MyAssert.HasNoError(connector); + Assert.AreEqual(emailedInvoice.DocumentNumber, createdInvoice.DocumentNumber); + + #region Delete arranged resources + new CustomerConnector().Delete(tmpCustomer.CustomerNumber); + new ArticleConnector().Delete(tmpArticle.ArticleNumber); + #endregion Delete arranged resources } } } diff --git a/FortnoxAPILibrary.Tests/GeneratedTests/LockedPeriodTests.cs b/FortnoxAPILibrary.Tests/GeneratedTests/LockedPeriodTests.cs index 2880f1be..2652dac4 100644 --- a/FortnoxAPILibrary.Tests/GeneratedTests/LockedPeriodTests.cs +++ b/FortnoxAPILibrary.Tests/GeneratedTests/LockedPeriodTests.cs @@ -42,6 +42,8 @@ public void Test_LockedPeriod_CRUD() MyAssert.HasNoError(connector); Assert.IsNotNull(retrievedLockedPeriod); + Assert.IsNull(retrievedLockedPeriod.EndDate); //No period is locked + #endregion READ / GET #region DELETE diff --git a/FortnoxAPILibrary.Tests/GeneratedTests/OfferTests.cs b/FortnoxAPILibrary.Tests/GeneratedTests/OfferTests.cs index 93962951..4450ef31 100644 --- a/FortnoxAPILibrary.Tests/GeneratedTests/OfferTests.cs +++ b/FortnoxAPILibrary.Tests/GeneratedTests/OfferTests.cs @@ -136,5 +136,75 @@ public void Test_Find() new ArticleConnector().Delete(tmpArticle.ArticleNumber); #endregion Delete arranged resources } + + [TestMethod] + public void Test_Print() + { + #region Arrange + var tmpCustomer = new CustomerConnector().Create(new Customer() { Name = "TmpCustomer", CountryCode = "SE", City = "Testopolis" }); + var tmpArticle = new ArticleConnector().Create(new Article() { Description = "TmpArticle", Type = ArticleType.STOCK, PurchasePrice = 100 }); + #endregion Arrange + + IOfferConnector connector = new OfferConnector(); + var newOffer = new Offer() + { + Comments = "TestOrder", + CustomerNumber = tmpCustomer.CustomerNumber, + OfferDate = new DateTime(2019, 1, 20), //"2019-01-20", + OfferRows = new List() + { + new OfferRow(){ ArticleNumber = tmpArticle.ArticleNumber, Quantity = 10}, + new OfferRow(){ ArticleNumber = tmpArticle.ArticleNumber, Quantity = 20}, + new OfferRow(){ ArticleNumber = tmpArticle.ArticleNumber, Quantity = 15} + } + }; + + var createdOffer = connector.Create(newOffer); + MyAssert.HasNoError(connector); + + var fileData = connector.Print(createdOffer.DocumentNumber); + MyAssert.HasNoError(connector); + MyAssert.IsPDF(fileData); + + #region Delete arranged resources + new CustomerConnector().Delete(tmpCustomer.CustomerNumber); + new ArticleConnector().Delete(tmpArticle.ArticleNumber); + #endregion Delete arranged resources + } + + [TestMethod] + public void Test_Email() + { + #region Arrange + var tmpCustomer = new CustomerConnector().Create(new Customer() { Name = "TmpCustomer", CountryCode = "SE", City = "Testopolis", Email = "richard.randak@softwerk.se"}); + var tmpArticle = new ArticleConnector().Create(new Article() { Description = "TmpArticle", Type = ArticleType.STOCK, PurchasePrice = 100 }); + #endregion Arrange + + IOfferConnector connector = new OfferConnector(); + var newOffer = new Offer() + { + Comments = "TestOrder", + CustomerNumber = tmpCustomer.CustomerNumber, + OfferDate = new DateTime(2019, 1, 20), //"2019-01-20", + OfferRows = new List() + { + new OfferRow(){ ArticleNumber = tmpArticle.ArticleNumber, Quantity = 10}, + new OfferRow(){ ArticleNumber = tmpArticle.ArticleNumber, Quantity = 20}, + new OfferRow(){ ArticleNumber = tmpArticle.ArticleNumber, Quantity = 15} + } + }; + + var createdOffer = connector.Create(newOffer); + MyAssert.HasNoError(connector); + + var emailedInvoice = connector.Email(createdOffer.DocumentNumber); + MyAssert.HasNoError(connector); + Assert.AreEqual(emailedInvoice.DocumentNumber, createdOffer.DocumentNumber); + + #region Delete arranged resources + new CustomerConnector().Delete(tmpCustomer.CustomerNumber); + new ArticleConnector().Delete(tmpArticle.ArticleNumber); + #endregion Delete arranged resources + } } } diff --git a/FortnoxAPILibrary.Tests/GeneratedTests/OrderTests.cs b/FortnoxAPILibrary.Tests/GeneratedTests/OrderTests.cs index d6cf68c7..4eeedf76 100644 --- a/FortnoxAPILibrary.Tests/GeneratedTests/OrderTests.cs +++ b/FortnoxAPILibrary.Tests/GeneratedTests/OrderTests.cs @@ -136,5 +136,75 @@ public void Test_Find() new ArticleConnector().Delete(tmpArticle.ArticleNumber); #endregion Delete arranged resources } + + [TestMethod] + public void Test_Print() + { + #region Arrange + var tmpCustomer = new CustomerConnector().Create(new Customer() { Name = "TmpCustomer", CountryCode = "SE", City = "Testopolis" }); + var tmpArticle = new ArticleConnector().Create(new Article() { Description = "TmpArticle", Type = ArticleType.STOCK, PurchasePrice = 100 }); + #endregion Arrange + + IOrderConnector connector = new OrderConnector(); + var newOrder = new Order() + { + Comments = "TestOrder", + CustomerNumber = tmpCustomer.CustomerNumber, + OrderDate = new DateTime(2019, 1, 20), //"2019-01-20", + OrderRows = new List() + { + new OrderRow(){ ArticleNumber = tmpArticle.ArticleNumber, OrderedQuantity = 20, DeliveredQuantity = 10}, + new OrderRow(){ ArticleNumber = tmpArticle.ArticleNumber, OrderedQuantity = 20, DeliveredQuantity = 20}, + new OrderRow(){ ArticleNumber = tmpArticle.ArticleNumber, OrderedQuantity = 20, DeliveredQuantity = 15} + } + }; + + var createdOrder = connector.Create(newOrder); + MyAssert.HasNoError(connector); + + var fileData = connector.Print(createdOrder.DocumentNumber); + MyAssert.HasNoError(connector); + MyAssert.IsPDF(fileData); + + #region Delete arranged resources + new CustomerConnector().Delete(tmpCustomer.CustomerNumber); + new ArticleConnector().Delete(tmpArticle.ArticleNumber); + #endregion Delete arranged resources + } + + [TestMethod] + public void Test_Email() + { + #region Arrange + var tmpCustomer = new CustomerConnector().Create(new Customer() { Name = "TmpCustomer", CountryCode = "SE", City = "Testopolis", Email = "richard.randak@softwerk.se" }); + var tmpArticle = new ArticleConnector().Create(new Article() { Description = "TmpArticle", Type = ArticleType.STOCK, PurchasePrice = 100 }); + #endregion Arrange + + IOrderConnector connector = new OrderConnector(); + var newOrder = new Order() + { + Comments = "TestOrder", + CustomerNumber = tmpCustomer.CustomerNumber, + OrderDate = new DateTime(2019, 1, 20), //"2019-01-20", + OrderRows = new List() + { + new OrderRow(){ ArticleNumber = tmpArticle.ArticleNumber, OrderedQuantity = 20, DeliveredQuantity = 10}, + new OrderRow(){ ArticleNumber = tmpArticle.ArticleNumber, OrderedQuantity = 20, DeliveredQuantity = 20}, + new OrderRow(){ ArticleNumber = tmpArticle.ArticleNumber, OrderedQuantity = 20, DeliveredQuantity = 15} + } + }; + + var createdOrder = connector.Create(newOrder); + MyAssert.HasNoError(connector); + + var emailedInvoice = connector.Email(createdOrder.DocumentNumber); + MyAssert.HasNoError(connector); + Assert.AreEqual(emailedInvoice.DocumentNumber, createdOrder.DocumentNumber); + + #region Delete arranged resources + new CustomerConnector().Delete(tmpCustomer.CustomerNumber); + new ArticleConnector().Delete(tmpArticle.ArticleNumber); + #endregion Delete arranged resources + } } } diff --git a/FortnoxAPILibrary.Tests/MyAssert.cs b/FortnoxAPILibrary.Tests/MyAssert.cs index c5d1a49f..5a848a43 100644 --- a/FortnoxAPILibrary.Tests/MyAssert.cs +++ b/FortnoxAPILibrary.Tests/MyAssert.cs @@ -14,5 +14,21 @@ public static void HasNoError(IConnector connector) if (connector.HasError) throw new AssertFailedException($"Request failed due to '{connector.Error.Message}'."); } + + public static void IsPDF(byte[] data) + { + try + { + Assert.AreEqual(data[0], 0x25); // % + Assert.AreEqual(data[1], 0x50); // P + Assert.AreEqual(data[2], 0x44); // D + Assert.AreEqual(data[3], 0x46); // F + Assert.AreEqual(data[4], 0x2D); // - + } + catch (AssertFailedException) + { + throw new AssertFailedException($"File is not valid PDF file."); + } + } } } \ No newline at end of file diff --git a/FortnoxAPILibrary/EntityConnector.cs b/FortnoxAPILibrary/EntityConnector.cs index aec4f6cd..8a14f8c3 100644 --- a/FortnoxAPILibrary/EntityConnector.cs +++ b/FortnoxAPILibrary/EntityConnector.cs @@ -227,7 +227,7 @@ protected async Task DoActionAsync(string documentNumber, Action action break; case Action.Email: RequestInfo.Method = RequestMethod.Get; - RequestInfo.ResponseType = RequestResponseType.Email; + RequestInfo.ResponseType = RequestResponseType.JSON; break; default: RequestInfo.Method = RequestMethod.Put; diff --git a/FortnoxAPILibrary/Generated/Entities/Invoices/Labels.cs b/FortnoxAPILibrary/Generated/Entities/Invoices/Labels.cs deleted file mode 100644 index 39b459ac..00000000 --- a/FortnoxAPILibrary/Generated/Entities/Invoices/Labels.cs +++ /dev/null @@ -1,17 +0,0 @@ -using FortnoxAPILibrary.Serialization; -using Newtonsoft.Json; -using System; -using System.Collections.Generic; -using System.Runtime.Serialization; - -namespace FortnoxAPILibrary.Entities -{ - [Entity(SingularName = "Labels", PluralName = "Labels")] - public class Labels - { - - /// Id of the label. - [JsonProperty] - public int? Id { get; set; } - } -} \ No newline at end of file diff --git a/FortnoxAPILibrary/Generated/Entities/Locked Period/LockedPeriod.cs b/FortnoxAPILibrary/Generated/Entities/Locked Period/LockedPeriod.cs index 76ba3ed2..3cdd8309 100644 --- a/FortnoxAPILibrary/Generated/Entities/Locked Period/LockedPeriod.cs +++ b/FortnoxAPILibrary/Generated/Entities/Locked Period/LockedPeriod.cs @@ -9,12 +9,6 @@ namespace FortnoxAPILibrary.Entities [Entity(SingularName = "LockedPeriod", PluralName = "LockedPeriods")] public class LockedPeriod { - - /// Direct URL to the record - [ReadOnly] - [JsonProperty("@url")] - public string Url { get; private set; } - /// End date of the locked period [ReadOnly] [JsonProperty] diff --git a/FortnoxAPILibrary/RequestInfo.cs b/FortnoxAPILibrary/RequestInfo.cs index f1c93356..fef106ab 100644 --- a/FortnoxAPILibrary/RequestInfo.cs +++ b/FortnoxAPILibrary/RequestInfo.cs @@ -54,8 +54,7 @@ public enum RequestResponseType { JSON, PDF, - File, - Email + File } public enum RequestMethod