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