diff options
| author | Joseph Kocherhans <joseph@jkocherhans.com> | 2006-06-19 15:23:57 +0000 |
|---|---|---|
| committer | Joseph Kocherhans <joseph@jkocherhans.com> | 2006-06-19 15:23:57 +0000 |
| commit | adf4b9311d5d64a2bdd58da50271c121ea22e397 (patch) | |
| tree | a69b3b023595cf1ce67a14c4c1ecd3290d94088e /tests/modeltests | |
| parent | e976ed1f7910fad03704f88853c5c5b36cbab134 (diff) | |
multi-auth: Merged to [3151]archive/attic/multi-auth
git-svn-id: http://code.djangoproject.com/svn/django/branches/multi-auth@3152 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'tests/modeltests')
| -rw-r--r-- | tests/modeltests/custom_pk/models.py | 3 | ||||
| -rw-r--r-- | tests/modeltests/empty/__init__.py | 0 | ||||
| -rw-r--r-- | tests/modeltests/empty/models.py | 24 | ||||
| -rw-r--r-- | tests/modeltests/generic_relations/__init__.py | 0 | ||||
| -rw-r--r-- | tests/modeltests/generic_relations/models.py | 108 | ||||
| -rw-r--r-- | tests/modeltests/get_or_create/__init__.py | 0 | ||||
| -rw-r--r-- | tests/modeltests/get_or_create/models.py | 52 | ||||
| -rw-r--r-- | tests/modeltests/invalid_models/models.py | 2 | ||||
| -rw-r--r-- | tests/modeltests/properties/models.py | 12 |
9 files changed, 199 insertions, 2 deletions
diff --git a/tests/modeltests/custom_pk/models.py b/tests/modeltests/custom_pk/models.py index 6193852adf..f7b790ca21 100644 --- a/tests/modeltests/custom_pk/models.py +++ b/tests/modeltests/custom_pk/models.py @@ -8,7 +8,8 @@ this behavior by explicitly adding ``primary_key=True`` to a field. from django.db import models class Employee(models.Model): - employee_code = models.CharField(maxlength=10, primary_key=True) + employee_code = models.CharField(maxlength=10, primary_key=True, + db_column = 'code') first_name = models.CharField(maxlength=20) last_name = models.CharField(maxlength=20) class Meta: diff --git a/tests/modeltests/empty/__init__.py b/tests/modeltests/empty/__init__.py new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/tests/modeltests/empty/__init__.py diff --git a/tests/modeltests/empty/models.py b/tests/modeltests/empty/models.py new file mode 100644 index 0000000000..c50878398d --- /dev/null +++ b/tests/modeltests/empty/models.py @@ -0,0 +1,24 @@ +""" +Empty model tests + +These test that things behave sensibly for the rare corner-case of a model with +no fields. +""" + +from django.db import models + +class Empty(models.Model): + pass + +API_TESTS = """ +>>> m = Empty() +>>> m.id +>>> m.save() +>>> m2 = Empty() +>>> m2.save() +>>> len(Empty.objects.all()) +2 +>>> m.id is not None +True + +""" diff --git a/tests/modeltests/generic_relations/__init__.py b/tests/modeltests/generic_relations/__init__.py new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/tests/modeltests/generic_relations/__init__.py diff --git a/tests/modeltests/generic_relations/models.py b/tests/modeltests/generic_relations/models.py new file mode 100644 index 0000000000..a9a775ad6e --- /dev/null +++ b/tests/modeltests/generic_relations/models.py @@ -0,0 +1,108 @@ +""" +33. Generic relations + +Generic relations let an object have a foreign key to any object through a +content-type/object-id field. A generic foreign key can point to any object, +be it animal, vegetable, or mineral. + +The cannonical example is tags (although this example implementation is *far* +from complete). +""" + +from django.db import models +from django.contrib.contenttypes.models import ContentType + +class TaggedItem(models.Model): + """A tag on an item.""" + tag = models.SlugField() + content_type = models.ForeignKey(ContentType) + object_id = models.PositiveIntegerField() + + content_object = models.GenericForeignKey() + + class Meta: + ordering = ["tag"] + + def __str__(self): + return self.tag + +class Animal(models.Model): + common_name = models.CharField(maxlength=150) + latin_name = models.CharField(maxlength=150) + + tags = models.GenericRelation(TaggedItem) + + def __str__(self): + return self.common_name + +class Vegetable(models.Model): + name = models.CharField(maxlength=150) + is_yucky = models.BooleanField(default=True) + + tags = models.GenericRelation(TaggedItem) + + def __str__(self): + return self.name + +class Mineral(models.Model): + name = models.CharField(maxlength=150) + hardness = models.PositiveSmallIntegerField() + + # note the lack of an explicit GenericRelation here... + + def __str__(self): + return self.name + +API_TESTS = """ +# Create the world in 7 lines of code... +>>> lion = Animal(common_name="Lion", latin_name="Panthera leo") +>>> platypus = Animal(common_name="Platypus", latin_name="Ornithorhynchus anatinus") +>>> eggplant = Vegetable(name="Eggplant", is_yucky=True) +>>> bacon = Vegetable(name="Bacon", is_yucky=False) +>>> quartz = Mineral(name="Quartz", hardness=7) +>>> for o in (lion, platypus, eggplant, bacon, quartz): +... o.save() + +# Objects with declared GenericRelations can be tagged directly -- the API +# mimics the many-to-many API +>>> lion.tags.create(tag="yellow") +<TaggedItem: yellow> +>>> lion.tags.create(tag="hairy") +<TaggedItem: hairy> +>>> bacon.tags.create(tag="fatty") +<TaggedItem: fatty> +>>> bacon.tags.create(tag="salty") +<TaggedItem: salty> + +>>> lion.tags.all() +[<TaggedItem: hairy>, <TaggedItem: yellow>] +>>> bacon.tags.all() +[<TaggedItem: fatty>, <TaggedItem: salty>] + +# You can easily access the content object like a foreign key +>>> t = TaggedItem.objects.get(tag="salty") +>>> t.content_object +<Vegetable: Bacon> + +# Recall that the Mineral class doesn't have an explicit GenericRelation +# defined. That's OK since you can create TaggedItems explicitally. +>>> tag1 = TaggedItem(content_object=quartz, tag="shiny") +>>> tag2 = TaggedItem(content_object=quartz, tag="clearish") +>>> tag1.save() +>>> tag2.save() + +# However, not having the convience takes a small toll when it comes +# to do lookups +>>> from django.contrib.contenttypes.models import ContentType +>>> ctype = ContentType.objects.get_for_model(quartz) +>>> TaggedItem.objects.filter(content_type__pk=ctype.id, object_id=quartz.id) +[<TaggedItem: clearish>, <TaggedItem: shiny>] + +# You can set a generic foreign key in the way you'd expect +>>> tag1.content_object = platypus +>>> tag1.save() +>>> platypus.tags.all() +[<TaggedItem: shiny>] +>>> TaggedItem.objects.filter(content_type__pk=ctype.id, object_id=quartz.id) +[<TaggedItem: clearish>] +"""
\ No newline at end of file diff --git a/tests/modeltests/get_or_create/__init__.py b/tests/modeltests/get_or_create/__init__.py new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/tests/modeltests/get_or_create/__init__.py diff --git a/tests/modeltests/get_or_create/models.py b/tests/modeltests/get_or_create/models.py new file mode 100644 index 0000000000..10a8721afc --- /dev/null +++ b/tests/modeltests/get_or_create/models.py @@ -0,0 +1,52 @@ +""" +32. get_or_create() + +get_or_create() does what it says: it tries to look up an object with the given +parameters. If an object isn't found, it creates one with the given parameters. +""" + +from django.db import models + +class Person(models.Model): + first_name = models.CharField(maxlength=100) + last_name = models.CharField(maxlength=100) + birthday = models.DateField() + + def __str__(self): + return '%s %s' % (self.first_name, self.last_name) + +API_TESTS = """ +# Acting as a divine being, create an Person. +>>> from datetime import date +>>> p = Person(first_name='John', last_name='Lennon', birthday=date(1940, 10, 9)) +>>> p.save() + +# Only one Person is in the database at this point. +>>> Person.objects.count() +1 + +# get_or_create() a person with similar first names. +>>> p, created = Person.objects.get_or_create(first_name='John', last_name='Lennon', defaults={'birthday': date(1940, 10, 9)}) + +# get_or_create() didn't have to create an object. +>>> created +False + +# There's still only one Person in the database. +>>> Person.objects.count() +1 + +# get_or_create() a Person with a different name. +>>> p, created = Person.objects.get_or_create(first_name='George', last_name='Harrison', defaults={'birthday': date(1943, 2, 25)}) +>>> created +True +>>> Person.objects.count() +2 + +# If we execute the exact same statement, it won't create a Person. +>>> p, created = Person.objects.get_or_create(first_name='George', last_name='Harrison', defaults={'birthday': date(1943, 2, 25)}) +>>> created +False +>>> Person.objects.count() +2 +""" diff --git a/tests/modeltests/invalid_models/models.py b/tests/modeltests/invalid_models/models.py index 127cc9e0d2..1720dd96d3 100644 --- a/tests/modeltests/invalid_models/models.py +++ b/tests/modeltests/invalid_models/models.py @@ -74,7 +74,7 @@ invalid_models.fielderrors: "floatfield": FloatFields require a "decimal_places" invalid_models.fielderrors: "floatfield": FloatFields require a "max_digits" attribute. invalid_models.fielderrors: "filefield": FileFields require an "upload_to" attribute. invalid_models.fielderrors: "prepopulate": prepopulate_from should be a list or tuple. -invalid_models.fielderrors: "choices": "choices" should be either a tuple or list. +invalid_models.fielderrors: "choices": "choices" should be iterable (e.g., a tuple or list). invalid_models.fielderrors: "choices2": "choices" should be a sequence of two-tuples. invalid_models.fielderrors: "choices2": "choices" should be a sequence of two-tuples. invalid_models.fielderrors: "index": "db_index" should be either None, True or False. diff --git a/tests/modeltests/properties/models.py b/tests/modeltests/properties/models.py index e9d8da9594..3b0133bf8a 100644 --- a/tests/modeltests/properties/models.py +++ b/tests/modeltests/properties/models.py @@ -12,8 +12,14 @@ class Person(models.Model): def _get_full_name(self): return "%s %s" % (self.first_name, self.last_name) + + def _set_full_name(self, combined_name): + self.first_name, self.last_name = combined_name.split(' ', 1) + full_name = property(_get_full_name) + full_name_2 = property(_get_full_name, _set_full_name) + API_TESTS = """ >>> a = Person(first_name='John', last_name='Lennon') >>> a.save() @@ -25,4 +31,10 @@ API_TESTS = """ Traceback (most recent call last): ... AttributeError: can't set attribute + +# But "full_name_2" has, and it can be used to initialise the class. +>>> a2 = Person(full_name_2 = 'Paul McCartney') +>>> a2.save() +>>> a2.first_name +'Paul' """ |
