Skip to content

Commit

Permalink
Add test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
richardrandak committed May 8, 2020
1 parent 4acf00d commit 661ccf1
Show file tree
Hide file tree
Showing 10 changed files with 270 additions and 37 deletions.
59 changes: 53 additions & 6 deletions FortnoxAPILibrary.Tests/GeneratedTests/ExpenseTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using FortnoxAPILibrary.Connectors;
using FortnoxAPILibrary.Entities;
using FortnoxAPILibrary.Tests;
Expand All @@ -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
};

Expand All @@ -44,7 +47,9 @@ public void Test_Expense_CRUD()
#endregion CREATE

#region UPDATE

//Not supported

#endregion UPDATE

#region READ / GET
Expand All @@ -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
}
}
Expand Down
62 changes: 57 additions & 5 deletions FortnoxAPILibrary.Tests/GeneratedTests/InvoiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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" });
Expand Down Expand Up @@ -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();
Expand All @@ -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 = "[email protected]" });
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<InvoiceRow>()
{
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
}
}
}
2 changes: 2 additions & 0 deletions FortnoxAPILibrary.Tests/GeneratedTests/LockedPeriodTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
70 changes: 70 additions & 0 deletions FortnoxAPILibrary.Tests/GeneratedTests/OfferTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<OfferRow>()
{
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 = "[email protected]"});
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<OfferRow>()
{
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
}
}
}
70 changes: 70 additions & 0 deletions FortnoxAPILibrary.Tests/GeneratedTests/OrderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<OrderRow>()
{
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 = "[email protected]" });
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<OrderRow>()
{
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
}
}
}
16 changes: 16 additions & 0 deletions FortnoxAPILibrary.Tests/MyAssert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
}
}
}
}
2 changes: 1 addition & 1 deletion FortnoxAPILibrary/EntityConnector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ protected async Task<TEntity> 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;
Expand Down
17 changes: 0 additions & 17 deletions FortnoxAPILibrary/Generated/Entities/Invoices/Labels.cs

This file was deleted.

Loading

0 comments on commit 661ccf1

Please sign in to comment.