Migrate python-prior/tests_manual/ to pathlib (#14043)

This change has no functional impact. In my view, `pathlib` has a more
pleasant API than `os.path`.  Incidentally, this slightly reduces line
count.

cc @spacether
This commit is contained in:
Jeremy Audet 2022-11-19 10:46:42 -05:00 committed by GitHub
parent 1748d03fb9
commit 9039c83bc4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 18 additions and 29 deletions

View File

@ -9,7 +9,6 @@ $ cd OpenAPIetstore-python
$ nosetests -v $ nosetests -v
""" """
import os
import time import time
import atexit import atexit
import datetime import datetime

View File

@ -10,7 +10,6 @@ $ nosetests -v
""" """
from collections import namedtuple from collections import namedtuple
import json import json
import os
import time import time
import unittest import unittest
import datetime import datetime
@ -324,4 +323,4 @@ class DeserializationTests(unittest.TestCase):
number = self.deserialize(response, number = self.deserialize(response,
(number_with_validations.NumberWithValidations,), True) (number_with_validations.NumberWithValidations,), True)
self.assertTrue(isinstance(number, number_with_validations.NumberWithValidations)) self.assertTrue(isinstance(number, number_with_validations.NumberWithValidations))
self.assertTrue(number.value == number_val) self.assertTrue(number.value == number_val)

View File

@ -11,7 +11,6 @@
import sys import sys
from collections import namedtuple from collections import namedtuple
import os
import json import json
import unittest import unittest
from pathlib import Path from pathlib import Path
@ -381,9 +380,7 @@ class TestFakeApi(unittest.TestCase):
def test_upload_file(self): def test_upload_file(self):
# uploads a file # uploads a file
test_file_dir = os.path.realpath( file_path1 = Path(__file__, "..", "..", "testfiles", "1px_pic1.png").resolve()
os.path.join(os.path.dirname(__file__), "..", "testfiles"))
file_path1 = os.path.join(test_file_dir, "1px_pic1.png")
headers = {} headers = {}
def get_headers(): def get_headers():
@ -439,10 +436,8 @@ class TestFakeApi(unittest.TestCase):
self.api.upload_file(file=file) self.api.upload_file(file=file)
def test_upload_files(self): def test_upload_files(self):
test_file_dir = os.path.realpath( file_path1 = Path(__file__, "..", "..", "testfiles", "1px_pic1.png").resolve()
os.path.join(os.path.dirname(__file__), "..", "testfiles")) file_path2 = Path(__file__, "..", "..", "testfiles", "1px_pic2.png").resolve()
file_path1 = os.path.join(test_file_dir, "1px_pic1.png")
file_path2 = os.path.join(test_file_dir, "1px_pic2.png")
headers = {} headers = {}
def get_headers(): def get_headers():
@ -579,12 +574,10 @@ class TestFakeApi(unittest.TestCase):
self.assertEqual(file_object.read(), file_data.encode('utf-8')) self.assertEqual(file_object.read(), file_data.encode('utf-8'))
finally: finally:
file_object.close() file_object.close()
os.unlink(file_object.name) Path(file_object.name).unlink()
def test_upload_download_file(self): def test_upload_download_file(self):
test_file_dir = os.path.realpath( file_path1 = Path(__file__, "..", "..", "testfiles", "1px_pic1.png").resolve()
os.path.join(os.path.dirname(__file__), "..", "testfiles"))
file_path1 = os.path.join(test_file_dir, "1px_pic1.png")
with open(file_path1, "rb") as f: with open(file_path1, "rb") as f:
expected_file_data = f.read() expected_file_data = f.read()
@ -623,7 +616,7 @@ class TestFakeApi(unittest.TestCase):
finally: finally:
file1.close() file1.close()
downloaded_file.close() downloaded_file.close()
os.unlink(downloaded_file.name) Path(downloaded_file.name).unlink()
def test_test_body_with_file_schema(self): def test_test_body_with_file_schema(self):
"""Test case for test_body_with_file_schema """Test case for test_body_with_file_schema

View File

@ -19,6 +19,7 @@ import os
import re import re
import shutil import shutil
import unittest import unittest
from pathlib import Path
from urllib.parse import urlencode, urlparse from urllib.parse import urlencode, urlparse
from Crypto.Hash import SHA256, SHA512 from Crypto.Hash import SHA256, SHA512
@ -205,7 +206,7 @@ class PetApiTests(unittest.TestCase):
cls.ec_p521_key_path, cls.ec_p521_key_path,
] ]
for file_path in file_paths: for file_path in file_paths:
os.unlink(file_path) Path(file_path).unlink()
@classmethod @classmethod
def setUpModels(cls): def setUpModels(cls):
@ -226,22 +227,19 @@ class PetApiTests(unittest.TestCase):
@classmethod @classmethod
def setUpFiles(cls): def setUpFiles(cls):
cls.test_file_dir = os.path.join( cls.test_file_dir = Path(__file__, "..", "..", "testfiles").resolve()
os.path.dirname(__file__), "..", "testfiles") cls.test_file_dir.mkdir(exist_ok=True)
cls.test_file_dir = os.path.realpath(cls.test_file_dir)
if not os.path.exists(cls.test_file_dir):
os.mkdir(cls.test_file_dir)
cls.private_key_passphrase = 'test-passphrase' cls.private_key_passphrase = "test-passphrase"
cls.rsa_key_path = os.path.join(cls.test_file_dir, 'rsa.pem') cls.rsa_key_path = cls.test_file_dir / "rsa.pem"
cls.rsa4096_key_path = os.path.join(cls.test_file_dir, 'rsa4096.pem') cls.rsa4096_key_path = cls.test_file_dir / "rsa4096.pem"
cls.ec_p521_key_path = os.path.join(cls.test_file_dir, 'ecP521.pem') cls.ec_p521_key_path = cls.test_file_dir / "ecP521.pem"
if not os.path.exists(cls.rsa_key_path): if not cls.rsa_key_path.exists():
with open(cls.rsa_key_path, 'w') as f: with open(cls.rsa_key_path, 'w') as f:
f.write(RSA_TEST_PRIVATE_KEY) f.write(RSA_TEST_PRIVATE_KEY)
if not os.path.exists(cls.rsa4096_key_path): if not cls.rsa4096_key_path.exists():
key = RSA.generate(4096) key = RSA.generate(4096)
private_key = key.export_key( private_key = key.export_key(
passphrase=cls.private_key_passphrase, passphrase=cls.private_key_passphrase,
@ -250,7 +248,7 @@ class PetApiTests(unittest.TestCase):
with open(cls.rsa4096_key_path, "wb") as f: with open(cls.rsa4096_key_path, "wb") as f:
f.write(private_key) f.write(private_key)
if not os.path.exists(cls.ec_p521_key_path): if not cls.ec_p521_key_path.exists():
key = ECC.generate(curve='P-521') key = ECC.generate(curve='P-521')
private_key = key.export_key( private_key = key.export_key(
format='PEM', format='PEM',