Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
D
django
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
django
Commits
25b2ce89
Kaydet (Commit)
25b2ce89
authored
Nis 21, 2014
tarafından
Aymeric Augustin
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Consolidated get_or_create tests.
üst
3f01e82c
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
75 additions
and
90 deletions
+75
-90
models.py
tests/get_or_create/models.py
+14
-8
tests.py
tests/get_or_create/tests.py
+61
-1
__init__.py
tests/get_or_create_regress/__init__.py
+0
-0
models.py
tests/get_or_create_regress/models.py
+0
-15
tests.py
tests/get_or_create_regress/tests.py
+0
-66
No files found.
tests/get_or_create/models.py
Dosyayı görüntüle @
25b2ce89
"""
33. 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
__future__
import
unicode_literals
from
__future__
import
unicode_literals
from
django.db
import
models
from
django.db
import
models
...
@@ -42,3 +34,17 @@ class Tag(models.Model):
...
@@ -42,3 +34,17 @@ class Tag(models.Model):
class
Thing
(
models
.
Model
):
class
Thing
(
models
.
Model
):
name
=
models
.
CharField
(
max_length
=
256
)
name
=
models
.
CharField
(
max_length
=
256
)
tags
=
models
.
ManyToManyField
(
Tag
)
tags
=
models
.
ManyToManyField
(
Tag
)
class
Publisher
(
models
.
Model
):
name
=
models
.
CharField
(
max_length
=
100
)
class
Author
(
models
.
Model
):
name
=
models
.
CharField
(
max_length
=
100
)
class
Book
(
models
.
Model
):
name
=
models
.
CharField
(
max_length
=
100
)
authors
=
models
.
ManyToManyField
(
Author
,
related_name
=
'books'
)
publisher
=
models
.
ForeignKey
(
Publisher
,
related_name
=
'books'
,
db_column
=
"publisher_id_column"
)
tests/get_or_create/tests.py
Dosyayı görüntüle @
25b2ce89
...
@@ -8,7 +8,8 @@ from django.db import IntegrityError, DatabaseError
...
@@ -8,7 +8,8 @@ from django.db import IntegrityError, DatabaseError
from
django.utils.encoding
import
DjangoUnicodeDecodeError
from
django.utils.encoding
import
DjangoUnicodeDecodeError
from
django.test
import
TestCase
,
TransactionTestCase
from
django.test
import
TestCase
,
TransactionTestCase
from
.models
import
DefaultPerson
,
Person
,
ManualPrimaryKeyTest
,
Profile
,
Tag
,
Thing
from
.models
import
(
DefaultPerson
,
Person
,
ManualPrimaryKeyTest
,
Profile
,
Tag
,
Thing
,
Publisher
,
Author
)
class
GetOrCreateTests
(
TestCase
):
class
GetOrCreateTests
(
TestCase
):
...
@@ -237,3 +238,62 @@ class UpdateOrCreateTests(TestCase):
...
@@ -237,3 +238,62 @@ class UpdateOrCreateTests(TestCase):
except
IntegrityError
:
except
IntegrityError
:
formatted_traceback
=
traceback
.
format_exc
()
formatted_traceback
=
traceback
.
format_exc
()
self
.
assertIn
(
'obj.save'
,
formatted_traceback
)
self
.
assertIn
(
'obj.save'
,
formatted_traceback
)
def
test_related
(
self
):
p
=
Publisher
.
objects
.
create
(
name
=
"Acme Publishing"
)
# Create a book through the publisher.
book
,
created
=
p
.
books
.
get_or_create
(
name
=
"The Book of Ed & Fred"
)
self
.
assertTrue
(
created
)
# The publisher should have one book.
self
.
assertEqual
(
p
.
books
.
count
(),
1
)
# Try get_or_create again, this time nothing should be created.
book
,
created
=
p
.
books
.
get_or_create
(
name
=
"The Book of Ed & Fred"
)
self
.
assertFalse
(
created
)
# And the publisher should still have one book.
self
.
assertEqual
(
p
.
books
.
count
(),
1
)
# Add an author to the book.
ed
,
created
=
book
.
authors
.
get_or_create
(
name
=
"Ed"
)
self
.
assertTrue
(
created
)
# The book should have one author.
self
.
assertEqual
(
book
.
authors
.
count
(),
1
)
# Try get_or_create again, this time nothing should be created.
ed
,
created
=
book
.
authors
.
get_or_create
(
name
=
"Ed"
)
self
.
assertFalse
(
created
)
# And the book should still have one author.
self
.
assertEqual
(
book
.
authors
.
count
(),
1
)
# Add a second author to the book.
fred
,
created
=
book
.
authors
.
get_or_create
(
name
=
"Fred"
)
self
.
assertTrue
(
created
)
# The book should have two authors now.
self
.
assertEqual
(
book
.
authors
.
count
(),
2
)
# Create an Author not tied to any books.
Author
.
objects
.
create
(
name
=
"Ted"
)
# There should be three Authors in total. The book object should have two.
self
.
assertEqual
(
Author
.
objects
.
count
(),
3
)
self
.
assertEqual
(
book
.
authors
.
count
(),
2
)
# Try creating a book through an author.
_
,
created
=
ed
.
books
.
get_or_create
(
name
=
"Ed's Recipes"
,
publisher
=
p
)
self
.
assertTrue
(
created
)
# Now Ed has two Books, Fred just one.
self
.
assertEqual
(
ed
.
books
.
count
(),
2
)
self
.
assertEqual
(
fred
.
books
.
count
(),
1
)
# Use the publisher's primary key value instead of a model instance.
_
,
created
=
ed
.
books
.
get_or_create
(
name
=
'The Great Book of Ed'
,
publisher_id
=
p
.
id
)
self
.
assertTrue
(
created
)
# Try get_or_create again, this time nothing should be created.
_
,
created
=
ed
.
books
.
get_or_create
(
name
=
'The Great Book of Ed'
,
publisher_id
=
p
.
id
)
self
.
assertFalse
(
created
)
# The publisher should have three books.
self
.
assertEqual
(
p
.
books
.
count
(),
3
)
tests/get_or_create_regress/__init__.py
deleted
100644 → 0
Dosyayı görüntüle @
3f01e82c
tests/get_or_create_regress/models.py
deleted
100644 → 0
Dosyayı görüntüle @
3f01e82c
from
django.db
import
models
class
Publisher
(
models
.
Model
):
name
=
models
.
CharField
(
max_length
=
100
)
class
Author
(
models
.
Model
):
name
=
models
.
CharField
(
max_length
=
100
)
class
Book
(
models
.
Model
):
name
=
models
.
CharField
(
max_length
=
100
)
authors
=
models
.
ManyToManyField
(
Author
,
related_name
=
'books'
)
publisher
=
models
.
ForeignKey
(
Publisher
,
related_name
=
'books'
,
db_column
=
"publisher_id_column"
)
tests/get_or_create_regress/tests.py
deleted
100644 → 0
Dosyayı görüntüle @
3f01e82c
from
__future__
import
unicode_literals
from
django.test
import
TestCase
from
.models
import
Author
,
Publisher
class
GetOrCreateTests
(
TestCase
):
def
test_related
(
self
):
p
=
Publisher
.
objects
.
create
(
name
=
"Acme Publishing"
)
# Create a book through the publisher.
book
,
created
=
p
.
books
.
get_or_create
(
name
=
"The Book of Ed & Fred"
)
self
.
assertTrue
(
created
)
# The publisher should have one book.
self
.
assertEqual
(
p
.
books
.
count
(),
1
)
# Try get_or_create again, this time nothing should be created.
book
,
created
=
p
.
books
.
get_or_create
(
name
=
"The Book of Ed & Fred"
)
self
.
assertFalse
(
created
)
# And the publisher should still have one book.
self
.
assertEqual
(
p
.
books
.
count
(),
1
)
# Add an author to the book.
ed
,
created
=
book
.
authors
.
get_or_create
(
name
=
"Ed"
)
self
.
assertTrue
(
created
)
# The book should have one author.
self
.
assertEqual
(
book
.
authors
.
count
(),
1
)
# Try get_or_create again, this time nothing should be created.
ed
,
created
=
book
.
authors
.
get_or_create
(
name
=
"Ed"
)
self
.
assertFalse
(
created
)
# And the book should still have one author.
self
.
assertEqual
(
book
.
authors
.
count
(),
1
)
# Add a second author to the book.
fred
,
created
=
book
.
authors
.
get_or_create
(
name
=
"Fred"
)
self
.
assertTrue
(
created
)
# The book should have two authors now.
self
.
assertEqual
(
book
.
authors
.
count
(),
2
)
# Create an Author not tied to any books.
Author
.
objects
.
create
(
name
=
"Ted"
)
# There should be three Authors in total. The book object should have two.
self
.
assertEqual
(
Author
.
objects
.
count
(),
3
)
self
.
assertEqual
(
book
.
authors
.
count
(),
2
)
# Try creating a book through an author.
_
,
created
=
ed
.
books
.
get_or_create
(
name
=
"Ed's Recipes"
,
publisher
=
p
)
self
.
assertTrue
(
created
)
# Now Ed has two Books, Fred just one.
self
.
assertEqual
(
ed
.
books
.
count
(),
2
)
self
.
assertEqual
(
fred
.
books
.
count
(),
1
)
# Use the publisher's primary key value instead of a model instance.
_
,
created
=
ed
.
books
.
get_or_create
(
name
=
'The Great Book of Ed'
,
publisher_id
=
p
.
id
)
self
.
assertTrue
(
created
)
# Try get_or_create again, this time nothing should be created.
_
,
created
=
ed
.
books
.
get_or_create
(
name
=
'The Great Book of Ed'
,
publisher_id
=
p
.
id
)
self
.
assertFalse
(
created
)
# The publisher should have three books.
self
.
assertEqual
(
p
.
books
.
count
(),
3
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment