Completed
Push — master ( 8bc9c6...fbd831 )
by Paolo
22s queued 14s
created

ExportSubmissionViewTest.setUp()   A

Complexity

Conditions 1

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 14
rs 10
c 0
b 0
f 0
cc 1
nop 1
1
#!/usr/bin/env python3
2
# -*- coding: utf-8 -*-
3
"""
4
Created on Mon Jan 13 12:22:34 2020
5
6
@author: Paolo Cozzi <[email protected]>
7
"""
8
9
from django.test import TestCase, Client
10
from django.urls import resolve, reverse
11
12
from common.constants import NEED_REVISION
13
from common.tests import GeneralMixinTestCase, OwnerMixinTestCase
14
from uid.models import Submission
15
16
from ..views import ExportSubmissionView
17
18
19
class ExportSubmissionViewTest(
20
        GeneralMixinTestCase, OwnerMixinTestCase, TestCase):
21
22
    fixtures = [
23
        'uid/animal',
24
        'uid/dictbreed',
25
        'uid/dictcountry',
26
        'uid/dictrole',
27
        'uid/dictsex',
28
        'uid/dictspecie',
29
        'uid/dictstage',
30
        'uid/dictuberon',
31
        'uid/organization',
32
        'uid/publication',
33
        'uid/sample',
34
        'uid/submission',
35
        'uid/user'
36
    ]
37
38
    def setUp(self):
39
        # login a test user (defined in fixture)
40
        self.client = Client()
41
        self.client.login(username='test', password='test')
42
43
        # get a submission object
44
        self.submission = Submission.objects.get(pk=1)
45
46
        # update submission status with a permitted statuses
47
        self.submission.status = NEED_REVISION
48
        self.submission.save()
49
50
        self.url = reverse('submissions:export', kwargs={'pk': 1})
51
        self.response = self.client.get(self.url)
52
53
    def test_url_resolves_view(self):
54
        view = resolve('/submissions/1/export/')
55
        self.assertIsInstance(view.func.view_class(), ExportSubmissionView)
56
57
    def test_edit_not_found_status_code(self):
58
        url = reverse('submissions:export', kwargs={'pk': 99})
59
        response = self.client.get(url)
60
        self.assertEqual(response.status_code, 404)
61
62
    def test_file_attachment(self):
63
        # https://stackoverflow.com/a/8244317/4385116
64
        self.assertEqual(
65
            self.response.get('Content-Disposition'),
66
            'attachment; filename="submission_1_names.csv"'
67
        )
68
69
    def test_file_content(self):
70
        reference = [
71
            b'id,name,biosample_id,material,status,last_changed,'
72
            b'last_submitted\r\n',
73
            b'3,ANIMAL:::ID:::son,,Organism,Loaded,,\r\n',
74
            b'2,ANIMAL:::ID:::mother,,Organism,Loaded,,\r\n',
75
            b'1,ANIMAL:::ID:::132713,,Organism,Loaded,,\r\n',
76
            b'1,Siems_0722_393449,,Specimen from Organism,Loaded,,\r\n',
77
        ]
78
79
        # https://docs.djangoproject.com/en/dev/ref/request-response/#streaminghttpresponse-objects
80
        test = list(self.response.streaming_content)
81
82
        self.assertListEqual(sorted(reference), sorted(test))
83