Esempio n. 1
0
class CreateIndexBaseTest(DatabaseTest):
    """
    Create Elasticsearch and DB content for the unit tests.
    """
    def setUp(self, *args, **kwargs):
        super(CreateIndexBaseTest, self).setUp(*args, **kwargs)

        # Create 1 office.
        self.office = Office(
            siret=u"78548035101646",
            company_name=u"SUPERMARCHES MATCH",
            office_name=u"SUPERMARCHES MATCH",
            naf=u"4711D",
            street_number=u"45",
            street_name=u"AVENUE ANDRE MALRAUX",
            city_code=u"57463",
            zipcode=u"57000",
            email=u"*****@*****.**",
            tel=u"0387787878",
            website=u"http://www.supermarchesmatch.fr",
            flag_alternance=0,
            flag_junior=0,
            flag_senior=0,
            flag_handicap=0,
            departement=u"57",
            headcount=u"12",
            score=50,
            x=6.17952,
            y=49.1044,
        )
        self.office.save()

        # We should have 1 office in the DB.
        self.assertEquals(Office.query.count(), 1)

        # Put the office into ES.
        script.create_offices(index=self.ES_TEST_INDEX)
        time.sleep(1)  # Sleep required by ES to register new documents.

        # We should have 1 office in the ES.
        count = self.es.count(index=self.ES_TEST_INDEX,
                              doc_type=self.ES_OFFICE_TYPE,
                              body={'query': {
                                  'match_all': {}
                              }})
        self.assertEquals(count['count'], 1)
        # Ensure that the office is the one that has been indexed in ES.
        res = self.es.get(index=self.ES_TEST_INDEX,
                          doc_type=self.ES_OFFICE_TYPE,
                          id=self.office.siret)
        self.assertEquals(res['_source']['email'], self.office.email)

    def tearDown(self):
        script.drop_and_create_index(index=self.ES_TEST_INDEX)
        return super(CreateIndexBaseTest, self).tearDown()
Esempio n. 2
0
def make_geocoded_office():
    office = Office(
        siret=1234,
        street_number="30",
        street_name="rue Edouard Poisson",
        zipcode="93300",
        city_code="93001",
        x=1.1,
        y=1.1,
        headcount="11",
    )
    office.save()
Esempio n. 3
0
class CreateIndexBaseTest(DatabaseTest):
    """
    Create Elasticsearch and DB content for the unit tests.
    """
    def setUp(self):
        super(CreateIndexBaseTest, self).setUp()

        # Mute script logging
        script.logger.setLevel(script.logging.CRITICAL)

        self.office1 = Office(
            siret="78548035101646",
            company_name="SUPERMARCHES MATCH",
            office_name="SUPERMARCHES MATCH",
            naf="4711D",
            street_number="45",
            street_name="AVENUE ANDRE MALRAUX",
            city_code="57463",
            zipcode="57000",
            email="*****@*****.**",
            tel="0387787878",
            website="http://www.supermarchesmatch.fr",
            flag_alternance=0,
            flag_junior=0,
            flag_senior=0,
            flag_handicap=0,
            departement="57",
            headcount="12",
            score=90,
            score_alternance=90,
            x=6.17952,
            y=49.1044,
        )
        self.office1.save()

        self.office2 = Office(
            siret="78548035101647",
            company_name="HYPER U",
            office_name="HYPER U",
            naf="4711D",
            street_number="8",
            street_name="AVENUE DE LA LIBERATION",
            city_code="44101",
            zipcode="44620",
            email="*****@*****.**",
            tel="0240659515",
            website="http://www.hyper-u-lamontagne.fr",
            flag_alternance=0,
            flag_junior=0,
            flag_senior=0,
            flag_handicap=0,
            departement="44",
            headcount="21",
            score=77,
            score_alternance=75,
            x=-1.68333,
            y=47.183331,
        )
        self.office2.save()

        # We should have 2 offices in the DB.
        self.assertEqual(Office.query.count(), 2)

        # Put offices into ES.
        # Disable parallel computing because it does not play well with test environment (it hangs).
        script.create_offices(disable_parallel_computing=True)
        self.es.indices.flush(index=settings.ES_INDEX
                              )  # required by ES to register new documents.

        # We should have 3 offices in ES (2 + the fake office).
        count = self.es.count(index=settings.ES_INDEX,
                              doc_type=es.OFFICE_TYPE,
                              body={'query': {
                                  'match_all': {}
                              }})
        self.assertEqual(count['count'], 2 + 1)
        # Ensure that the office is the one that has been indexed in ES.
        res = self.es.get(index=settings.ES_INDEX,
                          doc_type=es.OFFICE_TYPE,
                          id=self.office1.siret)
        self.assertEqual(res['_source']['email'], self.office1.email)
        res = self.es.get(index=settings.ES_INDEX,
                          doc_type=es.OFFICE_TYPE,
                          id=self.office2.siret)
        self.assertEqual(res['_source']['email'], self.office2.email)
