Exemplo n.º 1
0
    def test_data_route_match(self):
        endpoint = Mock(return_value="cats")

        f = self.app
        f.debug = True

        route = {
            "product": {
                (("year", None), ("location", "department")): {
                    "name": "department_product_year",
                    "action": endpoint
                },
            },
            "year": {}
        }

        routing.add_routes(f, route)

        factories.Location(level="department", id=2)
        db.session.commit()

        with f.test_client() as c:
            response = c.get("/product?location=2&year=2007")
            assert response.status_code == 200
            assert response.data == b"cats"
            endpoint.assert_called_with(location=2, year=2007)
    def test_get_metadata_by_id_zero(self):
        """Tests for a bug Quinn caught - 'if not entity_id' vs if entity_id is
        not None"""

        l = factories.Location(id=0, code="03", level="department")
        factories.Location(id=2,
                           code="03222",
                           parent_id=0,
                           level="municipality")
        db.session.commit()

        response = self.client.get(url_for("metadata.location", entity_id=0))
        self.assert_200(response)

        response_json = response.json["data"]
        self.assert_json_matches_object(response_json, l, [
            "id", "code", "level", "parent_id", "name_en", "name_short_en",
            "description_en"
        ])
    def test_get_metadata_by_id_when_there_are_multiple(self):
        """Tests for a bug I caught where .first() was being called instead of
        .get()"""

        factories.Location(id=1, code="03", level="department")
        l = factories.Location(id=2,
                               code="03222",
                               parent_id=1,
                               level="municipality")
        db.session.commit()

        response = self.client.get(url_for("metadata.location",
                                           entity_id=l.id))
        self.assert_200(response)

        response_json = response.json["data"]
        self.assert_json_matches_object(response_json, l, [
            "id", "code", "level", "parent_id", "name_en", "name_short_en",
            "description_en"
        ])
    def test_get_location_levels(self):

        l1 = factories.Location(id=1, code="03", level="department")
        l2 = factories.Location(id=2,
                                code="03222",
                                parent_id=1,
                                level="municipality")
        l3 = factories.Location(id=7, code="XYZ", level="country")
        locs = [l1, l2, l3]
        db.session.commit()

        for l in locs:
            response = self.client.get(
                url_for("metadata.location", level=l.level))
            self.assert_200(response)

            response_json = response.json["data"]
            assert len(response_json) == 1

            self.assert_json_matches_object(response_json[0], l, [
                "id", "code", "level", "parent_id", "name_en", "name_short_en",
                "description_en"
            ])
    def test_get_locations(self):

        d1 = factories.Location(id=1, code="03", level="department")
        d2 = factories.Location(id=2,
                                code="03222",
                                parent_id=1,
                                level="municipality")
        d3 = factories.Location(id=7, code="04555", level="municipality")
        depts = {1: d1, 2: d2, 7: d3}
        db.session.commit()

        response = self.client.get(url_for("metadata.location"))
        self.assert_200(response)

        response_json = response.json["data"]
        assert len(response_json) == 3

        for dept_json in response_json:
            d = depts[dept_json["id"]]
            self.assert_json_matches_object(dept_json, d, [
                "id", "code", "level", "parent_id", "name_en", "name_short_en",
                "description_en"
            ])
    def test_get_location(self):

        dept = factories.Location(id=14,
                                  code="18",
                                  level="department",
                                  parent_id=None,
                                  name_en="Atlantico",
                                  name_short_en="Atlantico",
                                  description_en="The region of Atlantico",
                                  name_es="Atlantico")
        db.session.commit()

        api_url = url_for("metadata.location", entity_id=dept.id)
        response_json = self.assert_metadata_api(api_url)
        self.assert_json_matches_object(response_json, dept, [
            "id", "code", "level", "parent_id", "name_en", "name_short_en",
            "description_en", "name_es", "name_short_es", "description_es"
        ])
Exemplo n.º 7
0
def dummy(n=10):
    """Generate dummy data."""
    if not app.debug:
        raise Exception("Unsafe to generate dummy data while not in DEBUG.")

    # Generate a set of products and departments.
    departments = []
    for x in range(0, 6):
        departments.append(factories.Location(level="department"))

    sections = []
    for x in range(0, 4):
        sections.append(factories.HSProduct(level="section"))

    core.db.session.commit()

    two_digits = []
    for x in range(0, 8):
        parent_id = random.choice(sections).id
        two_digits.append(
            factories.HSProduct(level="2digit", parent_id=parent_id))

    core.db.session.commit()

    products = []
    for x in range(0, 20):
        parent_id = random.choice(two_digits).id
        products.append(
            factories.HSProduct(level="4digit", parent_id=parent_id))

    core.db.session.commit()

    # Generate what products exist in which departments and by how much
    for d in departments:
        if random.random() < 0.2:
            # This place focuses on a few products
            for x in range(4):
                factories.DepartmentProductYear(
                    department=d, product=random.choice(products), year=2008)
        else:
            # This place is a diverse economy
            for x in range(20):
                factories.DepartmentProductYear(
                    department=d, product=random.choice(products), year=2008)

    # Permute data for the following years according to this year.
    for d in departments:
        dpys = models.DepartmentProductYear\
            .query.filter_by(department=d).all()
        for year in range(2009, 2013):
            if random.random() < 0.1:
                delta = random.random() - 0.5
            else:
                delta = 5 * (random.random() - 0.5)

            for dpy in dpys:
                factories.DepartmentProductYear(
                    department=dpy.department,
                    product=dpy.product,
                    year=year,
                    import_value=dpy.import_value * delta,
                    export_value=dpy.export_value * delta)

    core.db.session.commit()