Skip to content

Commit

Permalink
Added tests and support for issue #4
Browse files Browse the repository at this point in the history
ResolveIdentifiers will determine whether the Cause or Classification is
expecting an int or string.
  • Loading branch information
Joseph Plant authored and Joseph Plant committed Oct 10, 2013
1 parent d6dc6a9 commit 4b76aab
Show file tree
Hide file tree
Showing 16 changed files with 464 additions and 47 deletions.
7 changes: 6 additions & 1 deletion src/AmplaWeb.Data.Tests/AmplaWeb.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,20 @@
<Compile Include="Data\AmplaData2008\SimpleDataWebServiceClient.cs" />
<Compile Include="Data\AmplaData2008\SimpleDataWebServiceClientUnitTests.cs" />
<Compile Include="Data\AmplaRepository\AmplaRepositoryDefaultFilterUnitTests.cs" />
<Compile Include="Data\AmplaRepository\DowntimeAmplaRepositoryUnitTests.cs" />
<Compile Include="Data\Binding\ModelData\DowntimeModelPropertiesUnitTests.cs" />
<Compile Include="Data\Downtime\IdentifierDowntimeModel.cs" />
<Compile Include="Data\Downtime\ResolveIdentifiersAmplaRepositoryUnitTests.cs" />
<Compile Include="Data\Downtime\DowntimeAmplaRepositoryUnitTests.cs" />
<Compile Include="Data\Attributes\AmplaDefaultFiltersAttributeUnitTests.cs" />
<Compile Include="Data\Binding\Mapping\ValidatedModelFieldMappingUnitTests.cs" />
<Compile Include="Data\Binding\ModelData\ModelPropertyDefaultFilterUnitTests.cs" />
<Compile Include="Data\Binding\ModelData\LocationFilterUnitTests.cs" />
<Compile Include="Data\Binding\ModelData\Validation\RequiredLocationValidatorUnitTests.cs" />
<Compile Include="Data\Binding\ModelData\Validation\LocationValidatorUnitTests.cs" />
<Compile Include="Data\Downtime\SimpleDowntimeModel.cs" />
<Compile Include="Data\FilterValueUnitTests.cs" />
<Compile Include="Data\Records\DowntimeRecords.cs" />
<Compile Include="Data\Records\FieldValueUnitTests.cs" />
<Compile Include="Data\Records\Filters\FieldFilterMatcher.cs" />
<Compile Include="Data\Records\Filters\FilterMatcher.cs" />
<Compile Include="Data\Records\Filters\IdFilterMatcher.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public SimpleDataWebServiceClient(string module, string[] locations)
this.module = Convert.ToString(amplaModule);
reportingPoints = locations;

GetViewFunc = ProductionViews.EmptyView;
GetViewFunc = StandardViews.EmptyView;
}

/// <summary>
Expand All @@ -69,6 +69,7 @@ public GetDataResponse GetData(GetDataRequest request)
InMemoryFilterMatcher filterMatcher = new InMemoryFilterMatcher(request.Filter);

List<InMemoryRecord> recordsToReturn = database.Values.Where(filterMatcher.Matches).ToList();
bool resolveIdentifiers = request.OutputOptions.ResolveIdentifiers;

List<Row> rows = new List<Row>();
foreach (InMemoryRecord record in recordsToReturn)
Expand All @@ -79,7 +80,7 @@ public GetDataResponse GetData(GetDataRequest request)
{
string name = XmlConvert.EncodeName(value.Name);
XmlElement element = xmlDoc.CreateElement(name, "http://www.citect.com/Ampla/Data/2008/06");
element.InnerText = value.Value;
element.InnerText = value.ResolveValue(resolveIdentifiers);
values.Add(element);
}
row.Any = values.ToArray();
Expand Down Expand Up @@ -410,7 +411,18 @@ public List<InMemoryRecord> DatabaseRecords
}
}

public Credentials CreateCredentials()
public int AddExistingRecord(InMemoryRecord record)
{
setId++;

InMemoryRecord clone = record.Clone();
clone.RecordId = setId;

database[setId] = clone;
return setId;
}

