mardi 29 septembre 2015

How to test the django webapp?

I'm trying to write a test for my django webapp but I can't seem to get a context object right when I manually populate the data structure.

def test_exclude_examed_courses_from_other():
    import social.users.views as views
    import social.users.menu_views as menu_views
    from django.utils.translation import ugettext as _
    td.login(td.users.ture)
    profile = td.users.ture.get_profile()
    grouped_subscriptions = profile.get_grouped_subscriptions()
    groupt = grouped_subscriptions.get(u'course', {})
    other_courses = groupt.get((-999, _(u'\xd6vriga')), {})
    print " OTHER COURSES " + str(other_courses)
    examed_courses = []
    examed_courses.append("context: Corporate Finance (ME1311)(5)")
    examed_courses.append("context: Electromagnetism and Waves (SK1114)(4)")      
    subscriptions_courses = views.exclude_examed_courses_from_other_courses(grouped_subscriptions.get(u'course', {}), examed_courses)

The function I'm testing is working when I run the app:

def exclude_examed_courses_from_other_courses(subscriptions_courses, examed_courses):
    if examed_courses:
        examed_contexts = [course.context for course in examed_courses]
        other_courses = subscriptions_courses.get((-999, _(u'\xd6vriga')), {})
        for course.context in examed_contexts:
            if course.context in [subscription.context for other_course, subscription in other_courses.iteritems()]:
                del subscriptions_courses.get((-999, _(u'\xd6vriga')))[course.context.name]
    return subscriptions_courses

How should I write the test? The error I'm getting is that the context object doesn't exist:

#929      test_my_subscriptions.test_exclude_examed_courses_from_other ...  OTHER COURSES {u'Artificial intelligence (DD2380)': <Subscription: (id: 1, user: Ture Tureson, context: Artificial intelligence (DD2380)(3)), as_email: False>, u'Corporate Finance (ME1311)': <Subscription: (id: 3, user: Ture Tureson, context: Corporate Finance (ME1311)(5)), as_email: False>, u'Electromagnetism and Waves (SK1114)': <Subscription: (id: 2, user: Ture Tureson, context: Electromagnetism and Waves (SK1114)(4)), as_email: False>}
> /etc/proj/social/social/users/views.py(310)exclude_examed_courses_from_other_courses()
-> examed_contexts = [course.context for course in examed_courses]
(Pdb) [course.context for course in examed_courses]
*** AttributeError: 'str' object has no attribute 'context'

Aucun commentaire:

Enregistrer un commentaire