Thanks.  In this case, I'm doing a bit extra in work order to learn
Python and Django.
And, I do see how testing models such that all the fields are equal is
a waste on the basis
that Django should be testing that that functionality works.



On Wed, Jun 18, 2008 at 2:48 AM, Norman Harman <[EMAIL PROTECTED]> wrote:
>
> Gene Campbell wrote:
>> Hello Djangonauts
>>
>> I'm a noob on both Django and Python, so this question might be easy
>> for the experts out there.  I am trying to do test first development
>> during the development of my model code.  (I have lots of experience
>> with test first coding in the Java world, no practical experience in
>> the Py world.)
>>
>> I want to do something like this
>>
>> class ModelTest(TestCase):
>>
>>     def test_crud_modelone(self):
>>         mo = ModelOne( name="A name" )
>>         mo.save()
>>
>>         saved_mo = mo.objects.get(pk=mo.id)
>>
>>         assertEqual( vars(mo), vars(saved_mo),"Something not getting saved")
>>
>> That assertEqual line fails.  I wrote this
>>
>>     def compare(self, obj1, obj2, verbose = False):
>>         other = vars(obj2)
>>         for d,v in vars(obj1).items():
>>             if isinstance(other.get(d,None),datetime.date):
>>                 if not other.get(d,None) == v.date():
>>                     return False
>>             elif not other.get(d,None) == v:
>>                 return False
>>
>>         return True
>>
>> This is probably horrible python, I'm still learning (unlearning
>> Java), but it compares correctly for strings and dates so far that
>> I've tested.
> I've been using python since 1.5, I had to look up what vars() did.
> Probably not idiomatic Python.  (I've always just looked at __dict__
> directly, which I do rarely).
>
> The assertEqual line (probably) fails because the order of dictionary
> keys is not defined.
>
>  > Some might be asking why I'm testing if my models can be saved, as
>  > most of that functionality is tested when Django is tested.  But, I
>  > still feel there's a enough that I have to write to warrant a simple
>  > sanity test like this.
>
> FWIW I believe testing what you're testing is a waste of time and
> violates unit testing.  Really the db should be mocked, but that is
> fairly hard.  If you didn't write save(), you shouldn't be testing it.
>
>
> But, if I were testing this I think I'd do something like this
>     assertEqual( db_mo, mo,  "local and db versions of %s not
> identical" % mo.__class__)
>
> In general, I trust == (either Python's or the object's author's) more
> than poking into the internals of an object, encapsulation and all that :)
>
>
> When I compare dictionaries I either convert them to sorted tuples (to
> put keys in proper order)
>
>   expected = dict(var1="dog", var2="cat")
>   answer = something_that_return_dict()
>   assertEqual( sorted(expected.items()), sorted(answer.items()) )
>
> Or check only certain keys are equal (for instance if the answer dict
> has other stuff in it I don't care about)
>
>   check_these = ["key1", "key3"] # = expected_dict.keys()
>   answer = something_that_return_dict()
>   for k in check_these:
>     self.assertEqual( expected_dict[k], answer[k],
>           "expected[%(key)s] != answer[%(key)s]" % {"key":k} )
>
>
> For Models in which I write a custom save method this is how I usually
> structure it.
>
>   def save(self):
>     if self.value is None:
>       self.value = self._calc_value()
>     super(FooModel, self).save()
>
>   def test_value_on_save(self):
>     test_data =( (dict(foo="", bar=""), 42 ),
>                  (dict(foo="1", bar="3"), 37 ),
>                  # test more edge cases, etc.
>                )
>     for (expected, data) in test_data
>       t = Model(**data)
>       t.save()
>       self.assertEqual( expected, t.value )
>
> If I had another custom value I'd write another test for it.
>
> Good luck with your continued Python adventures,
> --
> Norman J. Harman Jr.
> Senior Web Specialist, Austin American-Statesman
> ___________________________________________________________________________
> You've got fun!  Check out Austin360.com for all the entertainment
> info you need to live it up in the big city!
>
> >

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to