internal Credentials CreateCredentials()
{
return new Credentials { Username = userName, Password = password };
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

using System;
using System.ServiceModel;
using AmplaWeb.Data.Records;
using NUnit.Framework;
Expand Down Expand Up @@ -141,6 +142,82 @@ public void GetNavigationHierarchyTwoLocations()
CheckViewPoints(response.Hierarchy.ViewPoints[0].ViewPoints[0].ViewPoints[0].ReportingPoints, "Plant.Area.Equipment", "Production");
}

[Test]
public void GetData_ResolveIdentifiers_True()
{
InMemoryRecord record = DowntimeRecords.NewRecord().MarkAsNew();
record.SetFieldIdValue("Cause", "Shutdown", 100);
record.SetFieldIdValue("Classification", "Unplanned Process", 200);

SimpleDataWebServiceClient webServiceClient = new SimpleDataWebServiceClient(record.Module,
record.Location);
record.SaveTo(webServiceClient);

Assert.That(webServiceClient.DatabaseRecords, Is.Not.Empty);

string recordId = Convert.ToString(webServiceClient.DatabaseRecords[0].RecordId);

GetDataRequest request = new GetDataRequest
{
Credentials = CreateCredentials(),
Filter = new DataFilter {Location = record.Location, Criteria = new [] { new FilterEntry {Name = "Id", Value = recordId}}},
View = new GetDataView { Context = NavigationContext.Plant, Mode = NavigationMode.Location, Module = AmplaModules.Downtime},
Metadata = true,
OutputOptions = new GetDataOutputOptions {ResolveIdentifiers = true}
};
GetDataResponse response = webServiceClient.GetData(request);

AssertResponseContainsValue(response, "Cause", "Shutdown");
AssertResponseContainsValue(response, "Classification", "Unplanned Process");
}

[Test]
public void GetData_ResolveIdentifiers_False()
{
InMemoryRecord record = DowntimeRecords.NewRecord().MarkAsNew();
record.SetFieldIdValue("Cause", "Shutdown", 100);
record.SetFieldIdValue("Classification", "Unplanned Process", 200);

SimpleDataWebServiceClient webServiceClient = new SimpleDataWebServiceClient(record.Module,
record.Location);
record.SaveTo(webServiceClient);

Assert.That(webServiceClient.DatabaseRecords, Is.Not.Empty);


string recordId = Convert.ToString(webServiceClient.DatabaseRecords[0].RecordId);

GetDataRequest request = new GetDataRequest
{
Credentials = CreateCredentials(),
Filter = new DataFilter { Location = record.Location, Criteria = new[] { new FilterEntry { Name = "Id", Value = recordId } } },
View = new GetDataView { Context = NavigationContext.Plant, Mode = NavigationMode.Location, Module = AmplaModules.Downtime },
Metadata = true,
OutputOptions = new GetDataOutputOptions { ResolveIdentifiers = false },
};
GetDataResponse response = webServiceClient.GetData(request);
AssertResponseContainsValue(response, "Cause", "100");
AssertResponseContainsValue(response, "Classification", "200");
}

private void AssertResponseContainsValue(GetDataResponse response, string field, string value)
{
Assert.That(response.RowSets, Is.Not.Empty);
Assert.That(response.RowSets[0].Rows, Is.Not.Empty);
Assert.That(response.RowSets[0].Rows[0].Any, Is.Not.Empty);
int found = 0;
foreach (var rowValue in response.RowSets[0].Rows[0].Any)
{
if (rowValue.Name == field)
{
Assert.That(rowValue.InnerText, Is.EqualTo(value), "Field {0} value is not set", field);
found++;
}
}
Assert.That(found, Is.EqualTo(1), "Field: {0} not found", field);
}


