コード例 #1
0
ファイル: test_usage.py プロジェクト: Alwnikrotikz/open-hea
    def test_session_basic_use(self):
        """ Demonstrate the basic pattern of use for a session """
        session = TestSession()
        p = Project(
            name="A Project",
            createdBy="me",
            dateCreated=datetime.datetime.today(),
            currency="UKP",
            agroYearBegins=calendar.month_name[5],
        )

        # If the database is not fresh each time and test_session_multiple_use
        # ran first this will fail
        self.assertEqual(session.query(Project).count(), 0)

        # There are no new objects in the session, but we are about to
        # add one
        self.assertEqual(len(session.new), 0)
        session.add(p)
        self.assertEqual(len(session.new), 1)
        self.assertIn(p, session)

        # After a commit the object stays in the session but is not 'new'
        session.commit()
        self.assertEqual(len(session.new), 0)
        self.assertIn(p, session)

        # But the real proof is whether it's gone into the database
        self.assertEqual(session.query(Project).count(), 1)
コード例 #2
0
ファイル: test_usage.py プロジェクト: Alwnikrotikz/open-hea
    def test_association_object_pattern(self):
        """ Demonstrate the association object pattern (save-update)"""
        session = TestSession()
        p = Project(
            name="The test project",
            createdBy="me",
            dateCreated=datetime.datetime.today(),
            currency="UKP",
            agroYearBegins=calendar.month_name[5],
        )

        # explicitly create an association object
        # and populate it with one half of the relationship
        assocn = ProjectLivelihoodZone()
        assocn.zone = LivelihoodZone(
            name="LZ 23",
            createdBy="me",
            dateCreated=datetime.datetime.today(),
            population=632800,
            coordinates=("POLYGON((-79.8 38.5, -80.03 38.2, " "-80.02 37.89, -79.92 37.75, -79.8 38.5))"),
            consumptionYearBegins=calendar.month_name[5],
        )

        # make the other half of the relationship
        p.livelihoodzones.append(assocn)

        # Save-update cascade means we need to add only the project
        # to the session; the other objects will be pulled in
        # automatically
        session.add(p)
        self.assertEqual(len(session.new), 3)
        session.commit()

        self.assertEqual(session.query(ProjectLivelihoodZone).count(), 1)
        self.assertEqual(session.query(LivelihoodZone).count(), 1)
        self.assertEqual(session.query(Project).count(), 1)

        # Get the name of a project which surveys LZ 23
        self.assertEqual(
            session.query(Project.name)
            .join(Project.livelihoodzones)
            .join(LivelihoodZone)
            .filter(LivelihoodZone.name == "LZ 23")
            .first(),
            ("The test project",),
        )

        # Sanity check against the empty set
        self.assertEqual(
            session.query(Project)
            .join(Project.livelihoodzones)
            .join(LivelihoodZone)
            .filter(LivelihoodZone.name == "LZ 24")
            .count(),
            0,
        )
コード例 #3
0
ファイル: test_usage.py プロジェクト: Alwnikrotikz/open-hea
    def test_session_multiple_use(self):
        """ Test that a new test session gives you an empty database """
        session = TestSession()
        p = Project(
            name="A Project",
            createdBy="me",
            dateCreated=datetime.datetime.today(),
            currency="UKP",
            agroYearBegins=calendar.month_name[5],
        )

        # If the database is not fresh each time, and test_session_basic_use
        # ran first this will fail
        self.assertEqual(session.query(Project).count(), 0)

        session.add(p)
        session.commit()

        self.assertEqual(session.query(Project).count(), 1)
コード例 #4
0
ファイル: test_usage.py プロジェクト: Alwnikrotikz/open-hea
    def test_session_context_as_transaction(self):
        """ Demonstrate using the context of a session transaction """
        session = TestSession()
        p = Project(
            name="A Project",
            createdBy="me",
            dateCreated=datetime.datetime.today(),
            currency="UKP",
            agroYearBegins=calendar.month_name[5],
        )

        self.assertEqual(session.query(Project).count(), 0)

        with session.begin(subtransactions=True):
            session.add(p)
            self.assertEqual(len(session.new), 1)
            self.assertIn(p, session)

        # No need for explicit commit
        self.assertEqual(session.query(Project).count(), 1)
コード例 #5
0
    def test_yearbegins_validation(self):
        """ Test if year boundaries are a controlled set """
        session = TestSession()
        session.add(Project(
            name="A Project",
            createdBy="me",
            dateCreated=datetime.datetime.today(),
            currency="USD",
            agroYearBegins="This is not a calendar term or date"))

        session.commit()
        self.assertEqual(session.query(Project).count(), 0)
コード例 #6
0
    def test_currency_validation(self):
        """ Test if currency values are a controlled set """
        session = TestSession()
        session.add(Project(
            name="A Project",
            createdBy="me",
            dateCreated=datetime.datetime.today(),
            currency="This is not a currency category",
            agroYearBegins=calendar.month_name[5]))

        session.commit()
        self.assertEqual(session.query(Project).count(), 0)
