lordofthejars / nosql-unit

NoSQL Unit is a JUnit extension that helps you write NoSQL unit tests.
Other
383 stars 123 forks source link

How to use ISODate in dataset(MongoDB) #147

Closed hacking2 closed 8 years ago

hacking2 commented 8 years ago

Hi, I am using nosql-unit-mongodb, fongo, and have some problem.

when using @ShouldMatchDataSet or @UsingDataSet, I wrote like below

//expected.json

  {
      "people" : {
          "data" : [
              {
                  "key" : "12345",
                  "phone" : "33333",
                  "register" : "2011-01-05T10:09:15.210Z", //It is ISODate, How can I convert Joda DateTime?
                  "index" : 1
              }
          ]
      }
  }

my domain object like this

  @Id
  private ObjectId id;

  @Field("key")
  private String key;

  @Field("phone")
  private String phone;

  @Indexed(unique=true, direction=IndexDirection.DESCENDING)
  @Field("index")
  private long index;

  @Field("register")
  private DateTime register;

when I load data, register always null. How can I write ISODate format in expected file?

Thanks for your help

lordofthejars commented 8 years ago

Does inside MongoDB stored correctly as ISODate? Notice that from the point of view of NoSQLUnit we are using the MongoDB util class DBObject parsedData = (DBObject) JSON.parse(jsonData) so probably the data is correctly inserted into MongoDB, the problem you might find probably is related with mapper library used.

joergreichert commented 8 years ago

the following works for me (using java.util.Date instead of DateTime) { "people" : { "data" : [ { "key" : "12345", "phone" : "33333", "register" : { "$date": "2011-01-05T10:09:15.210Z" }, "index" : 1 } ] } }

choda commented 8 years ago

I can confirm the { "$date" : "..."} notation also works Joda DateTime objects.

It is actually the representation of _datadate objects the MongoDB Extended JSON strict mode.

Representation of datetime and other BSON data types (object ids, db refs...) in JSON strict mode is given on the MongoDB reference manual.

In addition, it is important to note that the ISO 8601 UTC notation "1994-11-05T13:15:30Z" must be used. Dates formatted with localtime and timeoffset "1994-11-05T08:15:30-05:00" are parsed incorrectly.

choda commented 8 years ago

Btw, the question was also asked and answered here, and here (by the same poster).