Skip to content

Commit

Permalink
test_metadata: show current behavior with internal quotes
Browse files Browse the repository at this point in the history
Shows the undesired behavior with internal quotes described in
<#1312> comes from the
`write_records_to_tsv` function.
  • Loading branch information
joverlee521 committed Jun 27, 2024
1 parent dc97cde commit f0bd23e
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions tests/io/test_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ def expected_record():
return {
'strain': 'SEQ_A',
'date': '2020-10-03',
'country': 'USA'
'country': 'USA',
'lab': 'A Virology Lab "Vector"'
}

@pytest.fixture
Expand All @@ -36,29 +37,29 @@ class TestReadMetadataToDict:
def test_read_table_to_dict_with_csv(self, tmpdir, expected_record):
path = str(tmpdir / 'metadata.csv')
with open(path, 'w') as fh:
fh.write('strain,date,country\n')
fh.write('SEQ_A,2020-10-03,USA\n')
fh.write('strain,date,country,lab\n')
fh.write('SEQ_A,2020-10-03,USA,A Virology Lab "Vector"\n')

record = next(read_table_to_dict(path, (',')))
assert record == expected_record

def test_read_table_to_dict_with_csv_from_stdin(self, mp_context, expected_record):
stdin = StringIO('strain,date,country\nSEQ_A,2020-10-03,USA\n')
stdin = StringIO('strain,date,country,lab\nSEQ_A,2020-10-03,USA,A Virology Lab "Vector"\n')
mp_context.setattr('sys.stdin', stdin)
record = next(read_table_to_dict(sys.stdin, (',')))
assert record == expected_record

def test_read_table_to_dict_with_tsv(self, tmpdir, expected_record):
path = str(tmpdir / 'metadata.tsv')
with open(path, 'w') as fh:
fh.write('strain\tdate\tcountry\n')
fh.write('SEQ_A\t2020-10-03\tUSA\n')
fh.write('strain\tdate\tcountry\tlab\n')
fh.write('SEQ_A\t2020-10-03\tUSA\tA Virology Lab "Vector"\n')

record = next(read_table_to_dict(path, ('\t')))
assert record == expected_record

def test_read_table_to_dict_with_tsv_from_stdin(self, mp_context, expected_record):
stdin = StringIO('strain\tdate\tcountry\nSEQ_A\t2020-10-03\tUSA\n')
stdin = StringIO('strain\tdate\tcountry\tlab\nSEQ_A\t2020-10-03\tUSA\tA Virology Lab "Vector"\n')
mp_context.setattr('sys.stdin', stdin)
record = next(read_table_to_dict(sys.stdin, ('\t')))
assert record == expected_record
Expand Down Expand Up @@ -457,15 +458,15 @@ def test_read_metadata_with_sequences_with_extra_and_dup_warn_both(self, capsys,
@pytest.fixture
def output_records():
return iter([
{"strain": "SEQ_A", "country": "USA", "date": "2020-10-01"},
{"strain": "SEQ_A", "country": "\"USA\"", "date": "2020-10-01"},
{"strain": "SEQ_T", "country": "USA", "date": "2020-10-02"}
])

@pytest.fixture
def expected_output_tsv():
return (
"strain\tcountry\tdate\n"
"SEQ_A\tUSA\t2020-10-01\n"
'SEQ_A\t"""USA"""\t2020-10-01\n'
"SEQ_T\tUSA\t2020-10-02\n"
)

Expand Down Expand Up @@ -564,7 +565,7 @@ def test_blank_lines(self, tmpdir):
',,\n',
'5,2,3\n',
])

m = Metadata(path, delimiters=',', id_columns=['a'])
assert list(m.rows()) == [
{'a': '1', 'b': '2', 'c': '3'},
Expand Down

0 comments on commit f0bd23e

Please sign in to comment.