Posts Tagged ‘Unit Testing’

Simulating User Login in a Django View Unit Test

Tuesday, July 26th, 2011

 

 

So attempting to simulate a user login for a unit test has been driving me crazy for about the last hour or so. Django offers a pretty sweet testing client that simulates the http request/response model allowing for fairly robust unit tests to be built around the application views themselves. I was admittedly skeptical of unit testing views, but given the context I’ve become a believer. But on that note, keep in mind that they are still unit tests, they are not a replacement for Integration or User Acceptance tests.

As such I was a very happy camper until I began writing a test for a view that used the request.user.id value of a logged in user. Of course to test the functional part of the view it checked if a user was logged in (request.user.id != null), and if null would skip processing. Well, that doesn’t do me any good. I needed to convince the view that I am logged in.

So I went through the ropes of using the RequestFactory and the User model to create a fake request with a simulated user. While this worked, there were still session issues. I’ll also note that I found very little on the web about simulating user authentication in unit tests which surprised me since django documentation has never been something I’ve had trouble finding.

Eventually, I discovered a login() method in the Client model. It accepts a username and password and performs the login without requiring the request like the login() method in the ‘auth’ module. Keep in mind that the primary database is not used in unit tests, so an empty replica is used. As such you will need to create the mock user at runtime.

So I’ll wrap up with a working example of the code. Enjoy.

from django.test import TestCase
from django.test.client import Client
from django.contrib.auth.models import User

class ViewsTestCase(TestCase):
    def setUp(self):
        self.client = Client()

    def test_MyView(self):
        User.objects.create_user('fakename', 'fake@pukkared.com', 'mypassword')

        #use test client to perform login
        user = self.client.login(username='fakename', password='mypassword')

        response = self.client.post('/myViewUrlPath/')

Comparing structs using CFCUnit testing framework

Tuesday, January 25th, 2011

A while back I wrote an entry describing how to use the Java Equal() method to compare two structs. I had looked this up because I was writing some unit tests and (obviously) needed to assert that one struct was equal to another struct.

As usual I probably should have referenced the CFC Unit docs before referencing the Java docs. Nonetheless, I did recently take a look at the assert methods provided in CFC Unit. In case your interested they can be viewed here. So there is an assert method assertEqualsStruct() that from what I can tell compares the data and structure of two structs. As such if you’re writing unit tests using CFCUnit you can also use this assert to perform the same check. Again from what I can tell they both do the same thing. So just wanted to post the update.

Using Java to compare Coldfusion objects

Monday, November 22nd, 2010

This past weekend I was building some unit tests when I came across a situation where I needed to compare two Coldfusion objects for equality. If I had a way to simply assert true the equality of two objects would make the test infinitely more simple than what it need be.

I have never really had a need to do this before and am not aware of any way in coldfusion to compare complex objects against one another. However, since CF is built on the very powerful Java libraries it didn’t take much digging to turn up the Java equals() function that accepts an object to be compared against. It essentially works with something like:

isequal = object1.equals(object2);

Pretty straight forward. Let’s look at an example illustrating how to grab this function and utilize it using Coldfusion on Coldfusion objects.

Java Equals() in Coldfusion

<!--- get the Java Comparator class --->
<cfset variables.comparator = createObject("java", "java.util.Comparator") />

<!--- build the first object --->
<cfset variables.objectOne = structNew() />
<cfset variables.objectOne.name = "matt" />
<cfset variables.objectOne.lastname = "cook" />

<!--- build the second object --->
<cfset variables.objectTwo = structNew() />
<cfset variables.objectTwo.name = "matt" />
<cfset variables.objectTwo.lastname = "cook" />

<!--- compare the objects --->
<cfset variables.isEqual = variables.objectTwo.Equals(variables.objectOne) />

<!--- output the boolean --->
<cfdump var="#variables.isEqual#" />

(more…)