I received the assignment to write some unit tests on an application that is used for a long time. So, full of confidence, I start to dig into the program. It comes out that the code I have to write tests for makes heavy use of SQLConnection, SQLDataReader and so on …. The fact that the program is a light utility program explains the situation, but this doesn’t help me achieving my goal. I want to avoid hitting a database for my unit tests because :
- the access to the database makes the tests slower to run.
- it is difficult to control the state of the database for each test (in order to be sure of the data used in the test).
I thought the task would be very difficult and then, I found some articles talking about a mocking library from Microsoft that allows to mock even framework classes : Mole. So let’s play with it.
In the first post (Create a tag map using a Dictionary) I have created a tag manager class that was based on "CompositeKey" class and a Dictionary<CompositeKey, int> for the map. It is now time to flesh the manager a little so he can provide more services.
In this post we will add methods to:
- Retrieve the list of tags
- Retrieve tags related to a given tag
The purpose of this post is to present an implementation of a tag map using the Dictionary class in .Net.
The map should be used to
- Store several tags and their weight (= the occurrence number of the tag).
- Store the links between tag.
Basically, 2 tags are linked if they are associated to the same document. The map will also count how many time 2 tags are linked, allowing to define strong or weak relation.
The map can be seen as an array whose row and column headers are the tags. The cell content is the weight
|The diagonal (tag_001, tag_001) to (tag_003,tag_003) represents the weight of each tag.
The cell (tag_001,tag_003) represents weight (or occurrence) of the relation between tag_001 and tag_003
As the relation between 2 tags is symmetric in my implementation we don’t have to use the section of the map below the diagonal (gray cells). This will help reduce the memory footprint of the map. Lets go to the implementation.
The purpose of the "Tips & Tricks" posts is to present some small problems I encountered in my all day work and of course show how I solved them.
Recently I had to write some automated integration tests that implied creating a lot of profiles. Each profile was made of an email address and several other settings. The email was used to uniquely indentify a profile. My tests were running against a backend repository, so in order to ensure that I don’t get false tests results I had to find a way to easily create unique email addresses.
First I thought about using GUIDs but I was a little affected by the size and the format of the GUID. Using them would require some reworking of the generated string. Not a tricky issue, but there is a more quick solution.
I used the Path.GetRandomFileName() method of the System.IO namespace. As the msdn documentation doesn’t tell, this method will generate a 8.3 string (for example la3taras.ovb ). So generating my random email was a simple as
This is it for this first and short post in what I hope will become a long series.
Happy programming and live long and prosper.