private static void CheckViewPoints(ViewPoint[] points, string parent, params string[] names)
{
Assert.That(points, Is.Not.Empty, parent);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System;
using AmplaWeb.Data.Attributes;
using NUnit.Framework;

namespace AmplaWeb.Data.Binding.ModelData
{
[TestFixture]
public class DowntimeModelPropertiesUnitTests : TestFixture
{
[AmplaLocation(Location = "Plant.Area.Downtime")]
[AmplaModule(Module = "Downtime")]
public class StringDowntimeModel
{
public int Id { get; set; }
public string Cause { get; set; }
public string Classification { get; set; }
}

[AmplaLocation(Location = "Plant.Area.Downtime")]
[AmplaModule(Module = "Downtime")]
public class IntDowntimeModel
{
public int Id { get; set; }
public int Cause { get; set; }
public int Classification { get; set; }
}

[Test]
public void IdModelProperties()
{
ModelProperties<IntDowntimeModel> modelProperties = new ModelProperties<IntDowntimeModel>();
Assert.That(modelProperties.ResolveIdentifiers, Is.EqualTo(false));
}

[Test]
public void StringModelProperties()
{
ModelProperties<StringDowntimeModel> modelProperties = new ModelProperties<StringDowntimeModel>();
Assert.That(modelProperties.ResolveIdentifiers, Is.EqualTo(true));
}

}
}
Original file line number Diff line number Diff line change
@@ -1,32 +1,14 @@
using System;
using AmplaWeb.Data.Attributes;
using AmplaWeb.Data.AmplaRepository;
using AmplaWeb.Data.Records;
using AmplaWeb.Data.Views;
using NUnit.Framework;

namespace AmplaWeb.Data.AmplaRepository
namespace AmplaWeb.Data.Downtime
{
[TestFixture]
public class DowntimeAmplaRepositoryUnitTests : AmplaRepositoryTestFixture<DowntimeAmplaRepositoryUnitTests.SimpleDowntimeModel>
public class DowntimeAmplaRepositoryUnitTests : AmplaRepositoryTestFixture<SimpleDowntimeModel>
{
[AmplaLocation(Location = "Enterprise.Site.Area.Point")]
[AmplaModule(Module = "Downtime")]
public class SimpleDowntimeModel
{
public int Id { get; set; }
public string Location { get; set; }

[AmplaField(Field="Start Time")]
public DateTime StartTime { get; set; }

[AmplaField(Field = "Cause Location")]
public string CauseLocation { get; set; }

public string Cause { get; set; }

public string Classification { get; set; }
}

private const string module = "Downtime";
private const string location = "Enterprise.Site.Area.Point";

Expand Down Expand Up @@ -77,8 +59,11 @@ public void SubmitWithValidCauseLocation()
Assert.That(record.GetFieldValue("Cause Location", string.Empty), Is.EqualTo("Enterprise.Site"));
}

/// <summary>
/// Cause will read the value as string but will only write as a int
/// </summary>
[Test]
public void SubmitWithCause()
public void SubmitWithCauseAsString()
{
SimpleDowntimeModel model = new SimpleDowntimeModel { Location = location, StartTime = DateTime.Now, Cause = "Broken"};
Repository.Add(model);
Expand All @@ -89,11 +74,11 @@ public void SubmitWithCause()

InMemoryRecord record = Records[0];
Assert.That(record.Location, Is.EqualTo(location));
Assert.That(record.GetFieldValue("Cause", string.Empty), Is.EqualTo("Broken"));
Assert.That(record.Find("Cause"), Is.Null);
}

[Test]
public void SubmitWithClassification()
public void SubmitWithClassificationAsString()
{
SimpleDowntimeModel model = new SimpleDowntimeModel { Location = location, StartTime = DateTime.Now, Classification = "Unplanned Process" };
Repository.Add(model);
Expand All @@ -104,7 +89,7 @@ public void SubmitWithClassification()

InMemoryRecord record = Records[0];
Assert.That(record.Location, Is.EqualTo(location));
Assert.That(record.GetFieldValue("Classification", string.Empty), Is.EqualTo("Unplanned Process"));
Assert.That(record.Find("Classification"), Is.Null);
}

}
Expand Down
26 changes: 26 additions & 0 deletions src/AmplaWeb.Data.Tests/Data/Downtime/IdentifierDowntimeModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using AmplaWeb.Data.Attributes;

namespace AmplaWeb.Data.Downtime
{
[AmplaLocation(Location = "Plant.Area.Downtime")]
[AmplaModule(Module = "Downtime")]
public class IdentifierDowntimeModel
{
public int Id { get; set; }
public string Location { get; set; }

[AmplaField(Field = "Start Time")]
public DateTime StartTime { get; set; }

[AmplaField(Field = "Cause Location")]
public string CauseLocation { get; set; }

/// <summary>
/// Uso
/// </summary>
public int Cause { get; set; }

public int Classification { get; set; }
}
}
Loading

0 comments on commit 4b76aab

Please sign in to comment.