by Unknown user
Having dealt with most of the messy UI code, I'm ready for some clean, efficient, highly rational business logic. This is work I usually do in unit tests. And the first thing I need is a unit test app, one that will run with my ClarionTest unit testing framework.
...
Which leaves me stumped again:
It's Coming up with a name is hard because the whole time I'm thinking about class design I'm trying to keep the class as generic as possible. A class that handles just one situation is a last resort. Also while
While I'll be using this class in conjunction with a UI class, it doesn't have anything to do with the UI. But it does have to do with data, and it does have to do with managing many-to-many relationships. So provisionally I've decided to call the class CML_Data_ManyToManyLinks and the app CML_Data_ManyToManyLinksTests.
I create the class using John Hickey's excellent ClarionLive! Class Creator, part of the ClarionLive! Utilities:
I've defined my My baseline class that uses the CML conventions. It's , and is included in CML as CML_BaseClass.inc and CML_BaseClass.clw.
The first unit test
The TXA created a sample unit test procedure for me. But what will my first unit test be?
The first unit test
At the risk of repeating myself even more than usual, one of the things I like best about test-driven development is it forces me to think about how I want to use my classes long before I think about how to write them. And that's almost always a more productive approach.
I see CML_Data_ManyToManyLinks as my repository of information about whether any two records from each of two files linked. As with the original class that is the inspiration for this series, I'm going to use the arbitrary terms "left" and "right" to define those two sets of records.
Let's say I have "left" file records A, B and C, and "right" file records X, Y and Z.
...
Step one, then, is to write the idealized, uncompilable unit test. I do have an empty class on hand, so I need to include
Step two is to get a clean compile and a failed test. If the test doesn't fail at this stage then it's clearly not a good test!
I do have an empty class on hand, so I need to include that globally:
When I compile I get a bunch of errors about the missing methods:
...
Now the unit test app compiles, but the test fails, as expected:
All I need to do now is write the minimum code necessary to get the code to pass.
...
Code Block |
---|
CML_Data_ManyToManyLinks_DataQ queue,TYPE LeftRecordID long RightRecordID long end CML_Data_ManyToManyLinks Class,Type,Module('CML_Data_ManyToManyLinks.CLW'),Link('CML_Data_ManyToManyLinks.CLW',_CML_Classes_LinkMode_),Dll(_CML_Classes_DllMode_) LinksQ &CML_Data_ManyToManyLinks_DataQ Construct Procedure() Destruct Procedure() AddLink procedure(long leftRecordID,long rightRecordID) HasLink procedure(long leftRecordID,long rightRecordID),bool End |
The constructor creates and an instance of the queue, ; the destructor empties and disposes of the queue. The AddLink and HasLink methods are almost identical except that one is looking up a record and the other one is adding a record.
...
So far so good. But can I wire this into my UITest program?That
Using the tested class
My UITest program used this queue declaration:
Code Block |
---|
CheckboxQueue queue Checked bool CheckedIcon long CheckedText string(30) end |
But I'll need another an ID field now, because I need to be able to identify my "right" record in the CML_Data_ManyToManyLinks class:
...
Now I can declare an instance of that my class:
Code Block |
---|
Links CML_Data_ManyToManyLinks |
and in In the code I'll initialize the queue of data and the linking class separately. If I were using this in a database setting, and using this code to display which classes a student takes, the first loop would be reading the list of potential classes and the second loop would be reading the records that indicate which classes the student actually takes (except that for purposes of demonstration I'm randomly selecting about half of them).
...
But there's something wrong with my code. I only have one parameter to AddLink, not two. This is effectively the "right" side value; there really isn't any particular reason to store the "left" side ID because there is only one record on the left side. I could create a default value for the first parameter, but that would mean omitting the first parameter and forgetting to do that is an easy mistake to make.
That This doesn't mean there's anything fundamentally wrong with the class design; there might be a situation somewhere down the road where I really do want to keep links for multiple left records. But probably most uses of the class will only have one left record and multiple right records.
This also leads me to want some slightly different method names:
- IsLinkedTo(long rightRecordID)
- IsLinkBetween(long leftRecordID,long rightRecordID)
- SetLinkTo(long rightRecordID)
- SetLinkBetween(long leftRecordID,long rightRecordID)
The naming isn't strictly consistent (IsLink vs IsLinked) but I think it's a bit better grammatically. I added a LeftRecordID property to the class, which defaults to zero, and two new methods. I also renamed the existing methods:
Code Block |
---|
CML_Data_ManyToManyLinks Class,Type,Module('CML_Data_ManyToManyLinks.CLW'),Link('CML_Data_ManyToManyLinks.CLW',_CML_Classes_LinkMode_),Dll(_CML_Classes_DllMode_)
LinksQ &CML_Data_ManyToManyLinks_DataQ
LeftRecordID long
Construct Procedure()
Destruct Procedure()
IsLinkBetween procedure(long leftRecordID,long rightRecordID),bool
IsLinkedTo procedure(long rightRecordID),bool
SetLinkBetween procedure(long leftRecordID,long rightRecordID)
SetLinkTo procedure(long rightRecordID)
End |
The naming isn't strictly consistent (IsLink vs IsLinked) but I think it's a bit better grammatically.
Here is the method code:
Code Block |
---|
CML_Data_ManyToManyLinks.IsLinkedTo procedure(long rightRecordID)!,bool
code
return self.IsLinkBetween(self.LeftRecordID,rightRecordID)
CML_Data_ManyToManyLinks.IsLinkBetween procedure(long leftRecordID,long rightRecordID)!,bool
code
self.LinksQ.LeftRecordID = LeftRecordID
self.LinksQ.RightRecordID = rightRecordID
get(self.LinksQ,self.LinksQ.LeftRecordID,self.LinksQ.RightRecordID)
if not errorcode() then return true.
return false
CML_Data_ManyToManyLinks.SetLinkTo procedure(long rightRecordID)
code
self.SetLinkBetween(self.LeftRecordID,rightRecordID)
CML_Data_ManyToManyLinks.SetLinkBetween procedure(long leftRecordID,long rightRecordID)
code
clear(self.LinksQ)
self.LinksQ.LeftRecordID = LeftRecordID
self.LinksQ.RightRecordID = rightRecordID
add(self.LinksQ,self.LinksQ.LeftRecordID,self.LinksQ.RightRecordID) |
I renamed the unit test procedure and added a second one:
Here's the code for the second test:
Code Block |
---|
ManyToMany.SetLinkTo(RightRecordX)
ManyToMany.SetLinkTo(RightRecordZ)
AssertThat(ManyToMany.IsLinkedTo(RightRecordX),IsEqualTo(true), 'Test 1 failed')
AssertThat(ManyToMany.IsLinkedTo(RightRecordY),IsEqualTo(false),'Test 2 failed')
AssertThat(ManyToMany.IsLinkedTo(RightRecordZ),IsEqualTo(true), 'Test 3 failed') |
Next time I'll finish wiring the Links object into my UITest application, which no doubt will lead to some further code changes.