/astropy/table/tests/conftest.py

https://gitlab.com/Rockyspade/astropy
Python | 169 lines | 111 code | 34 blank | 24 comment | 15 complexity | 221a3bb14119ff28b8ac6196785896e2 MD5 | raw file
  1. # Licensed under a 3-clause BSD style license - see LICENSE.rst
  2. """
  3. All of the py.test fixtures used by astropy.table are defined here.
  4. The fixtures can not be defined in the modules that use them, because
  5. those modules are imported twice: once with `from __future__ import
  6. unicode_literals` and once without. py.test complains when the same
  7. fixtures are defined more than once.
  8. `conftest.py` is a "special" module name for py.test that is always
  9. imported, but is not looked in for tests, and it is the recommended
  10. place to put fixtures that are shared between modules. These fixtures
  11. can not be defined in a module by a different name and still be shared
  12. between modules.
  13. """
  14. from copy import deepcopy
  15. try:
  16. import pandas
  17. except ImportError:
  18. HAS_PANDAS = False
  19. else:
  20. HAS_PANDAS = True
  21. from ...tests.helper import pytest
  22. from ... import table
  23. from ...table import table_helpers
  24. from ... import time
  25. from ... import units as u
  26. from ... import coordinates
  27. from .. import pprint
  28. from ...utils import OrderedDict
  29. @pytest.fixture(params=[table.Column, table.MaskedColumn])
  30. def Column(request):
  31. # Fixture to run all the Column tests for both an unmasked (ndarray)
  32. # and masked (MaskedArray) column.
  33. return request.param
  34. class MaskedTable(table.Table):
  35. def __init__(self, *args, **kwargs):
  36. kwargs['masked'] = True
  37. table.Table.__init__(self, *args, **kwargs)
  38. class MyRow(table.Row):
  39. pass
  40. class MyColumn(table.Column):
  41. pass
  42. class MyMaskedColumn(table.MaskedColumn):
  43. pass
  44. class MyTableColumns(table.TableColumns):
  45. pass
  46. class MyTableFormatter(pprint.TableFormatter):
  47. pass
  48. class MyTable(table.Table):
  49. Row = MyRow
  50. Column = MyColumn
  51. MaskedColumn = MyMaskedColumn
  52. TableColumns = MyTableColumns
  53. TableFormatter = MyTableFormatter
  54. # Fixture to run all the Column tests for both an unmasked (ndarray)
  55. # and masked (MaskedArray) column.
  56. @pytest.fixture(params=['unmasked', 'masked', 'subclass'])
  57. def table_types(request):
  58. class TableTypes:
  59. def __init__(self, request):
  60. if request.param == 'unmasked':
  61. self.Table = table.Table
  62. self.Column = table.Column
  63. elif request.param == 'masked':
  64. self.Table = MaskedTable
  65. self.Column = table.MaskedColumn
  66. elif request.param == 'subclass':
  67. self.Table = MyTable
  68. self.Column = MyColumn
  69. return TableTypes(request)
  70. # Fixture to run all the Column tests for both an unmasked (ndarray)
  71. # and masked (MaskedArray) column.
  72. @pytest.fixture(params=[False, True])
  73. def table_data(request):
  74. class TableData:
  75. def __init__(self, request):
  76. self.Table = MaskedTable if request.param else table.Table
  77. self.Column = table.MaskedColumn if request.param else table.Column
  78. self.COLS = [
  79. self.Column(name='a', data=[1, 2, 3], description='da',
  80. format='fa', meta={'ma': 1}, unit='ua'),
  81. self.Column(name='b', data=[4, 5, 6], description='db',
  82. format='fb', meta={'mb': 1}, unit='ub'),
  83. self.Column(name='c', data=[7, 8, 9], description='dc',
  84. format='fc', meta={'mc': 1}, unit='ub')]
  85. self.DATA = self.Table(self.COLS)
  86. return TableData(request)
  87. class SubclassTable(table.Table):
  88. pass
  89. @pytest.fixture(params=[True, False])
  90. def tableclass(request):
  91. return table.Table if request.param else SubclassTable
  92. @pytest.fixture(params=[0, 1, -1])
  93. def protocol(request):
  94. """
  95. Fixture to run all the tests for protocols 0 and 1, and -1 (most advanced).
  96. """
  97. return request.param
  98. # Fixture to run all tests for both an unmasked (ndarray) and masked
  99. # (MaskedArray) column.
  100. @pytest.fixture(params=[False, True])
  101. def table_type(request):
  102. # return MaskedTable if request.param else table.Table
  103. try:
  104. request.param
  105. return MaskedTable
  106. except AttributeError:
  107. return table.Table
  108. # Stuff for testing mixin columns
  109. MIXIN_COLS = {'quantity': [0, 1, 2, 3] * u.m,
  110. 'time': time.Time([2000, 2001, 2002, 2003], format='jyear'),
  111. 'skycoord': coordinates.SkyCoord(ra=[0, 1, 2, 3] * u.deg,
  112. dec=[0, 1, 2, 3] * u.deg),
  113. 'arraywrap': table_helpers.ArrayWrapper([0, 1, 2, 3])
  114. }
  115. if HAS_PANDAS:
  116. MIXIN_COLS['pandas'] = pandas.Series([0, 1, 2, 3])
  117. @pytest.fixture(params=sorted(MIXIN_COLS))
  118. def mixin_cols(request):
  119. """
  120. Fixture to return a set of columns for mixin testing which includes
  121. an index column 'i', two string cols 'a', 'b' (for joins etc), and
  122. one of the available mixin column types.
  123. """
  124. cols = OrderedDict()
  125. mixin_cols = deepcopy(MIXIN_COLS)
  126. if HAS_PANDAS:
  127. mixin_cols['pandas']._astropy_column_attrs = None
  128. cols['i'] = table.Column([0, 1, 2, 3], name='i')
  129. cols['a'] = table.Column(['a', 'b', 'b', 'c'], name='a')
  130. cols['b'] = table.Column(['b', 'c', 'a', 'd'], name='b')
  131. cols['m'] = mixin_cols[request.param]
  132. return cols