test_mock_writer.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. import pytest
  4. from draugr.writers import MockWriter, global_writer, set_global_writer
  5. __author__ = "Christian Heider Nielsen"
  6. __doc__ = r"""
  7. """
  8. @pytest.mark.parametrize(
  9. ["tag", "val", "step"],
  10. (("signal", 0, 0), ("signal", 20, 1), ("signal", -1, 6)),
  11. ids=["signal_first", "signal_second", "signal_sixth"],
  12. )
  13. def test_valid_scalars(tag, val, step):
  14. with MockWriter() as w:
  15. w.scalar(tag, val, step)
  16. @pytest.mark.parametrize(
  17. ["tag", "val", "step"],
  18. (("signal", "", 0), ("signal", None, 1), ("signal", object(), 6)),
  19. ids=["str_scalar", "None_scalar", "object_scalar"],
  20. )
  21. def test_invalid_val_type_scalars(tag, val, step):
  22. try:
  23. with MockWriter() as w:
  24. w.scalar(tag, val, step)
  25. assert False
  26. except Exception as e:
  27. assert True
  28. @pytest.mark.parametrize(
  29. ["tag", "val", "step"],
  30. ((1, 0, 0), (None, 20, 1), (object(), -1, 6)),
  31. ids=["numeral_tag", "None_tag", "object_tag"],
  32. )
  33. def test_invalid_tag_scalars(tag, val, step):
  34. try:
  35. with MockWriter() as w:
  36. w.scalar(tag, val, step)
  37. assert False
  38. except Exception as e:
  39. print(e)
  40. assert True
  41. @pytest.mark.parametrize(
  42. ["tag", "val", "step"],
  43. (("signal", 0, ""), ("signal", 20, None), ("tag1", -0, object())),
  44. ids=["str_step", "None_step", "object_step"],
  45. )
  46. def test_invalid_step_type_scalars(tag, val, step):
  47. try:
  48. with MockWriter() as w:
  49. w.scalar(tag, val, step)
  50. assert False
  51. except Exception as e:
  52. print(e)
  53. assert True
  54. def test_global_writer():
  55. with MockWriter() as writer_o:
  56. mw2 = MockWriter()
  57. assert writer_o == global_writer()
  58. assert mw2 != global_writer()
  59. assert writer_o != mw2
  60. with MockWriter() as writer_i:
  61. assert writer_i == global_writer()
  62. assert writer_o != global_writer()
  63. assert writer_o != mw2
  64. assert writer_o == global_writer()
  65. assert writer_o != mw2
  66. set_global_writer(mw2)
  67. assert mw2 == global_writer()
  68. assert writer_o != global_writer()