|
| 1 | +# |
| 2 | +# MobilityData 2023 |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +# |
| 16 | + |
| 17 | +from datetime import datetime |
| 18 | + |
| 19 | +from faker import Faker |
| 20 | +from geoalchemy2 import WKTElement |
| 21 | + |
| 22 | +from shared.database_gen.sqlacodegen_models import ( |
| 23 | + Gtfsfeed, |
| 24 | + Gtfsdataset, |
| 25 | + Validationreport, |
| 26 | + Notice, |
| 27 | +) |
| 28 | +from test_shared.test_utils.database_utils import clean_testing_db, get_testing_session |
| 29 | + |
| 30 | + |
| 31 | +def populate_database(): |
| 32 | + """ |
| 33 | + Populates the database with fake data with the following distribution: |
| 34 | + """ |
| 35 | + session = get_testing_session() |
| 36 | + fake = Faker() |
| 37 | + # Create GTFS feeds |
| 38 | + feed = Gtfsfeed( |
| 39 | + id="feed_1", |
| 40 | + data_type="gtfs", |
| 41 | + feed_name="feed_name", |
| 42 | + note="gtfs-Some fake note", |
| 43 | + producer_url="https://some_fake_producer_url", |
| 44 | + authentication_info_url=None, |
| 45 | + api_key_parameter_name=None, |
| 46 | + license_url="https://some_fake_license_url", |
| 47 | + stable_id="feed_stable_id", |
| 48 | + feed_contact_email="some_fake_email@fake.com", |
| 49 | + provider="Some fake company", |
| 50 | + operational_status="published", |
| 51 | + official=True, |
| 52 | + ) |
| 53 | + session.add(feed) |
| 54 | + |
| 55 | + # Create GTFS datasets |
| 56 | + gtfs_dataset = Gtfsdataset( |
| 57 | + id="dataset_1", |
| 58 | + feed_id="feed_1", |
| 59 | + latest=True, |
| 60 | + # Use a url containing the stable id. The program should replace all the is after the feed stable id |
| 61 | + # by latest.zip |
| 62 | + hosted_url="https://some_fake_hosted_url", |
| 63 | + note="dataset_1 Some fake note", |
| 64 | + hash=fake.sha256(), |
| 65 | + downloaded_at=datetime.utcnow(), |
| 66 | + stable_id="feed_stable_id", |
| 67 | + ) |
| 68 | + # Create validation reports |
| 69 | + validation_reports = [] |
| 70 | + validation_report = Validationreport( |
| 71 | + id="report1", |
| 72 | + validator_version="6.0.1", |
| 73 | + validated_at=datetime(2025, 1, 12), |
| 74 | + html_report=fake.url(), |
| 75 | + json_report=fake.url(), |
| 76 | + ) |
| 77 | + validation_reports.append(validation_report) |
| 78 | + session.add_all(validation_reports) |
| 79 | + gtfs_dataset.validation_reports.append(validation_report) |
| 80 | + session.add(gtfs_dataset) |
| 81 | + |
| 82 | + # Create notices |
| 83 | + notice_list = [] |
| 84 | + |
| 85 | + notice = Notice( |
| 86 | + dataset_id="dataset_1", |
| 87 | + validation_report_id="report1", |
| 88 | + severity="INFO", |
| 89 | + total_notices=5, |
| 90 | + notice_code="info_code_1", |
| 91 | + ) |
| 92 | + notice_list.append(notice) |
| 93 | + notice = Notice( |
| 94 | + dataset_id="dataset_1", |
| 95 | + validation_report_id="report1", |
| 96 | + severity="WARNING", |
| 97 | + total_notices=3, |
| 98 | + notice_code="warning_code_1", |
| 99 | + ) |
| 100 | + notice_list.append(notice) |
| 101 | + notice = Notice( |
| 102 | + dataset_id="dataset_1", |
| 103 | + validation_report_id="report1", |
| 104 | + severity="ERROR", |
| 105 | + total_notices=2, |
| 106 | + notice_code="error_code_1", |
| 107 | + ) |
| 108 | + notice_list.append(notice) |
| 109 | + notice = Notice( |
| 110 | + dataset_id="dataset_1", |
| 111 | + validation_report_id="report1", |
| 112 | + severity="ERROR", |
| 113 | + total_notices=1, |
| 114 | + notice_code="error_code_2", |
| 115 | + ) |
| 116 | + notice_list.append(notice) |
| 117 | + |
| 118 | + session.add_all(notice_list) |
| 119 | + |
| 120 | + session.commit() |
| 121 | + |
| 122 | + |
| 123 | +def pytest_configure(config): |
| 124 | + """ |
| 125 | + Allows plugins and conftest files to perform initial configuration. |
| 126 | + This hook is called for every plugin and initial conftest |
| 127 | + file after command line options have been parsed. |
| 128 | + """ |
| 129 | + |
| 130 | + |
| 131 | +def pytest_sessionstart(session): |
| 132 | + """ |
| 133 | + Called after the Session object has been created and |
| 134 | + before performing collection and entering the run test loop. |
| 135 | + """ |
| 136 | + clean_testing_db() |
| 137 | + populate_database() |
| 138 | + |
| 139 | + |
| 140 | +def pytest_sessionfinish(session, exitstatus): |
| 141 | + """ |
| 142 | + Called after whole test run finished, right before |
| 143 | + returning the exit status to the system. |
| 144 | + """ |
| 145 | + # Cleaned at the beginning instead of the end so we can examine the DB after the test. |
| 146 | + # clean_testing_db() |
| 147 | + |
| 148 | + |
| 149 | +def pytest_unconfigure(config): |
| 150 | + """ |
| 151 | + called before test process is exited. |
| 152 | + """ |
0 commit comments