16bf2cf1c7
Конфиг счётчиков и день/ночь-тарифы, учёт энергосистем (генераторы, инверторы), доступ к разделу РРО/ПРРО. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
134 lines
5.3 KiB
Python
134 lines
5.3 KiB
Python
from datetime import date
|
|
from decimal import Decimal
|
|
|
|
from django.contrib.auth.models import Group
|
|
from django.core.exceptions import ValidationError
|
|
from django.test import TestCase
|
|
|
|
from apps.locations.models.pharmacies import Pharmacy
|
|
from apps.utilities.helpers import get_reading_mode
|
|
from apps.utilities.models import UtilityMeterConfig, UtilityReading
|
|
from core.models.directories import Directory, DirectoryType
|
|
from core.test_utils import login_2fa
|
|
from core.tests_portal_access import make_employee
|
|
|
|
|
|
def _utility_type(name='Електроенергія', **extra):
|
|
dtype, _ = DirectoryType.objects.get_or_create(code='utility_type', defaults={'name': 'Тип лічильника'})
|
|
defaults = {'directory_type': dtype, 'code': name.lower()[:20], 'name': name, 'is_active': True}
|
|
defaults.update(extra)
|
|
return Directory.objects.create(**defaults)
|
|
|
|
|
|
def _pharmacy(n=1):
|
|
org_type, _ = DirectoryType.objects.get_or_create(code='organization', defaults={'name': 'Організація'})
|
|
region_type, _ = DirectoryType.objects.get_or_create(code='location_region', defaults={'name': 'Регион'})
|
|
org = Directory.objects.create(directory_type=org_type, code=f'org{n}', name=f'Org {n}')
|
|
region = Directory.objects.create(directory_type=region_type, code=f'reg{n}', name=f'Reg {n}')
|
|
return Pharmacy.objects.create(organization=org, location_region=region, pharmacy_number=n, location_type='pharmacy')
|
|
|
|
|
|
class UtilityMeterModeTests(TestCase):
|
|
def setUp(self):
|
|
self.pharmacy = _pharmacy()
|
|
self.electricity = _utility_type()
|
|
|
|
def test_default_single_mode(self):
|
|
self.assertEqual(get_reading_mode(self.pharmacy.pk, self.electricity), UtilityMeterConfig.READING_MODE_SINGLE)
|
|
|
|
def test_pharmacy_config_day_night(self):
|
|
UtilityMeterConfig.objects.create(
|
|
pharmacy=self.pharmacy,
|
|
utility_type=self.electricity,
|
|
reading_mode=UtilityMeterConfig.READING_MODE_DAY_NIGHT,
|
|
)
|
|
self.assertEqual(get_reading_mode(self.pharmacy.pk, self.electricity), UtilityMeterConfig.READING_MODE_DAY_NIGHT)
|
|
|
|
def test_single_reading_validation(self):
|
|
reading = UtilityReading(
|
|
pharmacy=self.pharmacy,
|
|
utility_type=self.electricity,
|
|
reading_date=date(2026, 9, 1),
|
|
value=Decimal('100'),
|
|
)
|
|
reading.full_clean()
|
|
|
|
def test_day_night_reading_validation(self):
|
|
UtilityMeterConfig.objects.create(
|
|
pharmacy=self.pharmacy,
|
|
utility_type=self.electricity,
|
|
reading_mode=UtilityMeterConfig.READING_MODE_DAY_NIGHT,
|
|
)
|
|
reading = UtilityReading(
|
|
pharmacy=self.pharmacy,
|
|
utility_type=self.electricity,
|
|
reading_date=date(2026, 7, 1),
|
|
value_day=Decimal('50'),
|
|
value_night=Decimal('30'),
|
|
)
|
|
reading.full_clean()
|
|
reading.save()
|
|
|
|
reading2 = UtilityReading(
|
|
pharmacy=self.pharmacy,
|
|
utility_type=self.electricity,
|
|
reading_date=date(2026, 8, 1),
|
|
value_day=Decimal('60'),
|
|
value_night=Decimal('35'),
|
|
)
|
|
reading2.full_clean()
|
|
self.assertEqual(reading2.consumption_day, Decimal('10'))
|
|
self.assertEqual(reading2.consumption_night, Decimal('5'))
|
|
self.assertEqual(reading2.consumption_total, Decimal('15'))
|
|
|
|
def test_day_night_rejects_single_value(self):
|
|
UtilityMeterConfig.objects.create(
|
|
pharmacy=self.pharmacy,
|
|
utility_type=self.electricity,
|
|
reading_mode=UtilityMeterConfig.READING_MODE_DAY_NIGHT,
|
|
)
|
|
reading = UtilityReading(
|
|
pharmacy=self.pharmacy,
|
|
utility_type=self.electricity,
|
|
reading_date=date(2026, 9, 1),
|
|
value=Decimal('100'),
|
|
)
|
|
with self.assertRaises(ValidationError):
|
|
reading.full_clean()
|
|
|
|
|
|
class UtilityReadingViewTests(TestCase):
|
|
def setUp(self):
|
|
self.pharmacy = _pharmacy()
|
|
self.water = _utility_type('Вода', code='water')
|
|
self.electricity = _utility_type()
|
|
self.user = make_employee(user_location_type='pharmacy')
|
|
self.user.user_pharmacy = self.pharmacy
|
|
self.user.save(update_fields=['user_pharmacy'])
|
|
self.user.groups.add(Group.objects.get(name='Фармацевт'))
|
|
for attr in ('_perm_cache', '_user_perm_cache', '_group_perm_cache'):
|
|
if hasattr(self.user, attr):
|
|
delattr(self.user, attr)
|
|
|
|
def test_post_day_night_reading_via_portal(self):
|
|
UtilityMeterConfig.objects.create(
|
|
pharmacy=self.pharmacy,
|
|
utility_type=self.electricity,
|
|
reading_mode=UtilityMeterConfig.READING_MODE_DAY_NIGHT,
|
|
)
|
|
login_2fa(self.client, self.user)
|
|
response = self.client.post(
|
|
'/utilities/',
|
|
{
|
|
'utility_type': self.electricity.pk,
|
|
'reading_date': '2026-09-02',
|
|
'value_day': '1200.5',
|
|
'value_night': '800.25',
|
|
},
|
|
HTTP_HX_REQUEST='true',
|
|
)
|
|
self.assertEqual(response.status_code, 200)
|
|
reading = UtilityReading.objects.get(pharmacy=self.pharmacy, utility_type=self.electricity)
|
|
self.assertEqual(reading.value_day, Decimal('1200.5'))
|
|
self.assertEqual(reading.value_night, Decimal('800.25'))
|