forked from srlearn/srlearn
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_database.py
More file actions
169 lines (130 loc) · 5.77 KB
/
test_database.py
File metadata and controls
169 lines (130 loc) · 5.77 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# Copyright 2017, 2018, 2019 Alexander L. Hayes
"""
Tests for srlearn.database.Database
"""
import pathlib
from srlearn.database import Database
def test_initialize_database_1():
"""
Test initializing a Database with defualt settings.
"""
_db = Database()
assert _db.pos == []
assert _db.neg == []
assert _db.facts == []
def test_initialize_database_2():
"""
Test initializing a Database with a specific target.
"""
_db = Database()
assert _db.pos == []
assert _db.neg == []
assert _db.facts == []
def test_initialize_from_files_lazy_strings():
"""Test initializing from string filename with lazy loading."""
_db = Database.from_files(
pos="datasets/ToyFather/train/pos.pl",
neg="datasets/ToyFather/train/neg.pl",
facts="datasets/ToyFather/train/facts.pl",
lazy_load=True,
)
assert _db.pos == "datasets/ToyFather/train/pos.pl"
assert _db.neg == "datasets/ToyFather/train/neg.pl"
assert _db.facts == "datasets/ToyFather/train/facts.pl"
def test_initialize_from_files_lazy_paths():
"""Test initializing from pathlib.Path filenames with lazy loading."""
_db = Database.from_files(
pos=pathlib.Path("datasets/ToyFather/train/pos.pl"),
neg=pathlib.Path("datasets/ToyFather/train/neg.pl"),
facts=pathlib.Path("datasets/ToyFather/train/facts.pl"),
lazy_load=True,
)
assert _db.pos == pathlib.Path("datasets/ToyFather/train/pos.pl")
assert _db.neg == pathlib.Path("datasets/ToyFather/train/neg.pl")
assert _db.facts == pathlib.Path("datasets/ToyFather/train/facts.pl")
def test_initialize_from_files_not_lazy():
"""Test initializing from files without lazy loading."""
_pos = "datasets/ToyFather/train/pos.pl"
_neg = "datasets/ToyFather/train/neg.pl"
_facts = "datasets/ToyFather/train/facts.pl"
_db = Database.from_files(pos=_pos, neg=_neg, facts=_facts, lazy_load=False)
assert isinstance(_db.pos, list)
assert isinstance(_db.neg, list)
assert isinstance(_db.facts, list)
assert _db.pos == open(_pos).read().splitlines()
assert _db.neg == open(_neg).read().splitlines()
assert _db.facts == open(_facts).read().splitlines()
def test_initialize_mix():
"""Test initializing from a mix of lazy and lists."""
_pos = "datasets/ToyFather/train/pos.pl"
_neg = "datasets/ToyFather/train/neg.pl"
_facts = pathlib.Path("datasets/ToyFather/train/facts.pl")
_db = Database.from_files(pos=_pos, neg=_neg, facts=_facts, lazy_load=True)
_db.neg = ["father(harrypotter,ronweasley)."]
assert isinstance(_db.pos, str)
assert isinstance(_db.neg, list)
assert isinstance(_db.facts, pathlib.Path)
def test_lazy_write(tmpdir):
"""Test writing to a location after a lazy load."""
_pos = "datasets/ToyFather/train/pos.pl"
_neg = "datasets/ToyFather/train/neg.pl"
_facts = "datasets/ToyFather/train/facts.pl"
_db = Database.from_files(pos=_pos, neg=_neg, facts=_facts, lazy_load=True)
assert isinstance(_db.pos, str)
assert isinstance(_db.neg, str)
assert isinstance(_db.facts, str)
_db.write(filename="train", location=pathlib.Path(tmpdir))
assert tmpdir.join("train_pos.txt").read() == open(_pos).read()
assert tmpdir.join("train_neg.txt").read() == open(_neg).read()
assert tmpdir.join("train_facts.txt").read() == open(_facts).read()
def test_nonlazy_write(tmpdir):
"""Test writing to a location after a non-lazy load."""
_pos = "datasets/ToyFather/train/pos.pl"
_neg = "datasets/ToyFather/train/neg.pl"
_facts = "datasets/ToyFather/train/facts.pl"
_db = Database.from_files(pos=_pos, neg=_neg, facts=_facts, lazy_load=False)
assert isinstance(_db.pos, list)
assert isinstance(_db.neg, list)
assert isinstance(_db.facts, list)
_db.write(filename="train", location=pathlib.Path(tmpdir))
assert tmpdir.join("train_pos.txt").read() == open(_pos).read()
assert tmpdir.join("train_neg.txt").read() == open(_neg).read()
assert tmpdir.join("train_facts.txt").read() == open(_facts).read()
def test_mixed_write(tmpdir):
"""Test writing to a location with a mix of lazy, non-lazy, and lists."""
_pos = "datasets/ToyFather/train/pos.pl"
_neg = "datasets/ToyFather/train/neg.pl"
_facts = pathlib.Path("datasets/ToyFather/train/facts.pl")
_db = Database.from_files(pos=_pos, neg=_neg, facts=_facts, lazy_load=True)
_db.neg = ["father(harrypotter,ronweasley)."]
assert isinstance(_db.pos, str)
assert isinstance(_db.neg, list)
assert isinstance(_db.facts, pathlib.Path)
_db.write(filename="test", location=pathlib.Path(tmpdir))
assert tmpdir.join("test_pos.txt").read() == open(_pos).read()
assert tmpdir.join("test_neg.txt").read() == "father(harrypotter,ronweasley).\n"
assert tmpdir.join("test_facts.txt").read() == open(_facts).read()
def test_write_to_location_1(tmpdir):
"""
Test that predicates are written to files in a target location with add_pos syntax.
"""
_db = Database()
_db.add_pos("a(b).")
_db.add_neg("a(c).")
_db.add_fact("d(b,c).")
_db.write(filename="train", location=pathlib.Path(tmpdir))
assert tmpdir.join("train_pos.txt").read() == "a(b).\n"
assert tmpdir.join("train_neg.txt").read() == "a(c).\n"
assert tmpdir.join("train_facts.txt").read() == "d(b,c).\n"
def test_write_to_location_2(tmpdir):
"""
Test that predicates are written to files in a target location with pos = [] syntax.
"""
_db = Database()
_db.pos = ["a(b)."]
_db.neg = ["a(c)."]
_db.facts = ["d(b,c)."]
_db.write(filename="train", location=pathlib.Path(tmpdir))
assert tmpdir.join("train_pos.txt").read() == "a(b).\n"
assert tmpdir.join("train_neg.txt").read() == "a(c).\n"
assert tmpdir.join("train_facts.txt").read() == "d(b,c).\n"