Skip to content

Commit

Permalink
fixed issue #35: Agency_id is required if multiple agencies in agency…
Browse files Browse the repository at this point in the history
….txt
  • Loading branch information
Ranko Orlic committed Feb 16, 2016
1 parent 02be44b commit 8480192
Show file tree
Hide file tree
Showing 8 changed files with 184 additions and 1 deletion.
21 changes: 21 additions & 0 deletions GTFS.Test/GTFS.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
<Compile Include="GTFSFeedTests.cs" />
<Compile Include="FileNotDisposedTest.cs" />
<Compile Include="ParseFeedTests.cs" />
<Compile Include="ParseAgenciesTests.cs" />
<Compile Include="ParseRoutesTests.cs" />
<Compile Include="ParseEntitiesTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down Expand Up @@ -183,6 +184,26 @@
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="other-feed\agency_with_id.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="other-feed\agency_no_id.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="other-feed\agencies_no_id.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="other-feed\agencies_with_id.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</EmbeddedResource>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
141 changes: 141 additions & 0 deletions GTFS.Test/ParseAgenciesTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
// The MIT License (MIT)

// Copyright (c) 2014 Ben Abelshausen

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

using GTFS.IO;
using GTFS.IO.CSV;
using NUnit.Framework;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using GTFS.Exceptions;

namespace GTFS.Test
{
/// <summary>
/// Contains additional parsing tests for agencies.
/// </summary>
[TestFixture]
public class ParseAgenciesTests
{
private static Assembly _executingAssembly;

/// <summary>
/// Setup the test environment.
/// </summary>
[OneTimeSetUp]
public static void Setup()
{
_executingAssembly = Assembly.GetExecutingAssembly();
}

/// <summary>
/// Tests parsing single agency without agency_id.
/// </summary>
[Test]
public void ParseSingleAgencyWithoutAgencyId()
{
var reader = new GTFSReader<GTFSFeed>();
var source = new List<IGTFSSourceFile>
{
new GTFSSourceFileStream(_executingAssembly.GetManifestResourceStream("GTFS.Test.other_feed.agency_no_id.txt"),"agency")
};


var feed = reader.Read(source, source.First(x => x.Name.Equals("agency")));


var agencies = feed.Agencies;
Assert.IsNotNull(agencies);

var agency = agencies.SingleOrDefault();
Assert.IsNotNull(agency);
Assert.IsNull(agency.Id);
}

/// <summary>
/// Tests parsing single agency with agency_id.
/// </summary>
[Test]
public void ParseSingleAgencyWithAgencyId()
{
var reader = new GTFSReader<GTFSFeed>();
var source = new List<IGTFSSourceFile>
{
new GTFSSourceFileStream(_executingAssembly.GetManifestResourceStream("GTFS.Test.other_feed.agency_with_id.txt"),"agency")
};


var feed = reader.Read(source, source.First(x => x.Name.Equals("agency")));


var agencies = feed.Agencies;
Assert.IsNotNull(agencies);

var agency = agencies.SingleOrDefault();
Assert.IsNotNull(agency);
Assert.IsNotNull(agency.Id);
}

/// <summary>
/// Tests parsing multiple agencies with agency_id.
/// </summary>
[Test]
public void ParseAgenciesWithAgencyId()
{
var reader = new GTFSReader<GTFSFeed>();
var source = new List<IGTFSSourceFile>
{
new GTFSSourceFileStream(_executingAssembly.GetManifestResourceStream("GTFS.Test.other_feed.agencies_with_id.txt"),"agency")
};


var feed = reader.Read(source, source.First(x => x.Name.Equals("agency")));


var agencies = feed.Agencies;
Assert.IsNotNull(agencies);
Assert.IsNotNull(agencies.SingleOrDefault(x => x.Id == "DTA"));
Assert.IsNotNull(agencies.SingleOrDefault(x => x.Id == "OTA"));
}

/// <summary>
/// Tests parsing multiple agencies with agency_id.
/// </summary>
[Test]
public void ParseAgenciesWithoutAgencyId()
{
const string Agency = "agency";
var reader = new GTFSReader<GTFSFeed>();
var source = new List<IGTFSSourceFile>
{
new GTFSSourceFileStream(_executingAssembly.GetManifestResourceStream("GTFS.Test.other_feed.agencies_no_id.txt"),Agency)
};


Assert.Throws<GTFSRequiredFieldMissingException>(() =>
{
reader.Read(source, source.First(x => x.Name.Equals(Agency)));
},
GTFSRequiredFieldMissingException.MessageFormat, "agency_id", Agency);
}
}
}
3 changes: 3 additions & 0 deletions GTFS.Test/other-feed/agencies_no_id.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
agency_name,agency_url,agency_timezone
Demo Transit Authority,http://google.com,America/Los_Angeles
Other Transit Authority,http://somewhere.com,Europe/Brussels
3 changes: 3 additions & 0 deletions GTFS.Test/other-feed/agencies_with_id.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
agency_id,agency_name,agency_url,agency_timezone
DTA,Demo Transit Authority,http://google.com,America/Los_Angeles
OTA,Other Transit Authority,http://somewhere.com,Europe/Brussels
2 changes: 2 additions & 0 deletions GTFS.Test/other-feed/agency_no_id.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
agency_name,agency_url,agency_timezone
Demo Transit Authority,http://google.com,America/Los_Angeles
2 changes: 2 additions & 0 deletions GTFS.Test/other-feed/agency_with_id.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
agency_id,agency_name,agency_url,agency_timezone
DTA,Demo Transit Authority,http://google.com,America/Los_Angeles
7 changes: 6 additions & 1 deletion GTFS/Exceptions/GTFSRequiredFieldMissingException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,18 @@ namespace GTFS.Exceptions
/// </summary>
public class GTFSRequiredFieldMissingException : GTFSExceptionBase
{
/// <summary>
/// Message format used for formatting the exception.
/// </summary>
public static readonly string MessageFormat = "Required field {0} not found in {1}.";

/// <summary>
/// Creates a new field missing exception.
/// </summary>
/// <param name="name"></param>
/// <param name="fieldName"></param>
public GTFSRequiredFieldMissingException(string name, string fieldName)
: base(string.Format("Required field {0} not found in {1}.", fieldName, name))
: base(string.Format(MessageFormat, fieldName, name))
{
this.Name = name;
this.FieldName = fieldName;
Expand Down
6 changes: 6 additions & 0 deletions GTFS/GTFSReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,12 @@ protected virtual Agency ParseAgency(T feed, GTFSSourceFileHeader header, string
this.CheckRequiredField(header, header.Name, this.AgencyMap, "agency_url");
this.CheckRequiredField(header, header.Name, this.AgencyMap, "agency_timezone");

// if we already have another agency, then the agency_id is required
if (feed.Agencies.Any())
{
CheckRequiredField(header, header.Name, AgencyMap, "agency_id");
}

// parse/set all fields.
Agency agency = new Agency();
for(int idx = 0; idx < data.Length; idx++)
Expand Down

0 comments on commit 8480192

Please sign in to comment.