Blame view

src/core/pyros_django/common/tests.py 6.66 KB
fafae58f   haribo   Cleaned directory
1
from django.test import TestCase
7c87794b   haribo   Date: 20/05/2016
2
from common.RequestBuilder import RequestBuilder
ddf59dd4   haribo   Remaniement :
3
from common.models import *
7c87794b   haribo   Date: 20/05/2016
4
from django.contrib.auth.models import User
ddf59dd4   haribo   Remaniement :
5
from django.utils import timezone
fafae58f   haribo   Cleaned directory
6

bca9a283   Jeremy   Reworked the sche...
7
from utils.Logger import *
53787d30   Jeremy   Alert now inherit...
8
9
log = setupLogger("common", "common")

7c87794b   haribo   Date: 20/05/2016
10
11
class RequestBuilderTests(TestCase):

d45d4a0b   ALEXIS-PC\alexis   fixing fixtures w...
12
    fixtures = ['tests/common_test_TZ.json']
7c87794b   haribo   Date: 20/05/2016
13
14
15
16
17
18
19
20

    def setUp(self):
        '''
            Creates the base for the tests : user, scientific program, etc
        '''

        country = Country.objects.create(name="France")
        usr_lvl = UserLevel.objects.create(name="default")
05f66454   ALEXIS-PC\alexis   adding and updati...
21
22
23
        self.pyusr = PyrosUser.objects.create(username="toto", country=country, quota=1111)
        # (AKo) We need to assign UserLevel after PyrosUser creation, we can't associate directly from creation because it's a m2m relation
        UserLevel.objects.all()[0].pyros_users.add(self.pyusr)
7c87794b   haribo   Date: 20/05/2016
24
25
26
27
28
29
30
31
32
        self.sp = ScientificProgram.objects.create(name="default")

    def test_full_creation(self):
        '''
            Goal : Create a full request with sequences, albums & plans, and check if they are in the DB
        '''
        request_builder = RequestBuilder()
        request_builder.start_new_request(self.pyusr, self.sp, False)

e573c1f1   Etienne Pallier   All unit tests no...
33
        # seq1
7c87794b   haribo   Date: 20/05/2016
34
        seq1 = request_builder.add_sequence(1, 0, 10, name="seq1")
abfb02e2   Jeremy   Device Model is n...
35
        alb11 = request_builder.add_album(seq1, Detector.NIR, name="alb11")
7c87794b   haribo   Date: 20/05/2016
36
        request_builder.add_plan(
abfb02e2   Jeremy   Device Model is n...
37
38
            alb11, Filter.NIR_FILTER_1, 120, 5, name="plan111")
        alb12 = request_builder.add_album(seq1, Detector.VIS, name="alb12")
7c87794b   haribo   Date: 20/05/2016
39
        request_builder.add_plan(
abfb02e2   Jeremy   Device Model is n...
40
            alb12, Filter.VIS_FILTER_1, 180, 1, name="plan121")
e573c1f1   Etienne Pallier   All unit tests no...
41
42

        # seq2
7c87794b   haribo   Date: 20/05/2016
43
        seq2 = request_builder.add_sequence(1, 0, 10, name="seq2")
abfb02e2   Jeremy   Device Model is n...
44
        alb21 = request_builder.add_album(seq2, Detector.NIR, name="alb21")
7c87794b   haribo   Date: 20/05/2016
45
        request_builder.add_plan(
abfb02e2   Jeremy   Device Model is n...
46
            alb21, Filter.NIR_FILTER_2, 60, 3, name="plan211")
e573c1f1   Etienne Pallier   All unit tests no...
47

7c87794b   haribo   Date: 20/05/2016
48
49
        request_builder.validate_request()

e573c1f1   Etienne Pallier   All unit tests no...
50
51
        # Check content in DB :

7c87794b   haribo   Date: 20/05/2016
52
53
54
55
56
57
58
59
60
61
        req = Request.objects.get()
        seq1 = Sequence.objects.get(name="seq1")
        seq2 = Sequence.objects.get(name="seq2")
        albums = Album.objects.all()

        self.assertEqual(len(req.sequences.all()), 2, "There should be 2 sequences")
        self.assertEqual(seq1.albums.count(), 2, "There should be 2 albums in seq1")
        self.assertEqual(seq2.albums.count(), 1, "There should be 1 album in seq2")
        self.assertEqual(Plan.objects.count(), 3, "There should be 3 plans")
        self.assertEqual(len(albums), 3, "There should be 3 albums")
7c87794b   haribo   Date: 20/05/2016
62
63
        for album in albums:
            self.assertEqual(album.plans.count(), 1, "There should be 1 plan in each album")
ddf59dd4   haribo   Remaniement :
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86


