Exemplo n.º 1
0
def test_modify2():
    modifier = FileModifier("test/minimal_multiline.view.lkml")
    assert len(modifier.lines) == 19
    modifier.modify(6, "dimension", "city_code", "a description", True)
    assert len(modifier.lines) == 14
    assert modifier.lines[
        5] == "    description: \"a description\"	# programmatically added by LookML modifier\n"
Exemplo n.º 2
0
def test_modify1():
    modifier = FileModifier("test/minimal.lkml")
    assert len(modifier.lines) == 13

    # add a description to city_code
    modifier.modify(1, "dimension", "city_code", "a description", False)
    assert len(modifier.lines) == 14
    assert modifier.lines[4] == "    description: \"a description\"	# programmatically added by LookML modifier\n"
Exemplo n.º 3
0
def test_handle_subscription_substitution():
    modifier = FileModifier("test/minimal_multiline.view.lkml")
    assert len(modifier.lines) == 19
    modifier.handle_desription_substitution(6, "dimension", "city_code",
                                            "a description")
    assert len(modifier.lines) == 14
    assert modifier.lines[
        5] == "    description: \"a description\"	# programmatically added by LookML modifier\n"
Exemplo n.º 4
0
def test_handle_addition():
    modifier = FileModifier("test/minimal.lkml")
    assert len(modifier.lines) == 13

    # add a description to city_code
    modifier.handle_description_addition("dimension", "city_code", "a description")
    assert len(modifier.lines) == 14
    assert modifier.lines[4] == "    description: \"a description\"	# programmatically added by LookML modifier\n"
Exemplo n.º 5
0
def test_write():
    modifier = FileModifier("test/minimal.lkml")
    filename = "test/test_write.lkml"
    if os.path.exists(filename):
        os.remove(filename)

    modifier.write(filename)
    assert os.path.exists(filename)

    original = open("test/minimal.lkml", 'r').readlines()
    written = open(filename, 'r').readlines()

    assert original == written

    if os.path.exists(filename):
        os.remove(filename)
Exemplo n.º 6
0
    def modify(self, infilepath, outfilepath):
        '''modify the LookML

        Notes:
            default behavior is to match on full path when matching LookML files
            with the definitions source. 
            However, you can configure to match on LookML file basename by setting
            ``"use_basename": true`` in the config

        Args:
            infilepath (str): path to input LookML file
            outfilepath (str): path of updated LookML to wtite to

        Returns:
            nothing. Writes out modified file contents to file

        '''
        modifier = FileModifier(infilepath)

        lookml = LookML(infilepath)

        # get definitions for this file. In some cases, we might not
        # easily know the full path (such as full_auto_updater.sh which
        # uses timestamp in the git clone). Thus, just match on basename
        if 'use_basename' in self.config and self.config['use_basename']:
            logging.info("Matching files based on basename")
            defs = self.definitions[self.definitions.file == os.path.basename(infilepath)]
        else:
            defs = self.definitions[self.definitions.file == infilepath]

        for definition in defs.T.to_dict().values():
            logging.info("Processing %s: %s", definition['type'], definition['name'])

            description, has_key = self.find_description(lookml, definition['type'], definition['name'])

            num_lines = len(description.split("\n"))

            if has_key:
                logging.info("Existing description for %s.%s: '%s'", definition['type'], definition['name'], description)
            else:
                logging.info("No description for %s.%s", definition['type'], definition['name'])

            exepected_description = definition['definition']

            if description != exepected_description:
                if has_key:
                    logging.info("Update needed: %s.%s -> '%s'", definition['type'], definition['name'], exepected_description)
                    logging.info("This is %d line existing description", num_lines)
                else:
                    logging.info("Injection needed: %s.%s -> '%s'", definition['type'], definition['name'], exepected_description)

                modifier.modify(num_lines, definition['type'], definition['name'], exepected_description, has_key)

        modifier.write(outfilepath)
Exemplo n.º 7
0
def test_init():
    modifier = FileModifier("test/minimal.lkml")
    assert len(modifier.lines) == 13
Exemplo n.º 8
0
def test_is_header2():
    modifier = FileModifier("test/minimal.lkml")
    assert not modifier.is_header("dimension: somethingelse {", "dimension",
                                  "myname")
Exemplo n.º 9
0
def test_is_header():
    modifier = FileModifier("test/minimal.lkml")
    assert modifier.is_header("dimension: myname {", "dimension", "myname")
Exemplo n.º 10
0
def test_init2():
    with pytest.raises(Exception) as e:
        modifier = FileModifier("test/doesnotexist")
    assert 'Filename does not exist: test/doesnotexist' in str(e.value)