tangled
alpha
login
or
join now
urschrei.eurosky.social
/
pyzotero
0
fork
atom
Pyzotero: a Python client for the Zotero API
pyzotero.readthedocs.io
zotero
0
fork
atom
overview
issues
pulls
pipelines
Add attachment tests
urschrei.eurosky.social
1 year ago
06999d72
95d98789
+196
1 changed file
expand all
collapse all
unified
split
tests
test_zotero.py
+196
tests/test_zotero.py
···
1192
1192
1193
1193
# Clean up
1194
1194
os.remove(temp_file_path)
1195
1195
+
1196
1196
+
@httpretty.activate
1197
1197
+
def testAttachmentSimple(self):
1198
1198
+
"""Test attachment_simple method with a single file"""
1199
1199
+
zot = z.Zotero("myuserID", "user", "myuserkey")
1200
1200
+
1201
1201
+
# Create a temporary test file
1202
1202
+
temp_file_path = os.path.join(self.cwd, "api_responses", "test_attachment.txt")
1203
1203
+
with open(temp_file_path, "w") as f:
1204
1204
+
f.write("Test attachment content")
1205
1205
+
1206
1206
+
# Mock the item template response
1207
1207
+
HTTPretty.register_uri(
1208
1208
+
HTTPretty.GET,
1209
1209
+
"https://api.zotero.org/items/new?itemType=attachment&linkMode=imported_file",
1210
1210
+
content_type="application/json",
1211
1211
+
body=json.dumps({"itemType": "attachment", "linkMode": "imported_file"})
1212
1212
+
)
1213
1213
+
1214
1214
+
# Mock the item creation response
1215
1215
+
HTTPretty.register_uri(
1216
1216
+
HTTPretty.POST,
1217
1217
+
"https://api.zotero.org/users/myuserID/items",
1218
1218
+
content_type="application/json",
1219
1219
+
body=json.dumps({"success": {"0": "ITEMKEY123"}})
1220
1220
+
)
1221
1221
+
1222
1222
+
# Patch the necessary methods to avoid HTTP calls and file system checks
1223
1223
+
with patch.object(z.Zupload, "_verify", return_value=None), \
1224
1224
+
patch.object(z.Zupload, "_get_auth", return_value={
1225
1225
+
"url": "https://uploads.zotero.org/",
1226
1226
+
"params": {"key": "abcdef1234567890"},
1227
1227
+
"uploadKey": "upload_key_123"
1228
1228
+
}), \
1229
1229
+
patch.object(z.Zupload, "_upload_file", return_value=None):
1230
1230
+
1231
1231
+
# Test attachment_simple with a single file
1232
1232
+
result = zot.attachment_simple([temp_file_path])
1233
1233
+
1234
1234
+
# Verify the result structure
1235
1235
+
self.assertIn("success", result)
1236
1236
+
self.assertEqual(len(result["success"]), 1)
1237
1237
+
1238
1238
+
# Verify that the correct attachment template was used
1239
1239
+
request = httpretty.last_request()
1240
1240
+
payload = json.loads(request.body.decode("utf-8"))
1241
1241
+
self.assertEqual(payload[0]["title"], "test_attachment.txt")
1242
1242
+
self.assertEqual(payload[0]["filename"], temp_file_path)
1243
1243
+
1244
1244
+
# Clean up
1245
1245
+
os.remove(temp_file_path)
1246
1246
+
1247
1247
+
@httpretty.activate
1248
1248
+
def testAttachmentSimpleWithParent(self):
1249
1249
+
"""Test attachment_simple method with a parent ID"""
1250
1250
+
zot = z.Zotero("myuserID", "user", "myuserkey")
1251
1251
+
1252
1252
+
# Create a temporary test file
1253
1253
+
temp_file_path = os.path.join(self.cwd, "api_responses", "test_attachment.txt")
1254
1254
+
with open(temp_file_path, "w") as f:
1255
1255
+
f.write("Test attachment content")
1256
1256
+
1257
1257
+
# Mock the item template response
1258
1258
+
HTTPretty.register_uri(
1259
1259
+
HTTPretty.GET,
1260
1260
+
"https://api.zotero.org/items/new?itemType=attachment&linkMode=imported_file",
1261
1261
+
content_type="application/json",
1262
1262
+
body=json.dumps({"itemType": "attachment", "linkMode": "imported_file"})
1263
1263
+
)
1264
1264
+
1265
1265
+
# Patch the _attachment method to verify it's called correctly
1266
1266
+
with patch.object(z.Zotero, "_attachment") as mock_attachment:
1267
1267
+
# Set up the mock return value
1268
1268
+
mock_attachment.return_value = {"success": [{"key": "ITEMKEY123"}]}
1269
1269
+
1270
1270
+
# Test attachment_simple with a parent ID
1271
1271
+
result = zot.attachment_simple([temp_file_path], parentid="PARENT123")
1272
1272
+
1273
1273
+
# Verify the result structure matches the mock return value
1274
1274
+
self.assertEqual(result, {"success": [{"key": "ITEMKEY123"}]})
1275
1275
+
1276
1276
+
# Check that _attachment was called with the parent ID
1277
1277
+
mock_attachment.assert_called_once()
1278
1278
+
args = mock_attachment.call_args[0]
1279
1279
+
# First argument is the templates list, second is parent ID
1280
1280
+
self.assertEqual(len(args), 2)
1281
1281
+
self.assertEqual(args[1], "PARENT123")
1282
1282
+
1283
1283
+
# Verify the template was correctly set up
1284
1284
+
templates = args[0]
1285
1285
+
self.assertEqual(len(templates), 1)
1286
1286
+
self.assertEqual(templates[0]["title"], "test_attachment.txt")
1287
1287
+
self.assertEqual(templates[0]["filename"], temp_file_path)
1288
1288
+
1289
1289
+
# Clean up
1290
1290
+
os.remove(temp_file_path)
1291
1291
+
1292
1292
+
@httpretty.activate
1293
1293
+
def testAttachmentBoth(self):
1294
1294
+
"""Test attachment_both method with custom title and filename"""
1295
1295
+
zot = z.Zotero("myuserID", "user", "myuserkey")
1296
1296
+
1297
1297
+
# Create a temporary test file
1298
1298
+
temp_file_path = os.path.join(self.cwd, "api_responses", "test_attachment.txt")
1299
1299
+
with open(temp_file_path, "w") as f:
1300
1300
+
f.write("Test attachment content")
1301
1301
+
1302
1302
+
# Mock the item template response
1303
1303
+
HTTPretty.register_uri(
1304
1304
+
HTTPretty.GET,
1305
1305
+
"https://api.zotero.org/items/new?itemType=attachment&linkMode=imported_file",
1306
1306
+
content_type="application/json",
1307
1307
+
body=json.dumps({"itemType": "attachment", "linkMode": "imported_file"})
1308
1308
+
)
1309
1309
+
1310
1310
+
# Mock the item creation response
1311
1311
+
HTTPretty.register_uri(
1312
1312
+
HTTPretty.POST,
1313
1313
+
"https://api.zotero.org/users/myuserID/items",
1314
1314
+
content_type="application/json",
1315
1315
+
body=json.dumps({"success": {"0": "ITEMKEY123"}})
1316
1316
+
)
1317
1317
+
1318
1318
+
# Patch the necessary methods to avoid HTTP calls and file system checks
1319
1319
+
with patch.object(z.Zupload, "_verify", return_value=None), \
1320
1320
+
patch.object(z.Zupload, "_get_auth", return_value={
1321
1321
+
"url": "https://uploads.zotero.org/",
1322
1322
+
"params": {"key": "abcdef1234567890"},
1323
1323
+
"uploadKey": "upload_key_123"
1324
1324
+
}), \
1325
1325
+
patch.object(z.Zupload, "_upload_file", return_value=None):
1326
1326
+
1327
1327
+
# Test attachment_both with custom title
1328
1328
+
custom_title = "Custom Attachment Title"
1329
1329
+
files = [(custom_title, temp_file_path)]
1330
1330
+
result = zot.attachment_both(files)
1331
1331
+
1332
1332
+
# Verify the result structure
1333
1333
+
self.assertIn("success", result)
1334
1334
+
self.assertEqual(len(result["success"]), 1)
1335
1335
+
1336
1336
+
# Verify that the correct attachment template was used
1337
1337
+
request = httpretty.last_request()
1338
1338
+
payload = json.loads(request.body.decode("utf-8"))
1339
1339
+
self.assertEqual(payload[0]["title"], custom_title)
1340
1340
+
self.assertEqual(payload[0]["filename"], temp_file_path)
1341
1341
+
1342
1342
+
# Clean up
1343
1343
+
os.remove(temp_file_path)
1344
1344
+
1345
1345
+
@httpretty.activate
1346
1346
+
def testAttachmentBothWithParent(self):
1347
1347
+
"""Test attachment_both method with a parent ID"""
1348
1348
+
zot = z.Zotero("myuserID", "user", "myuserkey")
1349
1349
+
1350
1350
+
# Create a temporary test file
1351
1351
+
temp_file_path = os.path.join(self.cwd, "api_responses", "test_attachment.txt")
1352
1352
+
with open(temp_file_path, "w") as f:
1353
1353
+
f.write("Test attachment content")
1354
1354
+
1355
1355
+
# Mock the item template response
1356
1356
+
HTTPretty.register_uri(
1357
1357
+
HTTPretty.GET,
1358
1358
+
"https://api.zotero.org/items/new?itemType=attachment&linkMode=imported_file",
1359
1359
+
content_type="application/json",
1360
1360
+
body=json.dumps({"itemType": "attachment", "linkMode": "imported_file"})
1361
1361
+
)
1362
1362
+
1363
1363
+
# Patch the _attachment method to verify it's called correctly
1364
1364
+
with patch.object(z.Zotero, "_attachment") as mock_attachment:
1365
1365
+
# Set up the mock return value
1366
1366
+
mock_attachment.return_value = {"success": [{"key": "ITEMKEY123"}]}
1367
1367
+
1368
1368
+
# Test attachment_both with a parent ID
1369
1369
+
custom_title = "Custom Attachment Title"
1370
1370
+
files = [(custom_title, temp_file_path)]
1371
1371
+
result = zot.attachment_both(files, parentid="PARENT123")
1372
1372
+
1373
1373
+
# Verify the result structure matches the mock return value
1374
1374
+
self.assertEqual(result, {"success": [{"key": "ITEMKEY123"}]})
1375
1375
+
1376
1376
+
# Check that _attachment was called with the parent ID
1377
1377
+
mock_attachment.assert_called_once()
1378
1378
+
args = mock_attachment.call_args[0]
1379
1379
+
# First argument is the templates list, second is parent ID
1380
1380
+
self.assertEqual(len(args), 2)
1381
1381
+
self.assertEqual(args[1], "PARENT123")
1382
1382
+
1383
1383
+
# Verify the template was correctly set up
1384
1384
+
templates = args[0]
1385
1385
+
self.assertEqual(len(templates), 1)
1386
1386
+
self.assertEqual(templates[0]["title"], custom_title)
1387
1387
+
self.assertEqual(templates[0]["filename"], temp_file_path)
1388
1388
+
1389
1389
+
# Clean up
1390
1390
+
os.remove(temp_file_path)
1195
1391
1196
1392
def tearDown(self):
1197
1393
"""Tear stuff down"""