Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix pytest suite allowing it to complete on Python 3.8 #791

Closed
wants to merge 1 commit into from

Conversation

Nischay-Pro
Copy link
Contributor

@Nischay-Pro Nischay-Pro commented Aug 4, 2023

Description

This PR adds the 2 missing __future__ import annotations statements in the pytest files. This allows pytest to run on Python 3.8


  • I agree to contribute to the project under Apache 2 License.
  • To the best of my knowledge, the proposed patch is not based on code under GPL or other license that is incompatible with FlexMeasures

Signed-off-by: Nischay Ram Mamidi <NischayPro@gmail.com>
@Nischay-Pro Nischay-Pro self-assigned this Aug 4, 2023
@Nischay-Pro
Copy link
Contributor Author

I am trying to run pytest however one of my test fails for Python 3.8

FAILED flexmeasures/api/v3_0/tests/test_sensor_schedules.py::test_trigger_and_get_schedule[message1-Test charging station] - AssertionError: assert False is True

More information

app = <Flask 'flexmeasures'>, add_market_prices = {'epex_da': <Sensor 1: epex_da, unit: EUR/MWh res.: 1:00:00>, 'epex_da_production': <Sensor 2: epex_da_production, unit: EUR/MWh res.: 1:00:00>}
add_battery_assets = {'Test battery': <GenericAsset 5: 'Test battery' (battery)>, 'Test battery with no known prices': <GenericAsset 6: 'Test battery with no known prices' (battery)>}, battery_soc_sensor = <Sensor 8: state of charge, unit: % res.: 0:00:00>
add_charging_station_assets = {'Test charging station': <GenericAsset 10: 'Test charging station' (one-way_evse)>, 'Test charging station (bidirectional)': <GenericAsset 11: 'Test charging station (bidirectional)' (two-way_evse)>}, keep_scheduling_queue_empty = None
message = {'duration': 'PT24H', 'flex-context': {'consumption-price-sensor': 1, 'production-price-sensor': 1}, 'flex-model': {'r...trip-efficiency': '98%', 'soc-at-start': 12.1, 'soc-max': 40, 'soc-min': 0, ...}, 'start': '2015-01-01T00:00:00+01:00'}
asset_name = 'Test charging station'

    @pytest.mark.parametrize(
        "message, asset_name",
        [
            (message_for_trigger_schedule(), "Test battery"),
            (message_for_trigger_schedule(with_targets=True), "Test charging station"),
        ],
    )
    def test_trigger_and_get_schedule(
        app,
        add_market_prices,
        add_battery_assets,
        battery_soc_sensor,
        add_charging_station_assets,
        keep_scheduling_queue_empty,
        message,
        asset_name,
    ):
        # Include the price sensor in the flex-context explicitly, to test deserialization
        price_sensor_id = add_market_prices["epex_da"].id
        message["flex-context"] = {
            "consumption-price-sensor": price_sensor_id,
            "production-price-sensor": price_sensor_id,
        }

        # trigger a schedule through the /sensors/<id>/schedules/trigger [POST] api endpoint
        assert len(app.queues["scheduling"]) == 0

        sensor = (
            Sensor.query.filter(Sensor.name == "power")
            .join(GenericAsset)
            .filter(GenericAsset.id == Sensor.generic_asset_id)
            .filter(GenericAsset.name == asset_name)
            .one_or_none()
        )
        with app.test_client() as client:
            auth_token = get_auth_token(client, "test_prosumer_user@seita.nl", "testtest")
            trigger_schedule_response = client.post(
                url_for("SensorAPI:trigger_schedule", id=sensor.id),
                json=message,
                headers={"Authorization": auth_token},
            )
            print("Server responded with:\n%s" % trigger_schedule_response.json)
            assert trigger_schedule_response.status_code == 200
            job_id = trigger_schedule_response.json["schedule"]

        # look for scheduling jobs in queue
        assert (
            len(app.queues["scheduling"]) == 1
        )  # only 1 schedule should be made for 1 asset
        job = app.queues["scheduling"].jobs[0]
        assert job.kwargs["sensor_id"] == sensor.id
        assert job.kwargs["start"] == parse_datetime(message["start"])
        assert job.id == job_id

        # process the scheduling queue
        work_on_rq(app.queues["scheduling"], exc_handler=handle_scheduling_exception)
