Repository: SebRut/pygrocy (python)

Issue:
Done time should be optional, as it is for execute_chore call
https://github.com/SebRut/pygrocy/blob/c0a6b1f966521c4de2eccd7ab774866e95b3032b/pygrocy/grocy.py#L198

Pull request:
make done_time optional for complete_task
fixes #176 

Patch:
diff --git a/pygrocy/grocy.py b/pygrocy/grocy.py
index 9d51c41..adc79e6 100644
--- a/pygrocy/grocy.py
+++ b/pygrocy/grocy.py
@@ -195,7 +195,7 @@ class Grocy(object):
         raw_tasks = self._api_client.get_tasks()
         return [Task(task) for task in raw_tasks]
 
-    def complete_task(self, task_id, done_time):
+    def complete_task(self, task_id, done_time: datetime = datetime.now()):
         return self._api_client.complete_task(task_id, done_time)
 
     def meal_plan(self, get_details: bool = False) -> List[MealPlanItem]:


Test patch:
diff --git a/test/cassettes/test_tasks/TestTasks.test_complete_task_invalid.yaml b/test/cassettes/test_tasks/TestTasks.test_complete_task_invalid.yaml
new file mode 100644
index 0000000..1a18f10
--- /dev/null
+++ b/test/cassettes/test_tasks/TestTasks.test_complete_task_invalid.yaml
@@ -0,0 +1,44 @@
+interactions:
+- request:
+    body: '{"done_time": "2021-08-23T09:10:00.314599+00:00"}'
+    headers:
+      Accept-Encoding:
+      - gzip, deflate
+      Connection:
+      - keep-alive
+      Content-Length:
+      - '49'
+      Content-Type:
+      - application/json
+      User-Agent:
+      - python-requests/2.26.0
+      accept:
+      - application/json
+    method: POST
+    uri: https://localhost/api/tasks/1000/complete
+  response:
+    body:
+      string: '{"error_message":"Task does not exist"}'
+    headers:
+      Access-Control-Allow-Headers:
+      - '*'
+      Access-Control-Allow-Methods:
+      - GET, POST, PUT, DELETE, OPTIONS
+      Access-Control-Allow-Origin:
+      - '*'
+      Connection:
+      - keep-alive
+      Content-Type:
+      - application/json
+      Date:
+      - Mon, 23 Aug 2021 09:10:01 GMT
+      Server:
+      - nginx/1.20.1
+      Transfer-Encoding:
+      - chunked
+      X-Powered-By:
+      - PHP/8.0.9
+    status:
+      code: 400
+      message: Bad Request
+version: 1
diff --git a/test/cassettes/test_tasks/TestTasks.test_complete_task_valid.yaml b/test/cassettes/test_tasks/TestTasks.test_complete_task_valid.yaml
new file mode 100644
index 0000000..5a3a791
--- /dev/null
+++ b/test/cassettes/test_tasks/TestTasks.test_complete_task_valid.yaml
@@ -0,0 +1,42 @@
+interactions:
+- request:
+    body: '{"done_time": "2021-08-23T09:06:38.501659+00:00"}'
+    headers:
+      Accept-Encoding:
+      - gzip, deflate
+      Connection:
+      - keep-alive
+      Content-Length:
+      - '49'
+      Content-Type:
+      - application/json
+      User-Agent:
+      - python-requests/2.26.0
+      accept:
+      - application/json
+    method: POST
+    uri: https://localhost/api/tasks/4/complete
+  response:
+    body:
+      string: ''
+    headers:
+      Access-Control-Allow-Headers:
+      - '*'
+      Access-Control-Allow-Methods:
+      - GET, POST, PUT, DELETE, OPTIONS
+      Access-Control-Allow-Origin:
+      - '*'
+      Connection:
+      - keep-alive
+      Content-Type:
+      - application/json
+      Date:
+      - Mon, 23 Aug 2021 09:06:38 GMT
+      Server:
+      - nginx/1.20.1
+      X-Powered-By:
+      - PHP/8.0.9
+    status:
+      code: 204
+      message: No Content
+version: 1
diff --git a/test/cassettes/test_tasks/TestTasks.test_complete_task_valid_with_defaults.yaml b/test/cassettes/test_tasks/TestTasks.test_complete_task_valid_with_defaults.yaml
new file mode 100644
index 0000000..c2a585c
--- /dev/null
+++ b/test/cassettes/test_tasks/TestTasks.test_complete_task_valid_with_defaults.yaml
@@ -0,0 +1,42 @@
+interactions:
+- request:
+    body: '{"done_time": "2021-08-23T09:07:57.290165+00:00"}'
+    headers:
+      Accept-Encoding:
+      - gzip, deflate
+      Connection:
+      - keep-alive
+      Content-Length:
+      - '49'
+      Content-Type:
+      - application/json
+      User-Agent:
+      - python-requests/2.26.0
+      accept:
+      - application/json
+    method: POST
+    uri: https://localhost/api/tasks/3/complete
+  response:
+    body:
+      string: ''
+    headers:
+      Access-Control-Allow-Headers:
+      - '*'
+      Access-Control-Allow-Methods:
+      - GET, POST, PUT, DELETE, OPTIONS
+      Access-Control-Allow-Origin:
+      - '*'
+      Connection:
+      - keep-alive
+      Content-Type:
+      - application/json
+      Date:
+      - Mon, 23 Aug 2021 09:07:58 GMT
+      Server:
+      - nginx/1.20.1
+      X-Powered-By:
+      - PHP/8.0.9
+    status:
+      code: 204
+      message: No Content
+version: 1
diff --git a/test/test_tasks.py b/test/test_tasks.py
index 42facd0..e1a1c6f 100644
--- a/test/test_tasks.py
+++ b/test/test_tasks.py
@@ -1,5 +1,9 @@
+from datetime import datetime
+
 import pytest
 
+from pygrocy.errors import GrocyError
+
 
 class TestTasks:
     @pytest.mark.vcr
@@ -9,3 +13,19 @@ class TestTasks:
         assert len(tasks) == 6
         assert tasks[0].id == 1
         assert tasks[0].name == "Repair the garage door"
+
+    @pytest.mark.vcr
+    def test_complete_task_valid_with_defaults(self, grocy):
+        grocy.complete_task(3)
+
+    @pytest.mark.vcr
+    def test_complete_task_valid(self, grocy):
+        grocy.complete_task(4, done_time=datetime.now())
+
+    @pytest.mark.vcr
+    def test_complete_task_invalid(self, grocy):
+        with pytest.raises(GrocyError) as exc_info:
+            grocy.complete_task(1000)
+
+        error = exc_info.value
+        assert error.status_code == 400