-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_cardinality.py
145 lines (106 loc) · 4.18 KB
/
test_cardinality.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import argparse
import os
import tempfile
import numpy as np
import pandas as pd
import pytest
from cardinality import (
format_cardinality,
check_keys,
examine_relation,
main,
parse_args,
is_file,
)
def get_testfile_path(filename):
"""Return path to a test file."""
test_file_dir = os.path.join(
os.path.dirname(os.path.realpath(__file__)), 'test_files')
return os.path.join(test_file_dir, filename)
class TestFormatCardinality():
def test_format_cardinality(self):
assert format_cardinality((0, 1), (2, 3)) == '0,1 to 2,3'
class TestCheckKeys():
pk_frame = pd.DataFrame({
'pk': [1, 2, 3, 4, 5],
'pk_null': [1, 2, 3, 4, np.nan],
'pk_duplicated': [1, 2, 3, 4, 4]})
fk_frame_empty = pd.DataFrame({'fk': []})
fk_frame_zero_one = pd.DataFrame({'fk': [1, 2, 2, 4, np.nan]})
fk_frame_alien = pd.DataFrame({'fk': [1, 1, 3, 3, 6]})
def test_check_keys_ok(self):
check_keys(self.pk_frame[['pk']], self.fk_frame_zero_one)
def test_check_keys_different_columns(self):
with pytest.raises(AssertionError):
check_keys(self.pk_frame, self.fk_frame_zero_one)
def test_check_keys_pk_nan(self):
with pytest.raises(AssertionError):
check_keys(self.pk_frame[['pk_null']], self.fk_frame_zero_one)
def test_check_keys_pk_duplicated(self):
with pytest.raises(AssertionError):
check_keys(
self.pk_frame[['pk_duplicated']],
self.fk_frame_zero_one)
def test_check_keys_fk_empty(self):
check_keys(self.pk_frame[['pk']], self.fk_frame_empty)
def test_check_keys_fk_alien(self):
with pytest.raises(AssertionError):
check_keys(self.pk_frame[['pk']], self.fk_frame_alien)
class TestCardinality():
pk_frame = pd.DataFrame({'pk': [1, 2, 3, 4, 5]})
fk_frame_one_one = pd.DataFrame({'fk': [1, 2, 3, 4, 5]})
fk_frame_zero_one = pd.DataFrame({'fk': [1, 2, 3, 4, np.nan]})
fk_frame_two_three = pd.DataFrame({'fk': [1, 2, 2, 3, 4, 5]})
fk_frame_empty = pd.DataFrame({'fk': []})
def test_relation_one_one(self):
assert examine_relation(
self.pk_frame, self.fk_frame_one_one) == ((1, 1), (1, 1))
def test_relation_zero_one(self):
assert examine_relation(
self.pk_frame, self.fk_frame_zero_one) == ((0, 1), (0, 1))
def test_relation_two_three(self):
assert examine_relation(
self.pk_frame, self.fk_frame_two_three) == ((1, 1), (1, 2))
def test_relation_empty(self):
assert examine_relation(
self.pk_frame, self.fk_frame_empty) == ((0, 0), (0, 0))
class TestArgumentParser():
def test_parser_help(self):
with pytest.raises(SystemExit):
parse_args(['-h'])
def test_parser(self):
pk_file = get_testfile_path('pk-data.tsv')
fk_file = get_testfile_path('fk-data.tsv')
parser = parse_args([pk_file, fk_file, '-p', 'pk', '-f', 'fk'])
assert parser.pk_file == pk_file
assert parser.fk_file == fk_file
assert parser.pk_columns == ['pk']
assert parser.fk_columns == ['fk']
def test_is_file(self):
with tempfile.NamedTemporaryFile() as tmp:
assert is_file(tmp.name) == tmp.name
def test_is_file_error(self):
with pytest.raises(argparse.ArgumentTypeError):
is_file('')
class TestMain():
pk_file = get_testfile_path('pk-data.tsv')
fk_file = get_testfile_path('fk-data.tsv')
def test_noargs(self):
with pytest.raises(SystemExit):
main()
def test_args_help(self):
with pytest.raises(SystemExit):
main(['-h'])
def test_args(self, capsys):
main([self.pk_file, self.fk_file, '-p', 'pk', '-f', 'fk'])
out, err = capsys.readouterr()
assert out.rstrip() == '0,1 to 0,3'
assert err == ''
def test_args_verbose(self, capsys):
main([self.pk_file, self.fk_file, '-v', '-p', 'pk', '-f', 'fk'])
out, err = capsys.readouterr()
assert out.rstrip() == (
self.pk_file + '\t' + self.fk_file + '\tpk\tfk\t0,1 to 0,3')
assert err == ''