1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3###############################################################################
4#
5# Copyright 2006 - 2021, Tomas Babej, Paul Beckingham, Federico Hernandez.
6#
7# Permission is hereby granted, free of charge, to any person obtaining a copy
8# of this software and associated documentation files (the "Software"), to deal
9# in the Software without restriction, including without limitation the rights
10# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11# copies of the Software, and to permit persons to whom the Software is
12# furnished to do so, subject to the following conditions:
13#
14# The above copyright notice and this permission notice shall be included
15# in all copies or substantial portions of the Software.
16#
17# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23# SOFTWARE.
24#
25# https://www.opensource.org/licenses/mit-license.php
26#
27###############################################################################
28
29import sys
30import os
31import unittest
32# Ensure python finds the local simpletap module
33sys.path.append(os.path.dirname(os.path.abspath(__file__)))
34
35from basetest import Task, TestCase
36
37
38class TestDelete(TestCase):
39    def setUp(self):
40        self.t = Task()
41
42    def test_add_delete_purge(self):
43        """Verify that add/delete/purge successfully purges a task"""
44        self.t("add one")
45        uuid = self.t("_get 1.uuid")[1].strip()
46
47        code, out, err = self.t("1 delete", input="y\n")
48        self.assertIn("Deleted 1 task.", out)
49
50        code, out, err = self.t(uuid + " purge", input="y\n")
51        self.assertIn("Purged 1 task.", out)
52
53        code, out, err = self.t("uuids")
54        self.assertNotIn(uuid, out)
55
56    def test_purge_remove_deps(self):
57        """Purge command removes broken dependency references"""
58        self.t("add one")
59        self.t("add two dep:1")
60        uuid = self.t("_get 1.uuid")[1].strip()
61
62        code, out, err = self.t("1 delete", input="y\n")
63        self.assertIn("Deleted 1 task.", out)
64
65        code, out, err = self.t(uuid + " purge", input="y\n")
66        self.assertIn("Purged 1 task.", out)
67
68        code, out, err = self.t("uuids")
69        self.assertNotIn(uuid, out)
70
71        dependencies = self.t("_get 1.depends")[1].strip()
72        self.assertNotIn(uuid, dependencies)
73
74    def test_purge_children(self):
75        """Purge command indirectly purges child tasks"""
76        self.t("add one recur:daily due:yesterday")
77        uuid = self.t("_get 1.uuid")[1].strip()
78
79        # A dummy call to report, so that recurrence tasks get generated
80        self.t("list")
81
82        code, out, err = self.t("1 delete", input="y\ny\n")
83        self.assertIn("Deleted 4 tasks.", out)
84
85        code, out, err = self.t(uuid + " purge", input="y\ny\n")
86        self.assertIn("Purged 4 tasks.", out)
87
88        code, out, err = self.t("uuids")
89        self.assertEqual('\n', out)
90
91    def test_purge_children_fail_pending(self):
92        """Purge aborts if task has pending children"""
93        self.t("add one recur:daily due:yesterday")
94        uuid = self.t("_get 1.uuid")[1].strip()
95
96        # A dummy call to report, so that recurrence tasks get generated
97        self.t("list")
98
99        code, out, err = self.t("1 delete", input="y\nn\n")
100        self.assertIn("Deleted 1 task.", out)
101
102        code, out, err = self.t.runError(uuid + " purge", input="y\n")
103        self.assertIn("child task 1 must be deleted before", err)
104
105        # Check that nothing was purged
106        code, out, err = self.t("count")
107        self.assertEqual('4\n', out)
108
109    def test_purge_children_fail_confirm(self):
110        """Purge aborts if user does not agree with it affecting child tasks"""
111        self.t("add one recur:daily due:yesterday")
112        uuid = self.t("_get 1.uuid")[1].strip()
113
114        # A dummy call to report, so that recurrence tasks get generated
115        self.t("list")
116
117        code, out, err = self.t("1 delete", input="y\ny\n")
118        self.assertIn("Deleted 4 tasks.", out)
119
120        # Do not agree with purging of the child tasks
121        code, out, err = self.t.runError(uuid + " purge", input="y\nn\n")
122        self.assertIn("Purge operation aborted.", err)
123
124        # Check that nothing was purged
125        code, out, err = self.t("count")
126        self.assertEqual('4\n', out)
127
128    def test_purge_children(self):
129        """Purge command removes dependencies on indirectly purged tasks"""
130        self.t("add one recur:daily due:yesterday")
131        uuid = self.t("_get 1.uuid")[1].strip()
132
133        # A dummy call to report, so that recurrence tasks get generated
134        self.t("list")
135        self.t("add two dep:4")
136
137        # Check that the dependency is present
138        dependencies = self.t("_get 5.depends")[1].strip()
139        self.assertNotEqual("", dependencies)
140
141        code, out, err = self.t("1 delete", input="y\ny\n")
142        self.assertIn("Deleted 4 tasks.", out)
143
144        code, out, err = self.t(uuid + " purge", input="y\ny\n")
145        self.assertIn("Purged 4 tasks.", out)
146
147        # Make sure we are dealing with the intended task
148        description = self.t("_get 1.description")[1].strip()
149        self.assertEqual("two", description)
150
151        # Check that the dependency was removed
152        dependencies = self.t("_get 1.depends")[1].strip()
153        self.assertEqual("", dependencies)
154
155if __name__ == "__main__":
156    from simpletap import TAPTestRunner
157    unittest.main(testRunner=TAPTestRunner())
158
159# vim: ai sts=4 et sw=4 ft=python
160