1
1
from django .contrib .auth import get_user_model
2
+ from django .contrib .contenttypes .forms import generic_inlineformset_factory
2
3
from django .urls import reverse
3
- from swapper import load_model
4
+ from django .utils .timezone import now
5
+ from swapper import get_model_name , load_model
4
6
7
+ from ...check .settings import CHECK_CLASSES
8
+ from ..admin import CheckInline , CheckInlineFormSet
5
9
from . import DeviceMonitoringTestCase
6
10
7
11
Chart = load_model ('monitoring' , 'Chart' )
8
12
Metric = load_model ('monitoring' , 'Metric' )
9
13
DeviceData = load_model ('device_monitoring' , 'DeviceData' )
10
14
User = get_user_model ()
15
+ Check = load_model ('check' , 'Check' )
11
16
12
17
13
18
class TestAdmin (DeviceMonitoringTestCase ):
@@ -21,14 +26,20 @@ def _login_admin(self):
21
26
22
27
def test_device_admin (self ):
23
28
dd = self .create_test_adata ()
29
+ check = Check .objects .create (
30
+ name = 'Ping check' , check = CHECK_CLASSES [0 ][0 ], content_object = dd , params = {},
31
+ )
24
32
url = reverse ('admin:config_device_change' , args = [dd .pk ])
25
33
self ._login_admin ()
26
34
r = self .client .get (url )
27
35
self .assertContains (r , '<h2>Status</h2>' )
28
36
self .assertContains (r , '<h2>Charts</h2>' )
37
+ self .assertContains (r , '<h2>Checks</h2>' )
29
38
self .assertContains (r , 'Storage' )
30
39
self .assertContains (r , 'CPU' )
31
40
self .assertContains (r , 'RAM status' )
41
+ self .assertContains (r , check .name )
42
+ self .assertContains (r , check .params )
32
43
33
44
def test_no_device_data (self ):
34
45
d = self ._create_device (organization = self ._create_org ())
@@ -64,3 +75,35 @@ def test_uuid_bug(self):
64
75
self ._login_admin ()
65
76
r = self .client .get (url )
66
77
self .assertContains (r , '<h2>Status</h2>' )
78
+
79
+ def test_check_inline_formset (self ):
80
+ d = self ._create_device (organization = self ._create_org ())
81
+ check_inline_formset = generic_inlineformset_factory (
82
+ model = Check , form = CheckInline .form , formset = CheckInlineFormSet
83
+ )
84
+ # model_name changes if swapped
85
+ model_name = get_model_name ('check' , 'Check' ).lower ().replace ('.' , '-' )
86
+ ct = f'{ model_name } -content_type-object_id'
87
+ data = {
88
+ f'{ ct } -TOTAL_FORMS' : '1' ,
89
+ f'{ ct } -INITIAL_FORMS' : '0' ,
90
+ f'{ ct } -MAX_NUM_FORMS' : '0' ,
91
+ f'{ ct } -0-name' : 'Ping Check' ,
92
+ f'{ ct } -0-check' : CHECK_CLASSES [0 ][0 ],
93
+ f'{ ct } -0-params' : '{}' ,
94
+ f'{ ct } -0-active' : True ,
95
+ f'{ ct } -0-created' : now (),
96
+ f'{ ct } -0-modified' : now (),
97
+ }
98
+ formset = check_inline_formset (data )
99
+ formset .instance = d
100
+ self .assertTrue (formset .is_valid ())
101
+ self .assertEqual (formset .errors , [{}])
102
+ self .assertEqual (formset .non_form_errors (), [])
103
+ form = formset .forms [0 ]
104
+ form .cleaned_data = data
105
+ form .save (commit = True )
106
+ self .assertEqual (Check .objects .count (), 1 )
107
+ c = Check .objects .first ()
108
+ self .assertEqual (c .name , 'Ping Check' )
109
+ self .assertEqual (c .content_object , d )
0 commit comments