conftest.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. from distutils.version import LooseVersion
  2. import os
  3. import pytest
  4. import pandas.util.testing as tm
  5. from pandas.io.parsers import read_csv
  6. @pytest.fixture
  7. def tips_file(datapath):
  8. """Path to the tips dataset"""
  9. return datapath('io', 'parser', 'data', 'tips.csv')
  10. @pytest.fixture
  11. def jsonl_file(datapath):
  12. """Path a JSONL dataset"""
  13. return datapath('io', 'parser', 'data', 'items.jsonl')
  14. @pytest.fixture
  15. def salaries_table(datapath):
  16. """DataFrame with the salaries dataset"""
  17. return read_csv(datapath('io', 'parser', 'data', 'salaries.csv'), sep='\t')
  18. @pytest.fixture
  19. def s3_resource(tips_file, jsonl_file):
  20. """Fixture for mocking S3 interaction.
  21. The primary bucket name is "pandas-test". The following datasets
  22. are loaded.
  23. - tips.csv
  24. - tips.csv.gz
  25. - tips.csv.bz2
  26. - items.jsonl
  27. A private bucket "cant_get_it" is also created. The boto3 s3 resource
  28. is yielded by the fixture.
  29. """
  30. pytest.importorskip('s3fs')
  31. boto3 = pytest.importorskip('boto3')
  32. botocore = pytest.importorskip('botocore')
  33. if LooseVersion(botocore.__version__) < LooseVersion("1.11.0"):
  34. # botocore leaks an uncatchable ResourceWarning before 1.11.0;
  35. # see GH 23731 and https://github.com/boto/botocore/issues/1464
  36. pytest.skip("botocore is leaking resources before 1.11.0")
  37. with tm.ensure_safe_environment_variables():
  38. # temporary workaround as moto fails for botocore >= 1.11 otherwise,
  39. # see https://github.com/spulec/moto/issues/1924 & 1952
  40. os.environ.setdefault("AWS_ACCESS_KEY_ID", "foobar_key")
  41. os.environ.setdefault("AWS_SECRET_ACCESS_KEY", "foobar_secret")
  42. moto = pytest.importorskip('moto')
  43. test_s3_files = [
  44. ('tips.csv', tips_file),
  45. ('tips.csv.gz', tips_file + '.gz'),
  46. ('tips.csv.bz2', tips_file + '.bz2'),
  47. ('items.jsonl', jsonl_file),
  48. ]
  49. def add_tips_files(bucket_name):
  50. for s3_key, file_name in test_s3_files:
  51. with open(file_name, 'rb') as f:
  52. conn.Bucket(bucket_name).put_object(
  53. Key=s3_key,
  54. Body=f)
  55. try:
  56. s3 = moto.mock_s3()
  57. s3.start()
  58. # see gh-16135
  59. bucket = 'pandas-test'
  60. conn = boto3.resource("s3", region_name="us-east-1")
  61. conn.create_bucket(Bucket=bucket)
  62. add_tips_files(bucket)
  63. conn.create_bucket(Bucket='cant_get_it', ACL='private')
  64. add_tips_files('cant_get_it')
  65. yield conn
  66. finally:
  67. s3.stop()