|
| 1 | +import unittest |
| 2 | +from datetime import datetime |
| 3 | + |
| 4 | +from geoalchemy2 import WKTElement |
| 5 | + |
| 6 | +from feeds_gen.models.source_info import SourceInfo |
| 7 | +from shared.database_gen.sqlacodegen_models import Gbfsfeed, Location, Gbfsversion |
| 8 | +from feeds.impl.models.gbfs_feed_impl import GbfsFeedImpl |
| 9 | +from feeds.impl.models.location_impl import LocationImpl |
| 10 | +from feeds.impl.models.gbfs_version_impl import GbfsVersionImpl |
| 11 | +from feeds.impl.models.bounding_box_impl import BoundingBoxImpl |
| 12 | + |
| 13 | +POLYGON = "POLYGON ((3.0 1.0, 4.0 1.0, 4.0 2.0, 3.0 2.0, 3.0 1.0))" |
| 14 | + |
| 15 | + |
| 16 | +class TestGbfsFeedImpl(unittest.TestCase): |
| 17 | + def setUp(self): |
| 18 | + self.location_orm = Location( |
| 19 | + id="loc1", |
| 20 | + country_code="US", |
| 21 | + country="United States", |
| 22 | + subdivision_name="California", |
| 23 | + municipality="San Francisco", |
| 24 | + ) |
| 25 | + self.version_orm = Gbfsversion( |
| 26 | + id="ver1", |
| 27 | + version="2.2", |
| 28 | + url="https://example.com/gbfs.json", |
| 29 | + ) |
| 30 | + self.bounding_box_orm = WKTElement(POLYGON, srid=4326) |
| 31 | + self.feed_orm = Gbfsfeed( |
| 32 | + id="feed1", |
| 33 | + stable_id="feed_stable_1", |
| 34 | + created_at=datetime(2024, 1, 1, 10, 0, 0), |
| 35 | + data_type="gbfs", |
| 36 | + system_id="sys1", |
| 37 | + operator_url="https://provider.com", |
| 38 | + locations=[self.location_orm], |
| 39 | + gbfsversions=[self.version_orm], |
| 40 | + bounding_box=self.bounding_box_orm, |
| 41 | + bounding_box_generated_at=datetime(2024, 1, 1, 12, 0, 0), |
| 42 | + authentication_type=0, |
| 43 | + authentication_info_url="https://auth.info", |
| 44 | + api_key_parameter_name="api_key", |
| 45 | + license_url="https://license.info", |
| 46 | + ) |
| 47 | + |
| 48 | + def test_from_orm_all_fields(self): |
| 49 | + expected = GbfsFeedImpl( |
| 50 | + id="feed_stable_1", |
| 51 | + system_id="sys1", |
| 52 | + data_type="gbfs", |
| 53 | + created_at=datetime(2024, 1, 1, 10, 0, 0), |
| 54 | + external_ids=[], |
| 55 | + redirects=[], |
| 56 | + provider_url="https://provider.com", |
| 57 | + locations=[LocationImpl.from_orm(self.location_orm)], |
| 58 | + versions=[GbfsVersionImpl.from_orm(self.version_orm)], |
| 59 | + bounding_box=BoundingBoxImpl.from_orm(self.bounding_box_orm), |
| 60 | + bounding_box_generated_at=datetime(2024, 1, 1, 12, 0, 0), |
| 61 | + source_info=SourceInfo( |
| 62 | + producer_url=None, |
| 63 | + authentication_type=0, |
| 64 | + authentication_info_url="https://auth.info", |
| 65 | + api_key_parameter_name="api_key", |
| 66 | + license_url="https://license.info", |
| 67 | + ), |
| 68 | + ) |
| 69 | + result = GbfsFeedImpl.from_orm(self.feed_orm) |
| 70 | + self.assertEqual(result, expected) |
| 71 | + |
| 72 | + def test_from_orm_empty_fields(self): |
| 73 | + feed_orm = Gbfsfeed( |
| 74 | + id="feed2", |
| 75 | + stable_id="feed_stable_2", |
| 76 | + system_id=None, |
| 77 | + operator_url=None, |
| 78 | + locations=[], |
| 79 | + gbfsversions=[], |
| 80 | + bounding_box=None, |
| 81 | + bounding_box_generated_at=None, |
| 82 | + ) |
| 83 | + expected = GbfsFeedImpl( |
| 84 | + id="feed_stable_2", |
| 85 | + system_id=None, |
| 86 | + provider_url=None, |
| 87 | + external_ids=[], |
| 88 | + redirects=[], |
| 89 | + locations=[], |
| 90 | + versions=[], |
| 91 | + bounding_box=None, |
| 92 | + bounding_box_generated_at=None, |
| 93 | + source_info=SourceInfo( |
| 94 | + producer_url=None, |
| 95 | + authentication_type=None, |
| 96 | + authentication_info_url=None, |
| 97 | + api_key_parameter_name=None, |
| 98 | + license_url=None, |
| 99 | + ), |
| 100 | + ) |
| 101 | + result = GbfsFeedImpl.from_orm(feed_orm) |
| 102 | + self.assertEqual(result, expected) |
| 103 | + |
| 104 | + def test_from_orm_none(self): |
| 105 | + result = GbfsFeedImpl.from_orm(None) |
| 106 | + self.assertIsNone(result) |
0 commit comments