You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 lines
1.6 KiB

  1. import unittest
  2. from unittest.mock import MagicMock, patch
  3. import pandas as pd
  4. from pandas.testing import assert_frame_equal
  5. from data_import.pipeline.readers import (
  6. FILE_NAME_COLUMN,
  7. UPLOAD_NAME_COLUMN,
  8. UUID_COLUMN,
  9. Reader,
  10. )
  11. class TestReader(unittest.TestCase):
  12. def setUp(self):
  13. self.parser = MagicMock()
  14. self.parser.parse.return_value = [{"a": 1}, {"a": 2}]
  15. filename = MagicMock()
  16. filename.generated_name = "filename"
  17. filename.upload_name = "upload_name"
  18. self.filenames = MagicMock()
  19. self.filenames.__iter__.return_value = [filename]
  20. self.rows = [
  21. {
  22. UUID_COLUMN: "uuid",
  23. FILE_NAME_COLUMN: filename.generated_name,
  24. UPLOAD_NAME_COLUMN: filename.upload_name,
  25. "a": 1,
  26. },
  27. {
  28. UUID_COLUMN: "uuid",
  29. FILE_NAME_COLUMN: filename.generated_name,
  30. UPLOAD_NAME_COLUMN: filename.upload_name,
  31. "a": 2,
  32. },
  33. ]
  34. @patch("data_import.pipeline.readers.uuid.uuid4")
  35. def test_iter_method(self, mock):
  36. mock.return_value = "uuid"
  37. reader = Reader(self.filenames, self.parser)
  38. self.assertEqual(list(reader), self.rows)
  39. @patch("data_import.pipeline.readers.uuid.uuid4")
  40. def test_batch(self, mock):
  41. mock.return_value = "uuid"
  42. reader = Reader(self.filenames, self.parser)
  43. batch = next(reader.batch(2))
  44. expected_df = pd.DataFrame(self.rows)
  45. assert_frame_equal(batch, expected_df)