-
Notifications
You must be signed in to change notification settings - Fork 2
/
test_factors.py
783 lines (661 loc) · 32.3 KB
/
test_factors.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
import operator
import pytest
from nettlesome.terms import ContextRegister, DuplicateTermError
from nettlesome.terms import Explanation, TermSequence, means
from nettlesome.entities import Entity
from nettlesome.groups import FactorGroup
from nettlesome.predicates import Predicate
from nettlesome.quantities import Comparison, Q_
from authorityspoke.facts import Fact, build_fact
class TestFacts:
def test_default_terms_for_fact(self, make_entity, make_predicate, watt_mentioned):
e = make_entity
f1 = build_fact(make_predicate["p1"], case_factors=watt_mentioned)
assert f1.terms == [e["motel"]]
def test_no_terms_for_fact(self):
predicate = Predicate(content="context was included", truth=False)
fact = Fact(predicate=predicate, terms=[])
assert str(fact) == "the fact it was false that context was included"
def test_build_fact(self, make_predicate, watt_mentioned):
"""
Check that terms is created as a (hashable) tuple, not list
"""
shooting = build_fact(
make_predicate["p_shooting"],
(2, 3),
case_factors=watt_mentioned,
standard_of_proof="preponderance of evidence",
)
assert isinstance(shooting.terms, list)
def test_terms_from_case_factor_indices(
self, make_entity, make_predicate, watt_mentioned
):
"""
If you pass in integers instead of Factor objects to fill the blanks
in the Predicate (which was the only way to do things in the first
version of the Fact class's __init__ method), then the integers
you pass in should be used as indices to select Factor objects
from case_factors.
"""
e = make_entity
f2 = build_fact(
make_predicate["p2"], indices=(1, 0), case_factors=watt_mentioned
)
assert f2.terms == [e["watt"], e["motel"]]
def test_correct_factors_from_indices_in_build_fact(
self, make_entity, make_predicate, watt_mentioned
):
e = make_entity
f2 = build_fact(
make_predicate["p2"],
indices=(1, 2),
case_factors=watt_mentioned,
)
assert f2.terms == [e["watt"], e["trees"]]
def test_wrong_type_in_terms_in_init(
self, make_entity, make_predicate, watt_mentioned
):
e = make_entity
with pytest.raises(TypeError):
f2 = build_fact(
make_predicate["p1"],
indices=("nonsense"),
case_factors=watt_mentioned,
)
def test_invalid_index_for_case_factors_in_init(self, make_predicate, make_entity):
with pytest.raises(IndexError):
_ = build_fact(
make_predicate["p1"],
indices=2,
case_factors=make_entity["watt"],
)
def test_convert_int_terms_to_tuple(self, make_predicate, watt_mentioned):
f = build_fact(make_predicate["p_irrelevant_1"], 3, case_factors=watt_mentioned)
assert f.terms == [watt_mentioned[3]]
def test_string_representation_of_factor(self, watt_factor):
assert "<Hideaway Lodge> was a motel" in str(watt_factor["f1"])
assert "absence of the fact" in str(watt_factor["f3_absent"]).lower()
def test_string_no_truth_value(self, watt_factor):
factor = watt_factor["f2_no_truth"]
assert "whether" in str(factor)
def test_repeating_entity_string(self, make_predicate):
"""I'm not convinced that a model of a Fact ever needs to include
multiple references to the same Entity just because the name of the
Entity appears more than once in the Predicate."""
with pytest.raises(DuplicateTermError):
Fact(
predicate=make_predicate["p_three_entities"],
terms=[Entity(name="Al"), Entity(name="Bob"), Entity(name="Al")],
)
def test_string_representation_with_concrete_entities(self, watt_factor):
"""
"Hideaway Lodge" is still a string representation of an Entity
object, but it's not in angle brackets because it can't be
replaced by another Entity object without changing the meaning
of the Fact.
"""
assert "Hideaway Lodge was a motel" in str(watt_factor["f1_specific"])
def test_string_for_fact_with_identical_terms(self):
devon = Entity(name="Devon", generic=True)
elaine = Entity(name="Elaine", generic=True)
opened_account = Fact(
predicate=Predicate(
content="$applicant opened a bank account for $applicant and $cosigner"
),
terms=(devon, elaine),
)
assert "<Devon> opened a bank account for <Devon> and <Elaine>" in str(
opened_account
)
def test_str_with_concrete_context(self, make_opinion_with_holding):
holding = list(make_opinion_with_holding["cardenas_majority"].holdings)[1]
longer_str = holding.inputs[0].str_with_concrete_context
assert "the exhibit in the form testimony" in longer_str.lower()
assert "the exhibit in the form testimony" not in str(holding.inputs[0]).lower()
def test_complex_fact_no_line_break_in_predicate(self, make_opinion_with_holding):
"""
Tests that the string representation of this Holding's only input
Fact does not contain indented new lines, except in the "SPECIFIC
CONTEXT" part, if present.
The representation of the Exhibit mentioned in the Fact should
not introduce any indented lines inside the Fact's string.
"""
holding = list(make_opinion_with_holding["cardenas_majority"].holdings)[1]
fact_text = str(holding.inputs[0])
if "SPECIFIC CONTEXT" in fact_text:
fact_text = fact_text.split("SPECIFIC CONTEXT")[0].strip()
assert "\n " not in fact_text
def test_new_context_replace_fact(self, make_entity, watt_factor):
changes = ContextRegister.from_lists(
[make_entity["watt"], watt_factor["f2"]],
[Entity(name="Darth Vader"), watt_factor["f10"]],
)
assert "was within the curtilage of <Hideaway Lodge>" in (
watt_factor["f2"].new_context(changes).short_string
)
def test_get_factor_from_recursive_search(self, make_opinion_with_holding):
holding_list = list(make_opinion_with_holding["cardenas_majority"].holdings)
factor_list = list(holding_list[0].recursive_terms.values())
assert any(
factor.compare_keys(Entity(name="parole officer"))
and factor.name == "parole officer"
for factor in factor_list
)
def test_no_duplicate_comma_in_evidence(self, make_opinion_with_holding):
holdings = list(make_opinion_with_holding["cardenas_majority"].holdings)
assert "addicted to heroin, in showing the fact" in str(holdings[0].outputs[0])
def test_new_concrete_context(self, make_entity, watt_factor):
register = ContextRegister.from_lists(
to_replace=[make_entity["watt"], make_entity["motel"]],
replacements=[Entity(name="Darth Vader"), Entity(name="Death Star")],
)
different = watt_factor["f2"].new_context(register)
assert "<Darth Vader> operated" in str(different)
def test_type_of_terms(self, watt_factor):
assert isinstance(watt_factor["f1"].term_sequence, TermSequence)
def test_concrete_to_abstract(self, make_entity, make_predicate):
motel = make_entity["motel_specific"]
d = make_entity["watt"]
fact = Fact(predicate=make_predicate["p2"], terms=(d, motel))
assert "<Wattenburg> operated and lived at Hideaway Lodge" in str(fact)
assert "<Wattenburg> operated and lived at Hideaway Lodge>" in str(
fact.make_generic()
)
def test_entity_slots_as_length_of_factor(self, watt_factor):
assert len(watt_factor["f1"].predicate) == 1
assert len(watt_factor["f1"]) == 1
def test_predicate_with_entities(self, make_entity, watt_factor):
assert "<Hideaway Lodge> was a motel" in str(watt_factor["f1"])
def test_factor_terms_do_not_match_predicate(self, make_predicate, watt_mentioned):
"""
make_predicate["p1"] has only one slot for context factors, but
this tells it to look for three.
"""
with pytest.raises(ValueError):
_ = build_fact(make_predicate["p1"], (0, 1, 2), case_factors=watt_mentioned)
def test_reciprocal_with_wrong_number_of_entities(self, make_entity, watt_factor):
with pytest.raises(ValueError):
watt_factor["f1"].predicate._content_with_terms(
(make_entity["motel"], make_entity["watt"])
)
def test_entity_and_human_in_predicate(self, make_entity, watt_factor):
assert "<Wattenburg> operated and lived at <Hideaway Lodge>" in watt_factor[
"f2"
].predicate._content_with_terms((make_entity["watt"], make_entity["motel"]))
def test_standard_of_proof_must_be_listed(self, make_predicate, watt_mentioned):
with pytest.raises(ValueError):
_ = build_fact(
make_predicate["p2"],
case_factors=watt_mentioned,
standard_of_proof="probably so",
)
def test_standard_of_proof_in_str(self, watt_factor):
factor = watt_factor["f2_preponderance_of_evidence"]
assert factor.standard_of_proof in factor.short_string
def test_case_factors_deleted_from_fact(self, watt_factor):
"""This attribute should have been deleted during Fact.__post_init__"""
predicate = Predicate(content="some things happened")
factor = build_fact(predicate)
assert not hasattr(factor, "case_factors")
def test_repeated_placeholder_in_fact(self, make_opinion_with_holding):
holding = make_opinion_with_holding["lotus_majority"].holdings[9]
fact = holding.inputs[1]
assert fact.short_string == (
"the fact it was false that the precise formulation "
"of <Lotus 1-2-3>'s code was necessary for <Lotus 1-2-3> to work"
)
assert len(fact.terms) == 1
class TestSameMeaning:
def test_equality_factor_from_same_predicate(self, watt_factor):
assert watt_factor["f1"].means(watt_factor["f1b"])
def test_equality_factor_from_equal_predicate(self, watt_factor):
assert watt_factor["f1"].means(watt_factor["f1c"])
def test_equality_because_factors_are_generic_entities(self, watt_factor):
assert watt_factor["f1"].means(watt_factor["f1_different_entity"])
def test_unequal_because_a_factor_is_not_generic(self, watt_factor):
assert not watt_factor["f9_swap_entities_4"].means(watt_factor["f9"])
def test_generic_terms_equal(self, watt_factor):
assert watt_factor["f2_generic"].means(watt_factor["f2_false_generic"])
assert watt_factor["f2_generic"].means(watt_factor["f3_generic"])
def test_equal_referencing_diffent_generic_terms(self, make_factor):
assert make_factor["f_murder"].means(make_factor["f_murder_craig"])
def test_generic_and_specific_factors_unequal(self, watt_factor):
assert not watt_factor["f2"].means(watt_factor["f2_generic"])
def test_factor_unequal_due_to_repeated_term(self, watt_factor):
assert not watt_factor["f2"].means(watt_factor["f2_reflexive"])
def test_factor_different_predicate_truth_unequal(self, watt_factor):
assert not watt_factor["f7"].means(watt_factor["f7_opposite"])
def test_unequal_because_one_factor_is_absent(self, watt_factor):
assert not watt_factor["f8"].means(watt_factor["f8_absent"])
def test_copies_of_identical_factor(self, make_factor):
"""
Even if the two factors have different entity markers in self.terms,
I expect them to evaluate equal because the choice of entity markers is
arbitrary.
"""
f = make_factor
assert f["f_irrelevant_3"].means(f["f_irrelevant_3"])
assert f["f_irrelevant_3"].means(f["f_irrelevant_3_new_context"])
def test_equal_with_different_generic_subfactors(self, make_complex_fact):
assert make_complex_fact["f_relevant_murder"].means(
make_complex_fact["f_relevant_murder_craig"]
)
def test_reciprocal_context_register(self, watt_factor):
"""
This test describes two objects with the same meaning that have been
made in two different ways, each with a different id and repr.
"""
assert watt_factor["f7"].means(watt_factor["f7_swap_entities"])
def test_interchangeable_concrete_terms(self):
"""Detect that placeholders differing only by a final digit are interchangeable."""
ann = Entity(name="Ann", generic=False)
bob = Entity(name="Bob", generic=False)
ann_and_bob_were_family = Fact(
predicate=Predicate(
content="$relative1 and $relative2 both were members of the same family"
),
terms=(ann, bob),
)
bob_and_ann_were_family = Fact(
predicate=Predicate(
content="$relative1 and $relative2 both were members of the same family"
),
terms=(bob, ann),
)
assert ann_and_bob_were_family.means(bob_and_ann_were_family)
def test_unequal_to_enactment(self, watt_factor, e_copyright):
assert not watt_factor["f1"].means(e_copyright)
with pytest.raises(TypeError):
e_copyright.means(watt_factor["f1"])
def test_standard_of_proof_inequality(self, watt_factor):
f = watt_factor
assert not f["f2_clear_and_convincing"].means(f["f2_preponderance_of_evidence"])
assert not f["f2_clear_and_convincing"].means(f["f2"])
assert f["f2_clear_and_convincing"].wrapped_string.endswith(
"\n by the STANDARD clear and convincing"
)
def test_means_despite_plural(self):
directory = Entity(name="Rural's telephone directory", plural=False)
listings = Entity(name="Rural's telephone listings", plural=True)
directory_original = Fact(
predicate=Predicate(content="$thing was original"), terms=directory
)
listings_original = Fact(
predicate=Predicate(content="$thing were original"), terms=listings
)
assert directory_original.means(listings_original)
def test_same_meaning_no_terms(self, make_predicate):
no_context = Fact(predicate=make_predicate["p_no_context"], terms=[])
assert no_context.means(no_context)
class TestImplication:
def test_fact_implies_none(self, watt_factor):
assert watt_factor["f1"].implies(None)
def test_no_implication_of_rule(self, watt_factor, make_rule):
assert not watt_factor["f1"].implies(make_rule["h1"])
def test_fact_does_not_imply_holding(self, watt_factor, make_holding):
assert not watt_factor["f1"].implies(make_holding["h1"])
def test_specific_factor_implies_generic(self, watt_factor):
assert watt_factor["f2"] > watt_factor["f2_generic"]
assert not watt_factor["f2_generic"] > watt_factor["f2"]
def test_specific_factor_implies_generic_explain(self, watt_factor):
answer = watt_factor["f2"].explain_implication(watt_factor["f2_generic"])
assert (
str(watt_factor["f2"]),
watt_factor["f2_generic"],
) in answer.context.items()
def test_specific_implies_generic_form_of_another_fact(self, watt_factor):
assert watt_factor["f2"] > watt_factor["f3_generic"]
def test_specific_fact_does_not_imply_generic_entity(
self, make_entity, watt_factor
):
assert not watt_factor["f2"] > make_entity["motel"]
def test_factor_does_not_imply_predicate(self, make_predicate, watt_factor):
with pytest.raises(TypeError):
assert not watt_factor["f8_meters"] > make_predicate["p8"]
def test_factor_implies_because_of_quantity(self, watt_factor):
assert watt_factor["f8_meters"] > watt_factor["f8"]
assert watt_factor["f8_higher_int"] > watt_factor["f8_float"]
assert watt_factor["f8_int"] < watt_factor["f8_higher_int"]
def test_factor_implies_no_truth_value(self, watt_factor):
assert watt_factor["f2"] > watt_factor["f2_no_truth"]
assert not watt_factor["f2_no_truth"] > watt_factor["f2"]
def test_comparison_implies_no_truth_value(self, watt_factor):
assert watt_factor["f8"] > watt_factor["f8_no_truth"]
assert not watt_factor["f8_no_truth"] > watt_factor["f8"]
def test_implication_standard_of_proof(self, make_factor):
assert (
not make_factor["f_shooting_craig_poe"]
> make_factor["f_shooting_craig_brd"]
)
assert make_factor["f_shooting_craig_brd"] > make_factor["f_shooting_craig_poe"]
def test_factor_implies_because_of_exact_quantity(self, watt_factor):
assert watt_factor["f8_exact"] > watt_factor["f7"]
assert watt_factor["f8_exact"] >= watt_factor["f8"]
def test_no_implication_pint_quantity_and_int(self, watt_factor):
assert not watt_factor["f8"] > watt_factor["f8_int"]
assert not watt_factor["f8"] < watt_factor["f8_int"]
def test_absent_factor_implies_absent_factor_with_lesser_quantity(
self, watt_factor
):
assert watt_factor["f9_absent_miles"] > watt_factor["f9_absent"]
def test_equal_factors_not_gt(self, watt_factor):
f = watt_factor
assert f["f7"] >= f["f7"]
assert f["f7"] <= f["f7"]
assert not f["f7"] > f["f7"]
def test_standard_of_proof_comparison(self, watt_factor):
f = watt_factor
assert f["f2_clear_and_convincing"].implies(f["f2_preponderance_of_evidence"])
assert f["f2_beyond_reasonable_doubt"] >= f["f2_clear_and_convincing"]
def test_no_implication_between_factors_with_and_without_standards(
self, watt_factor
):
f = watt_factor
assert not f["f2_clear_and_convincing"] > f["f2"]
assert not f["f2"] > f["f2_preponderance_of_evidence"]
def test_implication_complex(self, make_complex_fact):
assert (
make_complex_fact["f_relevant_murder"]
> make_complex_fact["f_relevant_murder_whether"]
)
def test_context_register_text(self, make_context_register):
assert str(make_context_register) == (
"ContextRegister(<Alice> is like <Craig>, and <Bob> is like <Dan>)"
)
def test_implication_complex_explain(
self, make_complex_fact, make_context_register
):
complex_true = make_complex_fact["f_relevant_murder"]
complex_whether = make_complex_fact["f_relevant_murder_whether"].new_context(
make_context_register
)
explanation = complex_true.explain_implication(complex_whether)
assert explanation.context[Entity(name="Alice").key].compare_keys(
Entity(name="Craig")
)
def test_implication_explain_keys_only_from_left(
self, make_complex_fact, make_context_register
):
"""
Check that when implies provides a ContextRegister as an "explanation",
it uses elements only from the left as keys and from the right as values.
"""
complex_true = make_complex_fact["f_relevant_murder"]
complex_whether = make_complex_fact["f_relevant_murder_whether"]
new = complex_whether.new_context(make_context_register)
explanations = list(complex_true.explanations_implication(new))
explanation = explanations.pop()
assert explanation.context.get("<Craig>") != Entity(name="Alice")
assert explanation.context.get("<Alice>").compare_keys(Entity(name="Craig"))
def test_context_registers_for_complex_comparison(self, make_complex_fact):
gen = make_complex_fact["f_relevant_murder_nested_swap"]._context_registers(
make_complex_fact["f_relevant_murder"], operator.ge
)
register = next(gen)
assert register.matches.get("<Alice>").compare_keys(Entity(name="Bob"))
def test_no_implication_complex(self, make_complex_fact):
left = make_complex_fact["f_relevant_murder"]
right = make_complex_fact["f_relevant_murder_alice_craig"]
assert not left >= right
assert left.explain_implication(right) is None
def test_implied_by(self, make_complex_fact):
assert make_complex_fact["f_relevant_murder_whether"].implied_by(
make_complex_fact["f_relevant_murder"]
)
def test_explanation_implied_by(self, make_complex_fact):
explanation = make_complex_fact["f_relevant_murder_whether"].explain_implied_by(
make_complex_fact["f_relevant_murder"]
)
assert explanation
def test_explain_not_implied_by(self, make_complex_fact):
left = make_complex_fact["f_relevant_murder"]
right = make_complex_fact["f_relevant_murder_whether"]
assert left.explain_implied_by(right) is None
def test_not_implied_by_none(self, make_complex_fact):
left = make_complex_fact["f_relevant_murder"]
assert not left.implied_by(None)
def test_some_interchangeable_entities(self):
hit = Fact(
predicate="$person1 hit $target1 and $target2",
terms=[Entity(name="Moe"), Entity(name="Curly"), Entity(name="Larry")],
)
hit2 = Fact(
predicate="$person1 hit $target1 and $target2",
terms=[
Entity(name="Joker"),
Entity(name="Batman"),
Entity(name="Superman"),
],
)
assert hit.means(hit2)
def test_interchangeable_entities_in_group(self):
fought = Fact(
predicate="$person1, $person2, and $person3 fought each other",
terms=[Entity(name="Larry"), Entity(name="Moe"), Entity(name="Curly")],
)
hit = Fact(
predicate="$person1 hit $target1 and $target2",
terms=[Entity(name="Moe"), Entity(name="Curly"), Entity(name="Larry")],
)
fought2 = Fact(
predicate="$person1, $person2, and $person3 fought each other",
terms=[
Entity(name="Superman"),
Entity(name="Batman"),
Entity(name="Joker"),
],
)
hit2 = Fact(
predicate="$person1 hit $target1 and $target2",
terms=[
Entity(name="Joker"),
Entity(name="Batman"),
Entity(name="Superman"),
],
)
assert FactorGroup([fought, hit]).implies(FactorGroup([fought2, hit2]))
class TestContradiction:
def test_factor_different_predicate_truth_contradicts(self, watt_factor):
assert watt_factor["f7"].contradicts(watt_factor["f7_opposite"])
assert watt_factor["f7_opposite"].contradicts(watt_factor["f7"])
def test_same_predicate_true_vs_false(self, watt_factor):
assert watt_factor["f10"].contradicts(watt_factor["f10_false"])
assert watt_factor["f10"].truth != watt_factor["f10_false"].truth
def test_factor_does_not_contradict_predicate(self, make_predicate, watt_factor):
with pytest.raises(TypeError):
_ = watt_factor["f7"].contradicts(make_predicate["p7_true"])
def test_factor_contradiction_absent_predicate(self, watt_factor):
assert watt_factor["f3"].contradicts(watt_factor["f3_absent"])
assert watt_factor["f3_absent"].contradicts(watt_factor["f3"])
def test_absences_of_contradictory_facts_consistent(self, watt_factor):
assert not watt_factor["f8_absent"].contradicts(watt_factor["f8_less_absent"])
def test_factor_no_contradiction_no_truth_value(self, watt_factor):
assert not watt_factor["f2"].contradicts(watt_factor["f2_no_truth"])
assert not watt_factor["f2_no_truth"].contradicts(watt_factor["f2_false"])
def test_absent_factor_contradicts_broader_quantity_statement(self, watt_factor):
assert watt_factor["f8_absent"].contradicts(watt_factor["f8_meters"])
assert watt_factor["f8_meters"].contradicts(watt_factor["f8_absent"])
def test_less_specific_absent_contradicts_more_specific(self, watt_factor):
assert watt_factor["f9_absent_miles"].contradicts(watt_factor["f9"])
assert watt_factor["f9"].contradicts(watt_factor["f9_absent_miles"])
def test_no_contradiction_with_more_specific_absent(self, watt_factor):
assert not watt_factor["f9_absent"].contradicts(watt_factor["f9_miles"])
assert not watt_factor["f9_miles"].contradicts(watt_factor["f9_absent"])
def test_contradiction_complex(self, make_complex_fact):
assert make_complex_fact["f_irrelevant_murder"].contradicts(
make_complex_fact["f_relevant_murder_craig"]
)
def test_no_contradiction_complex(self, make_complex_fact):
assert not make_complex_fact["f_irrelevant_murder"].contradicts(
make_complex_fact["f_relevant_murder_alice_craig"]
)
def test_no_contradiction_of_None(self, watt_factor):
assert not watt_factor["f1"].contradicts(None)
def test_contradicts_if_present_both_present(self, watt_factor):
"""
Test a helper function that checks whether there would
be a contradiction if neither Factor was "absent".
"""
assert watt_factor["f2"]._contradicts_if_present(
watt_factor["f2_false"], explanation=Explanation.from_context()
)
def test_contradicts_if_present_one_absent(self, watt_factor):
assert watt_factor["f2"]._contradicts_if_present(
watt_factor["f2_false_absent"], explanation=Explanation.from_context()
)
def test_false_does_not_contradict_absent(self):
absent_fact = Fact(
predicate=Predicate(
content="${rural_s_telephone_directory} was copyrightable", truth=True
),
terms=(Entity(name="Rural's telephone directory")),
absent=True,
)
false_fact = Fact(
predicate=Predicate(
content="${the_java_api} was copyrightable", truth=False
),
terms=(Entity(name="the Java API", generic=True, plural=False)),
absent=False,
)
assert not false_fact.contradicts(absent_fact)
assert not absent_fact.contradicts(false_fact)
def test_inconsistent_statements_about_different_entities(self):
"""
Alice and Bob are both generics. So it's possible to reach a
contradiction if you assume they correspond to one another.
"""
p_small_weight = Comparison(
content="the amount of gold $person possessed was",
sign="<",
expression=Q_("1 gram"),
)
p_large_weight = Comparison(
content="the amount of gold $person possessed was",
sign=">=",
expression=Q_("100 kilograms"),
)
alice = Entity(name="Alice")
bob = Entity(name="Bob")
alice_rich = Fact(predicate=p_large_weight, terms=alice)
bob_poor = Fact(predicate=p_small_weight, terms=bob)
assert alice_rich.contradicts(bob_poor)
def test_inconsistent_statements_about_corresponding_entities(self):
"""
Even though Alice and Bob are both generics, it's known that
Alice in the first context corresponds with Alice in the second.
So there's no contradiction.
"""
p_small_weight = Comparison(
content="the amount of gold $person possessed was",
sign="<",
expression=Q_("1 gram"),
)
p_large_weight = Comparison(
content="the amount of gold $person possessed was",
sign=">=",
expression=Q_("100 kilograms"),
)
alice = Entity(name="Alice")
bob = Entity(name="Bob")
alice_rich = Fact(predicate=p_large_weight, terms=alice)
bob_poor = Fact(predicate=p_small_weight, terms=bob)
register = ContextRegister()
register.insert_pair(alice, alice)
assert not alice_rich.contradicts(bob_poor, context=register)
def test_copy_with_foreign_context(self, watt_mentioned, watt_factor):
w = watt_mentioned
assert (
watt_factor["f1"]
.new_context(ContextRegister.from_lists([w[0]], [w[2]]))
.means(watt_factor["f1_different_entity"])
)
def test_check_entity_consistency_true(self, make_entity, make_factor):
left = make_factor["f_irrelevant_3"]
right = make_factor["f_irrelevant_3_new_context"]
e = make_entity
easy_register = ContextRegister.from_lists([e["dan"]], [e["craig"]])
easy_update = left.update_context_register(
right, easy_register, comparison=means
)
harder_register = ContextRegister.from_lists(
to_replace=[e["alice"], e["bob"], e["craig"], e["dan"], e["circus"]],
replacements=[e["bob"], e["alice"], e["dan"], e["craig"], e["circus"]],
)
harder_update = left.update_context_register(
right,
context=harder_register,
comparison=means,
)
assert any(register is not None for register in easy_update)
assert any(register is not None for register in harder_update)
def test_check_entity_consistency_false(self, make_entity, make_factor):
context = ContextRegister()
context.insert_pair(make_entity["circus"], make_entity["alice"])
update = make_factor["f_irrelevant_3"].update_context_register(
make_factor["f_irrelevant_3_new_context"], comparison=means, context=context
)
assert not any(register is not None for register in update)
def test_entity_consistency_identity_not_equality(self, make_entity, make_factor):
register = ContextRegister()
register.insert_pair(make_entity["dan"], make_entity["dan"])
update = make_factor["f_irrelevant_3"].update_context_register(
make_factor["f_irrelevant_3_new_context"],
context=register,
comparison=means,
)
assert not any(register is not None for register in update)
def test_check_entity_consistency_type_error(
self, make_entity, make_factor, make_predicate
):
"""
There would be no TypeError if it used "means"
instead of .gt. The comparison would just return False.
"""
update = make_factor["f_irrelevant_3"].update_context_register(
make_predicate["p2"],
{str(make_entity["dan"]): make_entity["dan"]},
operator.gt,
)
with pytest.raises(TypeError):
any(register is not None for register in update)
class TestConsistent:
def test_contradictory_facts_about_same_entity(self, watt_factor):
left = watt_factor["f8_less"]
right = watt_factor["f8_meters"]
register = ContextRegister()
register.insert_pair(left.generic_terms()[0], right.generic_terms()[0])
assert not left.consistent_with(right, register)
assert left.explain_consistent_with(right, register) is None
def test_explanations_consistent_with(self, watt_factor):
left = watt_factor["f8_less"]
right = watt_factor["f8_meters"]
register = ContextRegister()
register.insert_pair(left.generic_terms()[0], right.generic_terms()[0])
explanations = list(left.explanations_consistent_with(right, context=register))
assert not explanations
def test_factor_consistent_with_none(self, make_exhibit):
assert make_exhibit["no_shooting_testimony"].consistent_with(
make_exhibit["no_shooting_witness_unknown_testimony"]
)
class TestAddition:
@pytest.mark.parametrize(
"left, right, expected",
[
("f_shooting_craig_poe", "f_shooting_craig_brd", "f_shooting_craig_brd"),
("f_irrelevant_3", "f_irrelevant_3_new_context", "f_irrelevant_3"),
(
"f_irrelevant_3_new_context",
"f_irrelevant_3",
"f_irrelevant_3_new_context",
),
],
)
def test_addition(self, make_factor, left, right, expected):
answer = make_factor[left] + make_factor[right]
assert answer.means(make_factor[expected])
def test_add_unrelated_factors(self, make_factor):
assert make_factor["f_murder"] + make_factor["f_crime"] is None
def test_cant_add_enactment_to_fact(self, watt_factor, e_search_clause):
with pytest.raises(TypeError):
print(watt_factor["f3"] + e_search_clause)