Esempio n. 4
0
    def setUp(self, *args, **kwargs):
        super(ApiBaseTest, self).setUp(*args, **kwargs)

        # Insert test data into Elasticsearch.
        docs = [
            {
                'naf':
                '7320Z',  # Map to ROME D1405.
                'siret':
                '00000000000001',
                'company_name':
                'Raison sociale 1',
                'score':
                68,
                'score_alternance':
                18,
                'headcount':
                11,
                'locations':
                self.positions['bayonville_sur_mad']['coords'],
                'name':
                'Office 1',
                'flag_alternance':
                0,
                'flag_pmsmp':
                0,
                'department':
                self.positions['bayonville_sur_mad']['zip_code'][0:2],
            },
            {
                'naf':
                '7320Z',  # Map to ROME D1405.
                'siret':
                '00000000000002',
                'company_name':
                'Raison sociale 2',
                'score':
                69,
                'score_alternance':
                18,
                'headcount':
                31,
                'locations':
                self.positions['bayonville_sur_mad']['coords'],
                'name':
                'Office 2',
                'flag_alternance':
                0,
                'flag_pmsmp':
                0,
                'department':
                self.positions['bayonville_sur_mad']['zip_code'][0:2],
            },
            {
                'naf':
                '7320Z',  # Map to ROME D1405.
                'siret':
                '00000000000003',
                'score':
                70,
                'score_alternance':
                18,
                'headcount':
                31,
                'locations':
                self.positions['bayonville_sur_mad']['coords'],
                'name':
                'Office 3',
                'flag_alternance':
                0,
                'flag_pmsmp':
                0,
                'department':
                self.positions['bayonville_sur_mad']['zip_code'][0:2],
            },
            {
                'naf': '7320Z',  # Map to ROME D1405.
                'siret': '00000000000004',
                'score': 71,
                'score_alternance': 18,
                'headcount': 31,
                'locations': self.positions['caen']['coords'],
                'name': 'Office 4',
                'flag_alternance': 0,
                'flag_pmsmp': 0,
                'department': self.positions['caen']['zip_code'][0:2],
            },
            {
                'naf': '9511Z',  # Map to ROME M1801.
                'siret': '00000000000005',
                'score': 71,
                'score_alternance': 18,
                'headcount': 31,
                'locations': self.positions['caen']['coords'],
                'name': 'Office 5',
                'flag_alternance': 0,
                'flag_pmsmp': 0,
                'department': self.positions['caen']['zip_code'][0:2],
            },
            # For NAF filter
            {
                'naf': '4711C',  # Map to ROME D1508.
                'siret': '00000000000006',
                'score': 75,
                'score_alternance': 18,
                'headcount': 31,
                'locations': self.positions['metz']['coords'],
                'name': 'Office 6',
                'flag_alternance': 0,
                'flag_pmsmp': 0,
                'department': self.positions['metz']['zip_code'][0:2],
            },
            {
                'naf': '5610C',  # Map to ROME D1508.
                'siret': '00000000000007',
                'score': 70,
                'score_alternance': 18,
                'headcount': 50,
                'locations': self.positions['metz']['coords'],
                'name': 'Office 7',
                'flag_alternance': 0,
                'flag_pmsmp': 0,
                'department': self.positions['metz']['zip_code'][0:2],
            },
            # For result sort
            {
                'naf': '9103Z',  # Map to ROME D1211
                'siret': '00000000000008',
                'score': 75,
                'score_alternance': 51,
                'headcount': 50,
                'locations': self.positions['nantes']['coords'],
                'name': 'Office 8',
                'flag_alternance': 0,
                'flag_pmsmp': 0,
                'department': self.positions['nantes']['zip_code'][0:2],
            },
            {
                'naf': '7010Z',  # Map to ROME D1211
                'siret': '00000000000009',
                'score': 99,
                'score_alternance': 51,
                'headcount': 50,
                'locations':
                self.positions['reze']['coords'],  # City close to Nantes
                'name': 'Office 9',
                'flag_alternance': 0,
                'flag_pmsmp': 0,
                'department': self.positions['reze']['zip_code'][0:2],
            },
            # For contract filter
            {
                'naf': '4669A',  # Map to Rome D1213
                'siret': '00000000000010',
                'score': 78,
                'score_alternance': 0,
                'headcount': 34,
                'locations': self.positions['lille']['coords'],
                'name': 'Office 10',
                'flag_alternance': 0,
                'flag_pmsmp': 0,
                'department': self.positions['lille']['zip_code'][0:2],
            },
            {
                'naf': '4669A',  # Map to Rome D1213
                'siret': '00000000000011',
                'score': 82,
                'score_alternance': 80,
                'headcount': 65,
                'locations': self.positions['lille']['coords'],
                'name': 'Office 11',
                'flag_alternance': 1,
                'flag_pmsmp': 0,
                'department': self.positions['lille']['zip_code'][0:2],
            },
            # For headcount filter
            {
                'naf': '7022Z',  # Map to Rome M1202
                'siret': '00000000000012',
                'score': 82,
                'score_alternance': 18,
                'headcount': 11,
                'locations': self.positions['toulouse']['coords'],
                'name': 'Office 12',
                'flag_alternance': 0,
                'flag_pmsmp': 0,
                'department': self.positions['toulouse']['zip_code'][0:2],
            },
            {
                'naf': '7010Z',  # Map to Rome M1202
                'siret': '00000000000013',
                'score': 82,
                'score_alternance': 18,
                'headcount': 22,
                'locations': self.positions['toulouse']['coords'],
                'name': 'Office 13',
                'flag_alternance': 0,
                'flag_pmsmp': 0,
                'department': self.positions['toulouse']['zip_code'][0:2],
            },
            # For headcount_text
            {
                'naf': '4648Z',  # Map to Rome B1603
                'siret': '00000000000014',
                'score': 80,
                'score_alternance': 18,
                'headcount': 53,  # headcount_text : '10 000 salariés et plus'
                'locations': self.positions['pau']['coords'],
                'name': 'Office 14',
                'flag_alternance': 0,
                'flag_pmsmp': 0,
                'department': self.positions['pau']['zip_code'][0:2],
            },
            # For flag_alternance in response
            {
                'naf': '4648Z',  # Map to Rome B1603
                'siret': '00000000000015',
                'score': 80,
                'score_alternance': 18,
                'headcount': 53,
                'locations': self.positions['poitiers']['coords'],
                'name': 'Office 15',
                'flag_alternance': 0,
                'flag_pmsmp': 0,
                'department': self.positions['poitiers']['zip_code'][0:2],
            },
            {
                'naf': '4648Z',  # Map to Rome B1603
                'siret': '00000000000016',
                'score': 70,
                'score_alternance': 80,
                'headcount': 53,
                'locations': self.positions['poitiers']['coords'],
                'name': 'Office 16',
                'flag_alternance': 1,
                'flag_pmsmp': 0,
                'department': self.positions['poitiers']['zip_code'][0:2],
            },
            # For filter_by_department and filter_by_flag_pmsmp
            {
                'naf': '5229A',  # Map to Rome N1202
                'siret': '00000000000017',
                'score': 90,
                'score_alternance': 18,
                'headcount': 53,
                'locations': self.positions['paris']['coords'],
                'name': 'Office 17',
                'flag_alternance': 0,
                'flag_pmsmp': 1,
                'department': self.positions['paris']['zip_code'][0:2],
            },
            {
                'naf': '5229A',  # Map to Rome N1202
                'siret': '00000000000018',
                'score': 78,
                'score_alternance': 18,
                'headcount': 53,
                'locations': self.positions['neuilly-sur-seine']['coords'],
                'name': 'Office 18',
                'flag_alternance': 0,
                'flag_pmsmp': 0,
                'department':
                self.positions['neuilly-sur-seine']['zip_code'][0:2],
            },
            # For filters in response tests
            {
                'naf': '4910Z',  # Map to Rome N4403
                'siret': '00000000000019',
                'score': 76,
                'score_alternance': 18,
                'headcount': 0o1,
                'locations': self.positions['toulon']['coords'],
                'name': 'Office 19',
                'flag_alternance': 0,
                'flag_pmsmp': 0,
                'department': self.positions['toulon']['zip_code'][0:2],
            },
            {
                'naf': '4910Z',  # Map to Rome N4403
                'siret': '00000000000020',
                'score': 90,
                'score_alternance': 18,
                'headcount': 0o3,
                'locations': self.positions['toulon']['coords'],
                'name': 'Office 20',
                'flag_alternance': 0,
                'flag_pmsmp': 0,
                'department': self.positions['toulon']['zip_code'][0:2],
            },
            {
                'naf': '4920Z',  # Map to Rome N4403
                'siret': '00000000000021',
                'score': 43,
                'score_alternance': 18,
                'headcount': 53,
                'locations': self.positions['toulon']['coords'],
                'name': 'Office 21',
                'flag_alternance': 1,
                'flag_pmsmp': 0,
                'department': self.positions['toulon']['zip_code'][0:2],
            },
            # For distance filter => between 10-30km
            {
                'naf': '4910Z',  # Map to Rome N4403
                'siret': '00000000000023',
                'score': 89,
                'score_alternance': 18,
                'headcount': 31,
                'locations':
                self.positions['hyeres']['coords'],  # 15km of Toulon
                'name': 'Office 23',
                'flag_alternance': 0,
                'flag_pmsmp': 0,
                'department': self.positions['hyeres']['zip_code'][0:2],
            },
            # For distance filter => between 30-50km
            {
                'naf': '4910Z',  # Map to Rome N4403
                'siret': '00000000000024',
                'score': 30,
                'score_alternance': 18,
                'headcount': 12,
                'locations':
                self.positions['aubagne']['coords'],  # 35km of Toulon
                'name': 'Office 24',
                'flag_alternance': 0,
                'flag_pmsmp': 0,
                'department': self.positions['aubagne']['zip_code'][0:2],
            },
            # For distance between 50-100km
            {
                'naf': '4910Z',  # Map to Rome N4403
                'siret': '00000000000025',
                'score': 82,
                'score_alternance': 18,
                'headcount': 11,
                'locations':
                self.positions['draguignan']['coords'],  # 60km of Toulon
                'name': 'Office 25',
                'flag_alternance': 0,
                'flag_pmsmp': 0,
                'department': self.positions['draguignan']['zip_code'][0:2],
            },
            # For distance filter => between 100-3000km
            {
                'naf': '4910Z',  # Map to Rome N4403
                'siret': '00000000000026',
                'score': 67,
                'score_alternance': 18,
                'headcount': 51,
                'locations':
                self.positions['limoges']['coords'],  # 500km of Toulon
                'name': 'Office 26',
                'flag_alternance': 0,
                'flag_pmsmp': 0,
                'department': self.positions['limoges']['zip_code'][0:2],
            },
        ]
        for _, doc in enumerate(docs, start=1):
            # Build scores for relevant ROME codes.
            naf = doc['naf']
            rome_codes = list(mapping_util.MANUAL_NAF_ROME_MAPPING[naf].keys())

            # FIXME this is some dangerous code duplication with create_index, we should someday
            # make it more DNRY.
            score = doc['score']
            scores_by_rome = {}
            for rome_code in rome_codes:
                scores_by_rome[
                    rome_code] = scoring_util.get_score_adjusted_to_rome_code_and_naf_code(
                        score=score,
                        rome_code=rome_code,
                        naf_code=naf,
                    )
            if scores_by_rome:
                doc['scores_by_rome'] = scores_by_rome

            # FIXME this is some dangerous code duplication with create_index, we should someday
            # make it more DNRY.
            score_alternance = doc['score_alternance']
            scores_alternance_by_rome = {}
            for rome_code in rome_codes:
                raw_score = scoring_util.get_score_adjusted_to_rome_code_and_naf_code(
                    score=score_alternance,
                    rome_code=rome_code,
                    naf_code=naf,
                )
                if raw_score > 0:  # dirty fix until proper DNRY with create_index
                    scores_alternance_by_rome[rome_code] = raw_score
            if scores_alternance_by_rome:
                doc['scores_alternance_by_rome'] = scores_alternance_by_rome

            # just like in other environments, id should be the siret
            self.es.index(index=settings.ES_INDEX,
                          doc_type=es.OFFICE_TYPE,
                          id=doc['siret'],
                          body=doc)

        # need for ES to register our new documents, flaky test here otherwise
        self.es.indices.flush(index=settings.ES_INDEX)

        # Create related Office instances into MariaDB/MySQL.
        for doc in docs:

            # Set the right `commune_id` and `zipcode` depending on the location.
            commune_id = None
            zip_code = None
            for position in self.positions:
                if doc['locations'] == self.positions[position]['coords']:
                    commune_id = self.positions[position]['commune_id']
                    zip_code = self.positions[position]['zip_code']
                    break

            if not commune_id:
                raise ValueError(
                    "Cannot create an entry in Office with a city absent from self.positions."
                )

            office = Office(
                office_name=doc['name'],
                siret=doc['siret'],
                score=doc['score'],
                score_alternance=doc['score_alternance'],
                naf=doc['naf'],
                city_code=commune_id,
                zipcode=zip_code,
                email='*****@*****.**',
                departement=zip_code[:2],
                company_name=doc['company_name']
                if 'company_name' in doc else '',
                flag_alternance=doc['flag_alternance'],
                flag_pmsmp=doc['flag_pmsmp'],
                headcount=doc['headcount'],
                x=doc['locations'][0]['lon'],
                y=doc['locations'][0]['lat'],
            )
            office.save()

        # We should have as much entries in MariaDB/MySQL than in Elasticsearch, except
        # one more in ES for the fake document actually.
        es_count = self.es.count(index=settings.ES_INDEX,
                                 doc_type=es.OFFICE_TYPE,
                                 body={'query': {
                                     'match_all': {}
                                 }})
        self.assertEqual(Office.query.count() + 1, es_count['count'])
