mercredi 2 septembre 2015

How to write unit test cases for this Django Application?

I've been asked to write unit test cases for file generator classes which are present inside django app.

The structure and flow goes like this:

User fills a form from the frontend for server configuration setup and data gets saved in DB. Now using some file_generator classes we can generate files with predefined specifications, which will be having same data as input provided by user. I need to write unit test cases for each file_generator class. I am completely blank that how should i proceed with this? Probably I'll have to initialize input data in my test class and match it with the output file data. If yes then how this is to be done?

Note:Please help considering my programming skills are not good :(

sample files for reference:

Ro_Generator(There are multiple generators like this)

from api.lcp.models import Server
from .group_vars_generator import GroupVarsGenerator

class RoGenerator(GroupVarsGenerator):
    def data(self):
        data = {
            "ro_servers": Server.objects.filter(environment=self.env_id, server_type='ro')
        }

        return data

GroupVarsGenerator:

import os

from api.lcp.models import Server
from ..base_generator import BaseGenerator


class GroupVarsGenerator(BaseGenerator):
    def __init__(self, env_id):
        super(  ).__init__(env_id)
        self.out_dir_path += "/group_vars/"
        self.template_path += "/group_vars/"
        command = "mkdir -p " + self.out_dir_path
        os.system(command)

The output which I receive:

Generated File at some location looks like this -

ro1:
    domain_name: root
    ram: 3
    cpu: 3
    image_name: "ro"
    ip_address: "192.168.2.2"
    netmask: "192.168.2.2"
    gateway: "192.168.2.2"
    dns: "192.168.3.3"
    domain_user: root
    domain_password: root

and data in a dictionary as specified in class -

{'ro_servers': [<Server: sanchit ro>]}

Aucun commentaire:

Enregistrer un commentaire