@Test
  public void testGrouping() throws Exception {
    fact.register(HasEntitiesWithGroups.class);

    HasEntitiesWithGroups he = new HasEntitiesWithGroups();
    he.single = Ref.create(k1);
    he.multi.add(Ref.create(k1));
    he.multi.add(Ref.create(k2));
    HasEntitiesWithGroups fetched = this.putClearGet(he);

    Key<HasEntitiesWithGroups> hekey = Key.create(he);

    assert fetched.single.key().equals(k1);
    assertRefUninitialzied(fetched.single);

    assert fetched.multi.get(0).equals(fetched.single);
    for (Ref<Trivial> ref : fetched.multi) assertRefUninitialzied(ref);

    TestObjectify ofy = fact.begin();

    fetched = ofy.load().group(Single.class).key(hekey).get();
    assert fetched.single.get().getId().equals(t1.getId());
    assert fetched.single.get().getSomeString().equals(t1.getSomeString());
    assert fetched.multi.get(0).equals(fetched.single);
    for (Ref<Trivial> ref : fetched.multi) assertRefUninitialzied(ref);

    fetched = ofy.load().group(Multi.class).key(hekey).get();
    assert fetched.multi.get(0).get().getId().equals(t1.getId());
    assert fetched.multi.get(0).get().getSomeString().equals(t1.getSomeString());
    assert fetched.multi.get(1).get().getId().equals(t2.getId());
    assert fetched.multi.get(1).get().getSomeString().equals(t2.getSomeString());
    // Not valid anymore, everything is done separately
    // assert fetched.single.get() == fetched.multi.get(0).get();

    // This is an interesting question - do we "downgrade" refs which are no longer included in the
    // subsequent load?
    // Seems the answer should be no, but not sure.
    // assertRefUninitialzied(fetched.single);

    fetched = ofy.load().group(Single.class).group(Multi.class).key(hekey).get();
    assert fetched.multi.get(0).get().getId().equals(t1.getId());
    assert fetched.multi.get(0).get().getSomeString().equals(t1.getSomeString());
    assert fetched.multi.get(1).get().getId().equals(t2.getId());
    assert fetched.multi.get(1).get().getSomeString().equals(t2.getSomeString());
    assert fetched.single.get() == fetched.multi.get(0).get();
  }
  @Test
  public void testGrouping() throws Exception {
    fact.register(HasEntitiesWithGroups.class);

    HasEntitiesWithGroups he = new HasEntitiesWithGroups();
    he.single = Ref.create(k1);
    he.multi.add(Ref.create(k1));
    he.multi.add(Ref.create(k2));
    HasEntitiesWithGroups fetched = this.putClearGet(he);

    Key<HasEntitiesWithGroups> hekey = Key.create(he);

    assert fetched.single.key().equals(k1);
    assertRefUninitialzied(fetched.single);

    assert fetched.multi.get(0).equals(fetched.single);
    for (Ref<Trivial> ref : fetched.multi) assertRefUninitialzied(ref);

    TestObjectify ofy = fact.begin();

    ofy.clear();
    fetched = ofy.load().group(Single.class).key(hekey).get();
    assert fetched.single.get().getId().equals(t1.getId());
    assert fetched.single.get().getSomeString().equals(t1.getSomeString());
    assert fetched.multi.get(0).equals(fetched.single);
    for (Ref<Trivial> ref : fetched.multi) assertRefUninitialzied(ref);

    ofy.clear();
    fetched = ofy.load().group(Multi.class).key(hekey).get();
    assert fetched.multi.get(0).get().getId().equals(t1.getId());
    assert fetched.multi.get(0).get().getSomeString().equals(t1.getSomeString());
    assert fetched.multi.get(1).get().getId().equals(t2.getId());
    assert fetched.multi.get(1).get().getSomeString().equals(t2.getSomeString());

    assert fetched.multi.get(0).equals(fetched.single);
    assertRefUninitialzied(fetched.single);

    ofy.clear();
    fetched = ofy.load().group(Single.class).group(Multi.class).key(hekey).get();
    assert fetched.multi.get(0).get().getId().equals(t1.getId());
    assert fetched.multi.get(0).get().getSomeString().equals(t1.getSomeString());
    assert fetched.multi.get(1).get().getId().equals(t2.getId());
    assert fetched.multi.get(1).get().getSomeString().equals(t2.getSomeString());
    assert fetched.single.get() == fetched.multi.get(0).get();
  }
  /**
   * Delete creates a negative cache result, so when the value comes back we should not insert a
   * null but rather pretend the value does not exist.
   */
  @Test
  public void deleteWorks() throws Exception {
    // Should be an unapplied write
    ofy().delete().entity(triv1).now();

    List<Trivial> found = ofy().load().type(Trivial.class).list();
    assert found.size() == 1;
    assert found.get(0).getId().equals(triv2.getId());
  }
  @Test
  public void testTargetsExist() throws Exception {
    fact.register(HasEntities.class);

    HasEntities he = new HasEntities();
    he.single = Ref.create(k1);
    he.multi.add(Ref.create(k1));
    he.multi.add(Ref.create(k2));

    HasEntities fetched = this.putClearGet(he);

    assert fetched.single.get().getId().equals(t1.getId());
    assert fetched.single.get().getSomeString().equals(t1.getSomeString());

    assert fetched.multi.get(0).get() == fetched.single.get();

    assert fetched.multi.get(1).get().getId().equals(t2.getId());
    assert fetched.multi.get(1).get().getSomeString().equals(t2.getSomeString());
  }
示例#5
0
  @Test
  public void testBasicAllocation() throws Exception {
    KeyRange<Trivial> range = this.fact.allocateIds(Trivial.class, 5);

    Iterator<Key<Trivial>> it = range.iterator();

    long previousId = 0;
    for (int i = 0; i < 5; i++) {
      Key<Trivial> next = it.next();
      assert next.getId() > previousId;
      previousId = next.getId();
    }

    // Create an id with a put and verify it is > than the last
    Trivial triv = new Trivial("foo", 3);
    this.fact.begin().put(triv);

    assert triv.getId() > previousId;
  }