Esempio n. 5
0
    def setUp(self, *args, **kwargs):
        super(FavoriteBaseTest, self).setUp(*args, **kwargs)

        # Create a user.
        self.user = User.create(email=u'*****@*****.**', gender=u'male', first_name=u'John', last_name=u'Doe')

        # Delete index.
        self.es.indices.delete(index=self.ES_TEST_INDEX)

        # Create new index.
        request_body = {
            "mappings": {
                "office": {
                    "properties": {
                        "naf": {
                            "type": "string",
                            "index": "not_analyzed"
                        },
                        "siret": {
                            "type": "string",
                            "index": "not_analyzed"
                        },
                        "name": {
                            "type": "string",
                            "index": "not_analyzed"
                        },
                        "score": {
                            "type": "integer",
                            "index": "not_analyzed"
                        },
                        "headcount": {
                            "type": "integer",
                            "index": "not_analyzed"
                        },
                        "location": {
                            "type": "geo_point",
                        }
                    }
                }
            }
        }
        self.es.indices.create(index=self.ES_TEST_INDEX, body=request_body)

        # Insert test data into Elasticsearch.
        docs = [
            {
                'naf': u'4711F',
                'siret': u'00000000000001',
                'score': 98,
                'headcount': 32,
                'location': self.positions['metz']['location'],
                'name': u'Centre Distributeur E.Leclerc',
            },
            {
                'naf': u'4722Z',
                'siret': u'00000000000002',
                'score': 98,
                'headcount': 12,
                'location': self.positions['metz']['location'],
                'name': u'Maison Nicolas',
            },
            {
                'naf': u'4711F',
                'siret': u'00000000000003',
                'score': 98,
                'headcount': 32,
                'location': self.positions['paris']['location'],
                'name': u'Carrefour Paris',
            },
            {
                'naf': u'4722Z',
                'siret': u'00000000000004',
                'score': 98,
                'headcount': 84,
                'location': self.positions['paris']['location'],
                'name': u'Maistre Mathieu',
            },
        ]
        for i, doc in enumerate(docs, start=1):
            self.es.index(index=self.ES_TEST_INDEX, doc_type=self.ES_OFFICE_TYPE, id=i, body=doc)

        # Sleep required by ES to register new documents, flaky test here otherwise.
        time.sleep(1)

        # Create related Office instances into MariaDB/MySQL.
        for doc in docs:

            # Set the right `commune_id` and `zipcode` depending on the location.
            commune_id = None
            zip_code = None
            commune_id = None
            for position in self.positions:
                if doc['location'] == self.positions[position]['location']:
                    commune_id = self.positions[position]['commune_id']
                    zip_code = self.positions[position]['zip_code']
                    break

            if not commune_id:
                raise ValueError("Cannot create an entry in Office with a city absent from self.positions.")

            office = Office(
                company_name=doc['name'],
                siret=doc['siret'],
                score=doc['score'],
                naf=doc['naf'],
                city_code=commune_id,
                zipcode=zip_code,
                email=u'*****@*****.**',
                departement=zip_code[:2],
                x=doc['location']['lon'],
                y=doc['location']['lat'],
            )
            office.save()

        # We should have as much entries in MariaDB/MySQL than in Elasticsearch.
        self.assertEquals(Office.query.count(), len(docs))
