def test_choices_multiple_repairs(self, db, sample_device, sample_repair): """" Check that the choices are populated correctly""" Repair.create(name="Akku", price=123, device=sample_device) repair_form = SelectRepairForm(sample_device) assert repair_form.repairs.choices == [(d.id, d) for d in sample_device.repairs] assert repair_form.color.choices == [(d.id, d) for d in sample_device.colors] assert len(repair_form.repairs.choices) == 2
def test_valid_submit_with_multiple_repairs(self, db, sample_device, sample_repair): """ Check that invalid or malformed submits are handled""" another_repair = Repair.create(name="Akku", price=123, device=sample_device) repair_form = SelectRepairForm(sample_device) repair_form.color.data = sample_device.colors[0].id repair_form.repairs.data = [sample_repair.id, another_repair.id] assert repair_form.validate()
def test_cost(self, sample_color, sample_repair, sample_shop): another_repair = Repair.create(name="dfgdfgdfgdfg", price=49, device=sample_repair.device) dto = Order.create(color=sample_color, shop=sample_shop, repairs=[sample_repair, another_repair], problem_description="Some Text") assert dto.total_cost_including_tax_and_discount == 108.2 assert dto.total_cost == 118 assert dto.taxes == 20.56 assert dto.discount == 9.8
def create_new_or_skip_existing( rep_name: str, device: Device, price: decimal.Decimal) -> typing.Optional[Repair]: repairs: typing.List[Repair] = list( filter(lambda rep: rep.name == rep_name, device.repairs)) if not repairs: return Repair.create(name=rep_name, device=device, price=price) # Repair does already exist: rep = repairs[0] if rep.price != price: flash(f"Updating {rep}: Setting price from {rep.price} to {price}") else: logger.info(f"Skipping {rep} because it exists.") return rep
def test_query_order_by(self, sample_device): # Create a display repair, a back-cover and a battery change Repair.create(device=sample_device, name="Display") Repair.create(device=sample_device, name="Back-cover") Repair.create(device=sample_device, name="Battery") Repair.normalize() reps = sample_device.repairs assert reps[0].order_index == 0 assert reps[1].order_index == 1 assert reps[2].order_index == 2
def test_deserialize(self, sample_color, sample_repair, sample_shop): another_repair = Repair.create(name="dfgdfgdfgdfg", price=69, device=sample_repair.device) dto = Order.create(color=sample_color, shop=sample_shop, problem_description="Some Text") dto.append_repairs(repairs=[sample_repair, another_repair]) assert dto.color == sample_color assert dto.repairs == [sample_repair, another_repair] assert dto.problem_description == "Some Text" serialized = dto.serialize() assert serialized == { 'order_id': dto.id, } deserialized = Order.deserialize(serialized) assert dto == deserialized
def test_move_down(self, some_devices): for device in some_devices: # Create a display repair, a back-cover and a battery change Repair.create(device=device, name="Display") Repair.create(device=device, name="Back-cover") Repair.create(device=device, name="Battery") for i, rep in enumerate(some_devices[0].repairs): rep.order_index = i rep.save() device = some_devices[0] # If I move the last repair one down, it should be swapped with the below original = device.repairs device.repairs[0].move_down() now = device.repairs assert original[0] == now[1] assert original[1] == now[0] assert original[2] == now[2] # If I move it one more down, it should be swapped with the elem below device.repairs[1].move_down() now = device.repairs assert original[0] == now[2] assert original[1] == now[0] assert original[2] == now[1] # But if I move it up again nothing should happen device.repairs[-1].move_down() device.repairs[-1].move_down() device.repairs[-1].move_down() device.repairs[-1].move_down() assert device.repairs[-1].order_index == 2 # All other repairs should remain unchanged assert all(rep.order_index == 0 for rep in Repair.query.all() if rep.device != device)
def test_normalize(self, some_devices): for device in some_devices: # Create a display repair, a back-cover and a battery change Repair.create(device=device, name="Display") Repair.create(device=device, name="Back-cover") Repair.create(device=device, name="Battery") # Make sure all repairs were actually created assert Repair.query.count() == len(some_devices) * 3 assert all([r.order_index == 0 for r in Repair.query.all()]) Repair.normalize() repairs = Repair.query.all() # Normalize repairs should sort the repairs for every device # There should only be order indices from 0 to 2 for rep in repairs: assert rep.order_index < 3 assert rep.order_index >= 0 # There should be an even amount of 0,1 and 2 occurrences = {0: 0, 1: 0, 2: 0} for rep in repairs: occurrences[rep.order_index] += 1 assert occurrences[0] == len(some_devices) assert occurrences[1] == len(some_devices) assert occurrences[2] == len(some_devices) # For every device there should be a repair with the idx of 0 and 1 and 2 for dev in some_devices: reps = Repair.query.filter(Repair.device_id == dev.id).order_by( asc(Repair.order_index)) assert reps[0].order_index == 0 assert reps[1].order_index == 1 assert reps[2].order_index == 2
def another_repair(another_device): return Repair.create(name="Battery", price=49, device=another_device)
def sample_repair(sample_device): """ Return a sample repair """ return Repair.create(name="Display", price=69, device=sample_device)