From 21618991e892a9e54b738bb36dd024d94584043c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Herv=C3=A9=20M=C3=A9nager?= <herve.menager@gmail.com>
Date: Wed, 7 Oct 2020 11:04:46 +0200
Subject: [PATCH] add basic test for XMLImportTask

---
 tests/management/commands/xml_command.py      | 40 +++++++++++++++++++
 .../management/commands/xml_command_file.xml  |  6 +++
 tests/tests.py                                |  7 +++-
 3 files changed, 52 insertions(+), 1 deletion(-)
 create mode 100644 tests/management/commands/xml_command.py
 create mode 100644 tests/management/commands/xml_command_file.xml

diff --git a/tests/management/commands/xml_command.py b/tests/management/commands/xml_command.py
new file mode 100644
index 0000000..ee1d8b6
--- /dev/null
+++ b/tests/management/commands/xml_command.py
@@ -0,0 +1,40 @@
+import os
+
+from django.db import models
+from django_diu import import_command
+
+from tests.models import AModel
+
+class XMLImportTask(import_command.XMLImportTask):
+
+    description = "A very basic 'import from XML' test task"
+
+    option = "b"
+
+    target_classes = [AModel]
+
+    main_class = AModel
+
+    depends_on = []
+
+    xpathSelector = 'a'
+
+    def __init__(self, command, **kwargs):
+        super().__init__(command, **kwargs)
+        self.xmlFile = command.xmlFile
+
+    def migrate_row(self, row):
+        a = AModel()
+        a.a_field = row.get("value")[0]
+        a.save()
+        return a
+
+
+class Command(import_command.ImportCommand):
+
+    help = "Test command to test django-diu's ImportCommand"
+
+    xmlFile = os.path.join(os.path.dirname(__file__),'xml_command_file.xml')
+
+    # list all the import tasks that should be available via the command
+    task_classes = [XMLImportTask]
\ No newline at end of file
diff --git a/tests/management/commands/xml_command_file.xml b/tests/management/commands/xml_command_file.xml
new file mode 100644
index 0000000..f2f7ef5
--- /dev/null
+++ b/tests/management/commands/xml_command_file.xml
@@ -0,0 +1,6 @@
+<toto>
+    <a value="A" />
+    <a value="B" />
+    <a value="C" />
+    <a value="D" />
+</toto>
\ No newline at end of file
diff --git a/tests/tests.py b/tests/tests.py
index b257de3..8b5c24c 100644
--- a/tests/tests.py
+++ b/tests/tests.py
@@ -25,8 +25,13 @@ from .models import AModel
 
 class CommandTests(TestCase):
 
-    def test_command(self):
+    def test_list_command(self):
         management.call_command('a_command', 'a')
         count = AModel.objects.count()
         self.assertEquals(count, 4)
 
+    def test_xml_command(self):
+        management.call_command('xml_command', 'b')
+        count = AModel.objects.count()
+        self.assertEquals(count, 4)
+
-- 
GitLab