Ticket #1186: test-query-rework.patch

File test-query-rework.patch, 7.3 KB (added by freakboy@…, 19 years ago)

Unit tests to validate patch for primary key lookup in parse_lookup

  • tests/modeltests/one_to_one/models.py

     
    6666
    6767>>> Restaurant.objects.get_object(place__id__exact=1)
    6868Demon Dogs the restaurant
     69>>> Restaurant.objects.get_object(pk=1)
     70Demon Dogs the restaurant
     71>>> Restaurant.objects.get_object(place__exact=1)
     72Demon Dogs the restaurant
     73>>> Restaurant.objects.get_object(place__pk=1)
     74Demon Dogs the restaurant
    6975>>> Restaurant.objects.get_object(place__name__startswith="Demon")
    7076Demon Dogs the restaurant
    71 >>> Restaurant.objects.get_object(pk=1)
    72 Demon Dogs the restaurant
    7377
     78>>> Place.objects.get_object(id__exact=1)
     79Demon Dogs the place
     80>>> Place.objects.get_object(pk=1)
     81Demon Dogs the place
     82>>> Place.objects.get_object(restaurants__place__exact=1)
     83Demon Dogs the place
     84>>> Place.objects.get_object(restaurants__pk=1)
     85Demon Dogs the place
     86
    7487# Add a Waiter to the Restaurant.
    7588>>> w = r.add_waiter(name='Joe')
    7689>>> w.save()
    7790>>> w
    7891Joe the waiter at Demon Dogs the restaurant
    7992
     93# Query the waiters
     94>>> Waiter.objects.get_list(restaurant__place__exact=1)
     95[Joe the waiter at Demon Dogs the restaurant]
     96>>> Waiter.objects.get_list(restaurant__pk=1)
     97[Joe the waiter at Demon Dogs the restaurant]
     98>>> Waiter.objects.get_list(id__exact=1)
     99[Joe the waiter at Demon Dogs the restaurant]
     100>>> Waiter.objects.get_list(pk=1)
     101[Joe the waiter at Demon Dogs the restaurant]
     102
     103# Delete the restaurant; the waiter should also be removed
    80104>>> r = Restaurant.objects.get_object(pk=1)
    81105>>> r.delete()
    82106"""
  • tests/modeltests/many_to_many/models.py

     
    6868[Django lets you build Web apps easily, NASA uses Python]
    6969
    7070# We can perform kwarg queries across m2m relationships
     71>>> Article.objects.get_list(publications__id__exact=1)
     72[Django lets you build Web apps easily, NASA uses Python]
    7173>>> Article.objects.get_list(publications__pk=1)
    7274[Django lets you build Web apps easily, NASA uses Python]
    7375
     
    7880[NASA uses Python]
    7981
    8082# Reverse m2m queries (i.e., start at the table that doesn't have a ManyToManyField)
     83>>> Publication.objects.get_list(id__exact=1)
     84[The Python Journal]
     85>>> Publication.objects.get_list(pk=1)
     86[The Python Journal]
     87
    8188>>> Publication.objects.get_list(articles__headline__startswith="NASA")
    8289[The Python Journal, Science News, Science Weekly]
    8390
     91>>> Publication.objects.get_list(articles__id__exact=1)
     92[The Python Journal]
     93
    8494>>> Publication.objects.get_list(articles__pk=1)
    8595[The Python Journal]
    8696
  • tests/modeltests/custom_pk/models.py

     
    4747    ...
    4848DoesNotExist: Employee does not exist for {'pk': 'foo'}
    4949
     50# Use the name of the primary key, rather than pk.
     51>>> Employee.objects.get_object(employee_code__exact='ABC123')
     52Dan Jones
     53
    5054# Fran got married and changed her last name.
    5155>>> fran = Employee.objects.get_object(pk='XYZ456')
    5256>>> fran.last_name = 'Jones'
     
    6670[Sears]
    6771>>> Business.objects.get_in_bulk(['Sears'])
    6872{'Sears': Sears}
     73
     74>>> Business.objects.get_list(name__exact='Sears')
     75[Sears]
     76>>> Business.objects.get_list(pk='Sears')
     77[Sears]
     78
     79# Queries across tables, involving primary key
     80>>> Employee.objects.get_list(businesses__name__exact='Sears')
     81[Dan Jones, Fran Jones]
     82>>> Employee.objects.get_list(businesses__pk='Sears')
     83[Dan Jones, Fran Jones]
     84
     85>>> Business.objects.get_list(employees__employee_code__exact='ABC123')
     86[Sears]
     87>>> Business.objects.get_list(employees__pk='ABC123')
     88[Sears]
     89>>> Business.objects.get_list(employees__first_name__startswith='Fran')
     90[Sears]
     91
    6992"""
  • tests/modeltests/many_to_one/models.py

     
    2222    def __repr__(self):
    2323        return self.headline
    2424
     25
    2526API_TESTS = """
    2627# Create a Reporter.
    2728>>> r = Reporter(first_name='John', last_name='Smith', email='john@example.com')
     
    6061>>> r.get_article_count()
    61622
    6263
     64# Get articles by id
     65>>> Article.objects.get_list(id__exact=1)
     66[This is a test]
     67>>> Article.objects.get_list(pk=1)
     68[This is a test]
     69
     70# Query on an article property
     71>>> Article.objects.get_list(headline__startswith='This')
     72[This is a test]
     73
    6374# The API automatically follows relationships as far as you need.
    6475# Use double underscores to separate relationships.
    6576# This works as many levels deep as you want. There's no limit.
     
    8394# Find all Articles for the Reporter whose ID is 1.
    8495>>> Article.objects.get_list(reporter__id__exact=1, order_by=['pub_date'])
    8596[This is a test, John's second story]
     97>>> Article.objects.get_list(reporter__pk=1, order_by=['pub_date'])
     98[This is a test, John's second story]
    8699
    87 # Note you need two underscores between "reporter" and "id" -- not one.
     100# You need two underscores between "reporter" and "id" -- not one.
    88101>>> Article.objects.get_list(reporter_id__exact=1)
    89102Traceback (most recent call last):
    90103    ...
    91 TypeError: got unexpected keyword argument 'reporter_id__exact'
     104TypeError: Cannot resolve keyword 'reporter_id' into field
    92105
     106# You need to specify a comparison clause
     107>>> Article.objects.get_list(reporter_id=1)
     108Traceback (most recent call last):
     109    ...
     110TypeError: Cannot parse keyword query 'reporter_id'
     111
    93112# "pk" shortcut syntax works in a related context, too.
    94113>>> Article.objects.get_list(reporter__pk=1, order_by=['pub_date'])
    95114[This is a test, John's second story]
     
    109128>>> a4.get_reporter()
    110129John Smith
    111130
     131# Reporters can be queried
     132>>> Reporter.objects.get_list(id__exact=1)
     133[John Smith]
     134>>> Reporter.objects.get_list(pk=1)
     135[John Smith]
     136>>> Reporter.objects.get_list(first_name__startswith='John')
     137[John Smith]
     138
     139# Reporters can query in opposite direction of ForeignKey definition
     140>>> Reporter.objects.get_list(articles__id__exact=1)
     141[John Smith]
     142>>> Reporter.objects.get_list(articles__pk=1)
     143[John Smith]
     144>>> Reporter.objects.get_list(articles__headline__startswith='This')
     145[John Smith, John Smith, John Smith]
     146>>> Reporter.objects.get_list(articles__headline__startswith='This', distinct=True)
     147[John Smith]
     148
     149# Queries can go round in circles.
     150>>> Reporter.objects.get_list(articles__reporter__first_name__startswith='John')
     151[John Smith, John Smith, John Smith, John Smith]
     152>>> Reporter.objects.get_list(articles__reporter__first_name__startswith='John', distinct=True)
     153[John Smith]
     154
    112155"""
  • tests/modeltests/custom_columns/models.py

     
    3535>>> Person.objects.get_list(firstname__exact='John')
    3636Traceback (most recent call last):
    3737    ...
    38 TypeError: got unexpected keyword argument 'firstname__exact'
     38TypeError: Cannot resolve keyword 'firstname' into field
    3939
    4040>>> p = Person.objects.get_object(last_name__exact='Smith')
    4141>>> p.first_name
Back to Top