conftest.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import tempfile
  2. from pathlib import Path
  3. import pytest
  4. from django.core.files.base import ContentFile
  5. from django.utils.encoding import force_str
  6. from selenium import webdriver
  7. from selenium.common.exceptions import WebDriverException
  8. from s3file.storages import get_aws_location
  9. @pytest.fixture(scope="session")
  10. def driver():
  11. chrome_options = webdriver.ChromeOptions()
  12. chrome_options.headless = True
  13. try:
  14. b = webdriver.Chrome(options=chrome_options)
  15. except WebDriverException as e:
  16. pytest.skip(force_str(e))
  17. else:
  18. yield b
  19. b.quit()
  20. @pytest.fixture
  21. def freeze_upload_folder(monkeypatch):
  22. """Freeze the upload folder which by default contains a random UUID v4."""
  23. upload_folder = Path(get_aws_location()) / "tmp" / "s3file"
  24. monkeypatch.setattr(
  25. "s3file.forms.S3FileInputMixin.upload_folder",
  26. str(upload_folder),
  27. )
  28. return upload_folder
  29. @pytest.fixture
  30. def upload_file(request, freeze_upload_folder):
  31. path = Path(tempfile.mkdtemp()) / freeze_upload_folder / f"{request.node.name}.txt"
  32. path.parent.mkdir(parents=True, exist_ok=True)
  33. with path.open("w") as f:
  34. f.write(request.node.name)
  35. return str(path.absolute())
  36. @pytest.fixture
  37. def another_upload_file(request, freeze_upload_folder):
  38. path = (
  39. Path(tempfile.mkdtemp())
  40. / freeze_upload_folder
  41. / f"another_{request.node.name}.txt"
  42. )
  43. path.parent.mkdir(parents=True, exist_ok=True)
  44. with path.open("w") as f:
  45. f.write(request.node.name)
  46. return str(path.absolute())
  47. @pytest.fixture
  48. def yet_another_upload_file(request, freeze_upload_folder):
  49. path = (
  50. Path(tempfile.mkdtemp())
  51. / freeze_upload_folder
  52. / f"yet_another_{request.node.name}.txt"
  53. )
  54. path.parent.mkdir(parents=True, exist_ok=True)
  55. with path.open("w") as f:
  56. f.write(request.node.name)
  57. return str(path.absolute())
  58. @pytest.fixture
  59. def filemodel(request, db):
  60. from tests.testapp.models import FileModel
  61. return FileModel.objects.create(
  62. file=ContentFile(request.node.name, "%s.txt" % request.node.name)
  63. )