# Path: APITaxi_models2/unittest/factories.py
# class VehicleFactory(BaseFactory):
#     class Meta:
#         model = Vehicle
# 
#     licence_plate = factory.Sequence(lambda n: "AB-%03d-CD" % n)
# 
#     @factory.post_generation
#     def descriptions(self, create, extracted, **kwargs):
#         """Create a VehicleDescription for this Vehicle."""
#         if not create or extracted is not None:
#             return extracted
#         return [VehicleDescriptionFactory(vehicle=self, **kwargs)]


from sqlalchemy.orm import joinedload
from APITaxi_models2 import Vehicle, VehicleDescription
from APITaxi_models2.unittest.factories import VehicleFactory


class TestVehiclePost:
    def test_invalid(self, anonymous, moteur, operateur):
        # Login required
        resp = anonymous.client.post('/vehicles', json={})
        assert resp.status_code == 401

        resp = moteur.client.post('/vehicles', json={})
        assert resp.status_code == 403

        resp = operateur.client.post('/vehicles', json={'data': [{}]})
        assert resp.status_code == 400
        # The only required key is "licence_plate"
        assert list(resp.json['errors']['data']['0'].keys()) == ['licence_plate']

        # but it must not be empty either way
        resp = operateur.client.post('/vehicles', json={'data': [{
            'licence_plate': ""
        }]})
        assert resp.status_code == 400
        assert list(resp.json['errors']['data']['0']) == ['licence_plate']

    def test_ok(self, operateur, admin, QueriesTracker):
        with QueriesTracker() as qtracker:
            resp = operateur.client.post('/vehicles', json={
                'data': [{
                    'licence_plate': 'licence1',
                    'model_year': 1938,
                    'engine': 'v12',

# Path: APITaxi_models2/unittest/factories.py
# class VehicleFactory(BaseFactory):
#     class Meta:
#         model = Vehicle
# 
#     licence_plate = factory.Sequence(lambda n: "AB-%03d-CD" % n)
# 
#     @factory.post_generation
#     def descriptions(self, create, extracted, **kwargs):
#         """Create a VehicleDescription for this Vehicle."""
#         if not create or extracted is not None:
#             return extracted
#         return [VehicleDescriptionFactory(vehicle=self, **kwargs)]


# Path: APITaxi2/tests/test_vehicles.py
from sqlalchemy.orm import joinedload
from APITaxi_models2 import Vehicle, VehicleDescription
from APITaxi_models2.unittest.factories import VehicleFactory



class TestVehiclePost:
    def test_invalid(self, anonymous, moteur, operateur):
        # Login required
        resp = anonymous.client.post('/vehicles', json={})
        assert resp.status_code == 401

        resp = moteur.client.post('/vehicles', json={})
        assert resp.status_code == 403

        resp = operateur.client.post('/vehicles', json={'data': [{}]})
        assert resp.status_code == 400
        # The only required key is "licence_plate"
        assert list(resp.json['errors']['data']['0'].keys()) == ['licence_plate']

        # but it must not be empty either way
        resp = operateur.client.post('/vehicles', json={'data': [{
            'licence_plate': ""
        }]})
        assert resp.status_code == 400
        assert list(resp.json['errors']['data']['0']) == ['licence_plate']

    def test_ok(self, operateur, admin, QueriesTracker):
        with QueriesTracker() as qtracker:
            resp = operateur.client.post('/vehicles', json={
                'data': [{
                    'licence_plate': 'licence1',
                    'model_year': 1938,
                    'engine': 'v12',

                    'horse_power': 42.2,