Completed
Push — master ( a65260...307dd7 )
by Paolo
07:35
created

CleanUpTaskTestCase.test_clean_up()   A

Complexity

Conditions 2

Size

Total Lines 15
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 15
rs 10
c 0
b 0
f 0
cc 2
nop 1
1
#!/usr/bin/env python3
2
# -*- coding: utf-8 -*-
3
"""
4
Created on Thu Nov 14 16:19:41 2019
5
6
@author: Paolo Cozzi <[email protected]>
7
"""
8
9
from unittest.mock import patch
10
11
from django.test import TestCase
12
from django.utils import timezone
13
14
from common.constants import COMPLETED
15
16
from ..models import Submission
17
from ..tasks import CleanUpTask
18
19
20
class CleanUpTaskTestCase(TestCase):
21
22
    fixtures = [
23
        "biosample/submission",
24
        "uid/dictcountry",
25
        "uid/dictrole",
26
        "uid/organization",
27
        "uid/submission",
28
        "uid/user",
29
    ]
30
31
    def setUp(self):
32
        # calling my base setup
33
        super().setUp()
34
35
        # fix status for objects
36
        Submission.objects.update(status=COMPLETED)
37
38
        # get one objcet and updatete time
39
        Submission.objects.filter(pk=1).update(updated_at=timezone.now())
40
41
        # define my task
42
        self.my_task = CleanUpTask()
43
44
        # change lock_id (useful when running test during cron)
45
        self.my_task.lock_id = "test-CleanUpTask"
46
47
    def test_clean_up(self):
48
        """Test clean_up task"""
49
50
        for submission in Submission.objects.all():
51
            print(submission, submission.updated_at)
52
53
        # NOTE that I'm calling the function directly, without delay
54
        # (AsyncResult). I've patched the time consuming task
55
        res = self.my_task.run()
56
57
        # assert a success with data uploading
58
        self.assertEqual(res, "success")
59
60
        # assert one object in the database
61
        self.assertEqual(Submission.objects.count(), 1)
62
63
    # Test a non blocking instance
64
    @patch("redis.lock.Lock.acquire", return_value=False)
65
    def test_fetch_status_nb(self, my_lock):
66
        """Test CleanUpTask while a lock is present"""
67
68
        res = self.my_task.run()
69
70
        # assert database is locked
71
        self.assertEqual(res, "%s already running!" % (self.my_task.name))
72
73
        # assert two object in the database (fake running)
74
        self.assertEqual(Submission.objects.count(), 2)
75