Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Replaced [Factory]s with [StaticTestFactory]s.

Made DateTimeFieldParser.ParseDate static (it's pure)
  • Loading branch information...
commit acfca48649b9af8b17671578392a6b130e1df780 1 parent fb664e6
@mausch mausch authored
View
70 SolrNet.Tests/DateTimeFieldParserTests.cs
@@ -21,54 +21,52 @@
using MbUnit.Framework;
using SolrNet.Impl.FieldParsers;
using SolrNet.Impl.FieldSerializers;
+using SolrNet.Utils;
namespace SolrNet.Tests {
- [TestFixture]
public class DateTimeFieldParserTests {
- [Test]
- [PairwiseJoin]
- [Factory("DataFactory")]
- public void ParseYears(string d, DateTime dt) {
- var p = new DateTimeFieldParser();
- Assert.AreEqual(dt, p.ParseDate(d));
+ [StaticTestFactory]
+ public static IEnumerable<Test> ParseYears() {
+ return parsedDates.Select(pd => {
+ var name = "ParseYears " + pd.Key;
+ return new TestCase(name, () => Assert.AreEqual(pd.Value, DateTimeFieldParser.ParseDate(pd.Key)));
+ });
}
- public IEnumerable<object[]> DataFactory() {
- yield return new object[] { "1-01-01T00:00:00Z", new DateTime(1, 1, 1) };
- yield return new object[] { "2004-11-01T00:00:00Z", new DateTime(2004, 11, 1) };
- }
+ private static readonly IEnumerable<KeyValuePair<string, DateTime>> parsedDates =
+ new[] {
+ KV.Create("1-01-01T00:00:00Z", new DateTime(1, 1, 1)),
+ KV.Create("2004-11-01T00:00:00Z", new DateTime(2004, 11, 1)),
+ };
- [Test]
- [Factory("DateTimes")]
- public void RoundTrip(DateTime dt) {
- var parser = new DateTimeFieldParser();
+ [StaticTestFactory]
+ public static IEnumerable<Test> RoundTrip() {
var serializer = new DateTimeFieldSerializer();
- var s = serializer.SerializeDate(dt);
- Console.WriteLine(s);
- var value = parser.ParseDate(s);
- Console.WriteLine(value.ToString("r"));
- Assert.AreEqual(dt, value);
+ return dateTimes.Select(dt => new TestCase("RoundTrip " + dt, () => {
+ var value = DateTimeFieldParser.ParseDate(serializer.SerializeDate(dt));
+ Assert.AreEqual(dt, value);
+ }));
}
- [Test]
- [Factory("DateTimes")]
- public void NullableRoundTrips(DateTime? dt) {
+ [StaticTestFactory]
+ public static IEnumerable<Test> NullableRoundTrips() {
var parser = new NullableFieldParser(new DateTimeFieldParser());
var serializer = new NullableFieldSerializer(new DateTimeFieldSerializer());
- var s = serializer.Serialize(dt).First().FieldValue;
- Console.WriteLine(s);
- var xml = new XDocument();
- xml.Add(new XElement("date", s));
- var value = (DateTime?) parser.Parse(xml.Root, typeof(DateTime?));
- Console.WriteLine(value.Value.ToString("r"));
- Assert.AreEqual(dt, value);
+ return dateTimes.Select(dt => new TestCase("NullableRoundTrips " + dt, () => {
+ var s = serializer.Serialize(dt).First().FieldValue;
+ var xml = new XDocument();
+ xml.Add(new XElement("date", s));
+ var value = (DateTime?)parser.Parse(xml.Root, typeof(DateTime?));
+ Assert.AreEqual(dt, value);
+ }));
}
- public IEnumerable<object> DateTimes() {
- yield return new DateTime(1, 1, 1);
- yield return new DateTime(2004, 11, 1);
- yield return new DateTime(2004, 11, 1, 15, 41, 23);
- yield return new DateTime(2008, 5, 6, 14, 21, 23, 0, DateTimeKind.Local);
- }
+ private static readonly IEnumerable<DateTime> dateTimes =
+ new[] {
+ new DateTime(1, 1, 1),
+ new DateTime(2004, 11, 1),
+ new DateTime(2004, 11, 1, 15, 41, 23),
+ new DateTime(2008, 5, 6, 14, 21, 23, 0, DateTimeKind.Local),
+ };
}
}
View
68 SolrNet.Tests/LocalParamsTests.cs
@@ -1,4 +1,5 @@
#region license
+
// Copyright (c) 2007-2010 Mauricio Scheffer
//
// Licensed under the Apache License, Version 2.0 (the "License");
@@ -12,21 +13,46 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
+
#endregion
using System.Collections.Generic;
+using System.Linq;
using MbUnit.Framework;
using SolrNet.Exceptions;
using SolrNet.Impl.FieldSerializers;
using SolrNet.Impl.QuerySerializers;
+using SolrNet.Utils;
namespace SolrNet.Tests {
[TestFixture]
public class LocalParamsTests {
- [Test]
- [Factory("ParamsFactory")]
- public void Serialize(Dictionary<string, string> s, string result) {
- Assert.AreEqual(result, new LocalParams(s).ToString());
+ private static readonly IEnumerable<KeyValuePair<Dictionary<string, string>, string>> testParams =
+ new[] {
+ KV.Create(new Dictionary<string, string> {
+ {"type", "spatial"},
+ }, "{!type=spatial}"),
+ KV.Create(new Dictionary<string, string> {
+ {"type", "spatial"},
+ {"a", "b"},
+ }, "{!type=spatial a=b}"),
+ KV.Create(new Dictionary<string, string> {
+ {"type", "spatial"},
+ {"a", "1 2 3"},
+ }, "{!type=spatial a='1 2 3'}"),
+ KV.Create(new Dictionary<string, string> {
+ {"type", "spatial"},
+ {"a", "1 2 '3"},
+ }, "{!type=spatial a='1 2 \\'3'}"),
+ };
+
+ [StaticTestFactory]
+ public static IEnumerable<Test> Tests() {
+ return testParams.Select(dv => {
+ var expectedResult = dv.Value;
+ var localParams = dv.Key;
+ return new TestCase(expectedResult, () => Assert.AreEqual(expectedResult, new LocalParams(localParams).ToString()));
+ });
}
public string SerializeQuery(object q) {
@@ -34,40 +60,8 @@ public class LocalParamsTests {
return serializer.Serialize(q);
}
-
- public IEnumerable<object[]> ParamsFactory() {
- yield return new object[] {new Dictionary<string, string>(), ""};
-
- yield return new object[] {
- new Dictionary<string, string> {
- {"type", "spatial"},
- }, "{!type=spatial}"
- };
-
- yield return new object[] {
- new Dictionary<string, string> {
- {"type", "spatial"},
- {"a", "b"},
- }, "{!type=spatial a=b}"
- };
-
- yield return new object[] {
- new Dictionary<string, string> {
- {"type", "spatial"},
- {"a", "1 2 3"},
- }, "{!type=spatial a='1 2 3'}"
- };
-
- yield return new object[] {
- new Dictionary<string, string> {
- {"type", "spatial"},
- {"a", "1 2 '3"},
- }, "{!type=spatial a='1 2 \\'3'}"
- };
- }
-
[Test]
- [ExpectedException(typeof(SolrNetException))]
+ [ExpectedException(typeof (SolrNetException))]
public void NullValueThrows() {
var p = new LocalParams {
{"a", null}
View
6 SolrNet.Tests/SolrQueryResultsParserTests.cs
@@ -186,15 +186,13 @@ public partial class SolrQueryResultsParserTests {
[Test]
public void ParseDate_without_milliseconds() {
- var parser = new DateTimeFieldParser();
- var dt = parser.ParseDate("2001-01-02T03:04:05Z");
+ var dt = DateTimeFieldParser.ParseDate("2001-01-02T03:04:05Z");
Assert.AreEqual(new DateTime(2001, 1, 2, 3, 4, 5), dt);
}
[Test]
public void ParseDate_with_milliseconds() {
- var parser = new DateTimeFieldParser();
- var dt = parser.ParseDate("2001-01-02T03:04:05.245Z");
+ var dt = DateTimeFieldParser.ParseDate("2001-01-02T03:04:05.245Z");
Assert.AreEqual(new DateTime(2001, 1, 2, 3, 4, 5, 245), dt);
}
View
2  SolrNet/Impl/FieldParsers/DateTimeFieldParser.cs
@@ -37,7 +37,7 @@ public class DateTimeFieldParser : ISolrFieldParser {
return ParseDate(field.Value);
}
- public DateTime ParseDate(string s) {
+ public static DateTime ParseDate(string s) {
var p = s.Split('-');
s = p[0].PadLeft(4, '0') + '-' + string.Join("-", p.Skip(1).ToArray());
View
7 SolrNet/Impl/ResponseParsers/FacetsResponseParser.cs
@@ -93,7 +93,6 @@ public class FacetsResponseParser<T> : ISolrAbstractResponseParser<T> {
public DateFacetingResult ParseDateFacetingNode(XElement node) {
var r = new DateFacetingResult();
- var dateParser = new DateTimeFieldParser();
var intParser = new IntFieldParser();
foreach (var dateFacetingNode in node.Elements()) {
var name = dateFacetingNode.Attribute("name").Value;
@@ -102,7 +101,7 @@ public class FacetsResponseParser<T> : ISolrAbstractResponseParser<T> {
r.Gap = dateFacetingNode.Value;
break;
case "end":
- r.End = (DateTime) dateParser.Parse(dateFacetingNode, typeof (DateTime));
+ r.End = DateTimeFieldParser.ParseDate(dateFacetingNode.Value);
break;
default:
// Temp fix to support Solr 3.1, which has added a new element <date name="start">...</date>
@@ -117,8 +116,8 @@ public class FacetsResponseParser<T> : ISolrAbstractResponseParser<T> {
r.OtherResults[FacetDateOther.Before] = count;
else if (name == FacetDateOther.Between.ToString())
r.OtherResults[FacetDateOther.Between] = count;
- else {
- var d = dateParser.ParseDate(name);
+ else {
+ var d = DateTimeFieldParser.ParseDate(name);
r.DateResults.Add(KV.Create(d, count));
}
break;
Please sign in to comment.
Something went wrong with that request. Please try again.