Esempio n. 6
0
    def setUp(self, *args, **kwargs):
        super(ApiBaseTest, self).setUp(*args, **kwargs)

        # Delete index.
        self.es.indices.delete(index=self.ES_TEST_INDEX, ignore=[404])

        # Create new index.
        request_body = {
            "mappings": {
                "office": {
                    "properties": {
                        "naf": {
                            "type": "string",
                            "index": "not_analyzed"
                        },
                        "siret": {
                            "type": "string",
                            "index": "not_analyzed"
                        },
                        "name": {
                            "type": "string",
                            "index": "not_analyzed"
                        },
                        "score": {
                            "type": "integer",
                            "index": "not_analyzed"
                        },
                        "headcount": {
                            "type": "integer",
                            "index": "not_analyzed"
                        },
                        "location": {
                            "type": "geo_point",
                        }
                    }
                }
            }
        }

        for rome_code in settings.ROME_DESCRIPTIONS.keys():
            request_body["mappings"]["office"]["properties"][
                "score_for_rome_%s" % rome_code] = {
                    "type": "integer",
                    "index": "not_analyzed"
                }
        self.es.indices.create(index=self.ES_TEST_INDEX, body=request_body)

        # Insert test data into Elasticsearch.
        docs = [
            {
                'naf': u'7320Z',  # Map to ROME D1405.
                'siret': u'00000000000001',
                'score': 68,
                'headcount': 11,
                'location': self.positions['bayonville_sur_mad']['location'],
                'name': u'Office 1',
            },
            {
                'naf': u'7320Z',  # Map to ROME D1405.
                'siret': u'00000000000002',
                'score': 69,
                'headcount': 31,
                'location': self.positions['bayonville_sur_mad']['location'],
                'name': u'Office 2',
            },
            {
                'naf': u'7320Z',  # Map to ROME D1405.
                'siret': u'00000000000003',
                'score': 70,
                'headcount': 31,
                'location': self.positions['bayonville_sur_mad']['location'],
                'name': u'Office 3',
            },
            {
                'naf': u'7320Z',  # Map to ROME D1405.
                'siret': u'00000000000004',
                'score': 71,
                'headcount': 31,
                'location': self.positions['caen']['location'],
                'name': u'Office 4',
            },
            {
                'naf': u'9511Z',  # Map to ROME M1801.
                'siret': u'00000000000005',
                'score': 71,
                'headcount': 31,
                'location': self.positions['caen']['location'],
                'name': u'Office 5',
            },
        ]
        for i, doc in enumerate(docs, start=1):
            # build scores for relevant ROME codes
            naf = doc['naf']
            score = doc['score']
            rome_codes = mapping_util.MANUAL_NAF_ROME_MAPPING[naf].keys()

            for rome_code in rome_codes:
                office_score_for_current_rome = scoring_util.get_score_adjusted_to_rome_code_and_naf_code(
                    score=score, rome_code=rome_code, naf_code=naf)
                doc['score_for_rome_%s' %
                    rome_code] = office_score_for_current_rome

            self.es.index(index=self.ES_TEST_INDEX,
                          doc_type=self.ES_OFFICE_TYPE,
                          id=i,
                          body=doc)

        # need for ES to register our new documents, flaky test here otherwise
        time.sleep(1)

        # Create related Office instances into MariaDB/MySQL.
        for doc in docs:

            # Set the right `commune_id` and `zipcode` depending on the location.
            commune_id = None
            zip_code = None
            for position in self.positions:
                if doc['location'] == self.positions[position]['location']:
                    commune_id = self.positions[position]['commune_id']
                    zip_code = self.positions[position]['zip_code']
                    break

            if not commune_id:
                raise ValueError(
                    "Cannot create an entry in Office with a city absent from self.positions."
                )

            office = Office(
                company_name=doc['name'],
                siret=doc['siret'],
                score=doc['score'],
                naf=doc['naf'],
                city_code=commune_id,
                zipcode=zip_code,
                email=u'*****@*****.**',
                departement=zip_code[:2],
                x=doc['location']['lon'],
                y=doc['location']['lat'],
            )
            office.save()

        # We should have as much entries in MariaDB/MySQL than in Elasticsearch.
        self.assertEquals(Office.query.count(), len(docs))
