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);
}
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));
}
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));
}
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));
}
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());
}
Aggregations