From 8a5b09f0539ac982eace3284d97659650c004983 Mon Sep 17 00:00:00 2001 From: Hironsan Date: Fri, 19 Nov 2021 15:23:54 +0900 Subject: [PATCH] Fix TextLineDataset to raise exception --- backend/api/tests/data/example.txt | 1 + backend/api/tests/test_tasks.py | 2 +- backend/api/views/upload/dataset.py | 15 +++++++++++---- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/backend/api/tests/data/example.txt b/backend/api/tests/data/example.txt index da38709c..e2420dd4 100644 --- a/backend/api/tests/data/example.txt +++ b/backend/api/tests/data/example.txt @@ -1,3 +1,4 @@ exampleA exampleB + exampleC diff --git a/backend/api/tests/test_tasks.py b/backend/api/tests/test_tasks.py index 25375c0c..0e08928d 100644 --- a/backend/api/tests/test_tasks.py +++ b/backend/api/tests/test_tasks.py @@ -107,7 +107,7 @@ class TestIngestClassificationData(TestIngestData): filename = 'example.txt' file_format = 'TextFile' dataset = [ - ('exampleA\nexampleB\nexampleC\n', []) + ('exampleA\nexampleB\n\nexampleC\n', []) ] self.ingest_data(filename, file_format) self.assert_examples(dataset) diff --git a/backend/api/views/upload/dataset.py b/backend/api/views/upload/dataset.py index 28ff8f48..bd5e33fd 100644 --- a/backend/api/views/upload/dataset.py +++ b/backend/api/views/upload/dataset.py @@ -153,11 +153,18 @@ class TextLineDataset(Dataset): def load(self, filename: str) -> Iterator[Record]: encoding = self.detect_encoding(filename) + errors = [] with open(filename, encoding=encoding) as f: - for line in f: - data = self.data_class.parse(filename=filename, text=line.rstrip()) - record = Record(data=data) - yield record + for line_num, line in enumerate(f, start=1): + try: + data = self.data_class.parse(filename=filename, text=line.rstrip()) + record = Record(data=data) + yield record + except ValidationError: + message = 'The empty text is not allowed.' + errors.append(FileParseException(filename, line_num, message)) + if errors: + raise FileParseExceptions(errors) class CsvDataset(Dataset):