Is there any reason to test model methods or I should assume that Django works properly and leave them untested?
Here is my model and couple methods from it:
class SlackTeam(models.Model):
...
def get_user(self, user_id):
return self.users.filter(user_id=user_id).first()
def deactivate(self):
self.active = False
self.initiator.access_token = ''
self.initiator.save()
self.initiator = None
self.deactivated_at = timezone.now()
self.save()
if hasattr(self, 'slackbot'):
self.slackbot.delete()
def set_initiator(self, user):
self.initiator = user
self.save(update_fields=['initiator'])
@classmethod
def initialize(cls, team_id, name):
return cls.objects.update_or_create(
team_id=team_id, defaults={'name': name})[0]
@classmethod
def get_by_team_id(cls, team_id):
return cls.objects.filter(team_id=team_id).first()
You can safely assume that methods defined on the base models.Model
class work. Your own methods - either custom ones or overridden - have to be tested, of course.
As a side note: the convention with Django models is to define methods working at the table level on the manager, not on the model itself so at least your get_by_team_id
and possibly initialize
should be defined on a custom manager.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.