redvox.tests.api900.test_migrations API documentation

# Module `redvox.tests.api900.test_migrations`

Expand source code

    from typing import Any, List
    import os
    import unittest

    import redvox.api900.migrations as migrations

    import numpy as np

    def arrays_equal(array_1: np.ndarray, array_2: np.ndarray) -> bool:
        return np.array_equal(array_1, array_2) and array_1.dtype == array_2.dtype

    def scalars_equal(scalar_1: Any, scalar_2: Any) -> bool:
        return scalar_1 == scalar_2 and type(scalar_1) == type(scalar_2)

    def lists_equal(list_1: List[Any], list_2: List[Any]) -> bool:
        if len(list_1) != len(list_2):
            return False

        for i in range(len(list_1)):
            v_1 = list_1[i]
            v_2 = list_2[i]

            if not scalars_equal(v_1, v_2):
                return False

        return True

    class MigrationsTests(unittest.TestCase):
        def setUp(self) -> None:
            self.list_of_ints = [1, 2, 3]
            self.list_of_floats = [1.0, 2.0, 3.0]
            self.array_of_ints = np.array(self.list_of_ints)
            self.array_of_floats = np.array(self.list_of_floats)
            self.scalar_int = 1
            self.scalar_float = 1.0
            self.scalar_str = "foo"
            self.prev_migration: bool = migrations.are_migrations_enabled()

        def tearDown(self) -> None:
            if self.prev_migration:
                migrations.enable_migrations(True)
            else:
                migrations.enable_migrations(False)

        # Since migrations are disabled by default, should always get the same value back
        def test_no_migrations(self):
            migrations.enable_migrations(False)
            self.assertTrue(arrays_equal(self.array_of_ints,
                                         migrations.maybe_get_float(self.array_of_ints)))
            self.assertTrue(arrays_equal(self.array_of_floats,
                                         migrations.maybe_get_float(self.array_of_floats)))
            self.assertTrue(lists_equal(self.list_of_ints,
                                        migrations.maybe_get_float(self.list_of_ints)))
            self.assertTrue(lists_equal(self.list_of_floats,
                                        migrations.maybe_get_float(self.list_of_floats)))
            self.assertTrue(scalars_equal(self.scalar_int,
                                          migrations.maybe_get_float(self.scalar_int)))
            self.assertTrue(scalars_equal(self.scalar_float,
                                          migrations.maybe_get_float(self.scalar_float)))
            self.assertTrue(scalars_equal(self.scalar_str,
                                          migrations.maybe_get_float(self.scalar_str)))

        def test_convert_array_to_float(self):
            migrations.enable_migrations(True)
            self.assertTrue(arrays_equal(self.array_of_floats,
                                         migrations.maybe_get_float(self.array_of_floats)))
            self.assertTrue(arrays_equal(self.array_of_floats,
                                         migrations.maybe_get_float(self.array_of_ints)))

        def test_convert_list_to_float(self):
            migrations.enable_migrations(True)
            self.assertTrue(lists_equal(self.list_of_floats,
                                        migrations.maybe_get_float(self.list_of_floats)))
            self.assertTrue(lists_equal(self.list_of_floats,
                                        migrations.maybe_get_float(self.list_of_ints)))

        def test_convert_scalar_to_float(self):
            migrations.enable_migrations(True)
            self.assertTrue(scalars_equal(self.scalar_float,
                                          migrations.maybe_get_float(self.scalar_float)))
            self.assertTrue(scalars_equal(self.scalar_float,
                                          migrations.maybe_get_float(self.scalar_int)))

        # Bad types should return themselves
        def test_bad_data(self):
            migrations.enable_migrations(True)
            self.assertTrue(scalars_equal(None,
                                          migrations.maybe_get_float(None)))
            self.assertTrue(scalars_equal(self.scalar_str,
                                          migrations.maybe_get_float(self.scalar_str)))

## Functions

`def arrays_equal(array_1: numpy.ndarray, array_2: numpy.ndarray) ‑> bool`
  Expand source code

      def arrays_equal(array_1: np.ndarray, array_2: np.ndarray) -> bool:
          return np.array_equal(array_1, array_2) and array_1.dtype == array_2.dtype

