Blame view

src/alert_manager/tests.py 5.41 KB
3df2d31a   haribo   #3430 : dates are...
1
from django.test import TestCase
fafae58f   haribo   Cleaned directory
2
3
4
5
import os
import shutil
import time
import alert_manager.tasks
3df2d31a   haribo   #3430 : dates are...
6

fafae58f   haribo   Cleaned directory
7
8
from django.conf import settings

ddf59dd4   haribo   Remaniement :
9
from common.models import *
fafae58f   haribo   Cleaned directory
10

bca9a283   Jeremy   Reworked the sche...
11
from utils.Logger import *
53787d30   Jeremy   Alert now inherit...
12
13
log = setupLogger("test", "test")

d84050e6   haribo   Date: 20/07/2016
14
TEST_FILE = "unittest_voevent.xml"
fafae58f   haribo   Cleaned directory
15
16
17
18
19
20

TEST_FILE_PATH = os.path.join(alert_manager.tasks.VOEVENTS_PATH, TEST_FILE)

VOEVENTS_TO_SEND_PATH = "alert_manager/events_to_send"


fd99569d   haribo   Date: 22/06/2016
21
22
class TestStrategyChange(TestCase):

e573c1f1   Etienne Pallier   All unit tests no...
23
    # In /src/misc/ folder !!!
ddf59dd4   haribo   Remaniement :
24
    fixtures = ['tests/alert_mgr_test.json']
fd99569d   haribo   Date: 22/06/2016
25
26

    def setUp(self):
e573c1f1   Etienne Pallier   All unit tests no...
27
        # (EP) BAD !!! Ca ne fonctionne que pcq la fixture ne contient que UNE alerte !!!
fd99569d   haribo   Date: 22/06/2016
28
        self.alert = Alert.objects.get()
e573c1f1   Etienne Pallier   All unit tests no...
29
30
31
32
        # Pour info, une alerte n'a pas d'id propre, mais on peut la lire par son Request id:
        #self.alert = Alert.objects.get(pk=65)
        #print("alert read is", self.alert)
        #print("alert request read is", self.alert.request)
fd99569d   haribo   Date: 22/06/2016
33
34
        self.strat1 = Alert.objects.get().strategyobs
        self.strat2 = StrategyObs.objects.exclude(id=self.strat1.id)[0]
e573c1f1   Etienne Pallier   All unit tests no...
35
36
        #print("strategies are", self.strat1, self.strat2)
        # A quoi ça sert ça, c'est déjà le cas non ???
fd99569d   haribo   Date: 22/06/2016
37
38
39
40
41
        self.alert.strategyobs = self.strat1
        self.alert.save()

    def test_change_inexistant_strat(self):
        self.client.login(username="test@test.test", password="test")
53787d30   Jeremy   Alert now inherit...
42

fd99569d   haribo   Date: 22/06/2016
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
        path = "/alert_manager/change_obs_strategy_validate/" + str(self.alert.id)
        response = self.client.post(path, {"strategy_choice": 42})
        self.assertTrue("error" in response.context.keys(), "There should be an error of non existant strategy")

    def test_change_inexistant_alert(self):
        self.client.login(username="test@test.test", password="test")
        path = "/alert_manager/change_obs_strategy_validate/" + str(self.alert.id + 1)
        response = self.client.post(path, {"strategy_choice": self.strat2.id})
        self.assertTrue("error" in response.context.keys(), "There should be an error of non existant alert")

    def test_change_same_strat(self):
        self.client.login(username="test@test.test", password="test")
        path = "/alert_manager/change_obs_strategy_validate/" + str(self.alert.id)
        response = self.client.post(path, {"strategy_choice": self.strat1.id})
        self.assertTrue("error" in response.context.keys(), "There should be an error of changing to same strategy")

    def test_change_all_working(self):
e573c1f1   Etienne Pallier   All unit tests no...
60
61
62
63
64
65
66
67
68
69
        print("\n===== test_change_all_working =====\n")

        # alert id is 65 with strategy strat1
        print("Alert request read is", self.alert.request)
        print("- its id is the same as its request id, which is", self.alert.id)
        print("- its current strategy is", self.alert.strategyobs)

        # strat1 and strat2
        print("Strategies available in fixture are", self.strat1, self.strat2)

fd99569d   haribo   Date: 22/06/2016
70
        self.client.login(username="test@test.test", password="test")
e573c1f1   Etienne Pallier   All unit tests no...
71
72

        # change strategy to strat2
fd99569d   haribo   Date: 22/06/2016
73
        path = "/alert_manager/change_obs_strategy_validate/" + str(self.alert.id)
e573c1f1   Etienne Pallier   All unit tests no...
74
        # call view alert_manager.views.change_obs_strategy_validate(65)
fd99569d   haribo   Date: 22/06/2016
75
76
77
78
79
80
        response = self.client.post(path, {"strategy_choice": self.strat2.id})
        self.assertFalse("error" in response.context.keys(), "There shouldn't be an error")
        self.assertEqual(Alert.objects.count(), 2, "There should be 2 alerts after the strategy change")
        new_alert = Alert.objects.exclude(id=self.alert.id)[0]
        self.assertEqual(new_alert.strategyobs.id, self.strat2.id, "The new alert should have the 'strat2' strategy")

1aed430d   jeremy   Alert handled + k...
81
# OBSOLETE
1808893a   Unknown   Fixing typo and c...
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# class AlertListenerTestsCelery(TestCase):
#     '''
#         IMPORTANT : As soon as you do a DB request in a test, the test DB will no longer be synchronized with the DB used by celery
#                     I have no idea why (Paul), but any call to the DB must be done in setup or after all celery actions are finished
#     '''
#
#     def test_alert_reception(self):
#         '''
#             Must be launched with scripts/celery_test.sh
#             Copy a VOEvent file in the events_received directory to start the workflow
#             Tests if the alert and children are well created
#         '''
#
#
#         if os.path.isfile(TEST_FILE_PATH):
#             os.remove(TEST_FILE_PATH)
#             print("================== DELETE FILE ==================")
#             time.sleep(3)
#
#         print("================== COPY FILE ==================")
#         shutil.copyfile(os.path.join(VOEVENTS_TO_SEND_PATH, TEST_FILE),
#                         TEST_FILE_PATH)
#         time.sleep(4)
#
#         self.assertEqual(Alert.objects.count(), 1)
#         alert = Alert.objects.all()[0]
#         self.assertEqual(alert.author, "ivo://nasa.gsfc.tan/gcn")
#         self.assertEqual(alert.burst_ra, 74.7412)
#         self.assertEqual(alert.burst_dec, 25.3137)
#         self.assertEqual(alert.trig_id, 532871)
#         self.assertEqual(alert.editor, "61")
#         self.assertEqual(alert.pkt_ser_num, 1)
# 
#         self.assertEqual(Request.objects.count(), 1)
#         self.assertEqual(Sequence.objects.count(), 2)
#         self.assertEqual(Album.objects.count(), 3)
#         self.assertEqual(Plan.objects.count(), 6)
#
#
#
#     def test_basic(self):
#         '''
#             Used to test scripts/celery_test.sh
#             Only work if the initial_fixture is on the celery test DB (an only it)
#         '''
#         Country.objects.create(name="TEEEEST")
#         self.assertEqual(Country.objects.count(), 2, "should be 2 countries")