Beispiel #1
0
    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
Beispiel #2
0
    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