TST: 100% coverage
This commit is contained in:
parent
02a21306fe
commit
8622e2323d
8 changed files with 166 additions and 79 deletions
26
ccdb/experiments/tests/test_admin.py
Normal file
26
ccdb/experiments/tests/test_admin.py
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
from django.test import TestCase
|
||||||
|
from django.contrib.admin.sites import AdminSite
|
||||||
|
|
||||||
|
from ..models import AliveDeadCount
|
||||||
|
from ..admin import AliveDeadCountAdmin
|
||||||
|
from .factories import AliveDeadCountFactory
|
||||||
|
|
||||||
|
|
||||||
|
class AliveDeadCountAdminTests(TestCase):
|
||||||
|
def setUp(self):
|
||||||
|
self.ad_count = AliveDeadCountFactory()
|
||||||
|
self.site = AdminSite()
|
||||||
|
|
||||||
|
def test_list_display(self):
|
||||||
|
admin_obj = AliveDeadCountAdmin(AliveDeadCount, self.site)
|
||||||
|
self.assertEqual(admin_obj.check(), [])
|
||||||
|
|
||||||
|
treatment_from_callable = admin_obj.treatment(self.ad_count)
|
||||||
|
self.assertEqual(treatment_from_callable,
|
||||||
|
self.ad_count.treatment_replicate.treatment)
|
||||||
|
|
||||||
|
tr_from_callable = admin_obj.tr(self.ad_count)
|
||||||
|
_tr = self.ad_count.treatment_replicate
|
||||||
|
tr_from_related = '_'.join([str(_tr.setup_date), _tr.name,
|
||||||
|
str(_tr.setup_sample_size)])
|
||||||
|
self.assertEqual(tr_from_callable, tr_from_related)
|
|
@ -71,9 +71,10 @@ class TreatmentTestCase(TestCase):
|
||||||
def test_uniqueness(self):
|
def test_uniqueness(self):
|
||||||
t1 = TreatmentFactory()
|
t1 = TreatmentFactory()
|
||||||
with transaction.atomic(), self.assertRaises(IntegrityError):
|
with transaction.atomic(), self.assertRaises(IntegrityError):
|
||||||
TreatmentFactory(treatment_type=t1.treatment_type, container=t1.container,
|
TreatmentFactory(treatment_type=t1.treatment_type,
|
||||||
study_location=t1.study_location, species=t1.species,
|
container=t1.container,
|
||||||
sex=t1.sex)
|
study_location=t1.study_location,
|
||||||
|
species=t1.species, sex=t1.sex)
|
||||||
t3 = TreatmentFactory()
|
t3 = TreatmentFactory()
|
||||||
self.assertTrue(isinstance(t3, Treatment))
|
self.assertTrue(isinstance(t3, Treatment))
|
||||||
|
|
||||||
|
|
|
@ -26,33 +26,27 @@ class SiteAdmin(admin.ModelAdmin):
|
||||||
|
|
||||||
|
|
||||||
class MunicipalLocationAdmin(admin.ModelAdmin):
|
class MunicipalLocationAdmin(admin.ModelAdmin):
|
||||||
list_display = ('name', 'code', 'site_name', 'municipal_location_type',
|
list_display = ('name', 'code', 'municipal_location_type', 'description',
|
||||||
'description', 'sort_order')
|
'sort_order')
|
||||||
list_display_links = ('name',)
|
list_display_links = ('name',)
|
||||||
search_fields = ('name', 'code', 'site__name', 'municipal_location_type',
|
search_fields = ('name', 'code', 'municipal_location_type', 'description')
|
||||||
'description')
|
|
||||||
list_per_page = 25
|
list_per_page = 25
|
||||||
fields = ('name', 'code', 'site', 'municipal_location_type',
|
fields = ('name', 'code', 'municipal_location_type', 'description',
|
||||||
'description', 'sort_order')
|
'sort_order')
|
||||||
|
|
||||||
def site_name(self, obj):
|
|
||||||
return obj.site.name
|
|
||||||
site_name.admin_order_field = 'site__name'
|
|
||||||
site_name.short_description = 'Site'
|
|
||||||
|
|
||||||
|
|
||||||
class StudyLocationAdmin(admin.ModelAdmin):
|
class StudyLocationAdmin(admin.ModelAdmin):
|
||||||
list_display = ('name', 'code', 'site_name', 'study_location_type',
|
list_display = ('name', 'code', 'site_name', 'study_location_type',
|
||||||
'treatment_type', 'ml_name', 'collecting_location',
|
'treatment_type', 'ml_name', 'collecting_location',
|
||||||
'description', 'sort_order')
|
'description', 'sort_order')
|
||||||
list_display_links = ('name',)
|
list_display_links = ('name',)
|
||||||
search_fields = ('name', 'code', 'site__name', 'study_location_type',
|
search_fields = ('name', 'code', 'site__name', 'study_location_type',
|
||||||
'treatment_type', 'municipal_location__name', 'collecting_location',
|
'treatment_type', 'municipal_location__name',
|
||||||
'description')
|
'collecting_location', 'description')
|
||||||
list_per_page = 25
|
list_per_page = 25
|
||||||
fields = ('name', 'code', 'site', 'study_location_type',
|
fields = ('name', 'code', 'site', 'study_location_type', 'treatment_type',
|
||||||
'treatment_type', 'municipal_location', 'collecting_location',
|
'municipal_location', 'collecting_location', 'description',
|
||||||
'description', 'sort_order')
|
'sort_order')
|
||||||
|
|
||||||
def site_name(self, obj):
|
def site_name(self, obj):
|
||||||
return obj.site.name
|
return obj.site.name
|
||||||
|
@ -60,22 +54,20 @@ class StudyLocationAdmin(admin.ModelAdmin):
|
||||||
site_name.short_description = 'Site'
|
site_name.short_description = 'Site'
|
||||||
|
|
||||||
def ml_name(self, obj):
|
def ml_name(self, obj):
|
||||||
if obj.municipal_location:
|
return str(obj.municipal_location) if obj.municipal_location else None
|
||||||
return obj.municipal_location.name
|
|
||||||
return obj.municipal_location
|
|
||||||
ml_name.admin_order_field = 'municipal_location__name'
|
ml_name.admin_order_field = 'municipal_location__name'
|
||||||
ml_name.short_description = 'Municipal Location'
|
ml_name.short_description = 'Municipal Location'
|
||||||
|
|
||||||
|
|
||||||
class StorageLocationAdmin(admin.ModelAdmin):
|
class StorageLocationAdmin(admin.ModelAdmin):
|
||||||
list_display = ('code', 'facility', 'building', 'room', 'freezer', 'temp_c',
|
list_display = ('code', 'facility', 'building', 'room', 'freezer',
|
||||||
'description', 'sort_order')
|
'temp_c', 'description', 'sort_order')
|
||||||
list_display_links = ('code',)
|
list_display_links = ('code',)
|
||||||
search_fields = ('code', 'facility', 'building', 'room', 'freezer',
|
search_fields = ('code', 'facility', 'building', 'room', 'freezer',
|
||||||
'temp_c', 'description')
|
'temp_c', 'description')
|
||||||
list_per_page = 25
|
list_per_page = 25
|
||||||
fields = ('code', 'facility', 'building', 'room', 'freezer', 'temp_c',
|
fields = ('code', 'facility', 'building', 'room', 'freezer', 'temp_c',
|
||||||
'description', 'sort_order')
|
'description', 'sort_order')
|
||||||
|
|
||||||
|
|
||||||
admin.site.register(Region, RegionAdmin)
|
admin.site.register(Region, RegionAdmin)
|
||||||
|
|
38
ccdb/locations/tests/test_admin.py
Normal file
38
ccdb/locations/tests/test_admin.py
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
from django.test import TestCase
|
||||||
|
from django.contrib.admin.sites import AdminSite
|
||||||
|
|
||||||
|
from ..models import Site, StudyLocation
|
||||||
|
from ..admin import SiteAdmin, StudyLocationAdmin
|
||||||
|
from .factories import SiteFactory, StudyLocationFactory
|
||||||
|
|
||||||
|
|
||||||
|
class SiteAdminTests(TestCase):
|
||||||
|
def setUp(self):
|
||||||
|
self.site = SiteFactory()
|
||||||
|
self.admin_site = AdminSite()
|
||||||
|
|
||||||
|
def test_list_display(self):
|
||||||
|
admin_obj = SiteAdmin(Site, self.admin_site)
|
||||||
|
self.assertEqual(admin_obj.check(), [])
|
||||||
|
|
||||||
|
region_name_from_callable = admin_obj.region_name(self.site)
|
||||||
|
self.assertEqual(region_name_from_callable,
|
||||||
|
self.site.region.name)
|
||||||
|
|
||||||
|
|
||||||
|
class StudyLocationAdminTests(TestCase):
|
||||||
|
def setUp(self):
|
||||||
|
self.sl = StudyLocationFactory()
|
||||||
|
self.site = AdminSite()
|
||||||
|
|
||||||
|
def test_list_display(self):
|
||||||
|
admin_obj = StudyLocationAdmin(StudyLocation, self.site)
|
||||||
|
self.assertEqual(admin_obj.check(), [])
|
||||||
|
|
||||||
|
site_name_from_callable = admin_obj.site_name(self.sl)
|
||||||
|
self.assertEqual(site_name_from_callable,
|
||||||
|
self.sl.site.name)
|
||||||
|
|
||||||
|
ml_name_from_callable = admin_obj.ml_name(self.sl)
|
||||||
|
self.assertEqual(ml_name_from_callable,
|
||||||
|
str(self.sl.municipal_location))
|
|
@ -1,6 +1,7 @@
|
||||||
from django.contrib import admin
|
from django.contrib import admin
|
||||||
|
|
||||||
from .models import MeasurementUnit, MeasurementType, Container, Material, Color
|
from .models import (MeasurementUnit, MeasurementType, Container, Material,
|
||||||
|
Color)
|
||||||
|
|
||||||
|
|
||||||
class MeasurementUnitAdmin(admin.ModelAdmin):
|
class MeasurementUnitAdmin(admin.ModelAdmin):
|
||||||
|
@ -13,56 +14,52 @@ class MeasurementUnitAdmin(admin.ModelAdmin):
|
||||||
|
|
||||||
class MeasurementTypeAdmin(admin.ModelAdmin):
|
class MeasurementTypeAdmin(admin.ModelAdmin):
|
||||||
list_display = ('name', 'code', 'measurement_type_class', 'description',
|
list_display = ('name', 'code', 'measurement_type_class', 'description',
|
||||||
'measurement_unit_code', 'sort_order')
|
'measurement_unit_code', 'sort_order')
|
||||||
list_display_links = ('name',)
|
list_display_links = ('name',)
|
||||||
search_fields = ('name', 'code', 'measurement_type_class',
|
search_fields = ('name', 'code', 'measurement_type_class',
|
||||||
'default_measurement_unit__code', 'description')
|
'default_measurement_unit__code', 'description')
|
||||||
list_per_page = 25
|
list_per_page = 25
|
||||||
fields = ('name', 'code', 'measurement_type_class', 'description',
|
fields = ('name', 'code', 'measurement_type_class', 'description',
|
||||||
'default_measurement_unit', 'sort_order')
|
'default_measurement_unit', 'sort_order')
|
||||||
|
|
||||||
def measurement_unit_code(self, obj):
|
def measurement_unit_code(self, obj):
|
||||||
if obj.default_measurement_unit:
|
dmu = obj.default_measurement_unit
|
||||||
return obj.default_measurement_unit.code
|
return dmu.code if dmu else dmu
|
||||||
return obj.default_measurement_unit
|
|
||||||
measurement_unit_code.admin_order_field = 'default_measurement_unit__code'
|
measurement_unit_code.admin_order_field = 'default_measurement_unit__code'
|
||||||
measurement_unit_code.short_description = 'Default Measurement Unit'
|
measurement_unit_code.short_description = 'Default Measurement Unit'
|
||||||
|
|
||||||
|
|
||||||
class ContainerAdmin(admin.ModelAdmin):
|
class ContainerAdmin(admin.ModelAdmin):
|
||||||
list_display = ('name', 'code', 'application', 'color_name', 'material_name',
|
list_display = ('name', 'code', 'application', 'color_name',
|
||||||
'volume', 'measurement_unit_name', 'sort_order')
|
'material_name', 'volume', 'measurement_unit_name',
|
||||||
|
'sort_order')
|
||||||
list_display_links = ('name',)
|
list_display_links = ('name',)
|
||||||
search_fields = ('name', 'code', 'application', 'color__name',
|
search_fields = ('name', 'code', 'application', 'color__name',
|
||||||
'material__name', 'volume', 'measurement_unit__name')
|
'material__name', 'volume', 'measurement_unit__name')
|
||||||
list_per_page = 25
|
list_per_page = 25
|
||||||
fields = ('name', 'code', 'application', 'color', 'material',
|
fields = ('name', 'code', 'application', 'color', 'material', 'volume',
|
||||||
'volume', 'measurement_unit', 'sort_order')
|
'measurement_unit', 'sort_order')
|
||||||
|
|
||||||
def color_name(self, obj):
|
def color_name(self, obj):
|
||||||
if obj.color:
|
return obj.color.name if obj.color else obj.color
|
||||||
return obj.color.name
|
|
||||||
return obj.color
|
|
||||||
color_name.admin_order_field = 'color__name'
|
color_name.admin_order_field = 'color__name'
|
||||||
color_name.short_description = 'Color'
|
color_name.short_description = 'Color'
|
||||||
|
|
||||||
def material_name(self, obj):
|
def material_name(self, obj):
|
||||||
if obj.material:
|
return obj.material.name if obj.material else obj.material
|
||||||
return obj.material.name
|
|
||||||
return obj.material
|
|
||||||
material_name.admin_order_field = 'material__name'
|
material_name.admin_order_field = 'material__name'
|
||||||
material_name.short_description = 'Material'
|
material_name.short_description = 'Material'
|
||||||
|
|
||||||
def measurement_unit_name(self, obj):
|
def measurement_unit_name(self, obj):
|
||||||
if obj.measurement_unit:
|
mu = obj.measurement_unit
|
||||||
return obj.measurement_unit.name
|
return mu.name if mu else mu
|
||||||
return obj.measurement_unit
|
|
||||||
measurement_unit_name.admin_order_field = 'measurement_unit__name'
|
measurement_unit_name.admin_order_field = 'measurement_unit__name'
|
||||||
measurement_unit_name.short_description = 'Measurement Unit'
|
measurement_unit_name.short_description = 'Measurement Unit'
|
||||||
|
|
||||||
|
|
||||||
class MaterialAdmin(admin.ModelAdmin):
|
class MaterialAdmin(admin.ModelAdmin):
|
||||||
list_display = ('name', 'code', 'material_class', 'description', 'sort_order')
|
list_display = ('name', 'code', 'material_class', 'description',
|
||||||
|
'sort_order')
|
||||||
list_display_links = ('name',)
|
list_display_links = ('name',)
|
||||||
search_fields = ('name', 'code', 'material_class', 'description')
|
search_fields = ('name', 'code', 'material_class', 'description')
|
||||||
list_per_page = 25
|
list_per_page = 25
|
||||||
|
|
41
ccdb/misc/tests/test_admin.py
Normal file
41
ccdb/misc/tests/test_admin.py
Normal file
|
@ -0,0 +1,41 @@
|
||||||
|
from django.test import TestCase
|
||||||
|
from django.contrib.admin.sites import AdminSite
|
||||||
|
|
||||||
|
from ..models import MeasurementType, Container
|
||||||
|
from ..admin import MeasurementTypeAdmin, ContainerAdmin
|
||||||
|
from .factories import MeasurementTypeFactory, ContainerFactory
|
||||||
|
|
||||||
|
|
||||||
|
class MeasurementTypeAdminTests(TestCase):
|
||||||
|
def setUp(self):
|
||||||
|
self.mu = MeasurementTypeFactory()
|
||||||
|
self.site = AdminSite()
|
||||||
|
|
||||||
|
def test_list_display(self):
|
||||||
|
admin_obj = MeasurementTypeAdmin(MeasurementType, self.site)
|
||||||
|
self.assertEqual(admin_obj.check(), [])
|
||||||
|
|
||||||
|
mu_code_from_callable = admin_obj.measurement_unit_code(self.mu)
|
||||||
|
self.assertEqual(mu_code_from_callable,
|
||||||
|
self.mu.default_measurement_unit.code)
|
||||||
|
|
||||||
|
|
||||||
|
class ContainerAdminTests(TestCase):
|
||||||
|
def setUp(self):
|
||||||
|
self.container = ContainerFactory()
|
||||||
|
self.site = AdminSite()
|
||||||
|
|
||||||
|
def test_list_display(self):
|
||||||
|
admin_obj = ContainerAdmin(Container, self.site)
|
||||||
|
self.assertEqual(admin_obj.check(), [])
|
||||||
|
|
||||||
|
color_name_from_callable = admin_obj.color_name(self.container)
|
||||||
|
self.assertEqual(color_name_from_callable, self.container.color.name)
|
||||||
|
|
||||||
|
material_name_from_callable = admin_obj.material_name(self.container)
|
||||||
|
self.assertEqual(material_name_from_callable,
|
||||||
|
self.container.material.name)
|
||||||
|
|
||||||
|
mu_name_from_callable = admin_obj.measurement_unit_name(self.container)
|
||||||
|
self.assertEqual(mu_name_from_callable,
|
||||||
|
self.container.measurement_unit.name)
|
20
ccdb/projects/tests/test_admin.py
Normal file
20
ccdb/projects/tests/test_admin.py
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
from django.test import TestCase
|
||||||
|
from django.contrib.admin.sites import AdminSite
|
||||||
|
|
||||||
|
from ..models import GrantReport
|
||||||
|
from ..admin import GrantReportAdmin
|
||||||
|
from .factories import GrantReportFactory
|
||||||
|
|
||||||
|
|
||||||
|
class GrantReportAdminTests(TestCase):
|
||||||
|
def setUp(self):
|
||||||
|
self.grant_report = GrantReportFactory()
|
||||||
|
self.site = AdminSite()
|
||||||
|
|
||||||
|
def test_list_display(self):
|
||||||
|
admin_obj = GrantReportAdmin(GrantReport, self.site)
|
||||||
|
self.assertEqual(admin_obj.check(), [])
|
||||||
|
|
||||||
|
grant_title_from_callable = admin_obj.grant_title(self.grant_report)
|
||||||
|
self.assertEqual(grant_title_from_callable,
|
||||||
|
self.grant_report.grant.title)
|
|
@ -1,34 +1,6 @@
|
||||||
from django import forms
|
|
||||||
from django.contrib import admin
|
from django.contrib import admin
|
||||||
from django.contrib.auth.admin import UserAdmin as AuthUserAdmin
|
|
||||||
from django.contrib.auth.forms import UserChangeForm, UserCreationForm
|
|
||||||
|
|
||||||
from .models import User
|
from .models import User
|
||||||
|
|
||||||
|
|
||||||
class MyUserChangeForm(UserChangeForm):
|
admin.site.register(User)
|
||||||
class Meta(UserChangeForm.Meta):
|
|
||||||
model = User
|
|
||||||
|
|
||||||
|
|
||||||
class MyUserCreationForm(UserCreationForm):
|
|
||||||
error_message = UserCreationForm.error_messages.update({
|
|
||||||
'duplicate_username': 'This username has already been taken.'
|
|
||||||
})
|
|
||||||
|
|
||||||
class Meta(UserCreationForm.Meta):
|
|
||||||
model = User
|
|
||||||
|
|
||||||
def clean_username(self):
|
|
||||||
username = self.cleaned_data["username"]
|
|
||||||
try:
|
|
||||||
User.objects.get(username=username)
|
|
||||||
except User.DoesNotExist:
|
|
||||||
return username
|
|
||||||
raise forms.ValidationError(self.error_messages['duplicate_username'])
|
|
||||||
|
|
||||||
|
|
||||||
@admin.register(User)
|
|
||||||
class UserAdmin(AuthUserAdmin):
|
|
||||||
form = MyUserChangeForm
|
|
||||||
add_form = MyUserCreationForm
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue