Ejemplo n.º 1
0
class TestModelRow(unittest.TestCase):
    # pylint: disable=invalid-name, bad-whitespace
    HEAD = [u"name", u"sex", u"age"]
    DATA = [u"Alice", u"female", u"12"]

    def setUp(self):
        self.row = Row(self.HEAD, self.DATA, 0)

    def test_len(self):
        assert len(self.row) == 3

    def test_getitem_with_valid_colname(self):
        # pylint: disable=bad-whitespace
        assert self.row["name"] == u"Alice"
        assert self.row["sex"] == u"female"
        assert self.row["age"] == u"12"

    def test_getitem_with_unknown_colname(self):
        with pytest.raises(KeyError):
            # pylint: disable=pointless-statement
            self.row["__UNKNOWN_COLUMN__"]

    def test_getitem_with_valid_index(self):
        assert self.row[0] == u"Alice"
        assert self.row[1] == u"female"
        assert self.row[2] == u"12"

    def test_getitem_with_invalid_index(self):
        colsize = len(self.row)
        assert colsize == 3
        with pytest.raises(IndexError):
            self.row[colsize]  # pylint: disable=pointless-statement

    def test_get_with_valid_colname(self):
        # pylint: disable=bad-whitespace
        assert self.row.get("name") == u"Alice"
        assert self.row.get("sex") == u"female"
        assert self.row.get("age") == u"12"

    def test_getitem_with_unknown_colname_should_return_default(self):
        assert self.row.get("__UNKNOWN_COLUMN__", "XXX") == u"XXX"

    def test_as_dict(self):
        data1 = self.row.as_dict()
        data2 = dict(self.row.as_dict())
        assert isinstance(data1, dict)
        assert isinstance(data2, dict)
        assert isinstance(data1, OrderedDict)
        # -- REQUIRES: Python2.7 or ordereddict installed.
        # assert not isinstance(data2, OrderedDict)
        assert data1 == data2
        # pylint: disable=bad-whitespace
        assert data1["name"] == u"Alice"
        assert data1["sex"] == u"female"
        assert data1["age"] == u"12"
Ejemplo n.º 2
0
class TestModelRow(unittest.TestCase):
    # pylint: disable=invalid-name, bad-whitespace
    HEAD = [u"name",  u"sex",   u"age"]
    DATA = [u"Alice", u"female", u"12"]

    def setUp(self):
        self.row = Row(self.HEAD, self.DATA, 0)

    def test_len(self):
        assert len(self.row) == 3

    def test_getitem_with_valid_colname(self):
        # pylint: disable=bad-whitespace
        assert self.row["name"] == u"Alice"
        assert self.row["sex"] == u"female"
        assert self.row["age"] == u"12"

    def test_getitem_with_unknown_colname(self):
        with pytest.raises(KeyError):
            # pylint: disable=pointless-statement
            self.row["__UNKNOWN_COLUMN__"]

    def test_getitem_with_valid_index(self):
        assert self.row[0] == u"Alice"
        assert self.row[1] == u"female"
        assert self.row[2] == u"12"

    def test_getitem_with_invalid_index(self):
        colsize = len(self.row)
        assert colsize == 3
        with pytest.raises(IndexError):
            self.row[colsize]   # pylint: disable=pointless-statement

    def test_get_with_valid_colname(self):
        # pylint: disable=bad-whitespace
        assert self.row.get("name") == u"Alice"
        assert self.row.get("sex") ==  u"female"
        assert self.row.get("age") ==  u"12"

    def test_getitem_with_unknown_colname_should_return_default(self):
        assert self.row.get("__UNKNOWN_COLUMN__", "XXX") == u"XXX"

    def test_as_dict(self):
        data1 = self.row.as_dict()
        data2 = dict(self.row.as_dict())
        assert isinstance(data1, dict)
        assert isinstance(data2, dict)
        assert isinstance(data1, OrderedDict)
        # -- REQUIRES: Python2.7 or ordereddict installed.
        # assert not isinstance(data2, OrderedDict)
        assert data1 == data2
        # pylint: disable=bad-whitespace
        assert data1["name"] == u"Alice"
        assert data1["sex"] == u"female"
        assert data1["age"] == u"12"
