|
| 1 | +package tests |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "testing" |
| 8 | + |
| 9 | + gbfs "github.com/MobilityData/gbfs-json-schema/models/golang/v3.1-RC2/gbfs" |
| 10 | + gbfsversions "github.com/MobilityData/gbfs-json-schema/models/golang/v3.1-RC2/gbfs_versions" |
| 11 | + geofencingzones "github.com/MobilityData/gbfs-json-schema/models/golang/v3.1-RC2/geofencing_zones" |
| 12 | + manifest "github.com/MobilityData/gbfs-json-schema/models/golang/v3.1-RC2/manifest" |
| 13 | + stationinformation "github.com/MobilityData/gbfs-json-schema/models/golang/v3.1-RC2/station_information" |
| 14 | + stationstatus "github.com/MobilityData/gbfs-json-schema/models/golang/v3.1-RC2/station_status" |
| 15 | + systemalerts "github.com/MobilityData/gbfs-json-schema/models/golang/v3.1-RC2/system_alerts" |
| 16 | + systeminformation "github.com/MobilityData/gbfs-json-schema/models/golang/v3.1-RC2/system_information" |
| 17 | + systempricingplans "github.com/MobilityData/gbfs-json-schema/models/golang/v3.1-RC2/system_pricing_plans" |
| 18 | + systemregions "github.com/MobilityData/gbfs-json-schema/models/golang/v3.1-RC2/system_regions" |
| 19 | + vehicleavailability "github.com/MobilityData/gbfs-json-schema/models/golang/v3.1-RC2/vehicle_availability" |
| 20 | + vehiclestatus "github.com/MobilityData/gbfs-json-schema/models/golang/v3.1-RC2/vehicle_status" |
| 21 | + vehicletypes "github.com/MobilityData/gbfs-json-schema/models/golang/v3.1-RC2/vehicle_types" |
| 22 | + |
| 23 | + "github.com/xeipuuv/gojsonschema" |
| 24 | +) |
| 25 | + |
| 26 | +const pathToTestFixturesV31RC2 = "./../../../testFixtures/v3.1-RC2/" |
| 27 | + |
| 28 | +func LoadSchemaAndFixtureV31RC2(t *testing.T, fileName string) (gojsonschema.JSONLoader, []byte) { |
| 29 | + |
| 30 | + pathToSchema := "./../../../v3.1-RC2/" |
| 31 | + schemaDataBytes, schemaErr := os.ReadFile(pathToSchema + fileName) |
| 32 | + if schemaErr != nil { |
| 33 | + t.Error("Error opening JSON file:", schemaErr) |
| 34 | + return nil, nil |
| 35 | + } |
| 36 | + var schemaData map[string]interface{} |
| 37 | + schemaErr = json.Unmarshal(schemaDataBytes, &schemaData) |
| 38 | + if schemaErr != nil { |
| 39 | + t.Error("Error opening JSON file:", schemaErr) |
| 40 | + return nil, nil |
| 41 | + } |
| 42 | + schemaLoader := gojsonschema.NewGoLoader(schemaData) |
| 43 | + jsonData, err2 := os.ReadFile(pathToTestFixturesV31RC2 + fileName) |
| 44 | + if err2 != nil { |
| 45 | + t.Error("Error opening JSON file:", err2) |
| 46 | + return nil, nil |
| 47 | + } |
| 48 | + return schemaLoader, jsonData |
| 49 | +} |
| 50 | + |
| 51 | +func ValidateSchemaToUnmarshalV31RC2(t *testing.T, schemaLoader gojsonschema.JSONLoader, gbfsData interface{}) { |
| 52 | + loader := gojsonschema.NewGoLoader(gbfsData) |
| 53 | + result, err3 := gojsonschema.Validate(schemaLoader, loader) |
| 54 | + if err3 != nil { |
| 55 | + t.Error("Error validating JSON file:", err3) |
| 56 | + return |
| 57 | + } |
| 58 | + |
| 59 | + if !result.Valid() { |
| 60 | + t.Error("The JSON is NOT valid:") |
| 61 | + for _, desc := range result.Errors() { |
| 62 | + fmt.Printf("- %s\n", desc) |
| 63 | + } |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +func TestGbfsV31RC2(t *testing.T) { |
| 68 | + schemaLoader, jsonData := LoadSchemaAndFixtureV31RC2(t, "gbfs.json") |
| 69 | + gbfsData, err := gbfs.UnmarshalGbfs(jsonData) |
| 70 | + if err != nil { |
| 71 | + t.Error("Error With Unmarshal:", err) |
| 72 | + return |
| 73 | + } |
| 74 | + ValidateSchemaToUnmarshalV31RC2(t, schemaLoader, gbfsData) |
| 75 | +} |
| 76 | + |
| 77 | +func TestGbfsVersionsV31RC2(t *testing.T) { |
| 78 | + schemaLoader, jsonData := LoadSchemaAndFixtureV31RC2(t, "gbfs_versions.json") |
| 79 | + gbfsData, err := gbfsversions.UnmarshalGbfsVersions(jsonData) |
| 80 | + if err != nil { |
| 81 | + t.Error("Error UnmarshalGbfs:", err) |
| 82 | + return |
| 83 | + } |
| 84 | + ValidateSchemaToUnmarshalV31RC2(t, schemaLoader, gbfsData) |
| 85 | +} |
| 86 | + |
| 87 | +func TestGeofencingZonesV31RC2(t *testing.T) { |
| 88 | + schemaLoader, jsonData := LoadSchemaAndFixtureV31RC2(t, "geofencing_zones.json") |
| 89 | + gbfsData, err := geofencingzones.UnmarshalGeofencingZones(jsonData) |
| 90 | + if err != nil { |
| 91 | + t.Error("Error UnmarshalGbfs:", err) |
| 92 | + return |
| 93 | + } |
| 94 | + ValidateSchemaToUnmarshalV31RC2(t, schemaLoader, gbfsData) |
| 95 | +} |
| 96 | + |
| 97 | +func TestManifestV31RC2(t *testing.T) { |
| 98 | + schemaLoader, jsonData := LoadSchemaAndFixtureV31RC2(t, "manifest.json") |
| 99 | + gbfsData, err := manifest.UnmarshalManifest(jsonData) |
| 100 | + if err != nil { |
| 101 | + t.Error("Error UnmarshalGbfs:", err) |
| 102 | + return |
| 103 | + } |
| 104 | + ValidateSchemaToUnmarshalV31RC2(t, schemaLoader, gbfsData) |
| 105 | +} |
| 106 | + |
| 107 | +func TestStationInformationV31RC2(t *testing.T) { |
| 108 | + schemaLoader, jsonData := LoadSchemaAndFixtureV31RC2(t, "station_information.json") |
| 109 | + gbfsData, err := stationinformation.UnmarshalStationInformation(jsonData) |
| 110 | + if err != nil { |
| 111 | + t.Error("Error UnmarshalGbfs:", err) |
| 112 | + return |
| 113 | + } |
| 114 | + ValidateSchemaToUnmarshalV31RC2(t, schemaLoader, gbfsData) |
| 115 | +} |
| 116 | + |
| 117 | +func TestStationStatusV31RC2(t *testing.T) { |
| 118 | + schemaLoader, jsonData := LoadSchemaAndFixtureV31RC2(t, "station_status.json") |
| 119 | + gbfsData, err := stationstatus.UnmarshalStationStatus(jsonData) |
| 120 | + if err != nil { |
| 121 | + t.Error("Error UnmarshalGbfs:", err) |
| 122 | + return |
| 123 | + } |
| 124 | + ValidateSchemaToUnmarshalV31RC2(t, schemaLoader, gbfsData) |
| 125 | +} |
| 126 | + |
| 127 | +func TestSystemAlertsV31RC2(t *testing.T) { |
| 128 | + schemaLoader, jsonData := LoadSchemaAndFixtureV31RC2(t, "system_alerts.json") |
| 129 | + gbfsData, err := systemalerts.UnmarshalSystemAlerts(jsonData) |
| 130 | + if err != nil { |
| 131 | + t.Error("Error UnmarshalGbfs:", err) |
| 132 | + return |
| 133 | + } |
| 134 | + ValidateSchemaToUnmarshalV31RC2(t, schemaLoader, gbfsData) |
| 135 | +} |
| 136 | + |
| 137 | +func TestSystemInformationV31RC2(t *testing.T) { |
| 138 | + schemaLoader, jsonData := LoadSchemaAndFixtureV31RC2(t, "system_information.json") |
| 139 | + gbfsData, err := systeminformation.UnmarshalSystemInformation(jsonData) |
| 140 | + if err != nil { |
| 141 | + t.Error("Error UnmarshalGbfs:", err) |
| 142 | + return |
| 143 | + } |
| 144 | + ValidateSchemaToUnmarshalV31RC2(t, schemaLoader, gbfsData) |
| 145 | +} |
| 146 | + |
| 147 | +func TestSystemPricingPlanV31RC2(t *testing.T) { |
| 148 | + schemaLoader, jsonData := LoadSchemaAndFixtureV31RC2(t, "system_pricing_plans.json") |
| 149 | + gbfsData, err := systempricingplans.UnmarshalSystemPricingPlans(jsonData) |
| 150 | + if err != nil { |
| 151 | + t.Error("Error UnmarshalGbfs:", err) |
| 152 | + return |
| 153 | + } |
| 154 | + ValidateSchemaToUnmarshalV31RC2(t, schemaLoader, gbfsData) |
| 155 | +} |
| 156 | + |
| 157 | +func TestSystemRegionsV31RC2(t *testing.T) { |
| 158 | + schemaLoader, jsonData := LoadSchemaAndFixtureV31RC2(t, "system_regions.json") |
| 159 | + gbfsData, err := systemregions.UnmarshalSystemRegions(jsonData) |
| 160 | + if err != nil { |
| 161 | + t.Error("Error UnmarshalGbfs:", err) |
| 162 | + return |
| 163 | + } |
| 164 | + ValidateSchemaToUnmarshalV31RC2(t, schemaLoader, gbfsData) |
| 165 | +} |
| 166 | + |
| 167 | +func TestVehicleStatusV31RC2(t *testing.T) { |
| 168 | + schemaLoader, jsonData := LoadSchemaAndFixtureV31RC2(t, "vehicle_status.json") |
| 169 | + gbfsData, err := vehiclestatus.UnmarshalVehicleStatus(jsonData) |
| 170 | + if err != nil { |
| 171 | + t.Error("Error UnmarshalGbfs:", err) |
| 172 | + return |
| 173 | + } |
| 174 | + ValidateSchemaToUnmarshalV31RC2(t, schemaLoader, gbfsData) |
| 175 | +} |
| 176 | + |
| 177 | +func TestVehicleTypesV31RC2(t *testing.T) { |
| 178 | + schemaLoader, jsonData := LoadSchemaAndFixtureV31RC2(t, "vehicle_types.json") |
| 179 | + gbfsData, err := vehicletypes.UnmarshalVehicleTypes(jsonData) |
| 180 | + if err != nil { |
| 181 | + t.Error("Error UnmarshalGbfs:", err) |
| 182 | + return |
| 183 | + } |
| 184 | + ValidateSchemaToUnmarshalV31RC2(t, schemaLoader, gbfsData) |
| 185 | +} |
| 186 | + |
| 187 | +func TestVehicleAvailabilityV31RC2(t *testing.T) { |
| 188 | + schemaLoader, jsonData := LoadSchemaAndFixtureV31RC2(t, "vehicle_availability.json") |
| 189 | + gbfsData, err := vehicleavailability.UnmarshalVehicleAvailability(jsonData) |
| 190 | + if err != nil { |
| 191 | + t.Error("Error UnmarshalGbfs:", err) |
| 192 | + return |
| 193 | + } |
| 194 | + ValidateSchemaToUnmarshalV31RC2(t, schemaLoader, gbfsData) |
| 195 | +} |
0 commit comments