コード例 #1
0
def create_rule(**fields):
    if not fields.get('device'):
        fields['device'] = create_device()

    limit_value_kwargs = {
        'right_digits': 2,
        'min_value': ceil(fields.get('device').min_value),
        'max_value': floor(fields.get('device').max_value)
    }
    fake_rule_fields = {
        'coefficient':
        fake.pyfloat(right_digits=2, min_value=0, max_value=1),
        'possible_deviation':
        fake.pyfloat(min_value=0, max_value=10),
        'time_interval':
        timedelta(hours=fake.pyint(max_value=24),
                  minutes=fake.pyint(max_value=60)),
        **{
            k: v
            for k, v in zip(['min_value', 'max_value'],
                            sorted([
                                fake.pyfloat(**limit_value_kwargs),
                                fake.pyfloat(**limit_value_kwargs)
                            ]))
        }
    }

    if not fields.get('freight'):
        fields['freight'] = create_freight()

    for field, value in fake_rule_fields.items():
        if field not in fields:
            fields[field] = value

    return Rule.objects.create(**fields)
コード例 #2
0
    def setUp(self) -> None:
        self.device = create_device()
        self.freight = create_freight(need_transfer=True)
        self.rule = create_rule(device=self.device, freight=self.freight)

        not_valid_max_value = fake.pyfloat(right_digits=2, min_value=ceil(self.device.max_value))

        self.valid_partial_data = {
            'max_value': fake.pyfloat(right_digits=2, min_value=ceil(self.rule.min_value),
                                      max_value=floor(self.device.max_value))
        }
        self.invalid_partial_data = {
            'max_value': not_valid_max_value
        }
        self.valid_data = {
            'coefficient': self.rule.coefficient,
            'possible_deviation': fake.pyfloat(right_digits=2, min_value=0, max_value=10),
            'time_interval': self.rule.time_interval,
            'device': self.device.id,
            'freight': self.freight.id,
            'min_value': self.rule.min_value,
            'max_value': self.rule.max_value,
        }

        self.invalid_data = {
            **self.valid_data,
            'min_value': self.valid_data['max_value'],
            'max_value': self.valid_data['min_value'],
        }

        self.rule_detail_url = reverse('freights:rule-detail', kwargs={'freight_pk': self.freight.id,
                                                                       'rule_pk': self.rule.id})
コード例 #3
0
    def setUp(self) -> None:
        self.freight = create_freight(need_transfer=True)
        self.rule = create_rule(freight=self.freight)
        self.state = create_state(rule=self.rule)

        invalid_value = fake.pyfloat(right_digits=2,
                                     min_value=ceil(
                                         self.rule.device.max_value))

        self.valid_partial_data = {
            'value':
            fake.pyfloat(right_digits=2,
                         min_value=ceil(self.rule.min_value),
                         max_value=floor(self.rule.max_value))
        }
        self.invalid_partial_data = {'value': invalid_value}
        self.valid_data = {
            'value':
            fake.pyfloat(right_digits=2,
                         min_value=ceil(self.rule.min_value),
                         max_value=floor(self.rule.max_value)),
            'rule':
            self.rule.id
        }
        self.invalid_data = {'value': invalid_value, 'rule': self.rule.id}

        self.state_detail_url = reverse('freights:state-detail',
                                        kwargs={
                                            'freight_pk': self.freight.id,
                                            'rule_pk': self.rule.id,
                                            'state_pk': self.state.id
                                        })
コード例 #4
0
    def setUp(self) -> None:
        self.valid_device = {
            'name': fake.sentence(1),
            'unit': fake.random_element(Device.Unit.values),
            'prefix': fake.random_element(Device.Prefix.values),
            **{k: v for k, v in zip(['min_value', 'max_value'],
                                    sorted([fake.pyfloat(right_digits=2), fake.pyfloat(right_digits=2)]))}
        }

        self.invalid_device = {
            **self.valid_device,
            'unit': fake.sentence(1)
        }
        self.device_list_url = reverse('devices:list')