Esempio n. 7
0
    def setUp(self):
        super(FavoriteBaseTest, self).setUp()

        # Create a user.
        self.user = User.create(email='*****@*****.**',
                                gender='male',
                                first_name='John',
                                last_name='Doe')

        # Insert test data into Elasticsearch.
        docs = [
            {
                'naf': '4711F',
                'siret': '00000000000001',
                'score': 98,
                'headcount': 32,
                'locations': self.positions['metz']['coords'],
                'name': 'Centre Distributeur E.Leclerc',
            },
            {
                'naf': '4722Z',
                'siret': '00000000000002',
                'score': 98,
                'headcount': 12,
                'locations': self.positions['metz']['coords'],
                'name': 'Maison Nicolas',
            },
            {
                'naf': '4711F',
                'siret': '00000000000003',
                'score': 98,
                'headcount': 32,
                'locations': self.positions['paris']['coords'],
                'name': 'Carrefour Paris',
            },
            {
                'naf': '4722Z',
                'siret': '00000000000004',
                'score': 98,
                'headcount': 84,
                'locations': self.positions['paris']['coords'],
                'name': 'Maistre Mathieu',
            },
        ]
        for i, doc in enumerate(docs, start=1):
            self.es.index(index=settings.ES_INDEX,
                          doc_type=es.OFFICE_TYPE,
                          id=i,
                          body=doc)

        # Required by ES to register new documents, flaky test here otherwise.
        self.es.indices.flush(index=settings.ES_INDEX)

        # Create related Office instances into MariaDB/MySQL.
        for doc in docs:

            # Set the right `commune_id` and `zipcode` depending on the location.
            commune_id = None
            zip_code = None
            commune_id = None
            for position in self.positions:
                if doc['locations'] == self.positions[position]['coords']:
                    commune_id = self.positions[position]['commune_id']
                    zip_code = self.positions[position]['zip_code']
                    break

            if not commune_id:
                raise ValueError(
                    "Cannot create an entry in Office with a city absent from self.positions."
                )

            office = Office(
                company_name=doc['name'],
                siret=doc['siret'],
                score=doc['score'],
                naf=doc['naf'],
                city_code=commune_id,
                zipcode=zip_code,
                email='*****@*****.**',
                departement=zip_code[:2],
                x=doc['locations'][0]['lon'],
                y=doc['locations'][0]['lat'],
            )
            office.save()

        # We should have as much entries in MariaDB/MySQL than in Elasticsearch.
        self.assertEqual(Office.query.count(), len(docs))
