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.

36 lines
1.2 KiB

  1. import os
  2. import shutil
  3. import tempfile
  4. import unittest
  5. from ...views.upload.data import TextData
  6. from ...views.upload.dataset import FastTextDataset
  7. from ...views.upload.label import CategoryLabel
  8. class TestFastTextDataset(unittest.TestCase):
  9. def setUp(self):
  10. self.test_dir = tempfile.mkdtemp()
  11. self.test_file = os.path.join(self.test_dir, 'test_file.txt')
  12. def tearDown(self):
  13. shutil.rmtree(self.test_dir)
  14. def create_file(self, content):
  15. with open(self.test_file, 'w') as f:
  16. f.write(content)
  17. def assert_record(self, content, dataset, data='Text', label=None):
  18. if label is None:
  19. label = [{'text': 'Label'}]
  20. self.create_file(content)
  21. record = next(dataset.load(self.test_file))
  22. self.assertEqual(record.data['text'], data)
  23. self.assertEqual(record.label, label)
  24. def test_can_load_default_column_names(self):
  25. content = '__label__sauce __label__cheese Text'
  26. dataset = FastTextDataset(filenames=[], label_class=CategoryLabel, data_class=TextData)
  27. label = [{'text': 'sauce'}, {'text': 'cheese'}]
  28. self.assert_record(content, dataset, label=label)