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.

87 lines
3.1 KiB

  1. import json
  2. import unittest
  3. from unittest.mock import call, patch
  4. from ..pipeline.data import Record
  5. from ..pipeline.writers import CsvWriter, IntentAndSlotWriter
  6. class TestCSVWriter(unittest.TestCase):
  7. def setUp(self):
  8. self.records = [
  9. Record(id=0, data='exampleA', label=['labelA'], user='admin', metadata={'hidden': 'secretA'}),
  10. Record(id=1, data='exampleB', label=['labelB'], user='admin', metadata={'hidden': 'secretB'}),
  11. Record(id=2, data='exampleC', label=['labelC'], user='admin', metadata={'meta': 'secretC'})
  12. ]
  13. def test_create_header(self):
  14. writer = CsvWriter('.')
  15. header = writer.create_header(self.records)
  16. expected = ['id', 'data', 'label', 'hidden', 'meta']
  17. self.assertEqual(header, expected)
  18. def test_create_line(self):
  19. writer = CsvWriter('.')
  20. record = self.records[0]
  21. line = writer.create_line(record)
  22. expected = {
  23. 'id': record.id,
  24. 'data': record.data,
  25. 'label': record.label[0],
  26. 'hidden': 'secretA'
  27. }
  28. self.assertEqual(line, expected)
  29. def test_label_order(self):
  30. writer = CsvWriter('.')
  31. record1 = Record(id=0, data='', label=['labelA', 'labelB'], user='', metadata={})
  32. record2 = Record(id=0, data='', label=['labelB', 'labelA'], user='', metadata={})
  33. line1 = writer.create_line(record1)
  34. line2 = writer.create_line(record2)
  35. expected = 'labelA#labelB'
  36. self.assertEqual(line1['label'], expected)
  37. self.assertEqual(line2['label'], expected)
  38. @patch('os.remove')
  39. @patch('zipfile.ZipFile')
  40. @patch('csv.DictWriter.writerow')
  41. @patch('builtins.open')
  42. def test_dump(self, mock_open_file, csv_io, zip_io, mock_remove_file):
  43. writer = CsvWriter('.')
  44. writer.write(self.records)
  45. self.assertEqual(mock_open_file.call_count, 1)
  46. mock_open_file.assert_called_with('./admin.csv', mode='a', encoding='utf-8')
  47. self.assertEqual(csv_io.call_count, len(self.records) + 1) # +1 is for a header
  48. calls = [
  49. call({'id': 'id', 'data': 'data', 'label': 'label', 'hidden': 'hidden', 'meta': 'meta'}),
  50. call({'id': 0, 'data': 'exampleA', 'label': 'labelA', 'hidden': 'secretA'}),
  51. call({'id': 1, 'data': 'exampleB', 'label': 'labelB', 'hidden': 'secretB'}),
  52. call({'id': 2, 'data': 'exampleC', 'label': 'labelC', 'meta': 'secretC'})
  53. ]
  54. csv_io.assert_has_calls(calls)
  55. class TestIntentWriter(unittest.TestCase):
  56. def setUp(self):
  57. self.record = Record(
  58. id=0,
  59. data='exampleA',
  60. label={'cats': ['positive'], 'entities': [(0, 1, 'LOC')]},
  61. user='admin',
  62. metadata={}
  63. )
  64. def test_create_line(self):
  65. writer = IntentAndSlotWriter('.')
  66. actual = writer.create_line(self.record)
  67. expected = {
  68. 'id': self.record.id,
  69. 'text': self.record.data,
  70. 'cats': ['positive'],
  71. 'entities': [[0, 1, 'LOC']],
  72. }
  73. self.assertEqual(json.loads(actual), expected)