Tough Tests before starting The Stock Take

This week was mostly focused on developing test scripts for the Stow List UI and building the Stock Take UI. Stock Take is the new module on which we will concentrate in the coming days.

The first thing I did this week was use Mr. Peter’s provided examples to write some test scripts. I created a script to verify that the Text Input resets to a blank state after an incorrect input. The test was more difficult than usual, and I ran into several problems while completing it. The fact that we are developing an asynchronous code is the primary cause of the difficulty, and it is often the reason why I am unable to locate the source of a particular fault.

The next thing I had to do was mock the Dto, which was not easy. It should have functioned after that, but it did not. Moreover, I started debugging the test script with the code under test, but I failed to run a proper test. eventually, it was caused by the timer thatΒ I needed to stop the timer in order to finish my test successfully.

After that, I was also unable to complete a separate test script that validated the “back” button. It took a lot of my time, and I did everything I could think of to mock the BackHandler, but it was difficult. In the end, I got through by using “/ @ts-ignore,” which tells the TypeScript compiler to disregard the line below it and skip over the error. And thus, the mock worked and did its job and I was able to finish my test successfully.

Finally, I began to create the new layout for our new module. I designed the main UI components. For the first time, I used new components such as a SelectList and a checkbox. Next week, I’ll get the data from the API and continue working on the stock take, which has to be ready to go by the end of the week.

Application of What I have Learnt

On the third week of my internship, I learnt on how to apply what I have learn so far. After setting up the sandbox application to practice the concepts like API, TDD and loose coupling. My task was to convert the stock threshold function of the existing app into an API. At the beginning, I was struggling to create the API for the stock threshold. After some guides from Mr Peter, I begin to understand on how to create the API.

Firstly, using the CQRS pattern, I separated the commands and the queries into separate files. In the queries folder contains the different Query which will return the entire list of the existing stock threshold to the user when it is called. Not only that, there’s also GetById Query which will return a stock threshold to the user based on the requested Id of the stock threshold.

Next, in the commands folder contains the create, update and delete functions for the stock threshold. The create command will take the item Id with the quantity for the stock threshold. Using the given item Id, the code will look for the item in the item database and map it to the stock threshold that was created.

Similar to the create command, the update command will take the stock threshold Id, item Id together with the quantity as well. In the update command, the code will take the item Id and check if the existing item Id is different with the inputted item Id. If the item Id is different, the code will look for the new item in the item database and update the existing item to the new one. As for the delete command, the user just has to input the stock threshold Id and the code will delete it. The Http method was implemented in the controller folder.

Lastly, the unit test was created for each of the function to make sure they are working properly. At the first, I was struggling to create the unit test as I was inexperience in creating unit test but after reference the other unit testing I was able to get the hang of it. The unit test was also accompany with some dummy data. All the unit test was able to pass without much problem.

Test-Driven Development (TDD)

The past week, i have been working on creating tests on modules .

What is TDD ?
TDD is a practice that focuses on creating test cases before developing the actual code . While it’s a slow process to understand from the beginning , not knowing why TDD applies the reversed traditional way of testing , I am starting to grasp the principals and benefits of TDD .

Why do I think TDD is advantageous ? While it also takes a lot of effort to create a test . It also makes a project easier to maintain , especially in large projects . Instead of setting countless breakpoints and debugging in the program over and over to find a bug . Running the test may save a lot of time .

TDD also documents the code better , when using the TDD approach , instead of documenting detailed and time-consuming comments in the program , most of the documentations are already illustrated in the tests on how the code is supposed to work .

However , the TDD approach is very challenging and difficult to learn and adjust to its behaviours appropriately. It is a slow process that takes time , skills and persistence .

Integration and checking of errors

For the past week I have worked on integration tests as well as checking the creation of modules for errors.

The checking involved checking whether a Tax, Supplier, PurchaseOrder, GoodsReceived and PurchaseInvoice can be created successfully without any errors, as well as checking the content inserted into the views.

When using tab navigation, the tests created will not be able to mock the NavigationContext, which will always lead the test being run to use the default value instead of navigating to the wanted and requested view of PurchaseInvoice, either from PurchaseOrder or GoodsReceived. Hence, to solve this problem another method is creating which includes parameters that carry the designated values, which will then be called from the method which includes the NavigationContext.

