Пример #1
0
def test_output_csv_special_char(random1, random2):
    """
    Test to check the behavior of printing out special characters to CSV.
    Special characters are '\n', '\r', '"', and ','.
    """
    assume(random1)
    assume(random2)
    data = [{
        'special': random1 + ',' + random2
    }, {
        'special': random1 + '\n' + random2
    }, {
        'special': random1 + '\r' + random2
    }, {
        'special': random1 + '"' + random2
    }]
    output_file = WriteTester()
    script_output.write_csv(output_file, data)
    lines = output_file.lines()
    assert len(lines) == 4
    for line in lines:
        assert line.endswith('\n')
        test_str = line[:-1]
        assert test_str.startswith('"')
        assert test_str.endswith('"')
    quote_line = lines[3][:-1]
    before_replacement = random1 + '"' + random2
    assert quote_line.count('"') == before_replacement.count('"') * 2 + 2
Пример #2
0
def test_output_csv_no_data():
    """Test CSV written from no data"""
    data = []
    output_file = WriteTester()
    script_output.write_csv(output_file, data, header=False)
    lines = output_file.lines()
    assert len(lines) == 0
Пример #3
0
def test_output_csv_no_data():
    """Test CSV written from no data"""
    data = []
    output_file = WriteTester()
    script_output.write_csv(output_file, data, header=False)
    lines = output_file.lines()
    assert len(lines) == 0
Пример #4
0
def test_output_csv_no_header(random1, random2):
    """
    Test to make sure the computers CSV is written out correctly, with no header.
    """
    assume(
        all(substring not in random_str
            for substring in [',', '"', '\n', '\r']
            for random_str in [random1, random2]))
    data = [{
        'link': 'courage',
        'zelda': 'wisdom',
        'ganon': 'power',
        'num': 3
    }, {
        'ganon': random1,
        'link': random2
    }]
    output_file = WriteTester()
    script_output.write_csv(output_file, data, header=False)
    lines = output_file.lines()
    assert len(lines) == 2
    assert lines[0].split(',') == ['power', 'courage', str(3), 'wisdom\n']
    assert lines[1].split(',') == [
        random1.encode('UTF-8'),
        random2.encode('UTF-8'), '', '\n'
    ]
Пример #5
0
def test_output_csv(random1, random2):
    """Test to make sure CSV is written out correctly"""
    assume(all(substring not in random_str
               for substring in [',', '"', '\n', '\r']
               for random_str in [random1, random2]))
    data = [{'link': 'courage', 'zelda': 'wisdom', 'ganon': 'power', 'num': 3},
            {'ganon': random1, 'link': random2}]
    output_file = WriteTester()
    script_output.write_csv(output_file, data, header=True)
    lines = output_file.lines()
    assert len(lines) == 3
    assert lines[0].split(',') == ['ganon', 'link', 'num', 'zelda\n']
    assert lines[1].split(',') == ['power', 'courage', str(3), 'wisdom\n']
    assert lines[2].split(',') == [random1.encode('UTF-8'), random2.encode('UTF-8'), '', '\n']
Пример #6
0
    def write_lookup_row(out, json_dict, key_set, hash_map):
        """
        Write a row to the given lookup table if it should (based on hashes).

        :param out:       File handle to the lookup table to write to.
        :param json_dict: The json dictionary to write as a lookup talbe row.
        :param key_set:   KeySet object used to write the dictionary.
        :param hash_map:  Hash map to check to see if the row should be
                           written.
        """
        json_hash = hash_dictionary(json_dict)
        uid = json_dict[uid_key]
        if not hash_in_map(json_hash, hash_map, uid):
            script_output.write_csv(out, [json_dict], header=False, shallow=True, keyset=key_set)
            add_hash_to_map(json_hash, hash_map, uid)
Пример #7
0
def test_output_csv_given_keyset():
    """
    Test that a csv write works when a KeySet is provided ahead of time.
    """
    key_set = csv.KeySet()
    sub_dict = {'meow': 'cat'}
    data_dict = {'test': sub_dict}
    for key, value in data_dict.items():
        json_key = csv.create_key(key, value, shallow=True)
        key_set.add_key(json_key)
    data = [data_dict]
    output_file = WriteTester()
    script_output.write_csv(output_file, data, header=False, shallow=True, keyset=key_set)
    lines = output_file.lines()
    assert len(lines) == 1
    assert lines[0] == '"{""meow"": ""cat""}"\n'
Пример #8
0
def test_output_csv_shallow(random_str):
    """
    Test to verify a shallow parse works.
    """
    assume(random_str)
    assume(all(substring not in random_str
               for substring in [',', '"', '\n', '\r']))
    sub_dict = {'meow': random_str}
    data = [{'test': sub_dict}]

    output_file = WriteTester()
    script_output.write_csv(output_file, data, header=True, shallow=True)
    lines = output_file.lines()
    assert len(lines) == 2
    assert lines[0] == 'test\n'
    assert lines[1].startswith('"{""meow"": ')
    test_str = lines[1].split(':')[1]
    assert random_str in test_str
    assert lines[1].endswith('"\n')
Пример #9
0
def test_output_csv_given_keyset():
    """
    Test that a csv write works when a KeySet is provided ahead of time.
    """
    key_set = csv.KeySet()
    sub_dict = {'meow': 'cat'}
    data_dict = {'test': sub_dict}
    for key, value in data_dict.items():
        json_key = csv.create_key(key, value, shallow=True)
        key_set.add_key(json_key)
    data = [data_dict]
    output_file = WriteTester()
    script_output.write_csv(output_file,
                            data,
                            header=False,
                            shallow=True,
                            keyset=key_set)
    lines = output_file.lines()
    assert len(lines) == 1
    assert lines[0] == '"{""meow"": ""cat""}"\n'
Пример #10
0
def test_output_csv_shallow(random_str):
    """
    Test to verify a shallow parse works.
    """
    assume(random_str)
    assume(
        all(substring not in random_str
            for substring in [',', '"', '\n', '\r']))
    sub_dict = {'meow': random_str}
    data = [{'test': sub_dict}]

    output_file = WriteTester()
    script_output.write_csv(output_file, data, header=True, shallow=True)
    lines = output_file.lines()
    assert len(lines) == 2
    assert lines[0] == 'test\n'
    assert lines[1].startswith('"{""meow"": ')
    test_str = lines[1].split(':')[1]
    assert random_str in test_str
    assert lines[1].endswith('"\n')
Пример #11
0
def test_output_csv_special_char(random1, random2):
    """
    Test to check the behavior of printing out special characters to CSV.
    Special characters are '\n', '\r', '"', and ','.
    """
    assume(random1)
    assume(random2)
    data = [{'special': random1 + ',' + random2},
            {'special': random1 + '\n' + random2},
            {'special': random1 + '\r' + random2},
            {'special': random1 + '"' + random2}]
    output_file = WriteTester()
    script_output.write_csv(output_file, data)
    lines = output_file.lines()
    assert len(lines) == 4
    for line in lines:
        assert line.endswith('\n')
        test_str = line[:-1]
        assert test_str.startswith('"')
        assert test_str.endswith('"')
    quote_line = lines[3][:-1]
    before_replacement = random1 + '"' + random2
    assert quote_line.count('"') == before_replacement.count('"') * 2 + 2