`def lists_equal(list_1: List[Any], list_2: List[Any]) ‑> bool`
  Expand source code

      def lists_equal(list_1: List[Any], list_2: List[Any]) -> bool:
          if len(list_1) != len(list_2):
              return False

          for i in range(len(list_1)):
              v_1 = list_1[i]
              v_2 = list_2[i]

              if not scalars_equal(v_1, v_2):
                  return False

          return True

`def scalars_equal(scalar_1: Any, scalar_2: Any) ‑> bool`
  Expand source code

      def scalars_equal(scalar_1: Any, scalar_2: Any) -> bool:
          return scalar_1 == scalar_2 and type(scalar_1) == type(scalar_2)

## Classes

`class MigrationsTests (methodName='runTest')`

  A class whose instances are single test cases.

  By default, the test code itself should be placed in a method named 'runTest'.

  If the fixture may be used for many test cases, create as many test methods as are needed. When instantiating such a TestCase subclass, specify in the constructor arguments the name of the test method that the instance is to execute.

  Test authors should subclass TestCase for their own tests. Construction and deconstruction of the test's environment ('fixture') can be implemented by overriding the 'setUp' and 'tearDown' methods respectively.

  If it is necessary to override the init method, the base class init method must always be called. It is important that subclasses should not change the signature of their init method, since instances of the classes are instantiated automatically by parts of the framework in order to be run.

  When subclassing TestCase, you can set these attributes: * failureException: determines which exception will be raised when the instance's assertion methods fail; test methods raising this exception will be deemed to have 'failed' rather than 'errored'. * longMessage: determines whether long messages (including repr of objects used in assert methods) will be printed on failure in addition to any explicit message passed. * maxDiff: sets the maximum length of a diff in failure messages by assert methods using difflib. It is looked up as an instance attribute so can be configured by individual tests if required.

  Create an instance of the class that will use the named test method when executed. Raises a ValueError if the instance does not have a method with the specified name.

  Expand source code

      class MigrationsTests(unittest.TestCase):
          def setUp(self) -> None:
              self.list_of_ints = [1, 2, 3]
              self.list_of_floats = [1.0, 2.0, 3.0]
              self.array_of_ints = np.array(self.list_of_ints)
              self.array_of_floats = np.array(self.list_of_floats)
              self.scalar_int = 1
              self.scalar_float = 1.0
              self.scalar_str = "foo"
              self.prev_migration: bool = migrations.are_migrations_enabled()

          def tearDown(self) -> None:
              if self.prev_migration:
                  migrations.enable_migrations(True)
              else:
                  migrations.enable_migrations(False)

          # Since migrations are disabled by default, should always get the same value back
          def test_no_migrations(self):
              migrations.enable_migrations(False)
              self.assertTrue(arrays_equal(self.array_of_ints,
                                           migrations.maybe_get_float(self.array_of_ints)))
              self.assertTrue(arrays_equal(self.array_of_floats,
                                           migrations.maybe_get_float(self.array_of_floats)))
              self.assertTrue(lists_equal(self.list_of_ints,
                                          migrations.maybe_get_float(self.list_of_ints)))
              self.assertTrue(lists_equal(self.list_of_floats,
                                          migrations.maybe_get_float(self.list_of_floats)))
              self.assertTrue(scalars_equal(self.scalar_int,
                                            migrations.maybe_get_float(self.scalar_int)))
              self.assertTrue(scalars_equal(self.scalar_float,
                                            migrations.maybe_get_float(self.scalar_float)))
              self.assertTrue(scalars_equal(self.scalar_str,
                                            migrations.maybe_get_float(self.scalar_str)))

          def test_convert_array_to_float(self):
              migrations.enable_migrations(True)
              self.assertTrue(arrays_equal(self.array_of_floats,
                                           migrations.maybe_get_float(self.array_of_floats)))
              self.assertTrue(arrays_equal(self.array_of_floats,
                                           migrations.maybe_get_float(self.array_of_ints)))

          def test_convert_list_to_float(self):
              migrations.enable_migrations(True)
              self.assertTrue(lists_equal(self.list_of_floats,
                                          migrations.maybe_get_float(self.list_of_floats)))
              self.assertTrue(lists_equal(self.list_of_floats,
                                          migrations.maybe_get_float(self.list_of_ints)))

          def test_convert_scalar_to_float(self):
              migrations.enable_migrations(True)
              self.assertTrue(scalars_equal(self.scalar_float,
                                            migrations.maybe_get_float(self.scalar_float)))
              self.assertTrue(scalars_equal(self.scalar_float,
                                            migrations.maybe_get_float(self.scalar_int)))

          # Bad types should return themselves
          def test_bad_data(self):
              migrations.enable_migrations(True)
              self.assertTrue(scalars_equal(None,
                                            migrations.maybe_get_float(None)))
              self.assertTrue(scalars_equal(self.scalar_str,
                                            migrations.maybe_get_float(self.scalar_str)))

  ### Ancestors

  - unittest.case.TestCase

  ### Methods

  `def setUp(self) ‑> None`

    Hook method for setting up the test fixture before exercising it.

    Expand source code

        def setUp(self) -> None:
            self.list_of_ints = [1, 2, 3]
            self.list_of_floats = [1.0, 2.0, 3.0]
            self.array_of_ints = np.array(self.list_of_ints)
            self.array_of_floats = np.array(self.list_of_floats)
            self.scalar_int = 1
            self.scalar_float = 1.0
            self.scalar_str = "foo"
            self.prev_migration: bool = migrations.are_migrations_enabled()

  `def tearDown(self) ‑> None`

    Hook method for deconstructing the test fixture after testing it.

    Expand source code

        def tearDown(self) -> None:
            if self.prev_migration:
                migrations.enable_migrations(True)
            else:
                migrations.enable_migrations(False)

  `def test_bad_data(self)`
    Expand source code

        def test_bad_data(self):
            migrations.enable_migrations(True)
            self.assertTrue(scalars_equal(None,
                                          migrations.maybe_get_float(None)))
            self.assertTrue(scalars_equal(self.scalar_str,
                                          migrations.maybe_get_float(self.scalar_str)))

  `def test_convert_array_to_float(self)`
    Expand source code

        def test_convert_array_to_float(self):
            migrations.enable_migrations(True)
            self.assertTrue(arrays_equal(self.array_of_floats,
                                         migrations.maybe_get_float(self.array_of_floats)))
            self.assertTrue(arrays_equal(self.array_of_floats,
                                         migrations.maybe_get_float(self.array_of_ints)))

  `def test_convert_list_to_float(self)`
    Expand source code

        def test_convert_list_to_float(self):
            migrations.enable_migrations(True)
            self.assertTrue(lists_equal(self.list_of_floats,
                                        migrations.maybe_get_float(self.list_of_floats)))
            self.assertTrue(lists_equal(self.list_of_floats,
                                        migrations.maybe_get_float(self.list_of_ints)))

  `def test_convert_scalar_to_float(self)`
    Expand source code

        def test_convert_scalar_to_float(self):
            migrations.enable_migrations(True)
            self.assertTrue(scalars_equal(self.scalar_float,
                                          migrations.maybe_get_float(self.scalar_float)))
            self.assertTrue(scalars_equal(self.scalar_float,
                                          migrations.maybe_get_float(self.scalar_int)))

  `def test_no_migrations(self)`
    Expand source code

        def test_no_migrations(self):
            migrations.enable_migrations(False)
            self.assertTrue(arrays_equal(self.array_of_ints,
                                         migrations.maybe_get_float(self.array_of_ints)))
            self.assertTrue(arrays_equal(self.array_of_floats,
                                         migrations.maybe_get_float(self.array_of_floats)))
            self.assertTrue(lists_equal(self.list_of_ints,
                                        migrations.maybe_get_float(self.list_of_ints)))
            self.assertTrue(lists_equal(self.list_of_floats,
                                        migrations.maybe_get_float(self.list_of_floats)))
            self.assertTrue(scalars_equal(self.scalar_int,
                                          migrations.maybe_get_float(self.scalar_int)))
            self.assertTrue(scalars_equal(self.scalar_float,
                                          migrations.maybe_get_float(self.scalar_float)))
            self.assertTrue(scalars_equal(self.scalar_str,
                                          migrations.maybe_get_float(self.scalar_str)))