Next, when adding LineItems using the Add button in PurchaseInvoice from GoodsReceived, only 1 line item is updated and the errors lied because I did not map the LineItemId.

Lastly, I started working on displaying a dialogue box when a PurchaseInvoice already exists. The test has been created with the help of my supervisor.

See you all on my last blog Next week πŸ™‚

Fixing errors and bugs in the project module

Since I have completed the purchase invoice from both purchase order and goods received, I spent the week checking the codes, and finding bugs to be solved to ensure that the module runs as intended without any problems.

I also had problems with the quantity mapping, as the total quantity being mapped in purchase invoice from purchase order is not the same as the total quantity from purchase invoice from goods received line item. The error was because I was initially reading the quantity from purchase invoice line item and goods receive line item instead of reading from the same entity which is goods receive line item. Due to this, different total quantity was being displayed in the UI.

Next, I wrote integration tests for purchase invoice from purchase order and goods received. The tests included creating new purchase invoices, and loading the list of purchase invoices, as well as viewing the individual purchase invoices.

When writing tests, mocks are being used. Mock uses the concept of testing a unit of code which depends on a group of other codes, such that the code will be tested even though its dependent codes are not written or contain bugs. In this case, mocks act as a platform which provides the test with its all needed requirements and environment to ensure that it works and pass as intended.

I also learnt how to resolve issues in Git. Meaning that once an issue is assigned to a person, and it is solved, to resolve the issue on Git, comments such as Fix, Close and Resolve are written alongside to the issue number created, as well as the codes need to be pushed so the changes can be viewed and tracked.

See you all next week πŸ™‚

Working more on the project structure

Continuing fixing errors from the pull and merging of codes, a lot of changes have been made to ensure that the codes compile and align with the new project structure.

The tests that have been written previously for Purchase Order and Goods Received were in need of changing and fixing, as the code structure have changed, the tests also got affected, as a result, the test bases were in need to be checked and fixed, because when debugging the tests to see where the error lies, the debugger does not run through the test, meaning it does not go into the test method environment indicating that the error lies in the test base.

Another thing is the save changes during initialization. Save changes is set to true without using the newly added save changes which is equipped with audit, because we are just initialization and setting the environment, thus, we do not need to audit or check and keep track to see who has made changes to the codes.
When writing tests, the Linq property Single() cannot be used for entities that are equipped with more than one element, as in this case we are trying to assume that entity has only one value which is not true. Thus First() is used when we want to retrieve one element only.

After the fixing and refactoring of codes, the views of the purchase invoice module were not being displayed, due to 2 main reasons which were that the Region Manager for purchase invoice was not added, as well as I mistakenly created a new view and removed the prism:ViewModelLocator.AutoWireViewModel="True" which resulted in the pages not being attached to each other and hence cannot be redirected once requested.

In the view models, often the properties are assigned with two access modifiers. Properties with private access modifiers when used do not make any changes to the User interface, on the other hand, properties with public as their access modifiers when changed or used will make changes on the user interface.

These are the major things that have been learnt the past week. See you all next week πŸ™‚

New changes added to the project structure

The week started by completing the conversion of parameters files into events by using the enum class.

Next, I created multiple tests for the my transaction function, which creates a new purchase invoice based on different services.

Unfortunately, I had an error with the previously created tested for my transaction function, as the SupplierId could not get updated, it was null, but I couldn’t find the way to solve it, only to realize later on with the help of my supervisor, that I did not initialize the SupplierId in the tests, because I thought by just initializing and getting access to the whole entity, it will automatically assign the SupplierId, which was obviously wrong and made the tests created fail.

I then started analyzing the aggregates in the project, and found myself not understanding the concept behind the many-to-many relationship between some entities in the project. With further discussion, the relationship was converted from many-to-many to one-to-many relationship, and due to this many changes were needed to made.

Among the changes done to the files, were to change the complete function in the transaction for the purpose of auditing. As well as, EventName, Time and UserId has been added to the service functions, for auditing purposes as well, such that the when changes have been made, the person who made the change, along with the time and the change made can be tracked. Thus, by making these changes new parameters have been added to the methods.

Previously, the services and repositories interface used to implement the IDisposable interface, but now a new interface has been created that implements the complete, commit and rollback functions, not forgetting that this new interface implements the IDisposable interface as well.

