-
Notifications
You must be signed in to change notification settings - Fork 1
/
bmerge_test.py
111 lines (103 loc) · 4.26 KB
/
bmerge_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import os
import string
import shell
from hypothesis.database import ExampleDatabase
from hypothesis import given, settings
from hypothesis.strategies import text, lists, composite, integers
from test_util import rm_whitespace, clone_source
import os
import shell
from test_util import unindent, rm_whitespace, clone_source
def setup_module(m):
m.tempdir = clone_source()
m.orig = os.getcwd()
m.path = os.environ['PATH']
os.chdir(m.tempdir)
os.environ['PATH'] = f'{os.getcwd()}/bin:/usr/bin:/usr/local/bin:/sbin:/usr/sbin:/bin'
shell.run('make clean && make bsv csv bsort bcut bmerge', stream=True)
def teardown_module(m):
os.chdir(m.orig)
os.environ['PATH'] = m.path
assert m.tempdir.startswith('/tmp/') or m.tempdir.startswith('/private/var/folders/')
shell.run('rm -rf', m.tempdir)
def test_dupes():
with shell.tempdir():
shell.run('echo -e "a,a\na,a\nc,c\nc,c\ne,e\ne,e\n" | bsv > a.bsv')
shell.run('echo -e "b,b\nd,d\nf,f\n" | bsv > b.bsv')
stdout = """
a,a
a,a
b,b
c,c
c,c
d,d
e,e
e,e
f,f
"""
assert rm_whitespace(unindent(stdout)) == shell.run('echo a.bsv b.bsv | bmerge | csv', stream=True)
assert rm_whitespace(unindent(stdout)) == shell.run('(echo a.bsv; echo b.bsv) | bmerge | csv', stream=True)
assert rm_whitespace(unindent(stdout)) == shell.run('(echo a.bsv; echo; echo b.bsv) | bmerge | csv', stream=True)
def test_basic():
with shell.tempdir():
shell.run('echo -e "a,a\nc,c\ne,e\n" | bsv > a.bsv')
shell.run('echo -e "b,b\nd,d\nf,f\n" | bsv > b.bsv')
stdout = """
a,a
b,b
c,c
d,d
e,e
f,f
"""
assert rm_whitespace(unindent(stdout)) == shell.run('echo a.bsv b.bsv | bmerge | csv', stream=True)
assert rm_whitespace(unindent(stdout)) == shell.run('(echo a.bsv; echo b.bsv) | bmerge | csv', stream=True)
assert rm_whitespace(unindent(stdout)) == shell.run('(echo a.bsv; echo; echo b.bsv) | bmerge | csv', stream=True)
@composite
def inputs(draw):
num_inputs = draw(integers(min_value=1, max_value=8))
csvs = []
for _ in range(num_inputs):
num_columns = draw(integers(min_value=1, max_value=2))
column = text(string.ascii_lowercase, min_size=1, max_size=4)
line = lists(column, min_size=num_columns, max_size=num_columns)
lines = draw(lists(line))
csv = '\n'.join(sorted([','.join(x) for x in lines])) + '\n'
csvs.append(csv)
return csvs
def expected(csvs):
xs = []
for csv in csvs:
xs += csv.splitlines()
xs = sorted([x.split(',')[0] for x in xs])
return '\n'.join(xs) + '\n'
@given(inputs())
@settings(database=ExampleDatabase(':memory:'), max_examples=100 * int(os.environ.get('TEST_FACTOR', 1)), deadline=os.environ.get("TEST_DEADLINE", 1000 * 60)) # type: ignore
def test_props(csvs):
result = expected(csvs)
if result.strip():
with shell.tempdir():
paths = []
for i, csv in enumerate(csvs):
path = f'file{i}.bsv'
shell.run(f'bsv > {path}', stdin=csv)
paths.append(path)
assert result.strip() == shell.run('echo', *paths, '| bmerge | bcut 1 | csv', echo=True)
assert shell.run('cat', *paths, '| bsort | bcut 1 | csv') == shell.run('echo', *paths, '| bmerge | bcut 1 | csv')
@given(inputs())
@settings(database=ExampleDatabase(':memory:'), max_examples=100 * int(os.environ.get('TEST_FACTOR', 1)), deadline=os.environ.get("TEST_DEADLINE", 1000 * 60)) # type: ignore
def test_props_compatability(csvs):
result = expected(csvs)
if result.strip():
with shell.tempdir():
bsv_paths = []
for i, csv in enumerate(csvs):
path = f'file{i}.bsv'
shell.run(f'bsv > {path}', stdin=csv)
bsv_paths.append(path)
csv_paths = []
for i, csv in enumerate(csvs):
path = f'file{i}.csv'
shell.run(f'cat - > {path}', stdin=csv)
csv_paths.append(path)
assert shell.run('LC_ALL=C sort -m -k1,1', *csv_paths, ' | cut -d, -f1 | grep -v ^$') == shell.run('echo', *bsv_paths, '| bmerge | bcut 1 | csv', echo=True)