test_checks.py 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258
  1. from django import forms
  2. from django.contrib.admin import BooleanFieldListFilter, SimpleListFilter
  3. from django.contrib.admin.options import VERTICAL, ModelAdmin, TabularInline
  4. from django.contrib.admin.sites import AdminSite
  5. from django.core.checks import Error
  6. from django.forms.models import BaseModelFormSet
  7. from django.test import SimpleTestCase
  8. from .models import (
  9. Band, Song, User, ValidationTestInlineModel, ValidationTestModel,
  10. )
  11. class CheckTestCase(SimpleTestCase):
  12. def assertIsInvalid(self, model_admin, model, msg, id=None, hint=None, invalid_obj=None, admin_site=None):
  13. if admin_site is None:
  14. admin_site = AdminSite()
  15. invalid_obj = invalid_obj or model_admin
  16. admin_obj = model_admin(model, admin_site)
  17. self.assertEqual(admin_obj.check(), [Error(msg, hint=hint, obj=invalid_obj, id=id)])
  18. def assertIsInvalidRegexp(self, model_admin, model, msg, id=None, hint=None, invalid_obj=None):
  19. """
  20. Same as assertIsInvalid but treats the given msg as a regexp.
  21. """
  22. invalid_obj = invalid_obj or model_admin
  23. admin_obj = model_admin(model, AdminSite())
  24. errors = admin_obj.check()
  25. self.assertEqual(len(errors), 1)
  26. error = errors[0]
  27. self.assertEqual(error.hint, hint)
  28. self.assertEqual(error.obj, invalid_obj)
  29. self.assertEqual(error.id, id)
  30. self.assertRegex(error.msg, msg)
  31. def assertIsValid(self, model_admin, model, admin_site=None):
  32. if admin_site is None:
  33. admin_site = AdminSite()
  34. admin_obj = model_admin(model, admin_site)
  35. self.assertEqual(admin_obj.check(), [])
  36. class RawIdCheckTests(CheckTestCase):
  37. def test_not_iterable(self):
  38. class TestModelAdmin(ModelAdmin):
  39. raw_id_fields = 10
  40. self.assertIsInvalid(
  41. TestModelAdmin, ValidationTestModel,
  42. "The value of 'raw_id_fields' must be a list or tuple.",
  43. 'admin.E001'
  44. )
  45. def test_missing_field(self):
  46. class TestModelAdmin(ModelAdmin):
  47. raw_id_fields = ('non_existent_field',)
  48. self.assertIsInvalid(
  49. TestModelAdmin, ValidationTestModel,
  50. "The value of 'raw_id_fields[0]' refers to 'non_existent_field', "
  51. "which is not an attribute of 'modeladmin.ValidationTestModel'.",
  52. 'admin.E002'
  53. )
  54. def test_invalid_field_type(self):
  55. class TestModelAdmin(ModelAdmin):
  56. raw_id_fields = ('name',)
  57. self.assertIsInvalid(
  58. TestModelAdmin, ValidationTestModel,
  59. "The value of 'raw_id_fields[0]' must be a foreign key or a "
  60. "many-to-many field.",
  61. 'admin.E003'
  62. )
  63. def test_valid_case(self):
  64. class TestModelAdmin(ModelAdmin):
  65. raw_id_fields = ('users',)
  66. self.assertIsValid(TestModelAdmin, ValidationTestModel)
  67. class FieldsetsCheckTests(CheckTestCase):
  68. def test_valid_case(self):
  69. class TestModelAdmin(ModelAdmin):
  70. fieldsets = (('General', {'fields': ('name',)}),)
  71. self.assertIsValid(TestModelAdmin, ValidationTestModel)
  72. def test_not_iterable(self):
  73. class TestModelAdmin(ModelAdmin):
  74. fieldsets = 10
  75. self.assertIsInvalid(
  76. TestModelAdmin, ValidationTestModel,
  77. "The value of 'fieldsets' must be a list or tuple.",
  78. 'admin.E007'
  79. )
  80. def test_non_iterable_item(self):
  81. class TestModelAdmin(ModelAdmin):
  82. fieldsets = ({},)
  83. self.assertIsInvalid(
  84. TestModelAdmin, ValidationTestModel,
  85. "The value of 'fieldsets[0]' must be a list or tuple.",
  86. 'admin.E008'
  87. )
  88. def test_item_not_a_pair(self):
  89. class TestModelAdmin(ModelAdmin):
  90. fieldsets = ((),)
  91. self.assertIsInvalid(
  92. TestModelAdmin, ValidationTestModel,
  93. "The value of 'fieldsets[0]' must be of length 2.",
  94. 'admin.E009'
  95. )
  96. def test_second_element_of_item_not_a_dict(self):
  97. class TestModelAdmin(ModelAdmin):
  98. fieldsets = (('General', ()),)
  99. self.assertIsInvalid(
  100. TestModelAdmin, ValidationTestModel,
  101. "The value of 'fieldsets[0][1]' must be a dictionary.",
  102. 'admin.E010'
  103. )
  104. def test_missing_fields_key(self):
  105. class TestModelAdmin(ModelAdmin):
  106. fieldsets = (('General', {}),)
  107. self.assertIsInvalid(
  108. TestModelAdmin, ValidationTestModel,
  109. "The value of 'fieldsets[0][1]' must contain the key 'fields'.",
  110. 'admin.E011'
  111. )
  112. class TestModelAdmin(ModelAdmin):
  113. fieldsets = (('General', {'fields': ('name',)}),)
  114. self.assertIsValid(TestModelAdmin, ValidationTestModel)
  115. def test_specified_both_fields_and_fieldsets(self):
  116. class TestModelAdmin(ModelAdmin):
  117. fieldsets = (('General', {'fields': ('name',)}),)
  118. fields = ['name']
  119. self.assertIsInvalid(
  120. TestModelAdmin, ValidationTestModel,
  121. "Both 'fieldsets' and 'fields' are specified.",
  122. 'admin.E005'
  123. )
  124. def test_duplicate_fields(self):
  125. class TestModelAdmin(ModelAdmin):
  126. fieldsets = [(None, {'fields': ['name', 'name']})]
  127. self.assertIsInvalid(
  128. TestModelAdmin, ValidationTestModel,
  129. "There are duplicate field(s) in 'fieldsets[0][1]'.",
  130. 'admin.E012'
  131. )
  132. def test_fieldsets_with_custom_form_validation(self):
  133. class BandAdmin(ModelAdmin):
  134. fieldsets = (('Band', {'fields': ('name',)}),)
  135. self.assertIsValid(BandAdmin, Band)
  136. class FieldsCheckTests(CheckTestCase):
  137. def test_duplicate_fields_in_fields(self):
  138. class TestModelAdmin(ModelAdmin):
  139. fields = ['name', 'name']
  140. self.assertIsInvalid(
  141. TestModelAdmin, ValidationTestModel,
  142. "The value of 'fields' contains duplicate field(s).",
  143. 'admin.E006'
  144. )
  145. def test_inline(self):
  146. class ValidationTestInline(TabularInline):
  147. model = ValidationTestInlineModel
  148. fields = 10
  149. class TestModelAdmin(ModelAdmin):
  150. inlines = [ValidationTestInline]
  151. self.assertIsInvalid(
  152. TestModelAdmin, ValidationTestModel,
  153. "The value of 'fields' must be a list or tuple.",
  154. 'admin.E004',
  155. invalid_obj=ValidationTestInline
  156. )
  157. class FormCheckTests(CheckTestCase):
  158. def test_invalid_type(self):
  159. class FakeForm:
  160. pass
  161. class TestModelAdmin(ModelAdmin):
  162. form = FakeForm
  163. self.assertIsInvalid(
  164. TestModelAdmin, ValidationTestModel,
  165. "The value of 'form' must inherit from 'BaseModelForm'.",
  166. 'admin.E016'
  167. )
  168. def test_fieldsets_with_custom_form_validation(self):
  169. class BandAdmin(ModelAdmin):
  170. fieldsets = (('Band', {'fields': ('name',)}),)
  171. self.assertIsValid(BandAdmin, Band)
  172. def test_valid_case(self):
  173. class AdminBandForm(forms.ModelForm):
  174. delete = forms.BooleanField()
  175. class BandAdmin(ModelAdmin):
  176. form = AdminBandForm
  177. fieldsets = (
  178. ('Band', {
  179. 'fields': ('name', 'bio', 'sign_date', 'delete')
  180. }),
  181. )
  182. self.assertIsValid(BandAdmin, Band)
  183. class FilterVerticalCheckTests(CheckTestCase):
  184. def test_not_iterable(self):
  185. class TestModelAdmin(ModelAdmin):
  186. filter_vertical = 10
  187. self.assertIsInvalid(
  188. TestModelAdmin, ValidationTestModel,
  189. "The value of 'filter_vertical' must be a list or tuple.",
  190. 'admin.E017'
  191. )
  192. def test_missing_field(self):
  193. class TestModelAdmin(ModelAdmin):
  194. filter_vertical = ('non_existent_field',)
  195. self.assertIsInvalid(
  196. TestModelAdmin, ValidationTestModel,
  197. "The value of 'filter_vertical[0]' refers to 'non_existent_field', "
  198. "which is not an attribute of 'modeladmin.ValidationTestModel'.",
  199. 'admin.E019'
  200. )
  201. def test_invalid_field_type(self):
  202. class TestModelAdmin(ModelAdmin):
  203. filter_vertical = ('name',)
  204. self.assertIsInvalid(
  205. TestModelAdmin, ValidationTestModel,
  206. "The value of 'filter_vertical[0]' must be a many-to-many field.",
  207. 'admin.E020'
  208. )
  209. def test_valid_case(self):
  210. class TestModelAdmin(ModelAdmin):
  211. filter_vertical = ('users',)
  212. self.assertIsValid(TestModelAdmin, ValidationTestModel)
  213. class FilterHorizontalCheckTests(CheckTestCase):
  214. def test_not_iterable(self):
  215. class TestModelAdmin(ModelAdmin):
  216. filter_horizontal = 10
  217. self.assertIsInvalid(
  218. TestModelAdmin, ValidationTestModel,
  219. "The value of 'filter_horizontal' must be a list or tuple.",
  220. 'admin.E018'
  221. )
  222. def test_missing_field(self):
  223. class TestModelAdmin(ModelAdmin):
  224. filter_horizontal = ('non_existent_field',)
  225. self.assertIsInvalid(
  226. TestModelAdmin, ValidationTestModel,
  227. "The value of 'filter_horizontal[0]' refers to 'non_existent_field', "
  228. "which is not an attribute of 'modeladmin.ValidationTestModel'.",
  229. 'admin.E019'
  230. )
  231. def test_invalid_field_type(self):
  232. class TestModelAdmin(ModelAdmin):
  233. filter_horizontal = ('name',)
  234. self.assertIsInvalid(
  235. TestModelAdmin, ValidationTestModel,
  236. "The value of 'filter_horizontal[0]' must be a many-to-many field.",
  237. 'admin.E020'
  238. )
  239. def test_valid_case(self):
  240. class TestModelAdmin(ModelAdmin):
  241. filter_horizontal = ('users',)
  242. self.assertIsValid(TestModelAdmin, ValidationTestModel)
  243. class RadioFieldsCheckTests(CheckTestCase):
  244. def test_not_dictionary(self):
  245. class TestModelAdmin(ModelAdmin):
  246. radio_fields = ()
  247. self.assertIsInvalid(
  248. TestModelAdmin, ValidationTestModel,
  249. "The value of 'radio_fields' must be a dictionary.",
  250. 'admin.E021'
  251. )
  252. def test_missing_field(self):
  253. class TestModelAdmin(ModelAdmin):
  254. radio_fields = {'non_existent_field': VERTICAL}
  255. self.assertIsInvalid(
  256. TestModelAdmin, ValidationTestModel,
  257. "The value of 'radio_fields' refers to 'non_existent_field', "
  258. "which is not an attribute of 'modeladmin.ValidationTestModel'.",
  259. 'admin.E022'
  260. )
  261. def test_invalid_field_type(self):
  262. class TestModelAdmin(ModelAdmin):
  263. radio_fields = {'name': VERTICAL}
  264. self.assertIsInvalid(
  265. TestModelAdmin, ValidationTestModel,
  266. "The value of 'radio_fields' refers to 'name', which is not an instance "
  267. "of ForeignKey, and does not have a 'choices' definition.",
  268. 'admin.E023'
  269. )
  270. def test_invalid_value(self):
  271. class TestModelAdmin(ModelAdmin):
  272. radio_fields = {'state': None}
  273. self.assertIsInvalid(
  274. TestModelAdmin, ValidationTestModel,
  275. "The value of 'radio_fields[\"state\"]' must be either admin.HORIZONTAL or admin.VERTICAL.",
  276. 'admin.E024'
  277. )
  278. def test_valid_case(self):
  279. class TestModelAdmin(ModelAdmin):
  280. radio_fields = {'state': VERTICAL}
  281. self.assertIsValid(TestModelAdmin, ValidationTestModel)
  282. class PrepopulatedFieldsCheckTests(CheckTestCase):
  283. def test_not_list_or_tuple(self):
  284. class TestModelAdmin(ModelAdmin):
  285. prepopulated_fields = {'slug': 'test'}
  286. self.assertIsInvalid(
  287. TestModelAdmin, ValidationTestModel,
  288. 'The value of \'prepopulated_fields["slug"]\' must be a list '
  289. 'or tuple.',
  290. 'admin.E029'
  291. )
  292. def test_not_dictionary(self):
  293. class TestModelAdmin(ModelAdmin):
  294. prepopulated_fields = ()
  295. self.assertIsInvalid(
  296. TestModelAdmin, ValidationTestModel,
  297. "The value of 'prepopulated_fields' must be a dictionary.",
  298. 'admin.E026'
  299. )
  300. def test_missing_field(self):
  301. class TestModelAdmin(ModelAdmin):
  302. prepopulated_fields = {'non_existent_field': ('slug',)}
  303. self.assertIsInvalid(
  304. TestModelAdmin, ValidationTestModel,
  305. "The value of 'prepopulated_fields' refers to 'non_existent_field', "
  306. "which is not an attribute of 'modeladmin.ValidationTestModel'.",
  307. 'admin.E027'
  308. )
  309. def test_missing_field_again(self):
  310. class TestModelAdmin(ModelAdmin):
  311. prepopulated_fields = {'slug': ('non_existent_field',)}
  312. self.assertIsInvalid(
  313. TestModelAdmin, ValidationTestModel,
  314. "The value of 'prepopulated_fields[\"slug\"][0]' refers to 'non_existent_field', "
  315. "which is not an attribute of 'modeladmin.ValidationTestModel'.",
  316. 'admin.E030'
  317. )
  318. def test_invalid_field_type(self):
  319. class TestModelAdmin(ModelAdmin):
  320. prepopulated_fields = {'users': ('name',)}
  321. self.assertIsInvalid(
  322. TestModelAdmin, ValidationTestModel,
  323. "The value of 'prepopulated_fields' refers to 'users', which must not be "
  324. "a DateTimeField, a ForeignKey, a OneToOneField, or a ManyToManyField.",
  325. 'admin.E028'
  326. )
  327. def test_valid_case(self):
  328. class TestModelAdmin(ModelAdmin):
  329. prepopulated_fields = {'slug': ('name',)}
  330. self.assertIsValid(TestModelAdmin, ValidationTestModel)
  331. def test_one_to_one_field(self):
  332. class TestModelAdmin(ModelAdmin):
  333. prepopulated_fields = {'best_friend': ('name',)}
  334. self.assertIsInvalid(
  335. TestModelAdmin, ValidationTestModel,
  336. "The value of 'prepopulated_fields' refers to 'best_friend', which must not be "
  337. "a DateTimeField, a ForeignKey, a OneToOneField, or a ManyToManyField.",
  338. 'admin.E028'
  339. )
  340. class ListDisplayTests(CheckTestCase):
  341. def test_not_iterable(self):
  342. class TestModelAdmin(ModelAdmin):
  343. list_display = 10
  344. self.assertIsInvalid(
  345. TestModelAdmin, ValidationTestModel,
  346. "The value of 'list_display' must be a list or tuple.",
  347. 'admin.E107'
  348. )
  349. def test_missing_field(self):
  350. class TestModelAdmin(ModelAdmin):
  351. list_display = ('non_existent_field',)
  352. self.assertIsInvalid(
  353. TestModelAdmin, ValidationTestModel,
  354. "The value of 'list_display[0]' refers to 'non_existent_field', "
  355. "which is not a callable, an attribute of 'TestModelAdmin', "
  356. "or an attribute or method on 'modeladmin.ValidationTestModel'.",
  357. 'admin.E108'
  358. )
  359. def test_invalid_field_type(self):
  360. class TestModelAdmin(ModelAdmin):
  361. list_display = ('users',)
  362. self.assertIsInvalid(
  363. TestModelAdmin, ValidationTestModel,
  364. "The value of 'list_display[0]' must not be a ManyToManyField.",
  365. 'admin.E109'
  366. )
  367. def test_valid_case(self):
  368. def a_callable(obj):
  369. pass
  370. class TestModelAdmin(ModelAdmin):
  371. def a_method(self, obj):
  372. pass
  373. list_display = ('name', 'decade_published_in', 'a_method', a_callable)
  374. self.assertIsValid(TestModelAdmin, ValidationTestModel)
  375. class ListDisplayLinksCheckTests(CheckTestCase):
  376. def test_not_iterable(self):
  377. class TestModelAdmin(ModelAdmin):
  378. list_display_links = 10
  379. self.assertIsInvalid(
  380. TestModelAdmin, ValidationTestModel,
  381. "The value of 'list_display_links' must be a list, a tuple, or None.",
  382. 'admin.E110'
  383. )
  384. def test_missing_field(self):
  385. class TestModelAdmin(ModelAdmin):
  386. list_display_links = ('non_existent_field',)
  387. self.assertIsInvalid(
  388. TestModelAdmin, ValidationTestModel, (
  389. "The value of 'list_display_links[0]' refers to "
  390. "'non_existent_field', which is not defined in 'list_display'."
  391. ), 'admin.E111'
  392. )
  393. def test_missing_in_list_display(self):
  394. class TestModelAdmin(ModelAdmin):
  395. list_display_links = ('name',)
  396. self.assertIsInvalid(
  397. TestModelAdmin, ValidationTestModel,
  398. "The value of 'list_display_links[0]' refers to 'name', which is not defined in 'list_display'.",
  399. 'admin.E111'
  400. )
  401. def test_valid_case(self):
  402. def a_callable(obj):
  403. pass
  404. class TestModelAdmin(ModelAdmin):
  405. def a_method(self, obj):
  406. pass
  407. list_display = ('name', 'decade_published_in', 'a_method', a_callable)
  408. list_display_links = ('name', 'decade_published_in', 'a_method', a_callable)
  409. self.assertIsValid(TestModelAdmin, ValidationTestModel)
  410. def test_None_is_valid_case(self):
  411. class TestModelAdmin(ModelAdmin):
  412. list_display_links = None
  413. self.assertIsValid(TestModelAdmin, ValidationTestModel)
  414. def test_list_display_links_check_skipped_if_get_list_display_overridden(self):
  415. """
  416. list_display_links check is skipped if get_list_display() is overridden.
  417. """
  418. class TestModelAdmin(ModelAdmin):
  419. list_display_links = ['name', 'subtitle']
  420. def get_list_display(self, request):
  421. pass
  422. self.assertIsValid(TestModelAdmin, ValidationTestModel)
  423. def test_list_display_link_checked_for_list_tuple_if_get_list_display_overridden(self):
  424. """
  425. list_display_links is checked for list/tuple/None even if
  426. get_list_display() is overridden.
  427. """
  428. class TestModelAdmin(ModelAdmin):
  429. list_display_links = 'non-list/tuple'
  430. def get_list_display(self, request):
  431. pass
  432. self.assertIsInvalid(
  433. TestModelAdmin, ValidationTestModel,
  434. "The value of 'list_display_links' must be a list, a tuple, or None.",
  435. 'admin.E110'
  436. )
  437. class ListFilterTests(CheckTestCase):
  438. def test_list_filter_validation(self):
  439. class TestModelAdmin(ModelAdmin):
  440. list_filter = 10
  441. self.assertIsInvalid(
  442. TestModelAdmin, ValidationTestModel,
  443. "The value of 'list_filter' must be a list or tuple.",
  444. 'admin.E112'
  445. )
  446. def test_missing_field(self):
  447. class TestModelAdmin(ModelAdmin):
  448. list_filter = ('non_existent_field',)
  449. self.assertIsInvalid(
  450. TestModelAdmin, ValidationTestModel,
  451. "The value of 'list_filter[0]' refers to 'non_existent_field', "
  452. "which does not refer to a Field.",
  453. 'admin.E116'
  454. )
  455. def test_not_filter(self):
  456. class RandomClass:
  457. pass
  458. class TestModelAdmin(ModelAdmin):
  459. list_filter = (RandomClass,)
  460. self.assertIsInvalid(
  461. TestModelAdmin, ValidationTestModel,
  462. "The value of 'list_filter[0]' must inherit from 'ListFilter'.",
  463. 'admin.E113')
  464. def test_not_filter_again(self):
  465. class RandomClass:
  466. pass
  467. class TestModelAdmin(ModelAdmin):
  468. list_filter = (('is_active', RandomClass),)
  469. self.assertIsInvalid(
  470. TestModelAdmin, ValidationTestModel,
  471. "The value of 'list_filter[0][1]' must inherit from 'FieldListFilter'.",
  472. 'admin.E115'
  473. )
  474. def test_not_filter_again_again(self):
  475. class AwesomeFilter(SimpleListFilter):
  476. def get_title(self):
  477. return 'awesomeness'
  478. def get_choices(self, request):
  479. return (('bit', 'A bit awesome'), ('very', 'Very awesome'))
  480. def get_queryset(self, cl, qs):
  481. return qs
  482. class TestModelAdmin(ModelAdmin):
  483. list_filter = (('is_active', AwesomeFilter),)
  484. self.assertIsInvalid(
  485. TestModelAdmin, ValidationTestModel,
  486. "The value of 'list_filter[0][1]' must inherit from 'FieldListFilter'.",
  487. 'admin.E115'
  488. )
  489. def test_not_associated_with_field_name(self):
  490. class TestModelAdmin(ModelAdmin):
  491. list_filter = (BooleanFieldListFilter,)
  492. self.assertIsInvalid(
  493. TestModelAdmin, ValidationTestModel,
  494. "The value of 'list_filter[0]' must not inherit from 'FieldListFilter'.",
  495. 'admin.E114'
  496. )
  497. def test_valid_case(self):
  498. class AwesomeFilter(SimpleListFilter):
  499. def get_title(self):
  500. return 'awesomeness'
  501. def get_choices(self, request):
  502. return (('bit', 'A bit awesome'), ('very', 'Very awesome'))
  503. def get_queryset(self, cl, qs):
  504. return qs
  505. class TestModelAdmin(ModelAdmin):
  506. list_filter = ('is_active', AwesomeFilter, ('is_active', BooleanFieldListFilter), 'no')
  507. self.assertIsValid(TestModelAdmin, ValidationTestModel)
  508. class ListPerPageCheckTests(CheckTestCase):
  509. def test_not_integer(self):
  510. class TestModelAdmin(ModelAdmin):
  511. list_per_page = 'hello'
  512. self.assertIsInvalid(
  513. TestModelAdmin, ValidationTestModel,
  514. "The value of 'list_per_page' must be an integer.",
  515. 'admin.E118'
  516. )
  517. def test_valid_case(self):
  518. class TestModelAdmin(ModelAdmin):
  519. list_per_page = 100
  520. self.assertIsValid(TestModelAdmin, ValidationTestModel)
  521. class ListMaxShowAllCheckTests(CheckTestCase):
  522. def test_not_integer(self):
  523. class TestModelAdmin(ModelAdmin):
  524. list_max_show_all = 'hello'
  525. self.assertIsInvalid(
  526. TestModelAdmin, ValidationTestModel,
  527. "The value of 'list_max_show_all' must be an integer.",
  528. 'admin.E119'
  529. )
  530. def test_valid_case(self):
  531. class TestModelAdmin(ModelAdmin):
  532. list_max_show_all = 200
  533. self.assertIsValid(TestModelAdmin, ValidationTestModel)
  534. class SearchFieldsCheckTests(CheckTestCase):
  535. def test_not_iterable(self):
  536. class TestModelAdmin(ModelAdmin):
  537. search_fields = 10
  538. self.assertIsInvalid(
  539. TestModelAdmin, ValidationTestModel,
  540. "The value of 'search_fields' must be a list or tuple.",
  541. 'admin.E126'
  542. )
  543. class DateHierarchyCheckTests(CheckTestCase):
  544. def test_missing_field(self):
  545. class TestModelAdmin(ModelAdmin):
  546. date_hierarchy = 'non_existent_field'
  547. self.assertIsInvalid(
  548. TestModelAdmin, ValidationTestModel,
  549. "The value of 'date_hierarchy' refers to 'non_existent_field', "
  550. "which does not refer to a Field.",
  551. 'admin.E127'
  552. )
  553. def test_invalid_field_type(self):
  554. class TestModelAdmin(ModelAdmin):
  555. date_hierarchy = 'name'
  556. self.assertIsInvalid(
  557. TestModelAdmin, ValidationTestModel,
  558. "The value of 'date_hierarchy' must be a DateField or DateTimeField.",
  559. 'admin.E128'
  560. )
  561. def test_valid_case(self):
  562. class TestModelAdmin(ModelAdmin):
  563. date_hierarchy = 'pub_date'
  564. self.assertIsValid(TestModelAdmin, ValidationTestModel)
  565. def test_related_valid_case(self):
  566. class TestModelAdmin(ModelAdmin):
  567. date_hierarchy = 'band__sign_date'
  568. self.assertIsValid(TestModelAdmin, ValidationTestModel)
  569. def test_related_invalid_field_type(self):
  570. class TestModelAdmin(ModelAdmin):
  571. date_hierarchy = 'band__name'
  572. self.assertIsInvalid(
  573. TestModelAdmin, ValidationTestModel,
  574. "The value of 'date_hierarchy' must be a DateField or DateTimeField.",
  575. 'admin.E128'
  576. )
  577. class OrderingCheckTests(CheckTestCase):
  578. def test_not_iterable(self):
  579. class TestModelAdmin(ModelAdmin):
  580. ordering = 10
  581. self.assertIsInvalid(
  582. TestModelAdmin, ValidationTestModel,
  583. "The value of 'ordering' must be a list or tuple.",
  584. 'admin.E031'
  585. )
  586. class TestModelAdmin(ModelAdmin):
  587. ordering = ('non_existent_field',)
  588. self.assertIsInvalid(
  589. TestModelAdmin, ValidationTestModel,
  590. "The value of 'ordering[0]' refers to 'non_existent_field', "
  591. "which is not an attribute of 'modeladmin.ValidationTestModel'.",
  592. 'admin.E033'
  593. )
  594. def test_random_marker_not_alone(self):
  595. class TestModelAdmin(ModelAdmin):
  596. ordering = ('?', 'name')
  597. self.assertIsInvalid(
  598. TestModelAdmin, ValidationTestModel,
  599. "The value of 'ordering' has the random ordering marker '?', but contains "
  600. "other fields as well.",
  601. 'admin.E032',
  602. hint='Either remove the "?", or remove the other fields.'
  603. )
  604. def test_valid_random_marker_case(self):
  605. class TestModelAdmin(ModelAdmin):
  606. ordering = ('?',)
  607. self.assertIsValid(TestModelAdmin, ValidationTestModel)
  608. def test_valid_complex_case(self):
  609. class TestModelAdmin(ModelAdmin):
  610. ordering = ('band__name',)
  611. self.assertIsValid(TestModelAdmin, ValidationTestModel)
  612. def test_valid_case(self):
  613. class TestModelAdmin(ModelAdmin):
  614. ordering = ('name', 'pk')
  615. self.assertIsValid(TestModelAdmin, ValidationTestModel)
  616. class ListSelectRelatedCheckTests(CheckTestCase):
  617. def test_invalid_type(self):
  618. class TestModelAdmin(ModelAdmin):
  619. list_select_related = 1
  620. self.assertIsInvalid(
  621. TestModelAdmin, ValidationTestModel,
  622. "The value of 'list_select_related' must be a boolean, tuple or list.",
  623. 'admin.E117'
  624. )
  625. def test_valid_case(self):
  626. class TestModelAdmin(ModelAdmin):
  627. list_select_related = False
  628. self.assertIsValid(TestModelAdmin, ValidationTestModel)
  629. class SaveAsCheckTests(CheckTestCase):
  630. def test_not_boolean(self):
  631. class TestModelAdmin(ModelAdmin):
  632. save_as = 1
  633. self.assertIsInvalid(
  634. TestModelAdmin, ValidationTestModel,
  635. "The value of 'save_as' must be a boolean.",
  636. 'admin.E101'
  637. )
  638. def test_valid_case(self):
  639. class TestModelAdmin(ModelAdmin):
  640. save_as = True
  641. self.assertIsValid(TestModelAdmin, ValidationTestModel)
  642. class SaveOnTopCheckTests(CheckTestCase):
  643. def test_not_boolean(self):
  644. class TestModelAdmin(ModelAdmin):
  645. save_on_top = 1
  646. self.assertIsInvalid(
  647. TestModelAdmin, ValidationTestModel,
  648. "The value of 'save_on_top' must be a boolean.",
  649. 'admin.E102'
  650. )
  651. def test_valid_case(self):
  652. class TestModelAdmin(ModelAdmin):
  653. save_on_top = True
  654. self.assertIsValid(TestModelAdmin, ValidationTestModel)
  655. class InlinesCheckTests(CheckTestCase):
  656. def test_not_iterable(self):
  657. class TestModelAdmin(ModelAdmin):
  658. inlines = 10
  659. self.assertIsInvalid(
  660. TestModelAdmin, ValidationTestModel,
  661. "The value of 'inlines' must be a list or tuple.",
  662. 'admin.E103'
  663. )
  664. def test_not_model_admin(self):
  665. class ValidationTestInline:
  666. pass
  667. class TestModelAdmin(ModelAdmin):
  668. inlines = [ValidationTestInline]
  669. self.assertIsInvalidRegexp(
  670. TestModelAdmin, ValidationTestModel,
  671. r"'.*\.ValidationTestInline' must inherit from 'InlineModelAdmin'\.",
  672. 'admin.E104'
  673. )
  674. def test_missing_model_field(self):
  675. class ValidationTestInline(TabularInline):
  676. pass
  677. class TestModelAdmin(ModelAdmin):
  678. inlines = [ValidationTestInline]
  679. self.assertIsInvalidRegexp(
  680. TestModelAdmin, ValidationTestModel,
  681. r"'.*\.ValidationTestInline' must have a 'model' attribute\.",
  682. 'admin.E105')
  683. def test_invalid_model_type(self):
  684. class SomethingBad:
  685. pass
  686. class ValidationTestInline(TabularInline):
  687. model = SomethingBad
  688. class TestModelAdmin(ModelAdmin):
  689. inlines = [ValidationTestInline]
  690. self.assertIsInvalidRegexp(
  691. TestModelAdmin, ValidationTestModel,
  692. r"The value of '.*\.ValidationTestInline.model' must be a Model\.",
  693. 'admin.E106'
  694. )
  695. def test_valid_case(self):
  696. class ValidationTestInline(TabularInline):
  697. model = ValidationTestInlineModel
  698. class TestModelAdmin(ModelAdmin):
  699. inlines = [ValidationTestInline]
  700. self.assertIsValid(TestModelAdmin, ValidationTestModel)
  701. class FkNameCheckTests(CheckTestCase):
  702. def test_missing_field(self):
  703. class ValidationTestInline(TabularInline):
  704. model = ValidationTestInlineModel
  705. fk_name = 'non_existent_field'
  706. class TestModelAdmin(ModelAdmin):
  707. inlines = [ValidationTestInline]
  708. self.assertIsInvalid(
  709. TestModelAdmin, ValidationTestModel,
  710. "'modeladmin.ValidationTestInlineModel' has no field named 'non_existent_field'.",
  711. 'admin.E202',
  712. invalid_obj=ValidationTestInline
  713. )
  714. def test_valid_case(self):
  715. class ValidationTestInline(TabularInline):
  716. model = ValidationTestInlineModel
  717. fk_name = 'parent'
  718. class TestModelAdmin(ModelAdmin):
  719. inlines = [ValidationTestInline]
  720. self.assertIsValid(TestModelAdmin, ValidationTestModel)
  721. class ExtraCheckTests(CheckTestCase):
  722. def test_not_integer(self):
  723. class ValidationTestInline(TabularInline):
  724. model = ValidationTestInlineModel
  725. extra = 'hello'
  726. class TestModelAdmin(ModelAdmin):
  727. inlines = [ValidationTestInline]
  728. self.assertIsInvalid(
  729. TestModelAdmin, ValidationTestModel,
  730. "The value of 'extra' must be an integer.",
  731. 'admin.E203',
  732. invalid_obj=ValidationTestInline
  733. )
  734. def test_valid_case(self):
  735. class ValidationTestInline(TabularInline):
  736. model = ValidationTestInlineModel
  737. extra = 2
  738. class TestModelAdmin(ModelAdmin):
  739. inlines = [ValidationTestInline]
  740. self.assertIsValid(TestModelAdmin, ValidationTestModel)
  741. class MaxNumCheckTests(CheckTestCase):
  742. def test_not_integer(self):
  743. class ValidationTestInline(TabularInline):
  744. model = ValidationTestInlineModel
  745. max_num = 'hello'
  746. class TestModelAdmin(ModelAdmin):
  747. inlines = [ValidationTestInline]
  748. self.assertIsInvalid(
  749. TestModelAdmin, ValidationTestModel,
  750. "The value of 'max_num' must be an integer.",
  751. 'admin.E204',
  752. invalid_obj=ValidationTestInline
  753. )
  754. def test_valid_case(self):
  755. class ValidationTestInline(TabularInline):
  756. model = ValidationTestInlineModel
  757. max_num = 2
  758. class TestModelAdmin(ModelAdmin):
  759. inlines = [ValidationTestInline]
  760. self.assertIsValid(TestModelAdmin, ValidationTestModel)
  761. class MinNumCheckTests(CheckTestCase):
  762. def test_not_integer(self):
  763. class ValidationTestInline(TabularInline):
  764. model = ValidationTestInlineModel
  765. min_num = 'hello'
  766. class TestModelAdmin(ModelAdmin):
  767. inlines = [ValidationTestInline]
  768. self.assertIsInvalid(
  769. TestModelAdmin, ValidationTestModel,
  770. "The value of 'min_num' must be an integer.",
  771. 'admin.E205',
  772. invalid_obj=ValidationTestInline
  773. )
  774. def test_valid_case(self):
  775. class ValidationTestInline(TabularInline):
  776. model = ValidationTestInlineModel
  777. min_num = 2
  778. class TestModelAdmin(ModelAdmin):
  779. inlines = [ValidationTestInline]
  780. self.assertIsValid(TestModelAdmin, ValidationTestModel)
  781. class FormsetCheckTests(CheckTestCase):
  782. def test_invalid_type(self):
  783. class FakeFormSet:
  784. pass
  785. class ValidationTestInline(TabularInline):
  786. model = ValidationTestInlineModel
  787. formset = FakeFormSet
  788. class TestModelAdmin(ModelAdmin):
  789. inlines = [ValidationTestInline]
  790. self.assertIsInvalid(
  791. TestModelAdmin, ValidationTestModel,
  792. "The value of 'formset' must inherit from 'BaseModelFormSet'.",
  793. 'admin.E206',
  794. invalid_obj=ValidationTestInline
  795. )
  796. def test_valid_case(self):
  797. class RealModelFormSet(BaseModelFormSet):
  798. pass
  799. class ValidationTestInline(TabularInline):
  800. model = ValidationTestInlineModel
  801. formset = RealModelFormSet
  802. class TestModelAdmin(ModelAdmin):
  803. inlines = [ValidationTestInline]
  804. self.assertIsValid(TestModelAdmin, ValidationTestModel)
  805. class ListDisplayEditableTests(CheckTestCase):
  806. def test_list_display_links_is_none(self):
  807. """
  808. list_display and list_editable can contain the same values
  809. when list_display_links is None
  810. """
  811. class ProductAdmin(ModelAdmin):
  812. list_display = ['name', 'slug', 'pub_date']
  813. list_editable = list_display
  814. list_display_links = None
  815. self.assertIsValid(ProductAdmin, ValidationTestModel)
  816. def test_list_display_first_item_same_as_list_editable_first_item(self):
  817. """
  818. The first item in list_display can be the same as the first in
  819. list_editable.
  820. """
  821. class ProductAdmin(ModelAdmin):
  822. list_display = ['name', 'slug', 'pub_date']
  823. list_editable = ['name', 'slug']
  824. list_display_links = ['pub_date']
  825. self.assertIsValid(ProductAdmin, ValidationTestModel)
  826. def test_list_display_first_item_in_list_editable(self):
  827. """
  828. The first item in list_display can be in list_editable as long as
  829. list_display_links is defined.
  830. """
  831. class ProductAdmin(ModelAdmin):
  832. list_display = ['name', 'slug', 'pub_date']
  833. list_editable = ['slug', 'name']
  834. list_display_links = ['pub_date']
  835. self.assertIsValid(ProductAdmin, ValidationTestModel)
  836. def test_list_display_first_item_same_as_list_editable_no_list_display_links(self):
  837. """
  838. The first item in list_display cannot be the same as the first item
  839. in list_editable if list_display_links is not defined.
  840. """
  841. class ProductAdmin(ModelAdmin):
  842. list_display = ['name']
  843. list_editable = ['name']
  844. self.assertIsInvalid(
  845. ProductAdmin, ValidationTestModel,
  846. "The value of 'list_editable[0]' refers to the first field "
  847. "in 'list_display' ('name'), which cannot be used unless "
  848. "'list_display_links' is set.",
  849. id='admin.E124',
  850. )
  851. def test_list_display_first_item_in_list_editable_no_list_display_links(self):
  852. """
  853. The first item in list_display cannot be in list_editable if
  854. list_display_links isn't defined.
  855. """
  856. class ProductAdmin(ModelAdmin):
  857. list_display = ['name', 'slug', 'pub_date']
  858. list_editable = ['slug', 'name']
  859. self.assertIsInvalid(
  860. ProductAdmin, ValidationTestModel,
  861. "The value of 'list_editable[1]' refers to the first field "
  862. "in 'list_display' ('name'), which cannot be used unless "
  863. "'list_display_links' is set.",
  864. id='admin.E124',
  865. )
  866. def test_both_list_editable_and_list_display_links(self):
  867. class ProductAdmin(ModelAdmin):
  868. list_editable = ('name',)
  869. list_display = ('name',)
  870. list_display_links = ('name',)
  871. self.assertIsInvalid(
  872. ProductAdmin, ValidationTestModel,
  873. "The value of 'name' cannot be in both 'list_editable' and "
  874. "'list_display_links'.",
  875. id='admin.E123',
  876. )
  877. class AutocompleteFieldsTests(CheckTestCase):
  878. def test_autocomplete_e036(self):
  879. class Admin(ModelAdmin):
  880. autocomplete_fields = 'name'
  881. self.assertIsInvalid(
  882. Admin, Band,
  883. msg="The value of 'autocomplete_fields' must be a list or tuple.",
  884. id='admin.E036',
  885. invalid_obj=Admin,
  886. )
  887. def test_autocomplete_e037(self):
  888. class Admin(ModelAdmin):
  889. autocomplete_fields = ('nonexistent',)
  890. self.assertIsInvalid(
  891. Admin, ValidationTestModel,
  892. msg=(
  893. "The value of 'autocomplete_fields[0]' refers to 'nonexistent', "
  894. "which is not an attribute of 'modeladmin.ValidationTestModel'."
  895. ),
  896. id='admin.E037',
  897. invalid_obj=Admin,
  898. )
  899. def test_autocomplete_e38(self):
  900. class Admin(ModelAdmin):
  901. autocomplete_fields = ('name',)
  902. self.assertIsInvalid(
  903. Admin, ValidationTestModel,
  904. msg=(
  905. "The value of 'autocomplete_fields[0]' must be a foreign "
  906. "key or a many-to-many field."
  907. ),
  908. id='admin.E038',
  909. invalid_obj=Admin,
  910. )
  911. def test_autocomplete_e039(self):
  912. class Admin(ModelAdmin):
  913. autocomplete_fields = ('band',)
  914. self.assertIsInvalid(
  915. Admin, Song,
  916. msg=(
  917. 'An admin for model "Band" has to be registered '
  918. 'to be referenced by Admin.autocomplete_fields.'
  919. ),
  920. id='admin.E039',
  921. invalid_obj=Admin,
  922. )
  923. def test_autocomplete_e040(self):
  924. class NoSearchFieldsAdmin(ModelAdmin):
  925. pass
  926. class AutocompleteAdmin(ModelAdmin):
  927. autocomplete_fields = ('featuring',)
  928. site = AdminSite()
  929. site.register(Band, NoSearchFieldsAdmin)
  930. self.assertIsInvalid(
  931. AutocompleteAdmin, Song,
  932. msg=(
  933. 'NoSearchFieldsAdmin must define "search_fields", because '
  934. 'it\'s referenced by AutocompleteAdmin.autocomplete_fields.'
  935. ),
  936. id='admin.E040',
  937. invalid_obj=AutocompleteAdmin,
  938. admin_site=site,
  939. )
  940. def test_autocomplete_is_valid(self):
  941. class SearchFieldsAdmin(ModelAdmin):
  942. search_fields = 'name'
  943. class AutocompleteAdmin(ModelAdmin):
  944. autocomplete_fields = ('featuring',)
  945. site = AdminSite()
  946. site.register(Band, SearchFieldsAdmin)
  947. self.assertIsValid(AutocompleteAdmin, Song, admin_site=site)
  948. def test_autocomplete_is_onetoone(self):
  949. class UserAdmin(ModelAdmin):
  950. search_fields = ('name',)
  951. class Admin(ModelAdmin):
  952. autocomplete_fields = ('best_friend',)
  953. site = AdminSite()
  954. site.register(User, UserAdmin)
  955. self.assertIsValid(Admin, ValidationTestModel, admin_site=site)