Пример #1
0
def test_open_output_file_list():
    # given
    config = {
        "template_dir":
        "templates",
        "cache_template_dir":
        "temp/mako_modules",
        "output_dir":
        "output",
        "template_list": [{
            "header_template_name": "csv/sample_header.template.csv",
            "data_template_name": "csv/sample.template.csv",
            "footer_template_name": "",
            "output_file_name": "sample.csv"
        }, {
            "header_template_name": "json/sample_header.template.json",
            "data_template_name": "json/sample.template.json",
            "footer_template_name": "json/sample_footer.template.json",
            "output_file_name": "sample{record_index}.json",
            "one_file_per_record": True
        }]
    }
    template_list = load_template_list_from_config(config, "my-stream")
    # when
    output_file_list = open_output_file_list(config, template_list,
                                             "my-stream")
    # then
    assert output_file_list is not None
    assert 1 == len(output_file_list)
    assert "sample.csv" in output_file_list
    assert output_file_list["sample.csv"] is not None
    assert isinstance(output_file_list["sample.csv"], TextIOWrapper)
    output_file_list["sample.csv"].close()
Пример #2
0
def prepare_data_for_rendering(config, stream):
    template_list = load_template_list_from_config(config, stream)
    templates = template_list[0]
    output_file = TemporaryFile("w+", encoding="utf8", newline='')
    record_dict = {
        "checked":
        False,
        "dimensions": {
            "width": 10,
            "height": 20
        },
        "id":
        4529370162,
        "name":
        "Ward",
        "color":
        "red",
        "price":
        -6013876.97,
        "tags": [
            "giuWZuYElGsAQRrAVkwoPhEkmGYAEW", "KxRwOKWSnFSkKCvlCi",
            "OvenWokplmxxJgokYsaTZFccqBCXFs"
        ],
        "hour":
        "11:05:30 PM"
    }
    record_values = TemplateValues(record_dict)
    schema = {}
    schema_values = TemplateValues(schema)
    rendering_functions = {}
    return output_file, record_values, schema_values, templates, rendering_functions
Пример #3
0
def test_get_or_open_file_for_template_one_file():
    # given
    config = {
        "template_dir":
        "templates",
        "cache_template_dir":
        "temp/mako_modules",
        "output_dir":
        "output",
        "template_list": [{
            "header_template_name": "csv/sample_header.template.csv",
            "data_template_name": "csv/sample.template.csv",
            "footer_template_name": "csv/sample_header.template.csv",
            "output_file_name": "sample.csv"
        }]
    }
    one_file_per_record = True
    output_file_list = {}
    output_file = TemporaryFile("w+", encoding="utf8", newline='')
    output_file_list["sample.csv"] = output_file
    record_dict = {
        "checked":
        False,
        "dimensions": {
            "width": 10,
            "height": 20
        },
        "id":
        4529370162,
        "name":
        "Ward",
        "color":
        "red",
        "price":
        -6013876.97,
        "tags": [
            "giuWZuYElGsAQRrAVkwoPhEkmGYAEW", "KxRwOKWSnFSkKCvlCi",
            "OvenWokplmxxJgokYsaTZFccqBCXFs"
        ],
        "hour":
        "11:05:30 PM"
    }
    template_list = load_template_list_from_config(config, "my-stream")
    templates = template_list[0]
    # when
    tested_file = get_or_open_file_for_template(config, one_file_per_record,
                                                output_file_list, record_dict,
                                                templates, "my-stream")
    # then
    assert tested_file is not None
    assert tested_file.readable()
    assert tested_file.writable()
    tested_file.close()
Пример #4
0
def test_load_template_list_from_config_stream_specific():
    # given
    config = {
        "template_dir": "default-templates",
        "cache_template_dir": "temp/mako_modules",
        "template_list": [],
        "stream_configs": {
            "my-stream": {
                "template_dir":
                "templates",
                "template_list": [{
                    "header_template_name": "csv/sample_header.template.csv",
                    "data_template_name": "csv/sample.template.csv",
                    "footer_template_name": "",
                    "output_file_name": "sample.csv"
                }, {
                    "header_template_name": "json/sample_header.template.json",
                    "data_template_name": "json/sample.template.json",
                    "footer_template_name": "json/sample_footer.template.json",
                    "output_file_name": "sample{record_index}.json",
                    "one_file_per_record": True
                }]
            }
        }
    }
    # when
    template_list = load_template_list_from_config(config, "my-stream")
    # then
    assert template_list is not None
    assert 2 == len(template_list)
    assert "header" in template_list[0]
    assert template_list[0]["header"] is not None
    assert "line" in template_list[0]
    assert template_list[0]["line"] is not None
    assert "footer" in template_list[0]
    assert template_list[0]["footer"] is None
    assert "output_filename" in template_list[0]
    assert template_list[0]["output_filename"] is not None
    assert "one_file_per_record" in template_list[0]
    assert template_list[0]["one_file_per_record"] is False
    assert "one_file_per_record" in template_list[1]
    assert template_list[1]["one_file_per_record"] is True