Search in sources :

Example 6 with Datastore

use of org.mongodb.morphia.Datastore in project morphia by mongodb.

the class VersionTest method testVersionsWithUpdate.

@Test
public void testVersionsWithUpdate() {
    final ALongPrimitive initial = new ALongPrimitive();
    Datastore ds = getDs();
    ds.save(initial);
    Query<ALongPrimitive> query = ds.find(ALongPrimitive.class).field("id").equal(initial.getId());
    UpdateOperations<ALongPrimitive> update = ds.createUpdateOperations(ALongPrimitive.class).set("text", "some new value");
    UpdateResults results = ds.update(query, update);
    assertEquals(1, results.getUpdatedCount());
    ALongPrimitive postUpdate = ds.get(ALongPrimitive.class, initial.getId());
    Assert.assertEquals(initial.version + 1, postUpdate.version);
}
Also used : Datastore(org.mongodb.morphia.Datastore) UpdateResults(org.mongodb.morphia.query.UpdateResults) Test(org.junit.Test)

Example 7 with Datastore

use of org.mongodb.morphia.Datastore in project morphia by mongodb.

the class TestEmbeddedClassname method testEmbeddedClassname.

@Test
public final void testEmbeddedClassname() {
    Datastore ds = getDs();
    Root r = new Root();
    r.singleA = new A();
    ds.save(r);
    ds.update(ds.find(Root.class), ds.createUpdateOperations(Root.class).addToSet("aList", new A()));
    r = ds.get(Root.class, "id");
    DBObject aRaw = r.singleA.raw;
    // Test that singleA does not contain the class name
    Assert.assertFalse(aRaw.containsField(Mapper.CLASS_NAME_FIELDNAME));
    // Test that aList does not contain the class name
    aRaw = r.aList.get(0).raw;
    Assert.assertFalse(aRaw.containsField(Mapper.CLASS_NAME_FIELDNAME));
    // Test that bList does not contain the class name of the subclass
    ds.update(ds.find(Root.class), ds.createUpdateOperations(Root.class).addToSet("bList", new B()));
    r = ds.get(Root.class, "id");
    aRaw = r.aList.get(0).raw;
    Assert.assertFalse(aRaw.containsField(Mapper.CLASS_NAME_FIELDNAME));
    DBObject bRaw = r.bList.get(0).getRaw();
    Assert.assertFalse(bRaw.containsField(Mapper.CLASS_NAME_FIELDNAME));
    ds.delete(ds.find(Root.class));
    //test saving an B in aList, and it should have the classname.
    Root entity = new Root();
    entity.singleA = new B();
    ds.save(entity);
    ds.update(ds.find(Root.class), ds.createUpdateOperations(Root.class).addToSet("aList", new B()));
    r = ds.get(Root.class, "id");
    // test that singleA.raw *does* contain the classname because we stored a subclass there
    aRaw = r.singleA.raw;
    Assert.assertTrue(aRaw.containsField(Mapper.CLASS_NAME_FIELDNAME));
    DBObject bRaw2 = r.aList.get(0).raw;
    Assert.assertTrue(bRaw2.containsField(Mapper.CLASS_NAME_FIELDNAME));
}
Also used : Datastore(org.mongodb.morphia.Datastore) DBObject(com.mongodb.DBObject) Test(org.junit.Test)

Example 8 with Datastore

use of org.mongodb.morphia.Datastore in project morphia by mongodb.

the class KeyMappingTest method keyMapping.

@Test
public void keyMapping() {
    getMorphia().map(User.class, Channel.class);
    insertData();
    final Datastore datastore = getDs();
    User user = datastore.find(User.class).get();
    List<Key<Channel>> followedChannels = user.followedChannels;
    Channel channel = datastore.find(Channel.class).filter("name", "Sport channel").get();
    Key<Channel> key = datastore.getKey(channel);
    Assert.assertTrue(followedChannels.contains(key));
}
Also used : Datastore(org.mongodb.morphia.Datastore) Key(org.mongodb.morphia.Key) Test(org.junit.Test)

Example 9 with Datastore

use of org.mongodb.morphia.Datastore in project morphia by mongodb.

the class KeyMappingTest method insertData.

private void insertData() {
    final Datastore datastore = getDs();
    Channel sportChannel = new Channel("Sport channel");
    datastore.save(sportChannel);
    datastore.save(new Channel("Art channel"));
    Channel fitnessChannel = new Channel("Fitness channel");
    datastore.save(fitnessChannel);
    final List<Key<Channel>> followedChannels = new ArrayList<Key<Channel>>();
    followedChannels.add(datastore.getKey(sportChannel));
    followedChannels.add(datastore.getKey(fitnessChannel));
    datastore.save(new User("Roberto", datastore.getKey(sportChannel), followedChannels));
}
Also used : Datastore(org.mongodb.morphia.Datastore) ArrayList(java.util.ArrayList) Key(org.mongodb.morphia.Key)

Example 10 with Datastore

use of org.mongodb.morphia.Datastore in project morphia by mongodb.

the class QueryFactoryTest method createQuery.

@Test
public void createQuery() {
    final AtomicInteger counter = new AtomicInteger();
    final QueryFactory queryFactory = new DefaultQueryFactory() {

        @Override
        public <T> Query<T> createQuery(final Datastore datastore, final DBCollection collection, final Class<T> type, final DBObject query) {
            counter.incrementAndGet();
            return super.createQuery(datastore, collection, type, query);
        }
    };
    getDs().setQueryFactory(queryFactory);
    final Query<String> query = getDs().find(String.class);
    final Query<String> other = getDs().find(String.class);
    Assert.assertNotSame(other, query);
    Assert.assertEquals(2, counter.get());
}
Also used : DBCollection(com.mongodb.DBCollection) Datastore(org.mongodb.morphia.Datastore) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DBObject(com.mongodb.DBObject) Test(org.junit.Test)

Aggregations

Datastore (org.mongodb.morphia.Datastore)21 Test (org.junit.Test)15 BasicDBObject (com.mongodb.BasicDBObject)5 DBObject (com.mongodb.DBObject)4 DBCollection (com.mongodb.DBCollection)2 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 Key (org.mongodb.morphia.Key)2 City (org.mongodb.morphia.geo.City)2 UpdateResults (org.mongodb.morphia.query.UpdateResults)2 MongoClient (com.mongodb.MongoClient)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Morphia (org.mongodb.morphia.Morphia)1 IndexOnValue (org.mongodb.morphia.entities.IndexOnValue)1 NamedIndexOnValue (org.mongodb.morphia.entities.NamedIndexOnValue)1 UniqueIndexOnValue (org.mongodb.morphia.entities.UniqueIndexOnValue)1