class CountryTests(TestCase):

    def test_creation(self):
        Country.objects.create(name="France", desc="Test", quota=1)
        france = Country.objects.get(name="France")
        self.assertEqual(france.name, "France")
        self.assertEqual(france.desc, "Test")
        self.assertEqual(france.quota, 1)
        france.quota = -1
        france.delete()
        france.save()
        Country.objects.create(name="Mexique")
        countries = Country.objects.all()
        self.assertEqual(countries.count(), 2)
        countries[0].name = "Toto"
        countries[0].save()


class DeviceTests(TestCase):

    def test_creation(self):
ddf59dd4   haribo   Remaniement :
87
88
89
90
91
92
93
94
95
96
        countries = Country.objects.all()
        self.assertEqual(countries.count(), 0, "perdu !")


class RequestTests(TestCase):

    def setUp(self):
        strat1 = StrategyObs.objects.create(name="strat1")
        france = Country.objects.create(name="France")
        admin = UserLevel.objects.create(name="Admin")
53787d30   Jeremy   Alert now inherit...
97
        haribo = PyrosUser.objects.create(username="Haribo",
05f66454   ALEXIS-PC\alexis   adding and updati...
98
99
            country=france)
        admin.pyros_users.add(haribo)
ddf59dd4   haribo   Remaniement :
100
101
102
        sp1 = ScientificProgram.objects.create(name="sp1")
        req1 = Request.objects.create(
            name="req1", pyros_user=haribo, scientific_program=sp1)
53787d30   Jeremy   Alert now inherit...
103
104
105
106
        alert = Alert(strategyobs=strat1, request_id=req1)
        alert.__dict__.update(req1.__dict__)
        alert.save()
        log.info(alert.__dict__)
ddf59dd4   haribo   Remaniement :
107
108
109
110
111

        sched = Schedule.objects.create()
        seq1 = Sequence.objects.create(
            name="seq1", request=req1)

abfb02e2   Jeremy   Device Model is n...
112
113
        tel = Telescope.objects.create(name="telescope")
        det = Detector.objects.create(name="detector", telescope=tel)
ddf59dd4   haribo   Remaniement :
114
115
        alb1 = Album.objects.create(name="alb1", sequence=seq1, detector=det)

abfb02e2   Jeremy   Device Model is n...
116
        fw = FilterWheel.objects.create(name="fw")
ddf59dd4   haribo   Remaniement :
117

abfb02e2   Jeremy   Device Model is n...
118
        fil = Filter.objects.create(name="filter", filter_wheel=fw)
ddf59dd4   haribo   Remaniement :
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
        plan1 = Plan.objects.create(name="plan1", album=alb1, filter=fil)
        img1 = Image.objects.create(name="img1", plan=plan1)
        img2 = Image.objects.create(name="img2", plan=plan1)

    def test_request_access(self):
        alert = Alert.objects.get()
        self.assertEqual(alert.request.name, "req1")

        req1 = Request.objects.get(name="req1")
        req1.name = "reqtiti"
        req1.save()
        # j'ai saved req1, pourtant alert1.request.name est toujours le même
        self.assertEqual(alert.request.name, "req1")

        alert = Alert.objects.get()
        # j'ai refait un get de mon alert, donc c'est mis à jour
        self.assertEqual(alert.request.name, "reqtiti")

53787d30   Jeremy   Alert now inherit...
137
        alert.name = "reqtoto"
ddf59dd4   haribo   Remaniement :
138
139
140
141
142
        alert.save()
        self.assertEqual(req1.name, "reqtiti")
        self.assertEqual(alert.request.name, "reqtoto")

        req1 = Request.objects.get()
53787d30   Jeremy   Alert now inherit...
143
        self.assertEqual(req1.name, "reqtoto")
ddf59dd4   haribo   Remaniement :
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178

        alert.request.save()
        req1 = Request.objects.get()
        self.assertEqual(req1.name, "reqtoto")

    def test_reinit_base(self):
        req1 = Request.objects.get()
        self.assertEqual(req1.name, "req1")

    def test_full_request(self):
        req1 = Request.objects.get()
        self.assertEqual(req1.sequences.count(), 1)
        albums = req1.sequences.all()[0].albums
        self.assertEqual(albums.count(), 1)
        images = albums.all()[0].plans.all()[0].images
        self.assertEqual(images.count(), 2)
        img1 = images.get(name="img1")
        img1.name = "img1.1"
        img11 = req1.sequences.get().albums.get().plans.get().images.get(
            name="img1")  # j'ai toujours pas sauvegardé mon image
        img1.save()
        img11 = req1.sequences.get().albums.get().plans.get().images.get(
            name="img1.1")  # j'ai sauvegardé mon image

        req1 = Request.objects.get()
        img1 = req1.sequences.get().albums.get().plans.get().images.get(
            name="img1.1")

        img11 = Image.objects.get(name="img1.1")
        img11.name = "img1.2"
        img11.save()

        self.assertEqual(img1.name, "img1.1")  # img1 et img11 sont différents
        img1 = req1.sequences.get().albums.get().plans.get().images.get(
            name="img1.2")