Skip to content

Commit

Permalink
Merge pull request #184 from mfenniak/issue_154
Browse files Browse the repository at this point in the history
Support for DateTime constructors in expressions; fixes #154.
  • Loading branch information
mfenniak committed Oct 31, 2014
2 parents aecbd58 + 68da8de commit e3cb24f
Show file tree
Hide file tree
Showing 10 changed files with 521 additions and 60 deletions.
4 changes: 4 additions & 0 deletions RELEASE-NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@

* Runtime errors in expressions can be added with ReQLExpression.Error() in an expression tree. [Issue #49](https://github.com/mfenniak/rethinkdb-net/issues/49)

* Now supports most DateTime and DateTimeOffset constructors server-side to create ReQL time objects. This allows for an operation like ```table.Group(obj => new DateTime(obj.CreatedAt.Year, obj.CreatedAt.Month, obj.CreatedAt.Day))```; this would group all the records in a table by the date they were created. [PR #184](https://github.com/mfenniak/rethinkdb-net/issues/184) & [Issue #154](https://github.com/mfenniak/rethinkdb-net/issues/154)

### Breaking Changes

* Added support for RethinkDB's binary format for byte[] conversion. If byte[] was previously used by a client application, reading and writing it will begin using a different and incompatible data format. Either migrate your data to the new format, or construct a datum converter that doesn't include the new BinaryDatumConverterFactory in it. [Issue #178](https://github.com/mfenniak/rethinkdb-net/issues/178)

* Fixed a bug in how DateTimeOffset objects are serialized to and from RethinkDB; they previously assumed the offset was incorporated into the epoch_time, where that was incorrect. This may change data being retrieved or stored with this data type. [PR #184](https://github.com/mfenniak/rethinkdb-net/issues/184)


## 0.9.0.0 (2014-10-26)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void ser_deser_a_datetimeoffset()
r_object =
{
new Datum.AssocPair {key = "$reql_type$", val = new Datum {type = Datum.DatumType.R_STR, r_str = "TIME"}},
new Datum.AssocPair {key = "epoch_time", val = new Datum {type = Datum.DatumType.R_NUM, r_num = 1383149522}},
new Datum.AssocPair {key = "epoch_time", val = new Datum {type = Datum.DatumType.R_NUM, r_num = 1383174722}},
new Datum.AssocPair {key = "timezone", val = new Datum {type = Datum.DatumType.R_STR, r_str = "-07:00"}},
}
}
Expand Down Expand Up @@ -126,7 +126,7 @@ public void ser_deser_datetimeoffsetnullable_dateset()
r_object =
{
new Datum.AssocPair {key = "$reql_type$", val = new Datum {type = Datum.DatumType.R_STR, r_str = "TIME"}},
new Datum.AssocPair {key = "epoch_time", val = new Datum {type = Datum.DatumType.R_NUM, r_num = 1383149522}},
new Datum.AssocPair {key = "epoch_time", val = new Datum {type = Datum.DatumType.R_NUM, r_num = 1383174722}},
new Datum.AssocPair {key = "timezone", val = new Datum {type = Datum.DatumType.R_STR, r_str = "-07:00"}},
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public void datum_to_datetimeoffset_test()
val = new Datum()
{
type = Datum.DatumType.R_NUM,
r_num = 1376704156.123
r_num = 1376716756.123
}
});
datum.r_object.Add(new Datum.AssocPair()
Expand Down Expand Up @@ -199,7 +199,7 @@ private Datum GetDefaultDatum()
r_object =
{
new Datum.AssocPair {key = "$reql_type$", val = new Datum {type = Datum.DatumType.R_STR, r_str = "TIME"}},
new Datum.AssocPair {key = "epoch_time", val = new Datum {type = Datum.DatumType.R_NUM, r_num = 1382819222}},
new Datum.AssocPair {key = "epoch_time", val = new Datum {type = Datum.DatumType.R_NUM, r_num = 1382844422}},
new Datum.AssocPair {key = "timezone", val = new Datum {type = Datum.DatumType.R_STR, r_str = "-07:00"}},
}
}
Expand All @@ -221,7 +221,7 @@ private Datum GetDefaultDatum()
r_object =
{
new Datum.AssocPair {key = "$reql_type$", val = new Datum {type = Datum.DatumType.R_STR, r_str = "TIME"}},
new Datum.AssocPair {key = "epoch_time", val = new Datum {type = Datum.DatumType.R_NUM, r_num = 1382819222}},
new Datum.AssocPair {key = "epoch_time", val = new Datum {type = Datum.DatumType.R_NUM, r_num = 1382844422}},
new Datum.AssocPair {key = "timezone", val = new Datum {type = Datum.DatumType.R_STR, r_str = "-07:00"}},
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System;
using System.Linq;
using FluentAssertions;
using NUnit.Framework;
using RethinkDb.DatumConverters;
using RethinkDb.Spec;
Expand All @@ -9,56 +9,103 @@ namespace RethinkDb.Test.DatumConverters
[TestFixture]
public class DateTimeOffsetDatumConverterTests
{
[Test]
public void ConvertDatum()
{
var datum = new Datum() {
type = Datum.DatumType.R_OBJECT
};
datum.r_object.Add(new Datum.AssocPair() {
key = "$reql_type$",
val = new Datum() {
type = Datum.DatumType.R_STR,
r_str = "TIME"
// r.time(2014, 10, 30, 6, 1, 2.5, '-06:00')
// Thu Oct 30 2014 06:01:02 GMT-06:00
// {"$reql_type$":"TIME","epoch_time":1414670462.5,"timezone":"-06:00"}
private static DateTimeOffset objectInLocal = new DateTimeOffset(
new DateTime(2014, 10, 30, 6, 1, 2, 500),
TimeSpan.FromHours(-6));
private static Datum datumInLocal = new Datum() {
type = Datum.DatumType.R_OBJECT,
r_object = {
new Datum.AssocPair() {
key = "$reql_type$",
val = new Datum() {
type = Datum.DatumType.R_STR,
r_str = "TIME"
}
},
new Datum.AssocPair() {
key = "epoch_time",
val = new Datum() {
type = Datum.DatumType.R_NUM,
r_num = 1414670462.5
}
},
new Datum.AssocPair() {
key = "timezone",
val = new Datum() {
type = Datum.DatumType.R_STR,
r_str = "-06:00"
}
}
});
datum.r_object.Add(new Datum.AssocPair() {
key = "epoch_time",
val = new Datum() {
type = Datum.DatumType.R_NUM,
r_num = 1376704156.123
}
});
datum.r_object.Add(new Datum.AssocPair() {
key = "timezone",
val = new Datum() {
type = Datum.DatumType.R_STR,
r_str = "-03:30"
}
};

// r.time(2014, 10, 30, 12, 1, 2.5, 'Z')
// Thu Oct 30 2014 12:01:02 GMT+00:00
// {"$reql_type$":"TIME","epoch_time":1414670462.5,"timezone":"+00:00"}
private static DateTimeOffset objectInUtc = new DateTimeOffset(
new DateTime(2014, 10, 30, 12, 1, 2, 500),
TimeSpan.Zero);
private static Datum datumInUtc = new Datum() {
type = Datum.DatumType.R_OBJECT,
r_object = {
new Datum.AssocPair() {
key = "$reql_type$",
val = new Datum() {
type = Datum.DatumType.R_STR,
r_str = "TIME"
}
},
new Datum.AssocPair() {
key = "epoch_time",
val = new Datum() {
type = Datum.DatumType.R_NUM,
r_num = 1414670462.5
}
},
new Datum.AssocPair() {
key = "timezone",
val = new Datum() {
type = Datum.DatumType.R_STR,
r_str = "+00:00"
}
}
});
}
};

var result = DateTimeOffsetDatumConverterFactory.Instance.Get<DateTimeOffset>().ConvertDatum(datum);
Assert.That(result, Is.EqualTo(new DateTimeOffset(new DateTime(2013, 8, 17, 1, 49, 16, 123), -(new TimeSpan(3, 30, 0)))));
}

[Test]
public void ConvertObject_ValidDateTime_ReturnsDatum()
public void ConvertDatumUtc()
{
var datetime = new DateTimeOffset(new DateTime(2013, 8, 17, 1, 49, 16, 123), -(new TimeSpan(3, 30, 0)));
var datum = DateTimeOffsetDatumConverterFactory.Instance.Get<DateTimeOffset>().ConvertObject(datetime);

Assert.That(datum.type, Is.EqualTo(Datum.DatumType.R_OBJECT));
var keys = datum.r_object.ToDictionary(kvp => kvp.key, kvp => kvp.val);
Assert.That(
DateTimeOffsetDatumConverterFactory.Instance.Get<DateTimeOffset>().ConvertDatum(datumInUtc),
Is.EqualTo(objectInUtc)
);
}

Assert.That(keys["$reql_type$"].type, Is.EqualTo(Datum.DatumType.R_STR));
Assert.That(keys["$reql_type$"].r_str, Is.EqualTo("TIME"));
[Test]
public void ConvertDatumLocal()
{
Assert.That(
DateTimeOffsetDatumConverterFactory.Instance.Get<DateTimeOffset>().ConvertDatum(datumInLocal),
Is.EqualTo(objectInLocal)
);
}

Assert.That(keys["epoch_time"].type, Is.EqualTo(Datum.DatumType.R_NUM));
Assert.That(keys["epoch_time"].r_num, Is.EqualTo(1376704156.123));
[Test]
public void ConvertObjectUtc()
{
DateTimeOffsetDatumConverterFactory.Instance.Get<DateTimeOffset>().ConvertObject(objectInUtc)
.ShouldBeEquivalentTo(datumInUtc);
}

Assert.That(keys["timezone"].type, Is.EqualTo(Datum.DatumType.R_STR));
Assert.That(keys["timezone"].r_str, Is.EqualTo("-03:30"));
[Test]
public void ConvertObjectLocal()
{
DateTimeOffsetDatumConverterFactory.Instance.Get<DateTimeOffset>().ConvertObject(objectInLocal)
.ShouldBeEquivalentTo(datumInLocal);
}
}
}

54 changes: 54 additions & 0 deletions rethinkdb-net-test/Expressions/DateTimeExpressionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -423,5 +423,59 @@ public void DateTimeOffsetUtcNow()
}
);
}

[Test]
public void DateTimeYearMonthDayConstructor()
{
var expr = ExpressionUtils.CreateFunctionTerm<int, DateTime>(queryConverter, (i) => new DateTime(i, i, i));
var variableRefenceTerm = new Term() {
type = Term.TermType.VAR,
args = {
new Term() {
type = Term.TermType.DATUM,
datum = new Datum() {
type = Datum.DatumType.R_NUM,
r_num = 2,
}
}
}
};

expr.ShouldBeEquivalentTo(
new Term()
{
type = Term.TermType.FUNC,
args = {
new Term() {
type = Term.TermType.MAKE_ARRAY,
args = {
new Term() {
type = Term.TermType.DATUM,
datum = new Datum() {
type = Datum.DatumType.R_NUM,
r_num = 2,
}
}
}
},
new Term() {
type = Term.TermType.TIME,
args = {
variableRefenceTerm,
variableRefenceTerm,
variableRefenceTerm,
new Term() {
type = Term.TermType.DATUM,
datum = new Datum() {
type = Datum.DatumType.R_STR,
r_str = "Z",
}
}
}
}
}
}
);
}
}
}
35 changes: 35 additions & 0 deletions rethinkdb-net-test/Integration/BlankTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -274,5 +274,40 @@ public void ConstructedError()