コード例 #5
0
def create_device(**fields):
    fake_device_fields = {
        'name': fake.sentence(1),
        'unit': fake.random_element(Device.Unit.values),
        'prefix': fake.random_element(Device.Prefix.values),
        **{
            k: v
            for k, v in zip(['min_value', 'max_value'],
                            sorted([
                                fake.pyfloat(right_digits=2),
                                fake.pyfloat(right_digits=2)
                            ]))
        }
    }

    for field, value in fake_device_fields.items():
        if field not in fields:
            fields[field] = value

    return Device.objects.create(**fields)
コード例 #6
0
    def setUp(self) -> None:
        self.device = create_device()
        self.freight = create_freight(need_transfer=True)

        limit_value_kwargs = {'right_digits': 2, 'min_value': ceil(self.device.min_value),
                              'max_value': floor(self.device.max_value)}
        self.valid_rule = {
            'coefficient': fake.pyfloat(right_digits=2, min_value=0, max_value=1),
            'possible_deviation': fake.pyfloat(min_value=0, max_value=10),
            'time_interval': str(timedelta(hours=fake.pyint(max_value=24), minutes=fake.pyint(max_value=60))),
            'device': self.device.id,
            'freight': self.freight.id,
            **{k: v for k, v in zip(['min_value', 'max_value'],
                                    sorted([fake.pyfloat(**limit_value_kwargs), fake.pyfloat(**limit_value_kwargs)]))}
        }
        not_existing_device = 10000000
        self.invalid_rule = {
            **self.valid_rule,
            'device': not_existing_device
        }
        self.rule_list_url = reverse('freights:rule-list', kwargs={'pk': self.freight.id})
コード例 #7
0
def create_state(limit_values=None, **fields):
    if not limit_values:
        min_value, max_value = sorted(
            [fake.pyfloat(right_digits=2),
             fake.pyfloat(right_digits=2)])
    else:
        min_value, max_value = limit_values

    fake_state_fields = {
        'value':
        fake.pyfloat(right_digits=2,
                     min_value=floor(min_value),
                     max_value=ceil(max_value)),
    }

    if not fields.get('rule'):
        fields['rule'] = create_rule()

    for field, value in fake_state_fields.items():
        if field not in fields:
            fields[field] = value

    return State.objects.create(**fields)
コード例 #8
0
 def setUp(self) -> None:
     self.freight = create_freight(need_transfer=True)
     self.rule = create_rule(freight=self.freight)
     self.valid_state = {
         'value':
         fake.pyfloat(right_digits=2,
                      min_value=floor(self.rule.min_value),
                      max_value=ceil(self.rule.max_value)),
         'rule':
         self.rule.id,
     }
     invalid_value = fake.pyfloat(right_digits=2,
                                  min_value=ceil(
                                      self.rule.device.max_value))
     self.invalid_state = {
         'value': invalid_value,
         'rule': self.rule.id,
     }
     self.state_list_url = reverse('freights:state-list',
                                   kwargs={
                                       'freight_pk': self.freight.id,
                                       'rule_pk': self.rule.id
                                   })
コード例 #9
0
    def setUp(self) -> None:
        self.device = create_device()

        self.valid_partial_data = {
            'unit': fake.random_element([value for value in Device.Unit.values if value != self.device.unit]),
        }
        self.invalid_partial_data = {
            'unit': fake.sentence(1)
        }
        self.valid_data = {
            'name': self.device.name,
            'unit': self.device.unit,
            'prefix': self.device.prefix,
            'min_value': fake.pyfloat(right_digits=2, max_value=floor(self.device.max_value)),
            'max_value': self.device.max_value,
        }
        self.invalid_data = {
            **self.valid_data,
            'min_value': self.valid_data['max_value'],
            'max_value': self.valid_data['min_value'],
        }

        self.device_detail_url = reverse('devices:detail', kwargs={'pk': self.device.id})