mercredi 31 octobre 2018

Should I test methods in Django models?

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()

Aucun commentaire:

Enregistrer un commentaire