返回 last30days-skill
test_company_names.py
根目录 / tests / test_company_names.py
1 """Tests for the shared company-name helper."""
2
3 import json
4 import sys
5 from pathlib import Path
6
7 import pytest
8
9 ROOT = Path(__file__).resolve().parent.parent
10 sys.path.insert(0, str(ROOT / "skills" / "last30days" / "scripts"))
11
12 from lib.company_names import ( # noqa: E402
13 CONFIRMED,
14 NAME_MATCHED,
15 NEITHER,
16 classify_applicant,
17 name_key,
18 )
19
20 FIXTURE = ROOT / "tests" / "fixtures" / "ppubs_lists.json"
21
22
23 def test_three_values():
24 assert (CONFIRMED, NAME_MATCHED, NEITHER) == ("confirmed", "name-matched", "neither")
25
26
27 @pytest.mark.parametrize(
28 "raw, key",
29 [
30 ("Apple Inc.", "apple"),
31 ("APPLE INC.", "apple"),
32 ("Apple, Inc.", "apple"),
33 ("Samsung Electronics Co., Ltd.", "samsung electronics"),
34 ("SEOUL NATIONAL UNIVERSITY R&DB FOUNDATION", "seoul national university r db foundation"),
35 ("Inc.", "inc"),
36 ("LLC", "llc"),
37 ("Inc. LLC", "inc"),
38 ("", ""),
39 (" ", ""),
40 ],
41 )
42 def test_name_key(raw, key):
43 assert name_key(raw) == key
44
45
46 @pytest.mark.parametrize("applicant", ["Apple Inc.", "APPLE INC.", "Apple, Inc."])
47 @pytest.mark.parametrize("company", ["Apple Inc.", "Apple"])
48 def test_apple_confirmed(applicant, company):
49 assert classify_applicant(applicant, company) == CONFIRMED
50 assert classify_applicant(company, applicant) == CONFIRMED
51
52
53 # (applicant, company, known names, expected)
54 CASES = [
55 ("Deep Apple Therapeutics, Inc.", "Apple", [], NEITHER),
56 ("Google LLC", "Google", [], CONFIRMED),
57 ("Anthropic, PBC", "Anthropic", [], CONFIRMED),
58 ("Meta Platforms Technologies, LLC", "Meta Platforms", [], NAME_MATCHED),
59 ("Metabase, Inc.", "Meta", [], NEITHER),
60 ("Google LLC", "Alphabet", ["Google"], CONFIRMED),
61 ("Google LLC", "Alphabet", [], NEITHER),
62 ("Waymo LLC", "Alphabet", ["Google", "Waymo"], CONFIRMED),
63 ("Waymo LLC", "Alphabet", ["Google"], NEITHER),
64 (
65 "International Business Machines Corporation",
66 "IBM",
67 ["International Business Machines Corporation"],
68 CONFIRMED,
69 ),
70 ("International Business Machines Corporation", "IBM", [], NEITHER),
71 ("Microsoft Technology Licensing, LLC", "Microsoft", [], CONFIRMED),
72 ("Samsung Electronics Co., Ltd.", "Samsung", [], NAME_MATCHED),
73 ("Microsoft Corporation", "Microsoft", [], CONFIRMED),
74 ("Block Imaging, Inc.", "Block", [], NAME_MATCHED),
75 ("Square Enix Co., Ltd.", "Square", [], NAME_MATCHED),
76 ("QUICK BLOCK LIMITED", "Block", [], NEITHER),
77 ("SEOUL NATIONAL UNIVERSITY R&DB FOUNDATION", "Samsung", [], NEITHER),
78 ("Amazon Technologies, Inc.", "Amazon", [], NAME_MATCHED),
79 ("OpenAI OpCo, LLC", "OpenAI", [], CONFIRMED),
80 ("Notion Labs, Inc.", "Notion", [], NAME_MATCHED),
81 ("Block Imaging, Inc.", "Alphabet", ["Block"], NAME_MATCHED),
82 # A company's own holding, licensing and numbered entities are the company.
83 ("Intellectual Ventures Holding 81 LLC", "Intellectual Ventures", [], CONFIRMED),
84 ("Intellectual Ventures II LLC", "Intellectual Ventures", [], CONFIRMED),
85 ("Apple Rubber Products Inc.", "Apple", [], NAME_MATCHED),
86 ("OpenAI Group PBC", "OpenAI", [], CONFIRMED),
87 ("Acme Holdco, Inc.", "Acme", [], CONFIRMED),
88 ("Acme Holdings Group LLC", "Acme", [], CONFIRMED),
89 ("Acme IP, LLC", "Acme", [], CONFIRMED),
90 ("Acme Intellectual Property Ltd.", "Acme", [], CONFIRMED),
91 ("Acme 2, Inc.", "Acme", [], CONFIRMED),
92 ("Waymo Holding Inc.", "Alphabet", ["Waymo"], CONFIRMED),
93 # A structure word among other words does not make the name the company's.
94 ("Acme Technology Holdings", "Acme", [], NAME_MATCHED),
95 ("Acme Licensing LLC", "Acme", [], NAME_MATCHED),
96 ("Acme Property Group", "Acme", [], NAME_MATCHED),
97 ("Acme Mix LLC", "Acme", [], NAME_MATCHED),
98 ("Group Acme", "Acme", [], NEITHER),
99 ]
100
101
102 @pytest.mark.parametrize("applicant, company, known, expected", CASES)
103 def test_classification(applicant, company, known, expected):
104 assert classify_applicant(applicant, company, known) == expected
105
106
107 def test_known_names_default_none_and_tuple():
108 assert classify_applicant("Google LLC", "Alphabet") == NEITHER
109 assert classify_applicant("Google LLC", "Alphabet", None) == NEITHER
110 assert classify_applicant("Google LLC", "Alphabet", ("Google",)) == CONFIRMED
111
112
113 def test_known_name_prefix_is_name_matched():
114 assert classify_applicant("Google Technology Holdings", "Alphabet", ["Google"]) == NAME_MATCHED
115
116
117 @pytest.mark.parametrize("junk", ["", " ", "Inc.", "LLC", "Co., Ltd.", None])
118 def test_junk_never_matches(junk):
119 assert classify_applicant(junk, "Apple") == NEITHER
120 assert classify_applicant("Apple Inc.", junk) == NEITHER
121 assert classify_applicant(junk, junk) == NEITHER
122 assert classify_applicant("Apple Inc.", "Apple", [junk, ""]) == CONFIRMED
123 assert classify_applicant("Inc.", "Apple", ["LLC", ""]) == NEITHER
124
125
126 def _names(patents):
127 for patent in patents:
128 for field in ("applicantName", "assigneeName"):
129 value = patent.get(field)
130 if isinstance(value, str):
131 value = [value]
132 for name in value or []:
133 yield name
134
135
136 def _lists():
137 return json.loads(FIXTURE.read_text())["lists"]
138
139
140 def _names_for(company):
141 out = []
142 for key, entry in _lists().items():
143 if key.split("|")[0] == company:
144 out.extend(_names(entry.get("patents") or []))
145 return out
146
147
148 # Recorded names that extend the company's name by structure words alone, and
149 # so count as the company's. Every other confirmed name has the company's key.
150 OWN_EXTENSIONS = {
151 "Microsoft": {"microsoft technology licensing"},
152 "Sony": {"sony group"},
153 "OpenAI": {"openai opco"},
154 }
155
156
157 @pytest.mark.parametrize(
158 "company",
159 ["Apple", "Nvidia", "Figure AI", "Google", "Block", "Microsoft", "Amazon", "Samsung", "Sony",
160 "OpenAI", "Notion"],
161 )
162 def test_real_data_confirmed_is_the_key_or_a_structure_extension(company):
163 names = _names_for(company)
164 assert names
165 extensions = {
166 name_key(name) for name in names
167 if classify_applicant(name, company) == CONFIRMED and name_key(name) != name_key(company)
168 }
169 assert extensions == OWN_EXTENSIONS.get(company, set())
170
171
172 @pytest.mark.parametrize("company", ["Amazon", "Samsung", "Sony", "Notion"])
173 def test_real_data_keeps_name_matched_names(company):
174 kinds = [classify_applicant(name, company) for name in _names_for(company)]
175 assert NAME_MATCHED in kinds
176
176 lines PYTHON