Django: how to do unit test in updating views and models for web functionality

Viewed 58

I am a testing learner and I have been learning to do unit test for a web project with django views and models.

I am taking one of the perspect I want to test on as a learning example, which is a updating functionality of the website.

class Tradie(models.Model):
    myUser = models.OneToOneField(MyUser, on_delete=models.CASCADE, primary_key=True)
    joinDate = models.DateField(auto_now_add=True)
    first_name = models.CharField(max_length=50)
    last_name = models.CharField(max_length=50)
    gender = models.CharField(
        null=False,
        max_length=10,
        choices=[(_type.name, _type.value) for _type in GenderChoice],
        default='Male',
        blank=True
    )
    description = models.CharField(max_length=200, blank=True)
    phone = models.CharField(max_length=10, blank=True)
    address1 = models.CharField(max_length=100, blank=True)
    address2 = models.CharField(max_length=100, blank=True, null=True)
    suburb = models.CharField(max_length=30, blank=True)
    state = models.CharField(
        null=False,
        max_length=10,
        choices=[(_type.name, _type.value) for _type in States],
        default='ACT',
        blank=False
    )
    postcode = models.CharField(max_length=5, blank=True)

and the corresponding tradie profile based on the class is as seen.

def tradie_profile(request):
    if request.user.is_authenticated:
        try:
            tradie = Tradie.objects.get(myUser=request.user)
        except Tradie.DoesNotExist:
            raise Http404("Tradie does not exist")

        context = {
            "login_status": json.dumps(True),
            "description": tradie.description,
            "fullname": tradie.first_name + " " + tradie.last_name,
            "address": str(tradie.address1 + " " + tradie.suburb + " " + tradie.state + " " + tradie.postcode),
            "phone": tradie.phone,
    }
        return render(request, "Tradie/tradie_profile.html", context)
    else:
        raise Http404("Haven't logged in")

and the corresponding update function for updating the profile is as seen.

def update_tradie_profile(request):
    tradie = Tradie.objects.get(myUser=request.user)
    tradie.description = request.POST["description"]
    full_name = request.POST["fullName"]
    first_name = full_name.split(" ", 1)[0]
    last_name = full_name.split(" ", 1)[-1]
    tradie.first_name = first_name
    tradie.last_name = last_name

    address = request.POST["address"]

    address = address.split()
    tradie.suburb = address[-3]
    tradie.state = address[-2]
    tradie.postcode = address[-1]
    str = ' '
    tradie.address1 = str.join(address[0:-3])
    number = request.POST["number"]
    tradie.phone = number


    tradie.save()
    return HttpResponseRedirect("tradie_profile")

I have been reading through documentation in regarding request and response that this function is using and various videos in regards to django testing. And I am having a hard time understanding how to test on such functionality of a website. Besides seting up a "mock user" as a utility for testing, I don't know where to get started on.

Any hint on how do I get started or some explanation on the code or some recommendation in reading will be very much appreciated. Thank you.

0 Answers
Related