Ejemplo n.º 3
0
class TestModelRow(unittest.TestCase):
    # pylint: disable=invalid-name, bad-whitespace
    HEAD = [u'name',  u'sex',   u'age']
    DATA = [u'Alice', u'female', u'12']

    def setUp(self):
        self.row = Row(self.HEAD, self.DATA, 0)

    def test_len(self):
        eq_(len(self.row), 3)

    def test_getitem_with_valid_colname(self):
        # pylint: disable=bad-whitespace
        eq_(self.row['name'], u'Alice')
        eq_(self.row['sex'],  u'female')
        eq_(self.row['age'],  u'12')

    @raises(KeyError)
    def test_getitem_with_unknown_colname(self):
        self.row['__UNKNOWN_COLUMN__']  # pylint: disable=pointless-statement

    def test_getitem_with_valid_index(self):
        eq_(self.row[0], u'Alice')
        eq_(self.row[1], u'female')
        eq_(self.row[2], u'12')

    @raises(IndexError)
    def test_getitem_with_invalid_index(self):
        colsize = len(self.row)
        eq_(colsize, 3)
        self.row[colsize]   # pylint: disable=pointless-statement

    def test_get_with_valid_colname(self):
        # pylint: disable=bad-whitespace
        eq_(self.row.get('name'), u'Alice')
        eq_(self.row.get('sex'),  u'female')
        eq_(self.row.get('age'),  u'12')

    def test_getitem_with_unknown_colname_should_return_default(self):
        eq_(self.row.get('__UNKNOWN_COLUMN__', 'XXX'), u'XXX')

    def test_as_dict(self):
        data1 = self.row.as_dict()
        data2 = dict(self.row.as_dict())
        assert isinstance(data1, dict)
        assert isinstance(data2, dict)
        assert isinstance(data1, OrderedDict)
        # -- REQUIRES: Python2.7 or ordereddict installed.
        # assert not isinstance(data2, OrderedDict)
        eq_(data1, data2)
        # pylint: disable=bad-whitespace
        eq_(data1['name'], u'Alice')
        eq_(data1['sex'],  u'female')
        eq_(data1['age'],  u'12')
Ejemplo n.º 4
0
class TestModelRow(unittest.TestCase):
    # pylint: disable=invalid-name, bad-whitespace
    HEAD = [u'name', u'sex', u'age']
    DATA = [u'Alice', u'female', u'12']

    def setUp(self):
        self.row = Row(self.HEAD, self.DATA, 0)

    def test_len(self):
        eq_(len(self.row), 3)

    def test_getitem_with_valid_colname(self):
        # pylint: disable=bad-whitespace
        eq_(self.row['name'], u'Alice')
        eq_(self.row['sex'], u'female')
        eq_(self.row['age'], u'12')

    @raises(KeyError)
    def test_getitem_with_unknown_colname(self):
        self.row['__UNKNOWN_COLUMN__']  # pylint: disable=pointless-statement

    def test_getitem_with_valid_index(self):
        eq_(self.row[0], u'Alice')
        eq_(self.row[1], u'female')
        eq_(self.row[2], u'12')

    @raises(IndexError)
    def test_getitem_with_invalid_index(self):
        colsize = len(self.row)
        eq_(colsize, 3)
        self.row[colsize]  # pylint: disable=pointless-statement

    def test_get_with_valid_colname(self):
        # pylint: disable=bad-whitespace
        eq_(self.row.get('name'), u'Alice')
        eq_(self.row.get('sex'), u'female')
        eq_(self.row.get('age'), u'12')

    def test_getitem_with_unknown_colname_should_return_default(self):
        eq_(self.row.get('__UNKNOWN_COLUMN__', 'XXX'), u'XXX')

    def test_as_dict(self):
        data1 = self.row.as_dict()
        data2 = dict(self.row.as_dict())
        assert isinstance(data1, dict)
        assert isinstance(data2, dict)
        assert isinstance(data1, OrderedDict)
        # -- REQUIRES: Python2.7 or ordereddict installed.
        # assert not isinstance(data2, OrderedDict)
        eq_(data1, data2)
        # pylint: disable=bad-whitespace
        eq_(data1['name'], u'Alice')
        eq_(data1['sex'], u'female')
        eq_(data1['age'], u'12')
