Unit Tests – Handling complex arguments with Suds

A small update this time. In a previous post I talked about how I finally managed to get a bunch of unit tests done. At that time I was still having trouble with methods which took complex hashes, or ArrayofArrays as parameters; I just couldn’t figure out how to use Suds to send these successfully to the server.

After quite a bit of searching and filtering, I found out that Suds has something called a ‘factory’ which helps you to create an object of the desired datatype. I’ll elaborate a little. When you query the WSDL from the server, using Suds, you get a client object. The client object contains pretty much everything that the WSDL contains. So if there’s a method in there, which takes a complex datatype as an input parameter, the client object will contain the exact information that this datatype needs. Okay okay..here’s an example :D. The CreateArticle method, that I was struggling with..for example, takes just 1 parameter as input. The data type for this is TeamMentor_Article. Now if the input was a string, its easy to have a sample value as “Abcd” but if its TeamMentor_Article, what do you do? Here’s how you handle it.

Write code using the factory API for that particular datatype. In Suds you can write this like this – article = client.factory.create(‘TeamMentor_Article’). Once the article is returned back, print it out..this reveals the entire structure of the TeamMentor_Article object. So here’s what it looks like when I print the object out:-

Metadata =
Id = None
Id_History = None
Library_Id = None
Title = None
Content =
Description = None
_Metadata_Hash = 
_Content_Hash = 

Neat. If you look at it carefully, you have a further 2 complex objects TeamMentor_Article_Metadata and Content embedded inside this. Now I was able to quickly set the last 2 parameters _Metadata_Hash and _Content_Hash, using article._Content_Hash and article._Metadata_Hash. I was still having trouble with the inner structures though as those weren’t directly part of the ‘article’ object, but an inner Metadata object.

After a lot of head banging and a little common sense ;).. it turns out to be quite simple. You can access it as follows – article.Metadata.Id OR article.Metadata.Title. Took me a while though to get there. The Control Panel – Admin Tasks menu, SoapUI as well as Burp proxy for monitoring what I was doing all helped me a lot; in figuring this out. So a quick tip is…Whenever you’re coding “Web” try and see if you can send that traffic through Burp. What you think you’re sending…and what you’re actually sending…isn’t always the same. How do I know..you ask? Er well… 😉

So once I figured this out, the rest of it was quite similar to my previous Unit tests and I quickly managed to push them to Git. Here is my unit test for CreateArticle. And now..finally…I am done with all 17 methods on this page and have a reasonably cool, repeatable methodology. Yay. Now I need to repeat this for the remaining 104 methods. Post back later then…Cya 🙂

This entry was posted in UnitTests, WebServices. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s