Skip to content

Commit

Permalink
Fixed precision of g:Date values to milliseconds
Browse files Browse the repository at this point in the history
  • Loading branch information
criminosis committed Mar 31, 2021
1 parent c6936ad commit c8e4106
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 11 deletions.
4 changes: 2 additions & 2 deletions gremlin-client/src/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ impl GraphSON {
"@type" : "g:UUID",
"@value" : s.to_string()
})),
(GraphSON::V1, GValue::Date(d)) => Ok(json!(d.timestamp())),
(GraphSON::V1, GValue::Date(d)) => Ok(json!(d.timestamp_millis())),
(_, GValue::Date(d)) => Ok(json!({
"@type" : "g:Date",
"@value" : d.timestamp()
"@value" : d.timestamp_millis()
})),
(GraphSON::V1, GValue::List(d)) => {
let elements: GremlinResult<Vec<Value>> = d.iter().map(|e| self.write(e)).collect();
Expand Down
6 changes: 3 additions & 3 deletions gremlin-client/src/io/serializer_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ where
T: Fn(&Value) -> GremlinResult<GValue>,
{
let val = expect_i64!(val);
Ok(GValue::from(Utc.timestamp(val, 0)))
Ok(GValue::from(Utc.timestamp_millis(val)))
}

// Long deserializer [docs](http://tinkerpop.apache.org/docs/current/dev/io/#_long_2)
Expand Down Expand Up @@ -502,8 +502,8 @@ mod tests {
"@value": 1551825863
});

let result = deserializer_v2(&value).expect("Failed to deserialize Double");
assert_eq!(result, GValue::Date(chrono::Utc.timestamp(1551825863, 0)));
let result = deserializer_v2(&value).expect("Failed to deserialize Date");
assert_eq!(result, GValue::Date(chrono::Utc.timestamp_millis(1551825863)));

// UUID
let value = json!({
Expand Down
6 changes: 3 additions & 3 deletions gremlin-client/src/io/serializer_v3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ where
T: Fn(&Value) -> GremlinResult<GValue>,
{
let val = expect_i64!(val);
Ok(GValue::from(Utc.timestamp(val, 0)))
Ok(GValue::from(Utc.timestamp_millis(val)))
}

// Long deserializer [docs](http://tinkerpop.apache.org/docs/current/dev/io/#_long_2)
Expand Down Expand Up @@ -532,8 +532,8 @@ mod tests {
"@value": 1551825863
});

let result = deserializer_v3(&value).expect("Failed to deserialize Double");
assert_eq!(result, GValue::Date(chrono::Utc.timestamp(1551825863, 0)));
let result = deserializer_v3(&value).expect("Failed to deserialize Date");
assert_eq!(result, GValue::Date(chrono::Utc.timestamp_millis(1551825863)));

// UUID
let value = json!({
Expand Down
37 changes: 36 additions & 1 deletion gremlin-client/tests/integration_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ fn test_complex_vertex_creation_with_properties() {
);

assert_eq!(
&chrono::Utc.timestamp(1551825863, 0),
&chrono::Utc.timestamp_millis(1551825863),
properties["date"].get::<List>().unwrap()[0]
.get::<VertexProperty>()
.unwrap()
Expand Down Expand Up @@ -271,6 +271,41 @@ fn test_complex_vertex_creation_with_properties() {
);
}

#[test]
fn test_inserting_date_with_milisecond_precision() {
use chrono::offset::TimeZone;
use chrono::DateTime;
use chrono::Utc;

let graph = graph();

let q = r#"g.addV('person').property('dateTime',dateTime).propertyMap()"#;

let expected = chrono::Utc.timestamp(1551825863, 0);
let params: &[(&str, &dyn ToGValue)] = &[("dateTime", &expected)];

let results = graph
.execute(q, params)
.expect("it should execute addV")
.filter_map(Result::ok)
.map(|f| f.take::<Map>())
.collect::<Result<Vec<Map>, _>>()
.expect("It should be ok");

let properties = &results[0];

assert_eq!(1, properties.len());

assert_eq!(
&expected,
properties["dateTime"].get::<List>().unwrap()[0]
.get::<VertexProperty>()
.unwrap()
.get::<DateTime<Utc>>()
.unwrap()
);
}

#[test]
fn test_edge_creation() {
let graph = graph();
Expand Down
2 changes: 1 addition & 1 deletion gremlin-client/tests/integration_client_v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ fn test_complex_vertex_creation_with_properties_v1() {
);

assert_eq!(
&1551825863,
&1551825863000,
properties["dateTime"].get::<List>().unwrap()[0]
.get::<VertexProperty>()
.unwrap()
Expand Down
36 changes: 35 additions & 1 deletion gremlin-client/tests/integration_client_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,40 @@ fn test_vertex_creation_v2() {
);
}

#[test]
fn test_inserting_date_with_milisecond_precision() {
use chrono::offset::TimeZone;
use chrono::DateTime;
use chrono::Utc;

let graph = graph_serializer(GraphSON::V2);

let q = r#"g.addV('person').property('dateTime',dateTime).propertyMap()"#;

let expected = chrono::Utc.timestamp(1551825863, 0);
let params: &[(&str, &dyn ToGValue)] = &[("dateTime", &expected)];

let results = graph
.execute(q, params)
.expect("it should execute addV")
.filter_map(Result::ok)
.map(|f| f.take::<Map>())
.collect::<Result<Vec<Map>, _>>()
.expect("It should be ok");

let properties = &results[0];
assert_eq!(1, properties.len());

assert_eq!(
&expected,
properties["dateTime"].get::<List>().unwrap()[0]
.get::<VertexProperty>()
.unwrap()
.get::<DateTime<Utc>>()
.unwrap()
);
}

#[test]
fn test_complex_vertex_creation_with_properties_v2() {
use chrono::offset::TimeZone;
Expand Down Expand Up @@ -208,7 +242,7 @@ fn test_complex_vertex_creation_with_properties_v2() {
);

assert_eq!(
&chrono::Utc.timestamp(1551825863, 0),
&chrono::Utc.timestamp_millis(1551825863),
properties["date"].get::<List>().unwrap()[0]
.get::<VertexProperty>()
.unwrap()
Expand Down

0 comments on commit c8e4106

Please sign in to comment.