Esempio n. 8
0
    def setUp(self, *args, **kwargs):
        super(ApiBaseTest, self).setUp(*args, **kwargs)

        self.docs = self.get_docs()

        # Insert test data into Elasticsearch.
        for _, doc in enumerate(self.docs, start=1):

            # just like in other environments, id should be the siret
            self.es.index(index=settings.ES_INDEX,
                          doc_type=es.OFFICE_TYPE,
                          id=doc['siret'],
                          body=doc)

        # need for ES to register our new documents, flaky test here otherwise
        self.es.indices.flush(index=settings.ES_INDEX)

        # Create related Office instances into MariaDB/MySQL.
        for doc in self.docs:

            # Set the right `commune_id` and `zipcode` depending on the location.
            commune_id = None
            zip_code = None
            for position in self.positions:
                if doc['locations'] == self.positions[position]['coords']:
                    commune_id = self.positions[position]['commune_id']
                    zip_code = self.positions[position]['zip_code']
                    break

            if not commune_id:
                raise ValueError(
                    "Cannot create an entry in Office with a city absent from self.positions."
                )

            office = Office(
                office_name=doc['name'],
                siret=doc['siret'],
                score=doc['score'],
                score_alternance=doc['score_alternance'],
                naf=doc['naf'],
                city_code=commune_id,
                zipcode=zip_code,
                email='*****@*****.**',
                departement=zip_code[:2],
                company_name=doc['company_name']
                if 'company_name' in doc else '',
                flag_alternance=doc['flag_alternance'],
                flag_pmsmp=doc['flag_pmsmp'],
                headcount=doc['headcount'],
                x=doc['locations'][0]['lon'],
                y=doc['locations'][0]['lat'],
            )
            office.save()

        # We should have as much entries in MariaDB/MySQL than in Elasticsearch, except
        # one more in ES for the fake document actually.
        es_count = self.es.count(index=settings.ES_INDEX,
                                 doc_type=es.OFFICE_TYPE,
                                 body={'query': {
                                     'match_all': {}
                                 }})
        self.assertEqual(Office.query.count() + 1, es_count['count'])
