def test_new_context_inferring_factors_to_change(self, make_opinion_with_holding, make_response): """ This changes watt's holdings; may break tests below. """ watt = make_opinion_with_holding["watt_majority"] brad = make_opinion_with_holding["brad_majority"] client = FakeClient(responses=make_response) watt.clear_holdings() watt_raw = loaders.load_holdings("holding_watt.yaml") watt.posit(readers.read_holdings(watt_raw, client=client)) brad.clear_holdings() brad_raw = loaders.load_holdings("holding_brad.yaml") brad.posit(readers.read_holdings(brad_raw, client=client)) context_items = [ "proof of Wattenburg's guilt", "<Wattenburg>", "<officers' search of the stockpile>", "<Hideaway Lodge>", "<the stockpile of trees>", ] watt.posit(brad.holdings[0], context=context_items) assert watt.holdings[-1].means(brad.holdings[0])
def test_repeating_read_holdings_has_same_result(self, make_analysis): raw = make_analysis["minimal"] holdings_and_anchors = readers.read_holdings(raw) holdings_and_anchors_again = readers.read_holdings(raw) assert all( left == right for left, right in zip(holdings_and_anchors, holdings_and_anchors_again) )
def test_same_enactment_in_two_opinions(self, make_response): mock_client = FakeClient(responses=make_response) to_read = load_holdings("holding_brad.yaml") brad_holdings = readers.read_holdings(to_read, client=mock_client) to_read = load_holdings("holding_watt.yaml") watt_holdings = readers.read_holdings(to_read, client=mock_client) assert any(watt_holdings[0].enactments[0].means(brad_enactment) for brad_enactment in brad_holdings[0].enactments)
def test_same_enactment_in_two_opinions(self, make_regime): to_read = load_holdings("holding_brad.json") brad_holdings = readers.read_holdings(to_read, regime=make_regime) to_read = load_holdings("holding_watt.json") watt_holdings = readers.read_holdings(to_read, regime=make_regime) assert any( watt_holdings[0].enactments[0].means(brad_enactment) for brad_enactment in brad_holdings[0].enactments )
def test_error_classname_does_not_exist(self): rule_dict = { "inputs": [{ "type": "RidiculousFakeClassName", "content": "officers' search of the yard was a warrantless search and seizure", }], "outputs": [{ "type": "fact", "content": "the dog bit the man" }], } with pytest.raises(ValidationError): readers.read_holdings([rule_dict])
def test_fact_from_loaded_holding(self, make_response): to_read = load_holdings("holding_watt.yaml") mock_client = FakeClient(responses=make_response) holdings = readers.read_holdings(to_read, client=mock_client) new_fact = holdings[0].inputs[1] assert "lived at <Hideaway Lodge>" in str(new_fact) assert isinstance(new_fact.terms[0], Entity)
def test_load_any_enactments(self, fake_beard_client): """Test bug where holding's enactment's aren't loaded.""" beard_dictionary = loaders.load_holdings("beard_rules.yaml") shorter = [beard_dictionary[0]] beard_rules = readers.read_holdings(shorter, client=fake_beard_client) expected = "facial hair no shorter than 5 millimetres" assert expected in beard_rules[0].enactments[0].selected_text()
def test_opinion_posits_holding_dict_context(self, make_entity, make_response): """ Having the Watt case posit a holding from the Brad case, but replacing one generic factor with a factor from Watt. """ mock_client = FakeClient(responses=make_response) to_read = load_holdings("holding_brad.yaml") holdings = readers.read_holdings(to_read, client=mock_client) breading = OpinionReading() breading.clear_holdings() breading.posit(holdings) expectation_not_reasonable = breading.holdings[6] changes = ContextRegister() changes.insert_pair( key=expectation_not_reasonable.generic_terms()[0], value=make_entity["watt"], ) context_holding = expectation_not_reasonable.new_context(changes) wreading = OpinionReading() wreading.clear_holdings() wreading.posit(context_holding) string = str(context_holding) assert "<Wattenburg> lived at <Bradley's house>" in string assert "<Wattenburg> lived at <Bradley's house>" in str( wreading.holdings[-1])
def read_holdings_from_file( filename: Optional[str] = None, directory: Optional[pathlib.Path] = None, filepath: Optional[pathlib.Path] = None, client: Optional[Client] = None, ) -> List[Holding]: r""" Read holdings from a file. :param filename: The name of the input JSON file. :param directory: The directory where the input JSON file is located. :param filepath: Complete path to the JSON file representing the :class:`.Opinion`, including filename. :param client: The client with an API key to download :class:`Enactment`\s mentioned in the holding. """ raw_holdings = load_holdings( filename=filename, directory=directory, filepath=filepath ) return readers.read_holdings(raw_holdings, client=client)
def test_posit_holding_with_selector(self, make_analysis, make_opinion): holdings = readers.read_holdings(make_analysis["minimal"]) holding_anchors = anchors.get_holding_anchors(make_analysis["minimal"]) brad = make_opinion["brad_majority"] brad.posit(holdings, holding_anchors=holding_anchors) assert brad.holding_anchors[holdings[0]][0].exact == "we hold"
def test_passage_from_imported_statute(self, make_regime): oracle_majority = loaders.load_and_read_decision(f"oracle_h.json").majority raw_holdings = loaders.load_holdings("holding_oracle.json") holdings = readers.read_holdings(raw_holdings, regime=make_regime) oracle_majority.posit(holdings) despite_text = str(list(oracle_majority.holdings)[5]) assert "In no case does copyright protection " in despite_text
def test_mentioned_context_changing(self): """ The "mentioned" context should not change while data is being loaded with the schema. This is to test that the "content" field of a value in the "mentioned" dict isn't changed to replace the name of a Factor with bracketed text. """ holdings = [ { "inputs": { "type": "fact", "content": "{Bradley} lived at Bradley's house", }, "outputs": { "type": "evidence", "to_effect": { "type": "fact", "name": "fact that Bradley committed a crime", "content": "Bradley committed a crime", }, "name": "evidence of Bradley's guilt", "absent": True, }, }, { "inputs": "fact that Bradley committed a crime", "outputs": {"type": "fact", "content": "Bradley committed a tort"}, }, ] expanded = text_expansion.expand_shorthand(holdings) built = readers.read_holdings(expanded) new_factor = built[0].outputs[0].to_effect.context_factors[0] assert new_factor.name == "Bradley"
def test_holding_flagged_exclusive(self, make_regime, make_enactment): """ Test that "exclusive" flag doesn't mess up the holding where it's placed. Test whether the Feist opinion object includes a holding with the output "Rural's telephone directory was copyrightable" and the input "Rural's telephone directory was original", when that holding was marked "exclusive" in the JSON. """ to_read = load_holdings("holding_feist.json") feist_holdings = readers.read_holdings(to_read, regime=make_regime) directory = Entity("Rural's telephone directory") original = Fact(Predicate("{} was an original work"), directory) copyrightable = Fact(Predicate("{} was copyrightable"), directory) originality_enactments = [ make_enactment["securing_for_authors"], make_enactment["right_to_writings"], make_enactment["copyright_requires_originality"], ] originality_rule = Rule( Procedure(outputs=copyrightable, inputs=original), mandatory=False, universal=False, enactments=originality_enactments, ) assert any( feist_holding.rule.means(originality_rule) for feist_holding in feist_holdings )
def test_specific_entity(self): different_entity_holdings = readers.read_holdings(self.smith_holdings) assert ( different_entity_holdings[1].generic_factors != different_entity_holdings[0].generic_factors ) assert not different_entity_holdings[1] >= different_entity_holdings[0]
def test_greater_than_implies_equal(self, beard_response, make_beard_rule): client = FakeClient(responses=beard_response) beard_dictionary = loaders.load_holdings("beard_rules.yaml") beard_dictionary[0]["inputs"][1][ "content"] = "the length of the suspected beard was = 8 millimetres" longer_hair_rule = readers.read_holdings([beard_dictionary[0]], client=client) assert make_beard_rule[0].implies(longer_hair_rule[0])
def test_same_enactment_objects_equal(self, make_regime): """ Don't expect the holdings imported from the JSON to exactly match the holdings created for testing in conftest. """ to_read = load_holdings("holding_watt.json") holdings = readers.read_holdings(to_read, regime=make_regime) assert holdings[0].enactments[0] == holdings[1].enactments[0]
def test_opinion_posits_holding(self, make_response): mock_client = FakeClient(responses=make_response) to_read = load_holdings("holding_brad.yaml") holdings = readers.read_holdings(to_read, client=mock_client) reading = OpinionReading() reading.posit(holdings[0]) assert "warrantless search and seizure" in reading.holdings[ 0].short_string
def test_passage_from_imported_statute(self, fake_usc_client): oracle = loaders.load_decision("oracle_h.json") oracle_decision = Decision(**oracle) reading = DecisionReading(decision=oracle_decision) loaded = loaders.load_holdings("holding_oracle.yaml") holdings = readers.read_holdings(loaded, client=fake_usc_client) reading.posit(holdings) despite_text = str(list(reading.holdings)[5]) assert "In no case does copyright protection " in despite_text
def test_same_object_for_enactment_in_import(self, make_regime): """ The JSON for Bradley repeats identical fields to create the same Factor for multiple Rules, instead of using the "name" field as a shortcut. This tests whether the loaded objects turn out equal. """ to_read = load_holdings("holding_brad.json") holdings = readers.read_holdings(to_read, regime=make_regime) assert any(holdings[6].inputs[0] == x for x in holdings[5].inputs)
def test_error_because_string_does_not_match_factor_name( self, make_response): rule_holding = { "inputs": ["this factor hasn't been mentioned"], "outputs": [{ "type": "fact", "content": "{the dog} bit {the man}" }], "enactments": [{ "enactment": { "node": "/us/const/amendment/IV" } }], "mandatory": True, } mock_client = FakeClient(responses=make_response) with pytest.raises(ValidationError): readers.read_holdings([rule_holding], client=mock_client)
def test_use_int_not_pint_without_dimension(self, make_regime, make_opinion): brad = make_opinion["brad_majority"] to_read = load_holdings("holding_brad.json") holdings = readers.read_holdings(to_read, regime=make_regime) brad.posit(holdings) expectation_not_reasonable = list(brad.holdings)[6] assert "dimensionless" not in str(expectation_not_reasonable) assert isinstance(expectation_not_reasonable.inputs[0].predicate.quantity, int)
def test_same_enactment_objects_equal(self, make_response): """ Don't expect the holdings imported from the JSON to exactly match the holdings created for testing in conftest. """ mock_client = FakeClient(responses=make_response) to_read = load_holdings("holding_watt.yaml") holdings = readers.read_holdings(to_read, client=mock_client) assert holdings[0].enactments[0].means(holdings[1].enactments[0])
def test_different_enactments_same_code(self, make_regime): """ Don't expect the holdings imported from the JSON to exactly match the holdings created for testing in conftest. """ to_read = load_holdings("holding_lotus.json") holdings = readers.read_holdings(to_read, regime=make_regime) assert holdings[0].enactments[0].code == holdings[1].enactments[0].code assert holdings[0].enactments[0].code is holdings[1].enactments[0].code
def test_same_object_for_enactment_in_import(self, make_response): """ The JSON for Bradley repeats identical fields to create the same Factor for multiple Rules, instead of using the "name" field as a shortcut. This tests whether the loaded objects turn out equal. """ mock_client = FakeClient(responses=make_response) to_read = load_holdings("holding_brad.yaml") holdings = readers.read_holdings(to_read, client=mock_client) assert any(holdings[6].inputs[0].means(x) for x in holdings[5].inputs)
def test_dump_and_read_holding(self, fake_usc_client, make_holding): """Dump holding and read it as if it came from YAML.""" holding = make_holding["h2"] dumped = holding.dict() content = dumped["rule"]["procedure"]["inputs"][0]["predicate"][ "content"] assert content == "$thing was on the premises of $place" loaded = readers.read_holdings([dumped], client=fake_usc_client) loaded_content = loaded[0].despite[0].predicate.content assert "the distance between $place1 and $place2 was" in loaded_content
def test_greater_than_contradicts_not_greater(self, beard_response, make_beard_rule): client = FakeClient(responses=beard_response) beard_dictionary = loaders.load_holdings("beard_rules.yaml") beard_dictionary[1]["inputs"][1][ "content"] = "the length of the suspected beard was >= 12 inches" beard_dictionary[1]["outputs"][0]["truth"] = False beard_dictionary[1]["mandatory"] = True long_hair_is_not_a_beard = readers.read_holdings([beard_dictionary[1]], client=client) assert make_beard_rule[1].contradicts(long_hair_is_not_a_beard[0])
def test_getting_factors_from_opinion(self, make_opinion_with_holding, make_response): client = FakeClient(responses=make_response) watt = make_opinion_with_holding["watt_majority"] watt.clear_holdings() watt_raw = loaders.load_holdings("holding_watt.yaml") holdings_to_posit = readers.read_holdings(watt_raw, client=client) watt.posit(holdings_to_posit) factors = watt.factors_by_name() assert "proof of Wattenburg's guilt" in factors.keys()
def test_name_inferred_from_content(self, make_regime): """ Test that a name field is generated for Factors without them. The Factors must be inserted in "mentioned" with the generated name. """ oracle_records = loaders.load_holdings("holding_oracle.json") oracle_holdings = readers.read_holdings(oracle_records, regime=make_regime) factor = oracle_holdings[2].inputs[0] assert factor.content == "{} was an original work"
def test_use_int_not_pint_without_dimension(self, make_response): mock_client = FakeClient(responses=make_response) to_read = load_holdings("holding_brad.yaml") loaded_holdings = readers.read_holdings(to_read, client=mock_client) anchored_holdings = AnchoredHoldings(holdings=[ HoldingWithAnchors(holding=item) for item in loaded_holdings ]) reading = OpinionReading(anchored_holdings=anchored_holdings) expectation_not_reasonable = list(reading.holdings)[6] assert "dimensionless" not in str(expectation_not_reasonable) assert expectation_not_reasonable.inputs[0].predicate.quantity == 3
def test_posit_list_of_holdings_and_imply(self, make_opinion_with_holding, make_response): watt = make_opinion_with_holding["watt_majority"] brad = make_opinion_with_holding["brad_majority"] watt.clear_holdings() brad.clear_holdings() client = FakeClient(responses=make_response) some_rules_raw = loaders.load_holdings(filename="holding_watt.yaml") some_rules = readers.read_holdings(some_rules_raw, client=client) for case in (watt, brad): case.clear_holdings() case.posit(some_rules[:3])