示例#1
0
def from_snapshot(
    local_snapshot: List[Snapshot], remote_snapshot: List[Snapshot]
) -> (List[TermToAdd], List[TermToReplace], List[TermToRemove]):
    terms_to_add = []
    for rs in remote_snapshot:
        term_to_add = TermToAdd(rs.url, rs.term)
        terms_to_add.append(term_to_add)

    local_snapshot_map = {}
    for ls in local_snapshot:
        local_snapshot_map[ls.id] = ls
    terms_to_replace = []
    for rs in remote_snapshot:
        if rs.id in local_snapshot_map:
            ls = local_snapshot_map[rs.id]
            if ls.term != rs.term:
                term_to_replace = TermToReplace(ls.url, rs.url, rs.term)
                terms_to_replace.append(term_to_replace)

    remote_snapshot_set = set()
    for rs in remote_snapshot:
        remote_snapshot_set.add(rs.id)
    terms_to_remove = []
    for ls in local_snapshot:
        if ls.id not in remote_snapshot_set:
            term_to_remove = TermToRemove(ls.url, ls.term)
            terms_to_remove.append(term_to_remove)

    return terms_to_add, terms_to_replace, terms_to_remove
 def test_basic(self):
     result = add_links("word", [TermToAdd("link", "word")])
     self.assertEqual("[word](link)", result)
 def test_english(self):
     result = add_links("[english](english url)",
                        [TermToAdd("link", "english")])
     self.assertEqual("[english](english url)", result)
 def test_link_in_text(self):
     result = add_links("[a 単語 in](link)", [TermToAdd("link", "単語")])
     self.assertEqual("[a 単語 in](link)", result)
 def test_overlapping_terminology(self):
     result = add_links(
         "単語帳 単語", [TermToAdd("link1", "単語"),
                    TermToAdd("link2", "単語帳")])
     self.assertEqual("[単語帳](link2) [単語](link1)", result)
 def test_multiple_matches(self):
     result = add_links("単語 単語", [TermToAdd("link", "単語")])
     self.assertEqual("[単語](link) [単語](link)", result)
 def test_multiple_terms(self):
     result = add_links(
         "単語1 単語2", [TermToAdd('link1', '単語1'),
                     TermToAdd('link2', '単語2')])
     self.assertEqual("[単語1](link1) [単語2](link2)", result)