def extract_anchors_from_holding_record( record: List[RawHolding], client: Optional[Client] = None ) -> Tuple[List[Holding], List[EnactmentWithAnchors], List[TermWithAnchors], List[Dict[str, str]], ]: r""" Load a list of Holdings from JSON, with text links. :param record: a list of dicts representing holdings, in the JSON input format :param client: Legislice client for downloading missing fields from `record` :returns: a tuple of four objects containing holdings, terms, enactments, and anchors. """ record_post_enactments, enactment_index = collect_enactments(record) if client: enactment_index_post_client = client.update_entries_in_enactment_index( enactment_index) else: enactment_index_post_client = enactment_index enactment_anchors, enactment_index_post_anchors = collect_anchors_from_index( enactment_index_post_client, "passage") enactment_result = [] for anchor in enactment_anchors: anchor["passage"] = enactment_index_post_anchors.get_if_present( anchor["passage"]) enactment_result.append(EnactmentWithAnchors(**anchor)) record_post_terms, factor_index = index_names(record_post_enactments) factor_anchors, factor_index_post_anchors = collect_anchors_from_index( factor_index, "term") factor_result = [] for anchor in factor_anchors: anchor["term"] = expand_holding( anchor["term"], factor_index=factor_index_post_anchors, enactment_index=enactment_index_post_anchors, ) factor_result.append(TermWithAnchors(**anchor)) factor_anchors = [TermWithAnchors(**anchor) for anchor in factor_anchors] expanded = expand_holdings( record_post_terms, factor_index=factor_index_post_anchors, enactment_index=enactment_index_post_anchors, ) holding_anchors = [holding.pop("anchors", None) for holding in expanded] result = [] for holding in expanded: result.append(Holding(**holding)) return result, enactment_result, factor_result, holding_anchors
def test_load_updated_enactment_data(self, test_client): example_rules, mentioned = collect_enactments(self.example_rules) updated = test_client.update_enactment_from_api( mentioned["ear rule"]["enactment"] ) enactment = Enactment(**updated) assert enactment.start_date == date(1935, 4, 1) assert enactment.content.startswith("exists in an uninterrupted")
def test_update_unloaded_enactment_from_api(self, test_client): example_rules, mentioned = collect_enactments(self.example_rules) updated = test_client.update_enactment_from_api( mentioned["ear rule"]["enactment"] ) assert updated["node"] == "/test/acts/47/4/b" assert updated["anchors"][0]["start"] == 10 assert updated["text_version"]["content"].startswith( "exists in an uninterrupted" ) assert updated["start_date"] == "1935-04-01"
def test_collect_enactments_from_list( self, section6d, section_11_subdivided, fifth_a ): data = { "outputs": [{"type": "fact", "content": "the beard grew"}], "enactments": [ {"name": "s11", "enactment": section_11_subdivided}, {"name": "6d", "enactment": section6d}, {"name": "5a", "enactment": fifth_a}, ], } obj, mentioned = collect_enactments(data) assert mentioned["s11"]["enactment"]["node"] == "/test/acts/47/11"
def test_retrieve_enactment_by_name(self, section6d, section_11_subdivided): data, indexed = collect_enactments([section6d, section_11_subdivided]) enactments = [Enactment(**item) for item in data] assert enactments[0].start_date.isoformat() == "1935-04-01"
def test_collect_enactment_anchors_from_dict(self): """Anchors for this Enactment are collected in two different places.""" example_rules, mentioned = collect_enactments(self.example_rules) assert mentioned["ear rule"]["enactment"]["anchors"][0]["start"] == 10 assert mentioned["ear rule"]["enactment"]["anchors"][2]["start"] == 100
def test_replace_enactment_in_source_with_name(self): example_rules, mentioned = collect_enactments(self.example_rules) assert example_rules[0]["enactments"][0] == "beard means" assert example_rules[0]["enactments"][1] == '/test/acts/47/4/a:suffix=", or"'
def test_collect_enactments_from_dict(self): obj, mentioned = collect_enactments(self.example_rules) assert mentioned["beard means"]["enactment"]["node"] == "/test/acts/47/4"