-
Notifications
You must be signed in to change notification settings - Fork 2
/
test_notebooks.py
201 lines (165 loc) · 8.34 KB
/
test_notebooks.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
"""
Tests of commands that appear in notebooks in
the notebooks/ directory
"""
from copy import deepcopy
import os
from dotenv import load_dotenv
import pytest
from justopinion import CAPClient
from authorityspoke.facts import Fact
from authorityspoke import Entity, Predicate, Comparison
load_dotenv()
CAP_API_KEY = os.getenv("CAP_API_KEY") or "wrong key"
class TestIntroduction:
"""
Tests of commands from the "Introduction to AuthoritySpoke" notebook
"""
client = CAPClient(api_token=CAP_API_KEY)
@pytest.mark.vcr
def test_download_case(self):
oracle = self.client.read_cite(cite="750 F.3d 1339", full_case=True)
citations = [out_citation.cite for out_citation in oracle.cites_to]
assert "527 F.3d 1318" in citations
def test_oracle_20_holdings(self, make_opinion_with_holding):
assert len(make_opinion_with_holding["oracle_majority"].holdings) == 20
def test_replace_generic_factor(self, make_opinion_with_holding):
lotus_majority = make_opinion_with_holding["lotus_majority"]
seinfeld_holding = lotus_majority.holdings[0].new_context(
terms_to_replace=[
Entity(name="Borland International"),
Entity(name="the Lotus menu command hierarchy"),
],
changes=[Entity(name="Carol Publishing Group"), Entity(name="Seinfeld")],
)
assert lotus_majority.holdings[0] != seinfeld_holding
assert lotus_majority.holdings[0].means(seinfeld_holding)
def test_inferred_holdings_after_exclusive_holding(self, make_opinion_with_holding):
"""
Test that when a holding is marked "exclusive" in the JSON input,
that holding is added first to the Opinion's set of holdings, and
any other inferred holdings, about the absence of the output from
the original holding, are added later.
"""
lotus_majority = make_opinion_with_holding["lotus_majority"]
assert lotus_majority.holdings[0].outputs[0].absent is False
assert lotus_majority.holdings[1].outputs[0].absent is True
def test_change_rule_replacing_enactment(
self, fake_usc_client, make_opinion_with_holding
):
oracle = make_opinion_with_holding["oracle_majority"]
works_of_authorship_passage = (
"Copyright protection subsists, in accordance with this title, "
+ "in original works of authorship"
)
works_of_authorship_clause = fake_usc_client.read("/us/usc/t17/s102/a")
passage = works_of_authorship_clause.select(works_of_authorship_passage)
holding_with_shorter_enactment = deepcopy(oracle.holdings[0])
holding_with_shorter_enactment.set_enactments(passage)
assert holding_with_shorter_enactment >= oracle.holdings[0]
assert not oracle.holdings[0] >= holding_with_shorter_enactment
def test_opinion_contradiction(self, make_opinion_with_holding):
oracle = make_opinion_with_holding["oracle_majority"]
lotus_majority = make_opinion_with_holding["lotus_majority"]
assert oracle.contradicts(lotus_majority)
assert lotus_majority.contradicts(oracle)
def test_opinion_explain_contradiction(self, make_opinion_with_holding):
"""
The notebook now uses Decisions instead of this.
"""
oracle = make_opinion_with_holding["oracle_majority"]
lotus = make_opinion_with_holding["lotus_majority"]
explanation = lotus.holdings[6].explain_contradiction(oracle.holdings[10])
assert (
explanation.context["<the Lotus menu command hierarchy>"].name
== "the Java API"
)
assert "<the Lotus menu command hierarchy> is like <the Java API>" in str(
explanation
)
assert "Entity(name='the Java API'" in repr(explanation)
def test_decision_explain_contradiction(self, make_decision_with_holding):
oracle = make_decision_with_holding["oracle"]
lotus = make_decision_with_holding["lotus"]
explanation = lotus.explain_contradiction(oracle)
lotus_like = "<the Lotus menu command hierarchy> is like <the Java API>"
java_like = "<the Java API> is like <the Lotus menu command hierarchy>"
assert lotus_like in str(explanation) or java_like in str(explanation)
assert (
"the Fact it was false that <the Lotus".lower() in str(explanation).lower()
)
def test_register_string(self, make_opinion_with_holding):
oracle = make_opinion_with_holding["oracle_majority"]
lotus = make_opinion_with_holding["lotus_majority"]
explanation = lotus.holdings[6].explain_contradiction(oracle.holdings[10])
string = (
"ContextRegister(<the Lotus menu command hierarchy> is like <the Java API>)"
)
assert str(explanation.context) == string
def test_specific_holding_contradiction(self, make_opinion_with_holding):
"""
Check the specific Holdings that should be causing a
contradiction to be found between the Opinions.
"""
oracle = make_opinion_with_holding["oracle_majority"]
lotus = make_opinion_with_holding["lotus_majority"]
assert oracle.holdings[10].contradicts(lotus.holdings[6])
def test_addition_some_to_some(self, make_opinion_with_holding):
"""
Demonstrates that adding two SOME Holdings returns None,
same as two SOME Rules.
"""
oracle = make_opinion_with_holding["oracle_majority"]
feist = make_opinion_with_holding["feist_majority"]
listings_not_original = feist.holdings[10]
original_not_copyrightable = oracle.holdings[0]
assert listings_not_original + original_not_copyrightable is None
def test_adding_holdings(self, make_opinion_with_holding):
feist = make_opinion_with_holding["feist_majority"]
listings_not_original = feist.holdings[10]
unoriginal_not_copyrightable = feist.holdings[3]
listings_not_copyrightable = (
listings_not_original + unoriginal_not_copyrightable
)
not_copyrightable = unoriginal_not_copyrightable.outputs[0]
assert listings_not_copyrightable.outputs[1].short_string == (
"absence of the fact that <Rural's telephone"
" listings> were copyrightable"
)
assert (
"act that <Rural's telephone listings> were names, towns, "
+ "and telephone numbers of telephone subscribers"
) in listings_not_copyrightable.inputs[0].short_string
def test_union_holdings_from_different_cases(self, make_opinion_with_holding):
oracle = make_opinion_with_holding["oracle_majority"]
feist = make_opinion_with_holding["feist_majority"]
new = oracle.holdings[1] | feist.holdings[2]
assert "it was false that <the Java API> was copyrightable" in str(new)
assert "<the Java API> was an original work" in str(new)
def test_only_one_explanation_for_contradiction(self, make_opinion_with_holding):
lotus = make_opinion_with_holding["lotus_majority"]
oracle = make_opinion_with_holding["oracle_majority"]
gen = lotus.holdings[6].explanations_contradiction(oracle.holdings[10])
first_explanation = next(gen)
with pytest.raises(StopIteration):
second_explanation = next(gen)
class TestTemplateStrings:
"""Tests from the notebook introducing template strings."""
def test_no_line_break_in_fact_string(self):
elaine = Entity(name="Elaine", generic=True)
tax_rate_over_25 = Comparison(
content="${taxpayer}'s marginal income tax rate was",
sign=">",
expression=0.25,
)
elaine_tax_rate = Fact(predicate=tax_rate_over_25, terms=elaine)
assert "\n" not in str(elaine_tax_rate)
def test_changing_order_of_concrete_terms_changes_meaning(self):
ann = Entity(name="Ann", generic=False)
bob = Entity(name="Bob", generic=False)
parent_sentence = Predicate(content="$mother was ${child}'s parent")
ann_parent = Fact(predicate=parent_sentence, terms=(ann, bob))
bob_parent = Fact(predicate=parent_sentence, terms=(bob, ann))
assert str(ann_parent).lower() == "the fact that Ann was Bob's parent".lower()
assert str(bob_parent).lower() == "the fact that Bob was Ann's parent".lower()
assert not ann_parent.means(bob_parent)