コード例 #7
0
    def test_coordinates_validation(self):
        """ Test if geo coordinates are validated """
        session = TestSession()
        session.add(LivelihoodZone(
            name="LZ 23",
            createdBy="me",
            dateCreated=datetime.datetime.today(),
            population=632800,
            coordinates="This is not a geospatial value",
            consumptionYearBegins=calendar.month_name[5]))

        session.commit()
        self.assertEqual(session.query(LivelihoodZone).count(), 0)
コード例 #8
0
ファイル: test_usage.py プロジェクト: Alwnikrotikz/open-hea
    def test_location_based_query(self):
        """ Demonstrate querying in 2D space """
        session = TestSession()
        zone = LivelihoodZone(
            name="Polygon test",
            createdBy="me",
            dateCreated=datetime.datetime.today(),
            population=3,
            coordinates=("POLYGON((1 9,5 12,8 9,8 4,5 1,2 3,1 9)," "(3 8,6 8,6 5,3 5,3 8))"),
            consumptionYearBegins=calendar.month_name[5],
        )

        session.add(zone)
        session.commit()

        # Point is below-left of polygon
        self.assertEqual(
            session.query(LivelihoodZone).filter(LivelihoodZone.coordinates.gcontains("POINT(1 2)")).count(), 0
        )

        # Point is on outer edge of polygon
        self.assertEqual(
            session.query(LivelihoodZone).filter(LivelihoodZone.coordinates.gcontains("POINT(6 2)")).count(), 0
        )

        # Point is within polygon
        self.assertEqual(
            session.query(LivelihoodZone).filter(LivelihoodZone.coordinates.gcontains("POINT(5 3)")).count(), 1
        )

        # Point is on inner edge of polygon
        self.assertEqual(
            session.query(LivelihoodZone).filter(LivelihoodZone.coordinates.gcontains("POINT(3 6)")).count(), 0
        )

        # Point is in void of polygon
        self.assertEqual(
            session.query(LivelihoodZone).filter(LivelihoodZone.coordinates.gcontains("POINT(4 7)")).count(), 0
        )
コード例 #9
0
ファイル: test_usage.py プロジェクト: Alwnikrotikz/open-hea
    def test_date_based_query(self):
        """ Demonstrate querying on date ranges """
        today = datetime.date.today()
        dates = [today + datetime.timedelta(days=i) for i in range(-3, 4)]

        self.assertEqual(len(dates), 7)
        session = TestSession()
        for n, d in enumerate(dates):
            session.add(
                Project(
                    name="Project %d" % n,
                    createdBy="me",
                    dateCreated=d,
                    currency="UKP",
                    agroYearBegins=calendar.month_name[5],
                )
            )
        session.commit()

        # There is a project on every one of the last three days,
        # the next three days, and today.
        self.assertEqual(session.query(Project).filter(Project.dateCreated <= today).count(), 4)

        self.assertEqual(session.query(Project).filter(Project.dateCreated > today).count(), 3)

        self.assertEqual(session.query(Project).filter(Project.dateCreated == today).count(), 1)

        # Beware! Date comparisons may not work how you think,
        # if you mix date.today() and datetime.today(), etc.

        # Use ranges to be safe, ie: the last 24 hours
        end = datetime.datetime.now()
        start = end - datetime.timedelta(hours=24)
        self.assertEqual(session.query(Project.dateCreated).filter(Project.dateCreated.between(start, end)).count(), 1)

        self.assertEqual(
            session.query(Project.dateCreated).filter(Project.dateCreated.between(start, end)).first(), (today,)
        )
コード例 #10
0
    def test_population_validation(self):
        """ Test if population figures are validated """
        session = TestSession()
        session.add(LivelihoodZone(
            name="LZ 23",
            createdBy="me",
            dateCreated=datetime.datetime.today(),
            population="This is not a numerical value",
            coordinates="POLYGON((-79.8 38.5, -80.03 38.2, -80.02 37.89, "
                        "-79.92 37.75, -79.8 38.5))",
            consumptionYearBegins=calendar.month_name[5]))

        session.commit()
        self.assertEqual(session.query(LivelihoodZone).count(), 0)
コード例 #11
0
    def test_population_query(self):
        """ Test if WKT field is interpreted correctly"""
        session = TestSession()
        polygonWKT = (
            "POLYGON((-79.8 38.5, -80.03 38.2, "
            "-80.02 37.89, -79.92 37.75, -79.8 38.5))")
        session.add(LivelihoodZone(
            name="LZ 23",
            createdBy="me",
            dateCreated=datetime.datetime.today(),
            population=632800,
            coordinates=polygonWKT,
            consumptionYearBegins=calendar.month_name[5]))

        session.commit()
        lz = session.query(LivelihoodZone).first()
        self.assertEqual(session.scalar(lz.coordinates.wkt), polygonWKT)