def make_office():
    for i in range(0, 200):
        office = Office(departement="57", siret=str(i), headcount="03")
        office.save()
Esempio n. 10
0
class RouteTest(DatabaseTest):

    def create_example_office(self):
        # Create an office.
        self.office = Office(
            departement=u'75',
            siret=u'78548035101646',
            company_name=u'NICOLAS',
            headcount=u'03',
            city_code=u'75110',
            zipcode=u'75010',
            naf=u'7320Z',
            tel=u'0100000000',
            score=80,
            x=2.3488,
            y=48.8534,
        )
        self.office.save()

    def test_office_fields_and_properties_are_unicode(self):
        """
        Check if office fields are unicode
        """

        self.create_example_office()

        self.assertEqual(type(self.office.company_name), unicode)
        self.assertEqual(type(self.office.address_as_text), unicode)
        self.assertEqual(type(self.office.phone), unicode)
        self.assertEqual(type(self.office.google_url), unicode)

    def test_office_details_page(self):
        """
        Test the office details page of a regular office.
        """

        self.create_example_office()

        rv = self.app.get('/%s/details' % self.office.siret)
        self.assertEqual(rv.status_code, 200)

    def test_office_details_page_of_non_existing_office(self):
        """
        Test the office details page of a non existing office.
        """

        # The details page of an nonexistent office should raise a 404.
        rv = self.app.get('/7x5x8x3x1x1x46/details')
        self.assertEqual(rv.status_code, 404)

    def test_office_details_page_of_office_having_buggy_naf(self):
        """
        Test the office details page of an office having NAF 9900Z.
        """

        self.create_example_office()
        self.office.naf = u'9900Z'
        self.office.save()

        rv = self.app.get('/%s/details' % self.office.siret)
        self.assertEqual(rv.status_code, 200)

    def test_download_regular_office(self):
        """
        Test the office PDF download
        """

        self.create_example_office()

        # normal behavior
        rv = self.app.get("/%s/download" % self.office.siret)
        self.assertEqual(rv.status_code, 200)

    def test_download_missing_siret(self):
        """
        Test the office PDF download of a non existing office
        """
        # siret does not exist
        rv = self.app.get("/1234567890/download")
        self.assertEqual(rv.status_code, 404)

    def test_download_of_office_having_buggy_naf(self):
        """
        Test the office PDF download of an office having NAF 9900Z.
        """

        self.create_example_office()
        self.office.naf = u'9900Z'
        self.office.save()

        rv = self.app.get('/%s/download' % self.office.siret)
        self.assertEqual(rv.status_code, 200)