#pragma warning restore 0429
}

[Test]
public void ExpressionDate()
{
var date = connection.Run(Query.Expr(() => new DateTime(2014, 2, 3)));
Assert.That(date, Is.EqualTo(new DateTime(2014, 2, 3)));
}

[Test]
public void ExpressionDateTime()
{
var datetime = connection.Run(Query.Expr(() => new DateTime(2014, 2, 3, 7, 30, 15)));
Assert.That(datetime, Is.EqualTo(new DateTime(2014, 2, 3, 7, 30, 15)));
}

[Test]
public void ExpressionDateTimeOffset()
{
var datetime = connection.Run(Query.Expr(() => new DateTimeOffset(new DateTime(2014, 2, 3, 7, 30, 15))));
Assert.That(datetime, Is.EqualTo(new DateTimeOffset(new DateTime(2014, 2, 3, 7, 30, 15, DateTimeKind.Utc))));
}

[Test]
public void ExpressionDateTimeOffsetReinterpret()
{
var datetime = connection.Run(Query.Expr(() => new DateTimeOffset(new DateTime(2014, 2, 3, 7, 30, 15), new TimeSpan(-7, 30, 0))));
Assert.That(datetime, Is.EqualTo(new DateTimeOffset(new DateTime(2014, 2, 3, 7, 30, 15), new TimeSpan(-7, 30, 0))));
}

[Test]
public void ExpressionDateTimeWithMilliseconds()
{
var datetimems = connection.Run(Query.Expr(() => new DateTime(2014, 2, 3, 7, 30, 15, 500)));
Assert.That(datetimems, Is.EqualTo(new DateTime(2014, 2, 3, 7, 30, 15, 500)));
}
}
}
Loading

0 comments on commit e3cb24f

Please sign in to comment.