Ejemplo n.º 5
0
def load_empty_table(context, model_name, vault_structure):
    """Creates an empty table"""

    context.target_model_name = model_name
    columns = context.vault_structure_columns

    if vault_structure == "stage":
        headings = context.stage_columns[model_name]
    else:
        headings = list(
            DBTVAULTGenerator.flatten(
                [val for key, val in columns[model_name].items()]))

    row = Row(cells=[], headings=headings)

    empty_table = Table(headings=headings, rows=row)

    seed_file_name = context.dbt_test_utils.context_table_to_csv(
        table=empty_table, model_name=model_name)

    dbtvault_generator.add_seed_config(
        seed_name=seed_file_name, seed_config=context.seed_config[model_name])

    logs = context.dbt_test_utils.run_dbt_seed(seed_file_name=seed_file_name)

    if not vault_structure == "stage":
        metadata = {
            "source_model": seed_file_name,
            **context.vault_structure_columns[model_name]
        }

        context.vault_structure_metadata = metadata

        dbtvault_generator.raw_vault_structure(model_name, vault_structure,
                                               **metadata)

        logs = context.dbt_test_utils.run_dbt_model(mode="run",
                                                    model_name=model_name)

    assert "Completed successfully" in logs
Ejemplo n.º 6
0
def convert_table_kv_to_skv(table, new_headings, new_column):
    """
    Convert kv based table to skv based table by adding one extra column.

    :param behave.model.Table table: Original kv based table
    :param list new_headings: Headings for the skv based table (3 elements, where 2 of them are also in the original table)
    :param list new_column: Strings to be added as the new column (empty string is used when there is not enough items)
    :return: New skv based table
    :rtype: behave.model.Table
    """
    headings = table.headings
    new_column_index = new_headings.index(list(set(new_headings) - set(headings))[0])
    i = 0
    new_rows = []
    for row in table.rows:
        value = new_column[i] if i < len(new_column) else ""
        i += 1
        new_values = row.cells[:]
        new_values.insert(new_column_index, value)
        new_row = Row(new_headings, new_values)
        new_rows.append(new_row)
    new_table = Table(new_headings, rows=new_rows)
    return new_table
Ejemplo n.º 7
0
def clear_schema(context):
    context.dbt_test_utils.replace_test_schema()

    model_names = context.dbt_test_utils.context_table_to_dict(
        table=context.table, orient="list")

    context.vault_model_names = model_names

    models = [
        name for name in DBTVAULTGenerator.flatten(
            [v for k, v in model_names.items()]) if name
    ]

    for model_name in models:
        headings_dict = dbtvault_generator.evaluate_hashdiff(
            copy.deepcopy(context.vault_structure_columns[model_name]))

        headings = list(
            DBTVAULTGenerator.flatten(
                [v for k, v in headings_dict.items() if k != "source_model"]))

        row = Row(cells=[], headings=headings)

        empty_table = Table(headings=headings, rows=row)

        seed_file_name = context.dbt_test_utils.context_table_to_csv(
            table=empty_table, model_name=model_name)

        dbtvault_generator.add_seed_config(
            seed_name=seed_file_name,
            seed_config=context.seed_config[model_name])

        logs = context.dbt_test_utils.run_dbt_seed(
            seed_file_name=seed_file_name)

        assert "Completed successfully" in logs
Ejemplo n.º 8
0
 def setUp(self):
     self.row = Row(self.HEAD, self.DATA, 0)
Ejemplo n.º 9
0
 def setUp(self):
     self.row = Row(self.HEAD, self.DATA, 0)
Ejemplo n.º 10
0
 def test_table_row_by_index(self):
     for i in range(3):
         eq_(self.table[i], Row(self.HEAD, self.DATA[i], 0))
Ejemplo n.º 11
0
def make_row(*data, **kwargs):
    line = kwargs.pop("line", None)
    data2 = dict(data, **kwargs)
    headings = list(data2.keys())
    cells = [text(value) for value in data2.values()]
    return Row(headings, cells, line=line)
Ejemplo n.º 12
0
 def test_table_row_by_index(self):
     for i in range(3):
         assert self.table[i] == Row(self.HEAD, self.DATA[i], 0)