1
1
import json
2
2
import httpx
3
+ import pymarc
3
4
import pytest
4
5
5
6
from jsonpath_ng .ext import parse
@@ -51,13 +52,29 @@ def marc_json():
51
52
}
52
53
53
54
55
+ @pytest .fixture
56
+ def marc_979 ():
57
+ return {
58
+ "979" : {
59
+ "ind1" : " " ,
60
+ "ind2" : " " ,
61
+ "subfields" : [
62
+ {"f" : "ABBOTT" },
63
+ {"b" : "druid:ws066yy0421" },
64
+ {"c" : "ws066yy0421_00_0001.jp2" },
65
+ {"d" : "The Donald P. Abbott Fund for Marine Invertebrates" },
66
+ ],
67
+ }
68
+ }
69
+
70
+
54
71
@pytest .fixture
55
72
def marc_instance_tags ():
56
73
return {
57
74
"979" : [
58
75
{
59
- "ind1" : "" ,
60
- "ind2" : "" ,
76
+ "ind1" : " " ,
77
+ "ind2" : " " ,
61
78
"subfields" : [
62
79
{"f" : "ABBOTT" },
63
80
{"b" : "druid:ws066yy0421" },
@@ -69,6 +86,34 @@ def marc_instance_tags():
69
86
}
70
87
71
88
89
+ @pytest .fixture
90
+ def marc_instance_two_tags ():
91
+ return {
92
+ "979" : [
93
+ {
94
+ "ind1" : " " ,
95
+ "ind2" : " " ,
96
+ "subfields" : [
97
+ {"f" : "STEINMETZ" },
98
+ {"b" : "druid:nc092rd1979" },
99
+ {"c" : "nc092rd1979_00_0001.jp2" },
100
+ {"d" : "Verna Pace Steinmetz Endowed Book Fund in History" },
101
+ ],
102
+ },
103
+ {
104
+ "ind1" : " " ,
105
+ "ind2" : " " ,
106
+ "subfields" : [
107
+ {"f" : "WHITEHEAD" },
108
+ {"b" : "druid:ph944pq1002" },
109
+ {"c" : "ph944pq1002_00_0001.jp2" },
110
+ {"d" : "Barry Whitehead Memorial Book Fund" },
111
+ ],
112
+ },
113
+ ]
114
+ }
115
+
116
+
72
117
@pytest .fixture
73
118
def mock_folio_add_marc_tags (mocker ):
74
119
mock_httpx = mocker .MagicMock ()
@@ -87,50 +132,15 @@ def mock_folio_add_marc_tags(mocker):
87
132
def mock_httpx_client ():
88
133
def mock_response (request ):
89
134
response = None
90
- # following is not used in test but leaving here for testing more parts of utils.py
91
- # match request.method:
92
-
93
- # case 'PUT':
94
- # if request.url.path.startswith('/change-manager/parsedRecords'):
95
- # response = httpx.Response(status_code=202)
96
-
97
135
return response
98
136
99
137
return httpx .Client (transport = httpx .MockTransport (mock_response ))
100
138
101
139
102
140
def mock_folio_client (mocker ):
103
- # following is not used in test but leaving here for testing more parts of utils.py
104
- # def __srs_response__(path: str):
105
- # output = {}
106
- # instance_uuid = path.split("instanceId=")[-1]
107
-
108
- # match instance_uuid:
109
- # case "06660d4f-982d-54e8-b34c-532c268868e1":
110
- # output = {
111
- # "sourceRecords": [
112
- # {
113
- # "recordId": "e60b77d3-3a76-59e2-88f7-3d1a045af3b1",
114
- # "parsedRecord": {"content": marc_json},
115
- # }
116
- # ]
117
- # }
118
-
119
- # return output
120
141
121
142
def mock_folio_get (* args , ** kwargs ):
122
143
output = {}
123
- # following is not used in test but leaving here for testing more parts of utils.py
124
- # if args[0].startswith("/source-storage/source-records"):
125
- # output = __srs_response__(args[0])
126
- # if args[0].startswith("/inventory/instances/"):
127
- # for instance_uuid in [
128
- # "64a5a15b-d89e-4bdd-bbd6-fcd215b367e4",
129
- # "242c6000-8485-5fcd-9b5e-adb60788ca59",
130
- # ]:
131
- # if args[0].endswith(instance_uuid):
132
- # output = {"_version": "1", "hrid": "a123456"}
133
-
134
144
return output
135
145
136
146
mock = mocker
@@ -141,7 +151,7 @@ def mock_folio_get(*args, **kwargs):
141
151
142
152
143
153
def test__marc_json_with_new_tags__ (
144
- mock_folio_add_marc_tags , marc_json , marc_instance_tags
154
+ mock_folio_add_marc_tags , marc_json , marc_instance_tags , caplog
145
155
):
146
156
add_marc_tag = utils .FolioAddMarcTags ()
147
157
marc_json_with_new_tags = add_marc_tag .__marc_json_with_new_tags__ (
@@ -151,3 +161,65 @@ def test__marc_json_with_new_tags__(
151
161
tag_979_exp = parse ("$.fields[?(@['979'])]" )
152
162
tag_979 = tag_979_exp .find (new_record_dict )[0 ].value
153
163
assert len (tag_979 ["979" ]["subfields" ]) == 4
164
+ assert "New field 979 is unique tag." in caplog .text
165
+
166
+
167
+ def test__marc_json_with_two_new_tags__ (
168
+ mock_folio_add_marc_tags , marc_json , marc_instance_two_tags , caplog
169
+ ):
170
+ add_marc_tag = utils .FolioAddMarcTags ()
171
+ marc_json_with_new_tags = add_marc_tag .__marc_json_with_new_tags__ (
172
+ marc_json , marc_instance_two_tags
173
+ )
174
+ new_record_dict = json .loads (marc_json_with_new_tags )
175
+ tag_979_exp = parse ("$.fields[?(@['979'])]" )
176
+ new_979_tags = tag_979_exp .find (new_record_dict )
177
+ assert len (new_979_tags ) == 2
178
+ assert (
179
+ "Record does not have existing 979's. New fields will be added." in caplog .text
180
+ )
181
+
182
+
183
+ def test__marc_json_existing_tags__ (
184
+ mock_folio_add_marc_tags , marc_json , marc_979 , marc_instance_tags , caplog
185
+ ):
186
+ add_marc_tag = utils .FolioAddMarcTags ()
187
+ marc_json ["fields" ].append (marc_979 )
188
+ marc_json_with_new_tags = add_marc_tag .__marc_json_with_new_tags__ (
189
+ marc_json , marc_instance_tags
190
+ )
191
+ new_record_dict = json .loads (marc_json_with_new_tags )
192
+ tag_979_exp = parse ("$.fields[?(@['979'])]" )
193
+ new_979_tags = tag_979_exp .find (new_record_dict )
194
+ assert len (new_979_tags ) == 1
195
+ assert (
196
+ "Record has existing 979's. New fields will be evaluated for uniqueness."
197
+ in caplog .text
198
+ )
199
+ assert (
200
+ "Skip adding duplicated ABBOTT druid:ws066yy0421 ws066yy0421_00_0001.jp2 The Donald P. Abbott Fund for Marine Invertebrates field"
201
+ in caplog .text
202
+ )
203
+ assert "New field 979 is not unique" in caplog .text
204
+
205
+
206
+ def test__tag_is_unique__ (mock_folio_add_marc_tags , marc_json ):
207
+ add_marc_tag = utils .FolioAddMarcTags ()
208
+ reader = pymarc .reader .JSONReader (json .dumps (marc_json ))
209
+ for record in reader :
210
+ existing_tags = record .get_fields ("979" )
211
+
212
+ new_field = pymarc .Field (
213
+ tag = "979" ,
214
+ indicators = [" " , " " ],
215
+ subfields = [
216
+ pymarc .Subfield (code = 'f' , value = 'ABBOTT' ),
217
+ pymarc .Subfield (code = 'b' , value = 'druid:ws066yy0421' ),
218
+ pymarc .Subfield (code = 'c' , value = 'ws066yy0421_00_0001.jp2' ),
219
+ pymarc .Subfield (
220
+ code = 'd' ,
221
+ value = 'The The Donald P. Abbott Fund for Marine Invertebrates' ,
222
+ ),
223
+ ],
224
+ )
225
+ assert add_marc_tag .__tag_is_unique__ (existing_tags , new_field ) is True
0 commit comments