From cf9ee87a2aadfb3b8678fcc14af91990570ba3b8 Mon Sep 17 00:00:00 2001 From: Hironsan Date: Fri, 12 Nov 2021 10:01:20 +0900 Subject: [PATCH] Sort exported labels, fix #1466 --- backend/api/tests/download/test_writer.py | 10 ++++++++++ backend/api/views/download/writer.py | 3 ++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/backend/api/tests/download/test_writer.py b/backend/api/tests/download/test_writer.py index 2c48cb92..720244dd 100644 --- a/backend/api/tests/download/test_writer.py +++ b/backend/api/tests/download/test_writer.py @@ -32,6 +32,16 @@ class TestCSVWriter(unittest.TestCase): } self.assertEqual(line, expected) + def test_label_order(self): + writer = CsvWriter('.') + record1 = Record(id=0, data='', label=['labelA', 'labelB'], user='', metadata={}) + record2 = Record(id=0, data='', label=['labelB', 'labelA'], user='', metadata={}) + line1 = writer.create_line(record1) + line2 = writer.create_line(record2) + expected = 'labelA#labelB' + self.assertEqual(line1['label'], expected) + self.assertEqual(line2['label'], expected) + @patch('os.remove') @patch('zipfile.ZipFile') @patch('csv.DictWriter.writerow') diff --git a/backend/api/views/download/writer.py b/backend/api/views/download/writer.py index 5de1264e..a4d5293a 100644 --- a/backend/api/views/download/writer.py +++ b/backend/api/views/download/writer.py @@ -84,7 +84,7 @@ class CsvWriter(BaseWriter): return { 'id': record.id, 'data': record.data, - 'label': '#'.join(record.label), + 'label': '#'.join(sorted(record.label)), **record.metadata } @@ -144,6 +144,7 @@ class FastTextWriter(LineWriter): def create_line(self, record): line = [f'__label__{label}' for label in record.label] + line.sort() line.append(record.data) line = ' '.join(line) return line