class DownloadTest(DatabaseTest):
    def setUp(self):
        super().setUp()

        # Create an office.
        self.office = Office(
            departement='75',
            siret='78548035101646',
            company_name='NICOLAS',
            headcount='03',
            city_code='75110',
            zipcode='75010',
            naf='4646Z',
            tel='0100000000',
            score=80,
            x=2.3488,
            y=48.8534,
        )
        self.office.save()

        # Remove pdf file if it already exists
        pdf.delete_file(self.office)

    def test_office_fields_and_properties_are_str(self):
        """
        Check if office fields are str
        """

        self.assertEqual(type(self.office.company_name), str)
        self.assertEqual(type(self.office.address_as_text), str)
        self.assertEqual(type(self.office.phone), str)
        self.assertEqual(type(self.office.google_url), str)

    def test_office_details_page(self):
        """
        Test the office details page of a regular office.
        """

        rv = self.app.get(
            self.url_for('office.details', siret=self.office.siret))
        self.assertEqual(rv.status_code, 200)

    def test_office_details_page_of_non_existing_office(self):
        """
        Test the office details page of a non existing office.
        """

        # The details page of an nonexistent office should raise a 404.
        rv = self.app.get(
            self.url_for('office.details', siret='7x5x8x3x1x1x46'))
        self.assertEqual(rv.status_code, 404)

    def test_office_details_page_of_office_having_buggy_naf(self):
        """
        Test the office details page of an office having NAF 9900Z.
        """

        self.office.naf = '9900Z'
        self.office.save()

        rv = self.app.get(
            self.url_for('office.details', siret=self.office.siret))
        self.assertEqual(rv.status_code, 200)

    def test_office_details_page_of_office_which_is_a_groupement_employeurs(
            self):
        """
        Test the office details page of an office being a groupement d'employeurs.
        """
        self.office.siret = '30651644400024'  # first siret in groupements_employeurs.csv
        self.office.save()

        self.assertIn(self.office.siret, load_groupements_employeurs())

        rv = self.app.get(
            self.url_for('office.details', siret=self.office.siret))
        self.assertEqual(rv.status_code, 200)
        self.assertIn(
            u"Cette entreprise est un groupement d'employeurs.",
            rv.data.decode(),
        )

    def test_office_details_page_of_office_which_is_not_a_groupement_employeurs(
            self):
        """
        Test the office details page of an office not being a groupement d'employeurs.
        """
        self.assertFalse(self.office.siret in load_groupements_employeurs())

        rv = self.app.get(
            self.url_for('office.details', siret=self.office.siret))
        self.assertEqual(rv.status_code, 200)
        self.assertNotIn(
            u"Cette entreprise est un groupement d'employeurs.",
            rv.data.decode(),
        )

    def test_download_regular_office(self):
        """
        Test the office PDF download
        """
        # normal behavior
        rv = self.app.get(
            self.url_for('office.download', siret=self.office.siret))

        self.assertEqual(rv.status_code, 200)
        self.assertEqual('application/pdf', rv.mimetype)
        self.assertLess(1000, rv.content_length)

    def test_download_triggers_activity_log(self):
        with unittest.mock.patch(
                'labonneboite.common.activity.log') as activity_log:
            self.app.get(
                self.url_for('office.download', siret=self.office.siret))
            activity_log.assert_called_with('telecharger-pdf',
                                            siret=self.office.siret)

    def test_download_missing_siret(self):
        """
        Test the office PDF download of a non existing office
        """
        # siret does not exist
        rv = self.app.get(self.url_for('office.download', siret='1234567890'))
        self.assertEqual(rv.status_code, 404)

    def test_download_of_office_having_buggy_naf(self):
        """
        Test the office PDF download of an office having NAF 9900Z.
        """
        self.office.naf = '9900Z'
        self.office.save()

        rv = self.app.get(
            self.url_for('office.download', siret=self.office.siret))
        self.assertEqual(rv.status_code, 200)

    def test_toggle_details_event(self):
        with unittest.mock.patch(
                'labonneboite.common.activity.log') as activity_log:
            rv = self.app.post(
                self.url_for('office.toggle_details_event',
                             siret=self.office.siret))
            activity_log.assert_called_with('afficher-details',
                                            siret=self.office.siret)
        self.assertEqual(rv.status_code, 200)