>       assert (
            Job.fetch(job_id, connection=app.queues["scheduling"].connection).is_finished
            is True
        )
E       AssertionError: assert False is True
E        +  where False = Job('9d3dc3c8-0e39-40fe-8356-9ebcfb525ea9', enqueued_at=datetime.datetime(2023, 8, 4, 12, 42, 1, 470320)).is_finished
E        +    where Job('9d3dc3c8-0e39-40fe-8356-9ebcfb525ea9', enqueued_at=datetime.datetime(2023, 8, 4, 12, 42, 1, 470320)) = <bound method Job.fetch of <class 'rq.job.Job'>>('9d3dc3c8-0e39-40fe-8356-9ebcfb525ea9', connection=FakeStrictRedis<ConnectionPool<FakeConnection<server=<fakeredis._server.FakeServer object at 0x7f7164ee8a60>,db=0>>>)
E        +      where <bound method Job.fetch of <class 'rq.job.Job'>> = Job.fetch
E        +      and   FakeStrictRedis<ConnectionPool<FakeConnection<server=<fakeredis._server.FakeServer object at 0x7f7164ee8a60>,db=0>>> = Queue('scheduling').connection

flexmeasures/api/v3_0/tests/test_sensor_schedules.py:219: AssertionError

Copy link
Contributor

@Flix6x Flix6x left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps the exception handler contains more info on why it failed?

@nhoening
Copy link
Contributor

nhoening commented Aug 7, 2023

@Nischay-Pro we somehow need the log of the worker who handled this job (and couldn't process it). Does that appear somewhere?

@nhoening nhoening added this to the 0.15.0 milestone Aug 7, 2023
@nhoening
Copy link
Contributor

nhoening commented Aug 7, 2023

I believe running the application on Python3.8 might already tell us the basic problems. I'm seeing these on the staging server:

2023-08-07 11:19:14,077: ModuleNotFoundError: No module named 'email_validator.deliverability'

2023-08-07 11:19:14,078: File "/var/www/staging_flexmeasures_seita_nl_wsgi.py", line 24, in
2023-08-07 11:19:14,078: application = create_app()
2023-08-07 11:19:14,078:
2023-08-07 11:19:14,078: File "/home/seita/flexmeasures-staging/flexmeasures-work/flexmeasures/app.py", line 139, in create
2023-08-07 11:19:14,078:
2023-08-07 11:19:14,078: File "/home/seita/flexmeasures-staging/flexmeasures-work/flexmeasures/cli/init.py", line 16, in register_at
2023-08-07 11:19:14,079: import flexmeasures.cli.data_add
2023-08-07 11:19:14,079:
2023-08-07 11:19:14,079: File "/home/seita/flexmeasures-staging/flexmeasures-work/flexmeasures/cli/data_add.py", line 37, in
2023-08-07 11:19:14,079: from flexmeasures.data.services.data_sources import get_or_create_source
2023-08-07 11:19:14,079:
2023-08-07 11:19:14,080: File "/home/seita/flexmeasures-staging/flexmeasures-work/flexmeasures/data/services/users.py", line 14, in
2023-08-07 11:19:14,080: from email_validator.deliverability import validate_email_deliverability

And then after moving the import of validate_email_deliverability back into the email_validator module (this was a backward-incompatible change lately, I see:

ImportError: urllib3 v2.0 only supports OpenSSL 1.1.1+, currently the 'ssl' module is compiled with 'OpenSSL 1.0.2g  1 Mar 2016'. See: https://github.com/urllib3/urllib3/issues/2168

@nhoening
Copy link
Contributor

nhoening commented Aug 7, 2023

We'll first see if #776 fixes this for 3.8 (it also includes the two conftest future imports already)

@nhoening
Copy link
Contributor

nhoening commented Aug 8, 2023

#776 and #796 deal with Python3.8 support.

@nhoening nhoening closed this Aug 8, 2023
@nhoening nhoening removed this from the 0.15.0 milestone Aug 8, 2023
@Nischay-Pro Nischay-Pro deleted the fix-python38-testing branch August 8, 2023 21:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants