예제 #1
0
    def generateCode(self, test):
        """
            Generate code has to be smart enough to determine if the json array should generate a test case, or a
            fixture or if it needs to generate both.

            * If setup or teardown is present, then its a test case
            * If parent is present, then it needs to extend a fixture
        """
        #TODO: Maybe we should have a yaml validation class?
        #Totaly agree - that makes perfect sense.
        from jinja2 import Environment, FileSystemLoader
        from time import strftime, gmtime

        # TODO: Find a more efficient way to pull in this template other than ../
        # Is is possible to parameterize the template directory?  It should be a static location... - Alex
        # Maybe we can use the PackageLoader
        out_path = "%s/../templates/" % os.path.dirname(os.path.abspath(__file__))
        j2_env = Environment(loader=FileSystemLoader(out_path), trim_blocks=True, lstrip_blocks=True)

        # Header lines created here and added to the templates as required
        header = "#!/usr/bin/python\n" \
                 "#\n" \
                 "# This file was created by etlUnit.\n#" \
                 " Create date: %s\n" \
                 "#\n" % \
                 strftime("%a, %d %b %Y %X +0000", gmtime())

        for yml in self.yaml_data.keys():
            self.log.info("Generating code from %s..." % yml)
            self.yml_data = self.yaml_data[yml]

            # TODO: Determine how we handle dependencies on single files.
            # TODO: Added fixture definition to the mix. Currently it generates a fixture but it has no variables.
            try:
                if self.yml_data['fixture'] is not None:
                    from etlunit.yaml_reader import YAMLReader
                    self.fixture = self.yml_data['fixture']
                    fixture_res = "../res/%s.yml" % self.fixture
                    reader = YAMLReader(fixture_res, None)
                    fixture_data = reader.readTests()[fixture_res]

                    self.template_output = j2_env.get_template("testfixture.jj2")\
                        .render(header=header,
                                fixture=self.fixture,
                                setup=fixture_data['setup'],
                                teardown=fixture_data['teardown'])

                    self.persist_output(self.yml_data['fixture'], self.template_output, test)
            except KeyError:
                self.fixture = "unittest.TestCase"  # Default value for fixture
                self.log.info("Fixture not present, generating TestSuite...")
            finally:
                self.template_output = j2_env.get_template("testsuite.jj2") \
                    .render(header=header,
                            fixture=self.fixture,
                            tests=self.yml_data['tests'],
                            suitename=self.yml_data['name'].replace(' ', ''))

                self.persist_output(self.yml_data['name'], self.template_output, test)
        self.log.info("Code generation complete.")
예제 #2
0
def main(argv):
    """
        This class is the entry point for the application. It takes the arguments, validates them, and passes
        them on to the appropriate classes to continue execution.

        There are three main functions of this application.
        1) Take in yaml
        2) Generate code from that yaml
        3) Execute that code so that we can take advantage of the unittest libraries
    """
    parser = optparse.OptionParser("usage: %prog [options]")

    # no arguments, print usage
    if len(argv) == 0:
        parser.print_usage()

    # all available options are defined here
    parser.add_option("-f", "--infile", dest="in_file", type="string", help="Specify the input file.")
    parser.add_option("-d", "--indir", dest="in_dir", type="string", help="Specify the input directory.")
    parser.add_option("-o", "--outdir", dest="out_dir", type="string", help="Specify the output directory.")
    parser.add_option("-g", "--gen", dest="gen_code", default=False, action="store_true",
                      help="Generate new test code.")
    parser.add_option("-e", "--exec", dest="exec_code", default=False, action="store_true",
                      help="Execute test code.")
    parser.add_option("-t", "--test", dest="test_run", default=False, action="store_true",
                      help="Run app as tests. Does not persist generated code or execute code.")
    (options, args) = parser.parse_args()

    # validating options
    if options.in_file and options.in_dir:
        parser.error("Options infile and indir are mutually exclusive. Please choose one.")

    if options.gen_code:
        from etlunit.yaml_reader import YAMLReader
        t = YAMLReader(options.in_file, options.in_dir)
        yaml_data = t.readTests()

        from etlunit.code_generator import CodeGenerator
        g = CodeGenerator(options.out_dir, yaml_data)
        # TODO: Decide if there should be a way to check if generated code should be updated or not.
        g.generateCode(options.test_run)

    if options.exec_code:
        from etlunit.code_executor import CodeExecutor
        e = CodeExecutor(options.out_dir)
        e.execute(options.test_run)
예제 #3
0
파일: etlUnit.py 프로젝트: yileye/etlUnit
def main(argv):
    """
        This class is the entry point for the application. It takes the arguments, validates them, and passes
        them on to the appropriate classes to continue execution.

        There are three main functions of this application.
        1) Take in yaml
        2) Generate code from that yaml
        3) Execute that code so that we can take advantage of the unittest libraries
    """
    parser = optparse.OptionParser("usage: %prog [options]")

    # no arguments, print usage
    if len(argv) == 0:
        parser.print_usage()

    # all available options are defined here
    parser.add_option("-f",
                      "--infile",
                      dest="in_file",
                      type="string",
                      help="Specify the input file.")
    parser.add_option("-d",
                      "--indir",
                      dest="in_dir",
                      type="string",
                      help="Specify the input directory.")
    parser.add_option("-o",
                      "--outdir",
                      dest="out_dir",
                      type="string",
                      help="Specify the output directory.")
    parser.add_option("-g",
                      "--gen",
                      dest="gen_code",
                      default=False,
                      action="store_true",
                      help="Generate new test code.")
    parser.add_option("-e",
                      "--exec",
                      dest="exec_code",
                      default=False,
                      action="store_true",
                      help="Execute test code.")
    parser.add_option(
        "-t",
        "--test",
        dest="test_run",
        default=False,
        action="store_true",
        help=
        "Run app as tests. Does not persist generated code or execute code.")
    (options, args) = parser.parse_args()

    # validating options
    if options.in_file and options.in_dir:
        parser.error(
            "Options infile and indir are mutually exclusive. Please choose one."
        )

    if options.gen_code:
        from etlunit.yaml_reader import YAMLReader
        t = YAMLReader(options.in_file, options.in_dir)
        yaml_data = t.readTests()

        from etlunit.code_generator import CodeGenerator
        g = CodeGenerator(options.out_dir, yaml_data)
        # TODO: Decide if there should be a way to check if generated code should be updated or not.
        g.generateCode(options.test_run)

    if options.exec_code:
        from etlunit.code_executor import CodeExecutor
        e = CodeExecutor(options.out_dir)
        e.execute(options.test_run)