# Path: APITaxi_models2/unittest/factories.py
# class ADSFactory(BaseFactory):
#     class Meta:
#         model = ADS
# 
#     numero = factory.Sequence(lambda n: 'ads_number_%d' % n)
#     added_at = factory.LazyFunction(datetime.datetime.now)
#     added_by = factory.SubFactory(UserFactory)
#     insee = '75056'
#     vehicle = factory.SubFactory(VehicleFactory, descriptions__added_by=factory.SelfAttribute('...added_by'))
#     owner_type = 'individual'
#     owner_name = 'Owner ADS'
#     category = ''
#     added_via = 'api'
#     source = 'added_by'
# 
# 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)]
# 
# class VehicleDescriptionFactory(BaseFactory):
#     class Meta:
#         model = VehicleDescription
# 
#     # Circular dependencies require to provide the full import path of the
#     # target.
#     vehicle = factory.SubFactory(__name__ + '.VehicleFactory')
#     constructor = 'Citroën'
#     model = 'C4 PICASSO'
#     added_at = factory.LazyFunction(datetime.datetime.now)
#     added_by = factory.SubFactory(UserFactory)
#     added_via = 'api'
#     source = 'added_by'
#     status = 'free'
# 
# class ZUPCFactory(BaseFactory):
#     class Meta:
#         model = ZUPC
#         sqlalchemy_get_or_create = ['nom']
# 
#     nom = 'Paris'
# 
#     zupc_id = factory.LazyAttribute(lambda o: str(uuid.uuid4()))
#     allowed = factory.LazyAttribute(lambda o: [TownFactory(), TownFactory(charenton=True)])
# 
#     class Params:
#         bordeaux = factory.Trait(
#             nom='Bordeaux',
#             allowed=factory.LazyAttribute(lambda o: [TownFactory(bordeaux=True)])
#         )


from unittest import mock
from APITaxi_models2 import db, ADS
from APITaxi_models2.unittest.factories import (
    ADSFactory,
    VehicleFactory,
    VehicleDescriptionFactory,
    ZUPCFactory
)
import psycopg2.errors
        ZUPCFactory()
        insee = '75056'

        with QueriesTracker() as qtracker:
            resp = operateur.client.post('/ads', json={'data': [{
                'numero': '1337',
                'insee': insee,
                'doublage': True,
                'owner_type': 'individual',
                'owner_name': 'Roger Federer',
                'category': 'category',
                'vehicle_id': vehicle.id
            }]})
            # SELECT permissions, SELECT vehicle, SELECT ZUPC corresponding to
            # insee, SELECT existing ADS, INSERT ADS
            assert qtracker.count == 5

        assert resp.status_code == 201
        assert resp.json['data'][0]['numero'] == '1337'
        assert resp.json['data'][0]['insee'] == insee
        assert resp.json['data'][0]['doublage'] is True
        assert resp.json['data'][0]['owner_type'] == 'individual'
        assert resp.json['data'][0]['owner_name'] == 'Roger Federer'
        assert resp.json['data'][0]['category'] == 'category'
        assert resp.json['data'][0]['vehicle_id'] == vehicle.id

        assert ADS.query.count() == 1

    def test_unique_violation(self, operateur):
        vehicle = VehicleFactory(descriptions=[])

# Path: APITaxi_models2/unittest/factories.py
# class ADSFactory(BaseFactory):
#     class Meta:
#         model = ADS
# 
#     numero = factory.Sequence(lambda n: 'ads_number_%d' % n)
#     added_at = factory.LazyFunction(datetime.datetime.now)
#     added_by = factory.SubFactory(UserFactory)
#     insee = '75056'
#     vehicle = factory.SubFactory(VehicleFactory, descriptions__added_by=factory.SelfAttribute('...added_by'))
#     owner_type = 'individual'
#     owner_name = 'Owner ADS'
#     category = ''
#     added_via = 'api'
#     source = 'added_by'
# 
# 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)]
# 
# class VehicleDescriptionFactory(BaseFactory):
#     class Meta:
#         model = VehicleDescription
# 
#     # Circular dependencies require to provide the full import path of the
#     # target.
#     vehicle = factory.SubFactory(__name__ + '.VehicleFactory')
#     constructor = 'Citroën'
#     model = 'C4 PICASSO'
#     added_at = factory.LazyFunction(datetime.datetime.now)
#     added_by = factory.SubFactory(UserFactory)
#     added_via = 'api'
#     source = 'added_by'
#     status = 'free'
# 
# class ZUPCFactory(BaseFactory):
#     class Meta:
#         model = ZUPC
#         sqlalchemy_get_or_create = ['nom']
# 
#     nom = 'Paris'
# 
#     zupc_id = factory.LazyAttribute(lambda o: str(uuid.uuid4()))
#     allowed = factory.LazyAttribute(lambda o: [TownFactory(), TownFactory(charenton=True)])
# 
#     class Params:
#         bordeaux = factory.Trait(
#             nom='Bordeaux',
#             allowed=factory.LazyAttribute(lambda o: [TownFactory(bordeaux=True)])
#         )


# Path: APITaxi2/tests/test_ads.py
from unittest import mock
from APITaxi_models2 import db, ADS
from APITaxi_models2.unittest.factories import (
    ADSFactory,
    VehicleFactory,
    VehicleDescriptionFactory,
    ZUPCFactory
)
import psycopg2.errors

        ZUPCFactory()
        insee = '75056'

        with QueriesTracker() as qtracker:
            resp = operateur.client.post('/ads', json={'data': [{
                'numero': '1337',
                'insee': insee,
                'doublage': True,
                'owner_type': 'individual',
                'owner_name': 'Roger Federer',
                'category': 'category',
                'vehicle_id': vehicle.id
            }]})
            # SELECT permissions, SELECT vehicle, SELECT ZUPC corresponding to
            # insee, SELECT existing ADS, INSERT ADS
            assert qtracker.count == 5

        assert resp.status_code == 201
        assert resp.json['data'][0]['numero'] == '1337'
        assert resp.json['data'][0]['insee'] == insee
        assert resp.json['data'][0]['doublage'] is True
        assert resp.json['data'][0]['owner_type'] == 'individual'
        assert resp.json['data'][0]['owner_name'] == 'Roger Federer'
        assert resp.json['data'][0]['category'] == 'category'
        assert resp.json['data'][0]['vehicle_id'] == vehicle.id

        assert ADS.query.count() == 1

    def test_unique_violation(self, operateur):
        vehicle = VehicleFactory(descriptions=[])

        VehicleDescriptionFactory(vehicle=vehicle, added_by=operateur.user)