Lastly, an important thing I learnt from Git, is that Pull merges two coding files together, in other words, it adds the changes that have been in both files and merges them into one. Push, on the other hand, add the branch into the project but does not merge it.

That’s all, see you all next week πŸ™‚

Implementation of new functions and classes

This week is also about UI.

I created a new button to separate the purchaseInvoice different options. One which focuses on purchaseInvoice from purchaseOrder and another from GoodsReceived.

purchaseInvoice from purchaseOrder creation is created completely in other words creating a new purchaseInvoice and GoodsReceived. However, when updating the tests created previously, the tests failed due to the supplier entity and supplierId being 0.

At first, I couldn’t understand the reason why it is zero, but with the help of my supervisor, I found out that my mistake was that I did not include the supplierId and entity nor did I map it to the purchaseInvoice.

I also created an integration test for purchase invoice from purchase order. This test is more into testing the UI of the system. Checking whether when the intended button are clicked they perform and function as required.

I was also exposed to adding relationships in the database. I mean, when I set relationships in the entities file, some relationship will get ignored or confused by the entity framework if not fixed and identified properly in the Config files. Such that relationships such as one-to-many and many-to-many need to be written in the config files, along with their foreign keys, to ensure that the relationship is properly understood by the framework.

The main purpose behind using the Linq properties Include and ThenInclude is to ensure that the wanted properties to be retrieved are not null.

Lastly, I have refactored the codes in my module, such that all event functions are placed in the Events folder instead of Parameters folder.

When using the functions in the Events folder, I was introduced to using the enum, which is a special class that groups a group of related variables together. These, variables can be separated by a comma as follows:

public enum Level () { Low, Medium, High, }

The items in enum can be later on used or retrieved by using dot operation to get access to the enum class then the item.

More into coding

Last week was a merge of creating tests and starting UI codes.

After so long, I was done with writing tests. I had a better understanding of how tests work, and refactoring codes as well. Moreover, I was exposed to not writing hard coded codes.

After 7 weeks, I can say that I have learnt a lot about TDD and EF Core, which is something I was not exposed to during my studies and this is something that would definitely benefit me.

Collection and Lists might sound the same, but collections are preferred over lists due to their ability to group outputs. In other words, List does not provide the feature of grouping.

For the past 3 years, I had the idea that the difference between void and static functions was related to having returns.

Little did I know that the static functions and variables are like global variables that can be used across the files, and when a method is static all its members should occupy the word “static” as well.

I have also read about bindable base which is an implementation of INotifyPropertyChanged interface in C#.

More about Entity Framework

I started getting more familiar with EF Core, although I don’t fully understand everything, I am able to grasp the main idea of how EF Core works, because I helped a friend who is doing his internship as well and was asked to use EF Core and had no idea how to use it. So, I was able to guide here and there which shows that I did learn something about EF Core after all.

If you find difficulties in understanding what I’m about to discuss refer to my previous blog by clicking on this link before proceeding https://www.tonghin.com.my/blog/2021/03/04/entity-framework-core-week/.

I finally have a better understanding of how tests work, and how to write a unit test in general.

A unit test compromises of 3 sections. Firstly, the arrange part which sets the environment for the test components. Next, act initiates the test or does the testing. Lastly, assert validates and verifies the result we expect to get by comparing our expected value to the value retrieved from the act section.

Last week, I wrote around 7 tests with the help of my supervisor, who guides me when I don’t understand or get stuck at one point or another.

Among the widely used assert extensions are First( ), Select( ) and Where( ). More information can be read from the below link https://www.tutorialsteacher.com/linq/what-is-linq.

The concept of Rollback( ), Commit( ) and Complete( ) in a transaction is among the things that I have been exposed to as well, whereby rollback undoes changes that have been committed into the database. Commit on the other hand, saves changes into the database, while complete saves the transaction, it does not update the database if commit was not initiated, as well as if complete was not written in the codes, updates that have been made wont be saved even during debugging of codes.

The keyword using in C# is used to ensure that the method dispose( ) is called after a method has been called, or or an object has been created and many other things. Dispose( ) is used to release unmanaged resources, but it does not free a managed memory.

That’s all for this week πŸ™‚