# Path: APITaxi_models2/unittest/factories.py
# class TownFactory(BaseFactory):
#     class Meta:
#         model = Town
#         sqlalchemy_get_or_create = ['insee']
# 
#     name = 'Paris'
#     insee = '75056'
# 
#     # The MUTLIPOLYGON below is a simple square containing Paris. It has been
#     # generated with a WKT editor such as:
#     #
#     # http://arthur-e.github.io/Wicket/sandbox-gmaps3.html
#     # https://clydedacruz.github.io/openstreetmap-wkt-playground/
#     #
#     # To convert a POLYGON (as returned by these editors) to a MULTIPOLYGON (as
#     # expected by ZUPC.shape), run the following query from a postgis database:
#     #
#     # SELECT ST_AsText(ST_Multi(ST_GeomFromText('POLYGON((2.24......))')));
#     shape = 'MULTIPOLYGON(((2.24332732355285 48.9066360266329,2.42460173761535 48.9066360266329,2.42460173761535 48.8122203058303,2.24332732355285 48.8122203058303,2.24332732355285 48.9066360266329)))'
# 
#     class Params:
#         bordeaux = factory.Trait(
#             name='Bordeaux',
#             insee='33063',
#             shape='MULTIPOLYGON(((-0.686737474226045 44.9009485734125,-0.494476732038545 44.9009485734125,-0.494476732038545 44.7826391041975,-0.686737474226045 44.7826391041975,-0.686737474226045 44.9009485734125)))',
#         )
#         charenton = factory.Trait(
#             name='Charenton-le-Pont',
#             insee='94018',
#             shape='MULTIPOLYGON(((2.40222930908203 48.8296390842375,2.41973876953125 48.8241580560601,2.41939544677734 48.8177721847637,2.40119934082031 48.8211630141963,2.39235877990723 48.8267008756382,2.40222930908203 48.8296390842375)))',
#         )
# 
# 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 APITaxi_models2.unittest.factories import (
    TownFactory,
    ZUPCFactory,
)

    def test_ok(self, operateur, QueriesTracker):
        zupc = ZUPCFactory()
        zupc2 = ZUPCFactory(bordeaux=True)

        with QueriesTracker() as qtracker:
            resp = operateur.client.get('/zupc/live')
            # List permissions, SELECT zupc
            assert qtracker.count == 2

        assert resp.status_code == 200
        assert len(resp.json['data']) == 2
        assert resp.json['data'][0]['id'] in (zupc.zupc_id, zupc2.zupc_id)
        assert resp.json['data'][0]['nom'] in (zupc.nom, zupc2.nom)
        assert resp.json['data'][0]['stats'] == {
            'operators': {
                operateur.user.email: 0
            }
        }


class TestTownList:
    def test_anonymous(self, anonymous):
        resp = anonymous.client.get('/towns')
        assert resp.status_code == 401

    def test_ok(self, operateur, moteur, QueriesTracker):
        ZUPCFactory()
        ZUPCFactory(bordeaux=True)


# Path: APITaxi_models2/unittest/factories.py
# class TownFactory(BaseFactory):
#     class Meta:
#         model = Town
#         sqlalchemy_get_or_create = ['insee']
# 
#     name = 'Paris'
#     insee = '75056'
# 
#     # The MUTLIPOLYGON below is a simple square containing Paris. It has been
#     # generated with a WKT editor such as:
#     #
#     # http://arthur-e.github.io/Wicket/sandbox-gmaps3.html
#     # https://clydedacruz.github.io/openstreetmap-wkt-playground/
#     #
#     # To convert a POLYGON (as returned by these editors) to a MULTIPOLYGON (as
#     # expected by ZUPC.shape), run the following query from a postgis database:
#     #
#     # SELECT ST_AsText(ST_Multi(ST_GeomFromText('POLYGON((2.24......))')));
#     shape = 'MULTIPOLYGON(((2.24332732355285 48.9066360266329,2.42460173761535 48.9066360266329,2.42460173761535 48.8122203058303,2.24332732355285 48.8122203058303,2.24332732355285 48.9066360266329)))'
# 
#     class Params:
#         bordeaux = factory.Trait(
#             name='Bordeaux',
#             insee='33063',
#             shape='MULTIPOLYGON(((-0.686737474226045 44.9009485734125,-0.494476732038545 44.9009485734125,-0.494476732038545 44.7826391041975,-0.686737474226045 44.7826391041975,-0.686737474226045 44.9009485734125)))',
#         )
#         charenton = factory.Trait(
#             name='Charenton-le-Pont',
#             insee='94018',
#             shape='MULTIPOLYGON(((2.40222930908203 48.8296390842375,2.41973876953125 48.8241580560601,2.41939544677734 48.8177721847637,2.40119934082031 48.8211630141963,2.39235877990723 48.8267008756382,2.40222930908203 48.8296390842375)))',
#         )
# 
# 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_zupc.py
from APITaxi_models2.unittest.factories import (
    TownFactory,
    ZUPCFactory,
)


    def test_ok(self, operateur, QueriesTracker):
        zupc = ZUPCFactory()
        zupc2 = ZUPCFactory(bordeaux=True)

        with QueriesTracker() as qtracker:
            resp = operateur.client.get('/zupc/live')
            # List permissions, SELECT zupc
            assert qtracker.count == 2

        assert resp.status_code == 200
        assert len(resp.json['data']) == 2
        assert resp.json['data'][0]['id'] in (zupc.zupc_id, zupc2.zupc_id)
        assert resp.json['data'][0]['nom'] in (zupc.nom, zupc2.nom)
        assert resp.json['data'][0]['stats'] == {
            'operators': {
                operateur.user.email: 0
            }
        }


class TestTownList:
    def test_anonymous(self, anonymous):
        resp = anonymous.client.get('/towns')
        assert resp.status_code == 401

    def test_ok(self, operateur, moteur, QueriesTracker):
        ZUPCFactory()
        ZUPCFactory(bordeaux=True)


        with QueriesTracker() as qtracker: