157 lines
6.2 KiB
Python
157 lines
6.2 KiB
Python
from django.test import TestCase, Client
|
|
from django.urls import reverse
|
|
from django.contrib.auth import get_user_model
|
|
from django.contrib.contenttypes.models import ContentType
|
|
from django.utils import timezone
|
|
|
|
from assets.models import NatureRWIZ, NatureRWIAsset, ParkingSpot
|
|
from interventions.models import Intervention
|
|
from common.models import Thematic
|
|
from .models import Inspection, ParkingSpotInspection, GiepInspection, InspectionDocument
|
|
from .registry import get_inspection_partial_template
|
|
|
|
User = get_user_model()
|
|
|
|
|
|
class InspectionModelAndViewsTestCase(TestCase):
|
|
def setUp(self):
|
|
self.client = Client()
|
|
self.user = User.objects.create_user(
|
|
username='inspector_user',
|
|
password='password123',
|
|
first_name='Jean',
|
|
last_name='Valjean'
|
|
)
|
|
|
|
self.thematic, _ = Thematic.objects.get_or_create(
|
|
code='parking',
|
|
defaults={'name_fr': 'Stationnement'}
|
|
)
|
|
|
|
self.spot = ParkingSpot.objects.create(
|
|
name_fr='Emplacement Vélo Test'
|
|
)
|
|
self.spot_ct = ContentType.objects.get_for_model(ParkingSpot)
|
|
|
|
self.rwiz = NatureRWIZ.objects.create(
|
|
name_fr='Zone GIEP Test'
|
|
)
|
|
self.rwiz_ct = ContentType.objects.get_for_model(NatureRWIZ)
|
|
|
|
self.mission_interv = Intervention.objects.create(
|
|
title="Mission inspection GIEP Nord",
|
|
thematic=self.thematic,
|
|
status="in_progress"
|
|
)
|
|
self.work_interv = Intervention.objects.create(
|
|
title="Travaux réfection par Prestataire A",
|
|
thematic=self.thematic,
|
|
status="done"
|
|
)
|
|
|
|
def test_inspection_creation_with_interventions(self):
|
|
insp = Inspection.objects.create(
|
|
asset_content_type=self.spot_ct,
|
|
asset_object_id=self.spot.id,
|
|
inspector=self.user,
|
|
mission_intervention=self.mission_interv,
|
|
checked_intervention=self.work_interv,
|
|
nature='technical',
|
|
inspector_role='provider',
|
|
result_status='compliant',
|
|
notes="Tiers contrôle OK"
|
|
)
|
|
|
|
self.assertTrue(insp.is_mission_inspection)
|
|
self.assertTrue(insp.is_cross_check)
|
|
self.assertEqual(insp.mission_intervention, self.mission_interv)
|
|
self.assertEqual(insp.checked_intervention, self.work_interv)
|
|
self.assertIn(insp, self.mission_interv.generated_inspections.all())
|
|
self.assertIn(insp, self.work_interv.quality_inspections.all())
|
|
|
|
def test_giep_inspection_model(self):
|
|
giep_insp = GiepInspection.objects.create(
|
|
asset_content_type=self.rwiz_ct,
|
|
asset_object_id=self.rwiz.id,
|
|
inspector=self.user,
|
|
result_status='compliant',
|
|
date_derniere_pluie='aujourdhui',
|
|
orage_moins_une_heure=True,
|
|
data={
|
|
'ouvrages': [
|
|
{'local_id': '0', 'device_type': 'evc', 'name': 'Noue 1'}
|
|
]
|
|
}
|
|
)
|
|
|
|
self.assertEqual(giep_insp.detailed_inspection, giep_insp)
|
|
self.assertIn("1 ouvrage(s) GIEP", giep_insp.details_summary)
|
|
self.assertIn(str(giep_insp.get_date_derniere_pluie_display()), giep_insp.details_summary)
|
|
|
|
def test_create_inspection_api(self):
|
|
self.client.login(username='inspector_user', password='password123')
|
|
url = reverse('inspections:create_inspection_api')
|
|
response = self.client.post(url, {
|
|
'content_type_id': self.spot_ct.id,
|
|
'object_id': self.spot.id,
|
|
'result_status': 'compliant',
|
|
'nature': 'visual',
|
|
'inspector_role': 'provider',
|
|
'mission_intervention_id': self.mission_interv.id,
|
|
'checked_intervention_id': self.work_interv.id,
|
|
'notes': 'Inspection via API'
|
|
})
|
|
self.assertEqual(response.status_code, 200)
|
|
data = response.json()
|
|
self.assertTrue(data['success'])
|
|
|
|
insp = Inspection.objects.get(pk=data['inspection_id'])
|
|
self.assertEqual(insp.mission_intervention, self.mission_interv)
|
|
self.assertEqual(insp.checked_intervention, self.work_interv)
|
|
|
|
def test_create_giep_inspection_api(self):
|
|
self.client.login(username='inspector_user', password='password123')
|
|
url = reverse('inspections:create_inspection_api')
|
|
import json
|
|
giep_json = json.dumps({
|
|
'ouvrages': [
|
|
{'local_id': '0', 'device_type': 'evc', 'questions': {'engorgement': 'aucun'}}
|
|
]
|
|
})
|
|
response = self.client.post(url, {
|
|
'content_type_id': self.rwiz_ct.id,
|
|
'object_id': self.rwiz.id,
|
|
'result_status': 'compliant',
|
|
'nature': 'visual',
|
|
'inspector_role': 'observer',
|
|
'date_derniere_pluie': 'moins_24h',
|
|
'orage_moins_une_heure': 'false',
|
|
'giep_data': giep_json,
|
|
'notes': 'Inspection GIEP via API'
|
|
})
|
|
self.assertEqual(response.status_code, 200)
|
|
data = response.json()
|
|
self.assertTrue(data['success'])
|
|
|
|
insp = GiepInspection.objects.get(pk=data['inspection_id'])
|
|
self.assertEqual(insp.date_derniere_pluie, 'moins_24h')
|
|
self.assertFalse(insp.orage_moins_une_heure)
|
|
self.assertEqual(len(insp.data.get('ouvrages', [])), 1)
|
|
|
|
def test_registry_giep_template(self):
|
|
tmpl_rwiz = get_inspection_partial_template('naturerwiz')
|
|
tmpl_asset = get_inspection_partial_template('naturerwiasset')
|
|
self.assertEqual(tmpl_rwiz, 'inspections/components/asset_fields/_naturerwiz.html')
|
|
self.assertEqual(tmpl_asset, 'inspections/components/asset_fields/_naturerwiz.html')
|
|
|
|
def test_registry_detail_templates(self):
|
|
from .registry import get_inspection_detail_partial_template
|
|
giep_insp = GiepInspection(asset=self.rwiz)
|
|
parking_insp = ParkingSpotInspection(asset=self.spot)
|
|
|
|
tmpl_giep = get_inspection_detail_partial_template(giep_insp)
|
|
tmpl_parking = get_inspection_detail_partial_template(parking_insp)
|
|
|
|
self.assertEqual(tmpl_giep, 'inspections/components/asset_details/_naturerwiz.html')
|
|
self.assertEqual(tmpl_parking, 'inspections/components/asset_details/_parkingspot.html')
|
|
|