mirror of
				https://github.com/django/django.git
				synced 2025-10-31 09:41:08 +00:00 
			
		
		
		
	magic-removal: Updated model tests for new syntax: 'basic', 'custom_managers' and 'custom_columns'
git-svn-id: http://code.djangoproject.com/svn/django/branches/magic-removal@2151 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
		| @@ -11,8 +11,9 @@ class Article(models.Model): | |||||||
|     pub_date = models.DateTimeField() |     pub_date = models.DateTimeField() | ||||||
|  |  | ||||||
| API_TESTS = """ | API_TESTS = """ | ||||||
|  |  | ||||||
| # No articles are in the system yet. | # No articles are in the system yet. | ||||||
| >>> Article.objects.get_list() | >>> list(Article.objects) | ||||||
| [] | [] | ||||||
|  |  | ||||||
| # Create an Article. | # Create an Article. | ||||||
| @@ -37,52 +38,58 @@ datetime.datetime(2005, 7, 28, 0, 0) | |||||||
| >>> a.headline = 'Area woman programs in Python' | >>> a.headline = 'Area woman programs in Python' | ||||||
| >>> a.save() | >>> a.save() | ||||||
|  |  | ||||||
| # get_list() displays all the articles in the database. Note that the article | # Listing objects displays all the articles in the database. Note that the article | ||||||
| # is represented by "<Article object>", because we haven't given the Article | # is represented by "<Article object>", because we haven't given the Article | ||||||
| # model a __repr__() method. | # model a __repr__() method. | ||||||
| >>> Article.objects.get_list() | >>> list(Article.objects) | ||||||
| [<Article object>] | [<Article object>] | ||||||
|  |  | ||||||
| # Django provides a rich database lookup API that's entirely driven by | # Django provides a rich database lookup API that's entirely driven by | ||||||
| # keyword arguments. | # keyword arguments. | ||||||
| >>> Article.objects.get_object(id__exact=1) | >>> Article.objects.get(id__exact=1) | ||||||
| <Article object> | <Article object> | ||||||
| >>> Article.objects.get_object(headline__startswith='Area woman') | >>> Article.objects.get(headline__startswith='Area woman') | ||||||
| <Article object> | <Article object> | ||||||
| >>> Article.objects.get_object(pub_date__year=2005) | >>> Article.objects.get(pub_date__year=2005) | ||||||
| <Article object> | <Article object> | ||||||
| >>> Article.objects.get_object(pub_date__year=2005, pub_date__month=7) | >>> Article.objects.get(pub_date__year=2005, pub_date__month=7) | ||||||
| <Article object> | <Article object> | ||||||
| >>> Article.objects.get_object(pub_date__year=2005, pub_date__month=7, pub_date__day=28) | >>> Article.objects.get(pub_date__year=2005, pub_date__month=7, pub_date__day=28) | ||||||
| <Article object> | <Article object> | ||||||
|  |  | ||||||
| >>> Article.objects.get_list(pub_date__year=2005) | # You can omit __exact if you want | ||||||
|  | >>> Article.objects.get(id=1) | ||||||
|  | <Article object> | ||||||
|  | >>> Article.objects.get(headline='Area woman programs in Python') | ||||||
|  | <Article object> | ||||||
|  |  | ||||||
|  | >>> list(Article.objects.filter(pub_date__year=2005)) | ||||||
| [<Article object>] | [<Article object>] | ||||||
| >>> Article.objects.get_list(pub_date__year=2004) | >>> list(Article.objects.filter(pub_date__year=2004)) | ||||||
| [] | [] | ||||||
| >>> Article.objects.get_list(pub_date__year=2005, pub_date__month=7) | >>> list(Article.objects.filter(pub_date__year=2005, pub_date__month=7)) | ||||||
| [<Article object>] | [<Article object>] | ||||||
|  |  | ||||||
| # Django raises an ArticleDoesNotExist exception for get_object() | # Django raises an ArticleDoesNotExist exception for get() | ||||||
| >>> Article.objects.get_object(id__exact=2) | >>> Article.objects.get(id__exact=2) | ||||||
| Traceback (most recent call last): | Traceback (most recent call last): | ||||||
|     ... |     ... | ||||||
| DoesNotExist: Article does not exist for {'id__exact': 2} | DoesNotExist: Article does not exist for {'id__exact': 2} | ||||||
|  |  | ||||||
| >>> Article.objects.get_object(pub_date__year=2005, pub_date__month=8) | >>> Article.objects.get(pub_date__year=2005, pub_date__month=8) | ||||||
| Traceback (most recent call last): | Traceback (most recent call last): | ||||||
|     ... |     ... | ||||||
| DoesNotExist: Article does not exist for ... | DoesNotExist: Article does not exist for ... | ||||||
|  |  | ||||||
| # Lookup by a primary key is the most common case, so Django provides a | # Lookup by a primary key is the most common case, so Django provides a | ||||||
| # shortcut for primary-key exact lookups. | # shortcut for primary-key exact lookups. | ||||||
| # The following is identical to articles.get_object(id__exact=1). | # The following is identical to articles.get(id__exact=1). | ||||||
| >>> Article.objects.get_object(pk=1) | >>> Article.objects.get(pk=1) | ||||||
| <Article object> | <Article object> | ||||||
|  |  | ||||||
| # Model instances of the same type and same ID are considered equal. | # Model instances of the same type and same ID are considered equal. | ||||||
| >>> a = Article.objects.get_object(pk=1) | >>> a = Article.objects.get(pk=1) | ||||||
| >>> b = Article.objects.get_object(pk=1) | >>> b = Article.objects.get(pk=1) | ||||||
| >>> a == b | >>> a == b | ||||||
| True | True | ||||||
|  |  | ||||||
| @@ -139,12 +146,12 @@ TypeError: 'foo' is an invalid keyword argument for this function | |||||||
| # give it. | # give it. | ||||||
| >>> a7 = Article(headline='Article 7', pub_date=datetime(2005, 7, 31, 12, 30)) | >>> a7 = Article(headline='Article 7', pub_date=datetime(2005, 7, 31, 12, 30)) | ||||||
| >>> a7.save() | >>> a7.save() | ||||||
| >>> Article.objects.get_object(id__exact=7).pub_date | >>> Article.objects.get(id__exact=7).pub_date | ||||||
| datetime.datetime(2005, 7, 31, 12, 30) | datetime.datetime(2005, 7, 31, 12, 30) | ||||||
|  |  | ||||||
| >>> a8 = Article(headline='Article 8', pub_date=datetime(2005, 7, 31, 12, 30, 45)) | >>> a8 = Article(headline='Article 8', pub_date=datetime(2005, 7, 31, 12, 30, 45)) | ||||||
| >>> a8.save() | >>> a8.save() | ||||||
| >>> Article.objects.get_object(id__exact=8).pub_date | >>> Article.objects.get(id__exact=8).pub_date | ||||||
| datetime.datetime(2005, 7, 31, 12, 30, 45) | datetime.datetime(2005, 7, 31, 12, 30, 45) | ||||||
| >>> a8.id | >>> a8.id | ||||||
| 8L | 8L | ||||||
| @@ -160,40 +167,65 @@ datetime.datetime(2005, 7, 31, 12, 30, 45) | |||||||
|  |  | ||||||
| >>> a7 == a8 | >>> a7 == a8 | ||||||
| False | False | ||||||
| >>> a8 == Article.objects.get_object(id__exact=8) | >>> a8 == Article.objects.get(id__exact=8) | ||||||
| True | True | ||||||
| >>> a7 != a8 | >>> a7 != a8 | ||||||
| True | True | ||||||
| >>> Article.objects.get_object(id__exact=8) != Article.objects.get_object(id__exact=7) | >>> Article.objects.get(id__exact=8) != Article.objects.get(id__exact=7) | ||||||
| True | True | ||||||
| >>> Article.objects.get_object(id__exact=8) == Article.objects.get_object(id__exact=7) | >>> Article.objects.get(id__exact=8) == Article.objects.get(id__exact=7) | ||||||
| False | False | ||||||
|  |  | ||||||
| >>> Article.objects.get_pub_date_list('year') | ## TODO - what should these be converted to? | ||||||
| [datetime.datetime(2005, 1, 1, 0, 0)] |  | ||||||
| >>> Article.objects.get_pub_date_list('month') |  | ||||||
| [datetime.datetime(2005, 7, 1, 0, 0)] |  | ||||||
| >>> Article.objects.get_pub_date_list('day') |  | ||||||
| [datetime.datetime(2005, 7, 28, 0, 0), datetime.datetime(2005, 7, 29, 0, 0), datetime.datetime(2005, 7, 30, 0, 0), datetime.datetime(2005, 7, 31, 0, 0)] |  | ||||||
| >>> Article.objects.get_pub_date_list('day', order='ASC') |  | ||||||
| [datetime.datetime(2005, 7, 28, 0, 0), datetime.datetime(2005, 7, 29, 0, 0), datetime.datetime(2005, 7, 30, 0, 0), datetime.datetime(2005, 7, 31, 0, 0)] |  | ||||||
| >>> Article.objects.get_pub_date_list('day', order='DESC') |  | ||||||
| [datetime.datetime(2005, 7, 31, 0, 0), datetime.datetime(2005, 7, 30, 0, 0), datetime.datetime(2005, 7, 29, 0, 0), datetime.datetime(2005, 7, 28, 0, 0)] |  | ||||||
|  |  | ||||||
| # Try some bad arguments to __get_date_list | ##>>> Article.objects.get_pub_date_list('year') | ||||||
| >>> Article.objects.get_pub_date_list('badarg') | ##[datetime.datetime(2005, 1, 1, 0, 0)] | ||||||
| Traceback (most recent call last): | ##>>> Article.objects.get_pub_date_list('month') | ||||||
|     ... | ##[datetime.datetime(2005, 7, 1, 0, 0)] | ||||||
| AssertionError: 'kind' must be one of 'year', 'month' or 'day'. | ##>>> Article.objects.get_pub_date_list('day') | ||||||
| >>> Article.objects.get_pub_date_list(order='ASC') | ##[datetime.datetime(2005, 7, 28, 0, 0), datetime.datetime(2005, 7, 29, 0, 0), datetime.datetime(2005, 7, 30, 0, 0), datetime.datetime(2005, 7, 31, 0, 0)] | ||||||
| Traceback (most recent call last): | ##>>> Article.objects.get_pub_date_list('day', order='ASC') | ||||||
|     ... | ##[datetime.datetime(2005, 7, 28, 0, 0), datetime.datetime(2005, 7, 29, 0, 0), datetime.datetime(2005, 7, 30, 0, 0), datetime.datetime(2005, 7, 31, 0, 0)] | ||||||
| TypeError: __get_date_list() takes at least 3 non-keyword arguments (2 given) | ##>>> Article.objects.get_pub_date_list('day', order='DESC') | ||||||
|  | ##[datetime.datetime(2005, 7, 31, 0, 0), datetime.datetime(2005, 7, 30, 0, 0), datetime.datetime(2005, 7, 29, 0, 0), datetime.datetime(2005, 7, 28, 0, 0)] | ||||||
|  | ## | ||||||
|  | ### Try some bad arguments to __get_date_list | ||||||
|  | ##>>> Article.objects.get_pub_date_list('badarg') | ||||||
|  | ##Traceback (most recent call last): | ||||||
|  | ##    ... | ||||||
|  | ##AssertionError: 'kind' must be one of 'year', 'month' or 'day'. | ||||||
|  | ##>>> Article.objects.get_pub_date_list(order='ASC') | ||||||
|  | ##Traceback (most recent call last): | ||||||
|  | ##    ... | ||||||
|  | ##TypeError: __get_date_list() takes at least 3 non-keyword arguments (2 given) | ||||||
|  |  | ||||||
|  | # You can combine queries with & and | | ||||||
|  | >>> s1 = Article.objects.filter(id__exact=1) | ||||||
|  | >>> s2 = Article.objects.filter(id__exact=2) | ||||||
|  | >>> tmp = [a.id for a in list(s1 | s2)] | ||||||
|  | >>> tmp.sort() | ||||||
|  | >>> tmp | ||||||
|  | [1L, 2L] | ||||||
|  | >>> list(s1 & s2) | ||||||
|  | [] | ||||||
|  |  | ||||||
|  | # You can get the number of objects like this: | ||||||
|  | >>> len(Article.objects.filter(id__exact=1)) | ||||||
|  | 1 | ||||||
|  |  | ||||||
|  | # You can get items using index and slice notation: | ||||||
|  | >>> Article.objects[0] | ||||||
|  | <Article object> | ||||||
|  | >>> Article.objects[1:2] | ||||||
|  | [<Article object>, <Article object>] | ||||||
|  | >>> s3 = Article.objects.filter(id__exact=3) | ||||||
|  | >>> (s1 | s2 | s3)[::2] | ||||||
|  | [<Article object>, <Article object>] | ||||||
|  |  | ||||||
|  |  | ||||||
| # An Article instance doesn't have access to the "objects" attribute. | # An Article instance doesn't have access to the "objects" attribute. | ||||||
| # That is only available as a class method. | # That is only available as a class method. | ||||||
| >>> a7.objects.get_list() | >>> list(a7.objects) | ||||||
| Traceback (most recent call last): | Traceback (most recent call last): | ||||||
|     ... |     ... | ||||||
| AttributeError: Manager isn't accessible via Article instances | AttributeError: Manager isn't accessible via Article instances | ||||||
| @@ -204,10 +236,10 @@ Traceback (most recent call last): | |||||||
| AttributeError: Manager isn't accessible via Article instances | AttributeError: Manager isn't accessible via Article instances | ||||||
|  |  | ||||||
| # Bulk delete test: How many objects before and after the delete? | # Bulk delete test: How many objects before and after the delete? | ||||||
| >>> Article.objects.get_count() | >>> Article.objects.count() | ||||||
| 8L | 8L | ||||||
| >>> Article.objects.delete(id__lte=4) | >>> Article.objects.delete(id__lte=4) | ||||||
| >>> Article.objects.get_count() | >>> Article.objects.count() | ||||||
| 4L | 4L | ||||||
|  |  | ||||||
| >>> Article.objects.delete() | >>> Article.objects.delete() | ||||||
| @@ -216,7 +248,7 @@ Traceback (most recent call last): | |||||||
| TypeError: SAFETY MECHANISM: Specify DELETE_ALL=True if you actually want to delete all data. | TypeError: SAFETY MECHANISM: Specify DELETE_ALL=True if you actually want to delete all data. | ||||||
|  |  | ||||||
| >>> Article.objects.delete(DELETE_ALL=True) | >>> Article.objects.delete(DELETE_ALL=True) | ||||||
| >>> Article.objects.get_count() | >>> Article.objects.count() | ||||||
| 0L | 0L | ||||||
|  |  | ||||||
| """ | """ | ||||||
| @@ -230,7 +262,7 @@ if building_docs or settings.DATABASE_ENGINE == 'postgresql': | |||||||
| # In PostgreSQL, microsecond-level precision is available. | # In PostgreSQL, microsecond-level precision is available. | ||||||
| >>> a9 = Article(headline='Article 9', pub_date=datetime(2005, 7, 31, 12, 30, 45, 180)) | >>> a9 = Article(headline='Article 9', pub_date=datetime(2005, 7, 31, 12, 30, 45, 180)) | ||||||
| >>> a9.save() | >>> a9.save() | ||||||
| >>> Article.objects.get_object(id__exact=9).pub_date | >>> Article.objects.get(id__exact=9).pub_date | ||||||
| datetime.datetime(2005, 7, 31, 12, 30, 45, 180) | datetime.datetime(2005, 7, 31, 12, 30, 45, 180) | ||||||
| """ | """ | ||||||
|  |  | ||||||
| @@ -240,7 +272,7 @@ if building_docs or settings.DATABASE_ENGINE == 'mysql': | |||||||
| # microsecond-level precision once the data is saved. | # microsecond-level precision once the data is saved. | ||||||
| >>> a9 = Article(headline='Article 9', pub_date=datetime(2005, 7, 31, 12, 30, 45, 180)) | >>> a9 = Article(headline='Article 9', pub_date=datetime(2005, 7, 31, 12, 30, 45, 180)) | ||||||
| >>> a9.save() | >>> a9.save() | ||||||
| >>> Article.objects.get_object(id__exact=9).pub_date | >>> Article.objects.get(id__exact=9).pub_date | ||||||
| datetime.datetime(2005, 7, 31, 12, 30, 45) | datetime.datetime(2005, 7, 31, 12, 30, 45) | ||||||
| """ | """ | ||||||
|  |  | ||||||
| @@ -249,7 +281,7 @@ API_TESTS += """ | |||||||
| # You can manually specify the primary key when creating a new objet | # You can manually specify the primary key when creating a new objet | ||||||
| >>> a101 = Article(id=101, headline='Article 101', pub_date=datetime(2005, 7, 31, 12, 30, 45)) | >>> a101 = Article(id=101, headline='Article 101', pub_date=datetime(2005, 7, 31, 12, 30, 45)) | ||||||
| >>> a101.save() | >>> a101.save() | ||||||
| >>> a101 = Article.objects.get_object(pk=101) | >>> a101 = Article.objects.get(pk=101) | ||||||
| >>> a101.headline | >>> a101.headline | ||||||
| 'Article 101' | 'Article 101' | ||||||
| """ | """ | ||||||
|   | |||||||
| @@ -23,21 +23,21 @@ API_TESTS = """ | |||||||
| >>> p.id | >>> p.id | ||||||
| 1 | 1 | ||||||
|  |  | ||||||
| >>> Person.objects.get_list() | >>> list(Person.objects) | ||||||
| [John Smith] | [John Smith] | ||||||
|  |  | ||||||
| >>> Person.objects.get_list(first_name__exact='John') | >>> list(Person.objects.filter(first_name__exact='John')) | ||||||
| [John Smith] | [John Smith] | ||||||
|  |  | ||||||
| >>> Person.objects.get_object(first_name__exact='John') | >>> Person.objects.get(first_name__exact='John') | ||||||
| John Smith | John Smith | ||||||
|  |  | ||||||
| >>> Person.objects.get_list(firstname__exact='John') | >>> list(Person.objects.filter(firstname__exact='John')) | ||||||
| Traceback (most recent call last): | Traceback (most recent call last): | ||||||
|     ... |     ... | ||||||
| TypeError: Cannot resolve keyword 'firstname' into field | TypeError: Cannot resolve keyword 'firstname' into field | ||||||
|  |  | ||||||
| >>> p = Person.objects.get_object(last_name__exact='Smith') | >>> p = Person.objects.get(last_name__exact='Smith') | ||||||
| >>> p.first_name | >>> p.first_name | ||||||
| 'John' | 'John' | ||||||
| >>> p.last_name | >>> p.last_name | ||||||
|   | |||||||
| @@ -8,7 +8,7 @@ from django.db import models | |||||||
|  |  | ||||||
| class PersonManager(models.Manager): | class PersonManager(models.Manager): | ||||||
|     def get_fun_people(self): |     def get_fun_people(self): | ||||||
|         return self.get_list(fun__exact=True) |         return list(self.filter(fun=True)) | ||||||
|  |  | ||||||
| class Person(models.Model): | class Person(models.Model): | ||||||
|     first_name = models.CharField(maxlength=30) |     first_name = models.CharField(maxlength=30) | ||||||
| @@ -22,9 +22,9 @@ class Person(models.Model): | |||||||
| # An example of providing a custom manager that isn't called "objects". | # An example of providing a custom manager that isn't called "objects". | ||||||
|  |  | ||||||
| class PublishedBookManager(models.Manager): | class PublishedBookManager(models.Manager): | ||||||
|     def get_list(self, **kwargs): |     def __init__(self): | ||||||
|         kwargs['is_published__exact'] = True |         super(PublishedBookManager, self).__init__() | ||||||
|         return models.Manager.get_list(self, **kwargs) |         self._set_core_filter(is_published__exact=True) | ||||||
|  |  | ||||||
| class Book(models.Model): | class Book(models.Model): | ||||||
|     title = models.CharField(maxlength=50) |     title = models.CharField(maxlength=50) | ||||||
| @@ -38,9 +38,9 @@ class Book(models.Model): | |||||||
| # An example of providing multiple custom managers. | # An example of providing multiple custom managers. | ||||||
|  |  | ||||||
| class FastCarManager(models.Manager): | class FastCarManager(models.Manager): | ||||||
|     def get_list(self, **kwargs): |     def __init__(self): | ||||||
|         kwargs['top_speed__gt'] = 150 |         super(FastCarManager, self).__init__() | ||||||
|         return models.Manager.get_list(self, **kwargs) |         self._set_core_filter(top_speed__gt=150) | ||||||
|  |  | ||||||
| class Car(models.Model): | class Car(models.Model): | ||||||
|     name = models.CharField(maxlength=10) |     name = models.CharField(maxlength=10) | ||||||
| @@ -72,20 +72,20 @@ Traceback (most recent call last): | |||||||
|     ... |     ... | ||||||
| AttributeError: type object 'Book' has no attribute 'objects' | AttributeError: type object 'Book' has no attribute 'objects' | ||||||
|  |  | ||||||
| >>> Book.published_objects.get_list() | >>> list(Book.published_objects) | ||||||
| [How to program] | [How to program] | ||||||
|  |  | ||||||
| >>> c1 = Car(name='Corvette', mileage=21, top_speed=180) | >>> c1 = Car(name='Corvette', mileage=21, top_speed=180) | ||||||
| >>> c1.save() | >>> c1.save() | ||||||
| >>> c2 = Car(name='Neon', mileage=31, top_speed=100) | >>> c2 = Car(name='Neon', mileage=31, top_speed=100) | ||||||
| >>> c2.save() | >>> c2.save() | ||||||
| >>> Car.cars.get_list(order_by=('name',)) | >>> list(Car.cars.order_by('name')) | ||||||
| [Corvette, Neon] | [Corvette, Neon] | ||||||
| >>> Car.fast_cars.get_list() | >>> list(Car.fast_cars) | ||||||
| [Corvette] | [Corvette] | ||||||
|  |  | ||||||
| # Each model class gets a "_default_manager" attribute, which is a reference | # Each model class gets a "_default_manager" attribute, which is a reference | ||||||
| # to the first manager defined in the class. In this case, it's "cars". | # to the first manager defined in the class. In this case, it's "cars". | ||||||
| >>> Car._default_manager.get_list(order_by=('name',)) | >>> list(Car._default_manager.order_by('name')) | ||||||
| [Corvette, Neon] | [Corvette, Neon] | ||||||
| """ | """ | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user