tdd/lab-workbook.rst
changeset 129 6e237b9442cd
parent 128 29e6b1d5e5f5
child 130 e5d183dce985
--- a/tdd/lab-workbook.rst	Tue Sep 07 15:16:40 2010 +0530
+++ b/tdd/lab-workbook.rst	Tue Sep 07 16:49:56 2010 +0530
@@ -56,4 +56,88 @@
 Lab - 4
 =======
 
-  1. 
+  1. Consider the following use case: We are given a large list of
+     items called *data* where each item is a again a list with three
+     values: username, which is a string; status of the user which
+     can be one of the following three strings 'new', 'valid' or
+     'invalid'; and the last login time which is a datetime Python
+     object.  Write a function called **query** that takes a filter
+     dictionary as a parameter and returns the result of the items in
+     the *data* list. They keys of the dictionary can be 'user',
+     'status' and 'logtime' and their corresponding values can be any
+     of the valid values for the corresponding key. Example filter
+     dictionary::
+
+       filter = {
+           'user': 'john'
+           'status': 'new'
+           }
+
+     Place your function in a file called query.py. Before writing the
+     actual function, follow the test driven development
+     approach. First write a stub, fail the tests and then write the
+     code and make sure the tests pass. Specifically use unittest
+     framework to test this function. Place your tests in a file
+     called test_query.py
+
+     A developer wrote a small utility function in a file named
+     user_utils.py which uses your **query** function which looks as
+     follows::
+
+       def login_util(user=None):
+           """Takes a user name and returns his last login time if the
+           user is a valid user, else return None. If the user is
+           'host' it returns the last login time of all the users.
+           """
+
+           filter_dict = {
+               'user': user
+               'status': 'active'
+               }
+
+           if user == 'host':
+               filter_dict['status'] + ['new', 'invalid']
+
+           return query(filter_dict)
+
+     Unfortunately the developer did not provide us with the test
+     cases. We wrote the following test cases for you to only discover
+     that the function miserably fails. 
+
+     The tests were placed in a file called test_user_utils.py and we
+     have used the unittest framework::
+
+       import query
+       import user_utils
+       import unittest
+
+       class TestUserUtils(unittest.TestCase):
+        
+           def setUp(self):
+               """Boiler plate method to provide common data to all
+               the test methods.
+               """
+               self.test_names = ['Alex', 'Guido', 'Thomas', 'host',
+                   'Tom', 'James']
+               self.data_len = len(query.data)
+
+           def test_login_utils(self):
+               """Tests for the login_utils function.
+               """
+
+               for name in self.test_names:
+                   if name == 'host':
+                       assertEqual(len(user_utils.login_utils(name)), self.data_len)
+                   else:
+                       assertLess(len(user_utils.login_utils(name)), self.data_len)
+
+           def tearDown(self):
+               """Boiler plate method to clean up all the data created
+               for tests.
+               """
+
+               del self.test_names
+               del self.data_len
+
+     Fix the bug, run the tests to make sure the function passes the
+     tests and if possible refactor the code with a better approach.