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
54d3ba84
Kaydet (Commit)
54d3ba84
authored
Ock 08, 2016
tarafından
Tim Graham
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Added a helper function in schema tests.
üst
ade54ffa
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
26 additions
and
44 deletions
+26
-44
tests.py
tests/schema/tests.py
+26
-44
No files found.
tests/schema/tests.py
Dosyayı görüntüle @
54d3ba84
...
...
@@ -116,6 +116,14 @@ class SchemaTests(TransactionTestCase):
with
connection
.
cursor
()
as
cursor
:
return
connection
.
introspection
.
get_constraints
(
cursor
,
table
)
def
get_constraints_for_column
(
self
,
model
,
column_name
):
constraints
=
self
.
get_constraints
(
model
.
_meta
.
db_table
)
constraints_for_column
=
[]
for
name
,
details
in
constraints
.
items
():
if
details
[
'columns'
]
==
[
column_name
]:
constraints_for_column
.
append
(
name
)
return
sorted
(
constraints_for_column
)
# Tests
def
test_creation_deletion
(
self
):
...
...
@@ -1710,68 +1718,42 @@ class SchemaTests(TransactionTestCase):
@unittest.skipUnless
(
connection
.
vendor
==
'postgresql'
,
"PostgreSQL specific"
)
def
test_alter_field_add_index_to_charfield
(
self
):
# Create the table
# Create the table
and verify no initial indexes.
with
connection
.
schema_editor
()
as
editor
:
editor
.
create_model
(
Author
)
# Ensure the table is there and has no index
self
.
assertNotIn
(
'name'
,
self
.
get_indexes
(
Author
.
_meta
.
db_table
))
# Alter to add the index
self
.
assertEqual
(
self
.
get_constraints_for_column
(
Author
,
'name'
),
[])
# Alter to add db_index=True and create 2 indexes.
old_field
=
Author
.
_meta
.
get_field
(
'name'
)
new_field
=
CharField
(
max_length
=
255
,
db_index
=
True
)
new_field
.
set_attributes_from_name
(
'name'
)
with
connection
.
schema_editor
()
as
editor
:
editor
.
alter_field
(
Author
,
old_field
,
new_field
,
strict
=
True
)
# Check that all the constraints are there
constraints
=
self
.
get_constraints
(
Author
.
_meta
.
db_table
)
name_indexes
=
[]
for
name
,
details
in
constraints
.
items
():
if
details
[
'columns'
]
==
[
'name'
]:
name_indexes
.
append
(
name
)
self
.
assertEqual
(
2
,
len
(
name_indexes
),
'Indexes are missing for name column'
)
# Check that one of the indexes ends with `_like`
like_index
=
[
x
for
x
in
name_indexes
if
x
.
endswith
(
'_like'
)]
self
.
assertEqual
(
1
,
len
(
like_index
),
'Index with the operator class is missing for the name column'
)
# Remove the index
self
.
assertEqual
(
self
.
get_constraints_for_column
(
Author
,
'name'
),
[
'schema_author_name_1fbc5617_like'
,
'schema_author_name_1fbc5617_uniq'
]
)
# Remove db_index=True to drop both indexes.
with
connection
.
schema_editor
()
as
editor
:
editor
.
alter_field
(
Author
,
new_field
,
old_field
,
strict
=
True
)
# Ensure the name constraints where dropped
constraints
=
self
.
get_constraints
(
Author
.
_meta
.
db_table
)
name_indexes
=
[]
for
details
in
constraints
.
values
():
if
details
[
'columns'
]
==
[
'name'
]:
name_indexes
.
append
(
details
)
self
.
assertEqual
(
0
,
len
(
name_indexes
),
'Indexes were not dropped for the name column'
)
self
.
assertEqual
(
self
.
get_constraints_for_column
(
Author
,
'name'
),
[])
@unittest.skipUnless
(
connection
.
vendor
==
'postgresql'
,
"PostgreSQL specific"
)
def
test_alter_field_add_index_to_textfield
(
self
):
# Create the table
# Create the table
and verify no initial indexes.
with
connection
.
schema_editor
()
as
editor
:
editor
.
create_model
(
Note
)
# Ensure the table is there and has no index
self
.
assertNotIn
(
'info'
,
self
.
get_indexes
(
Note
.
_meta
.
db_table
))
# Alter to add the index
self
.
assertEqual
(
self
.
get_constraints_for_column
(
Note
,
'info'
),
[])
# Alter to add db_index=True and create 2 indexes.
old_field
=
Note
.
_meta
.
get_field
(
'info'
)
new_field
=
TextField
(
db_index
=
True
)
new_field
.
set_attributes_from_name
(
'info'
)
with
connection
.
schema_editor
()
as
editor
:
editor
.
alter_field
(
Note
,
old_field
,
new_field
,
strict
=
True
)
# Check that all the constraints are there
constraints
=
self
.
get_constraints
(
Note
.
_meta
.
db_table
)
info_indexes
=
[]
for
name
,
details
in
constraints
.
items
():
if
details
[
'columns'
]
==
[
'info'
]:
info_indexes
.
append
(
name
)
self
.
assertEqual
(
2
,
len
(
info_indexes
),
'Indexes are missing for info column'
)
# Check that one of the indexes ends with `_like`
like_index
=
[
x
for
x
in
info_indexes
if
x
.
endswith
(
'_like'
)]
self
.
assertEqual
(
1
,
len
(
like_index
),
'Index with the operator class is missing for the info column'
)
# Remove the index
self
.
assertEqual
(
self
.
get_constraints_for_column
(
Note
,
'info'
),
[
'schema_note_info_4b0ea695_like'
,
'schema_note_info_4b0ea695_uniq'
]
)
# Remove db_index=True to drop both indexes.
with
connection
.
schema_editor
()
as
editor
:
editor
.
alter_field
(
Note
,
new_field
,
old_field
,
strict
=
True
)
# Ensure the info constraints where dropped
constraints
=
self
.
get_constraints
(
Note
.
_meta
.
db_table
)
info_indexes
=
[]
for
details
in
constraints
.
values
():
if
details
[
'columns'
]
==
[
'info'
]:
info_indexes
.
append
(
details
)
self
.
assertEqual
(
0
,
len
(
info_indexes
),
'Indexes were not dropped for the info column'
)
self
.
assertEqual
(
self
.
get_constraints_for_column
(
Note
,
'info'
),
[])
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