Search in sources :

Example 11 with User

use of dev.morphia.test.models.User in project morphia by mongodb.

the class TestTransactions method before.

@BeforeMethod
public void before() {
    checkMinServerVersion(4.0);
    assumeTrue(isReplicaSet(), "These tests require a replica set");
    getDs().save(new Rectangle(1, 1));
    getDs().find(Rectangle.class).findAndDelete();
    getDs().save(new User("", LocalDate.now()));
    getDs().find(User.class).findAndDelete();
}
Also used : User(dev.morphia.test.models.User) Rectangle(dev.morphia.test.models.Rectangle) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 12 with User

use of dev.morphia.test.models.User in project morphia by mongodb.

the class TestDateExpression method testDateToString.

@Test
public void testDateToString() {
    LocalDate joined = LocalDate.parse("2016-05-01 UTC", DateTimeFormatter.ofPattern("yyyy-MM-dd z"));
    getDs().getCollection(User.class).drop();
    getDs().save(new User("John Doe", joined));
    Aggregation<User> pipeline = getDs().aggregate(User.class).project(project().include("string", dateToString().format("%Y-%m-%d").date(field("joined"))));
    MorphiaCursor<StringDates> it = pipeline.execute(StringDates.class);
    while (it.hasNext()) {
        assertEquals(it.next().getString(), "2016-05-01");
    }
}
Also used : User(dev.morphia.test.models.User) StringDates(dev.morphia.test.aggregation.experimental.model.StringDates) LocalDate(java.time.LocalDate) Test(org.testng.annotations.Test)

Example 13 with User

use of dev.morphia.test.models.User in project morphia by mongodb.

the class FiltersTest method testBitsAnyClear.

@Test
public void testBitsAnyClear() {
    MongoCollection<Document> collection = getDatabase().getCollection("users");
    collection.insertMany(asList(new Document("a", 54).append("binaryValueofA", "00110110").append("_t", "User"), new Document("a", 20).append("binaryValueofA", "00010100").append("_t", "User"), new Document("a", 20.0).append("binaryValueofA", "00010100").append("_t", "User")));
    FindOptions options = new FindOptions().logQuery();
    Query<User> query = getDs().find(User.class).disableValidation().filter(bitsAnyClear("a", 35));
    assertEquals(query.iterator(options).toList().size(), 3, query.getLoggedQuery());
    query = getDs().find(User.class).disableValidation().filter(bitsAnyClear("a", new int[] { 1, 5 }));
    assertEquals(query.iterator(options).toList().size(), 2, query.getLoggedQuery());
}
Also used : FindOptions(dev.morphia.query.FindOptions) User(dev.morphia.test.models.User) Document(org.bson.Document) Test(org.testng.annotations.Test)

Example 14 with User

use of dev.morphia.test.models.User in project morphia by mongodb.

the class FiltersTest method testBitsAnySet.

@Test
public void testBitsAnySet() {
    MongoCollection<Document> collection = getDatabase().getCollection("users");
    collection.insertMany(asList(new Document("a", 54).append("binaryValueofA", "00110110").append("_t", "User"), new Document("a", 20).append("binaryValueofA", "00010100").append("_t", "User"), new Document("a", 20.0).append("binaryValueofA", "00010100").append("_t", "User")));
    FindOptions options = new FindOptions().logQuery();
    Query<User> query = getDs().find(User.class).disableValidation().filter(bitsAnySet("a", 35));
    assertEquals(query.iterator(options).toList().size(), 1, query.getLoggedQuery());
    assertEquals(getDs().find(User.class).disableValidation().filter(bitsAnySet("a", new int[] { 1, 5 })).iterator(options).toList().size(), 1, query.getLoggedQuery());
}
Also used : FindOptions(dev.morphia.query.FindOptions) User(dev.morphia.test.models.User) Document(org.bson.Document) Test(org.testng.annotations.Test)

Example 15 with User

use of dev.morphia.test.models.User in project morphia by mongodb.

the class FiltersTest method testBitsAllSet.

@Test
public void testBitsAllSet() {
    MongoCollection<Document> collection = getDatabase().getCollection("users");
    collection.insertMany(asList(new Document("a", 54).append("binaryValueofA", "00110110").append("_t", "User"), new Document("a", 20).append("binaryValueofA", "00010100").append("_t", "User"), new Document("a", 20.0).append("binaryValueofA", "00010100").append("_t", "User")));
    final FindOptions options = new FindOptions().logQuery();
    Query<User> query = getDs().find(User.class).disableValidation().filter(bitsAllSet("a", 50));
    assertEquals(query.iterator(options).toList().size(), 1, query.getLoggedQuery());
    query = getDs().find(User.class).disableValidation().filter(bitsAllSet("a", new int[] { 1, 5 }));
    assertEquals(query.iterator(options).toList().size(), 1, query.getLoggedQuery());
}
Also used : FindOptions(dev.morphia.query.FindOptions) User(dev.morphia.test.models.User) Document(org.bson.Document) Test(org.testng.annotations.Test)

Aggregations

User (dev.morphia.test.models.User)16 Test (org.testng.annotations.Test)14 Document (org.bson.Document)5 FindOptions (dev.morphia.query.FindOptions)4 LocalDate (java.time.LocalDate)4 UpdateResult (com.mongodb.client.result.UpdateResult)2 UpdateOptions (dev.morphia.UpdateOptions)2 UpdateOperators.currentDate (dev.morphia.query.experimental.updates.UpdateOperators.currentDate)2 Rectangle (dev.morphia.test.models.Rectangle)2 Calendar (java.util.Calendar)2 Date (java.util.Date)2 BeforeMethod (org.testng.annotations.BeforeMethod)2 MongoQueryException (com.mongodb.MongoQueryException)1 MongoWriteException (com.mongodb.MongoWriteException)1 Point (com.mongodb.client.model.geojson.Point)1 InsertOneOptions (dev.morphia.InsertOneOptions)1 MorphiaSession (dev.morphia.experimental.MorphiaSession)1 StringDates (dev.morphia.test.aggregation.experimental.model.StringDates)1 DateTimeFormatter (java.time.format.DateTimeFormatter)1