Skip to content

Commit e1b65a9

Browse files
committed
Add a remote test for !=
The local emulator doesn't seem to support it
1 parent 85ec488 commit e1b65a9

3 files changed

Lines changed: 121 additions & 1 deletion

File tree

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
*/
3+
4+
package com.googlecode.objectify.test;
5+
6+
import com.googlecode.objectify.Key;
7+
import com.googlecode.objectify.test.entity.Trivial;
8+
import com.googlecode.objectify.test.util.LocalMemcacheExtension;
9+
import com.googlecode.objectify.test.util.MockitoExtension;
10+
import com.googlecode.objectify.test.util.RemoteObjectifyExtension;
11+
import com.googlecode.objectify.test.util.TestBase;
12+
import org.junit.jupiter.api.BeforeEach;
13+
import org.junit.jupiter.api.Test;
14+
import org.junit.jupiter.api.extension.ExtendWith;
15+
16+
import java.util.ArrayList;
17+
import java.util.List;
18+
import java.util.Map;
19+
20+
import static com.google.common.truth.Truth.assertThat;
21+
import static com.googlecode.objectify.ObjectifyService.factory;
22+
import static com.googlecode.objectify.ObjectifyService.ofy;
23+
24+
/**
25+
* The datastore emulator does not yet support certain operations. These run against a production database.
26+
*/
27+
@ExtendWith({
28+
MockitoExtension.class,
29+
LocalMemcacheExtension.class,
30+
RemoteObjectifyExtension.class,
31+
})
32+
class RemoteQueryTests extends TestBase {
33+
34+
/** */
35+
private Trivial triv1;
36+
private Trivial triv2;
37+
private List<Key<Trivial>> keys;
38+
39+
/** */
40+
@BeforeEach
41+
void setUp() {
42+
factory().register(Trivial.class);
43+
44+
this.triv1 = new Trivial("foo1", 1);
45+
this.triv2 = new Trivial("foo2", 2);
46+
47+
final Map<Key<Trivial>, Trivial> result = ofy().save().entities(triv1, triv2).now();
48+
49+
this.keys = new ArrayList<>(result.keySet());
50+
}
51+
52+
@Test
53+
void testNotEquals() throws Exception {
54+
{
55+
final List<Trivial> result = ofy().load().type(Trivial.class).filter("someString !=", "foo1").list();
56+
assertThat(result).containsExactly(triv2);
57+
}
58+
59+
{
60+
final List<Trivial> result = ofy().load().type(Trivial.class).filter("someString <>", "foo2").list();
61+
assertThat(result).containsExactly(triv1);
62+
}
63+
}
64+
}

src/test/java/com/googlecode/objectify/test/util/ObjectifyExtension.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.googlecode.objectify.test.util;
22

33
import com.google.cloud.datastore.Datastore;
4+
import com.google.cloud.datastore.testing.LocalDatastoreHelper;
5+
import com.google.common.base.Preconditions;
46
import com.googlecode.objectify.ObjectifyFactory;
57
import com.googlecode.objectify.ObjectifyService;
68
import com.googlecode.objectify.util.Closeable;
@@ -19,8 +21,13 @@ public class ObjectifyExtension implements BeforeEachCallback, AfterEachCallback
1921

2022
@Override
2123
public void beforeEach(final ExtensionContext context) throws Exception {
22-
final Datastore datastore = LocalDatastoreExtension.getHelper(context).getOptions().getService();
24+
final LocalDatastoreHelper helper = LocalDatastoreExtension.getHelper(context);
25+
Preconditions.checkNotNull(helper, "This extension depends on " + LocalDatastoreExtension.class.getSimpleName());
26+
27+
final Datastore datastore = helper.getOptions().getService();
28+
2329
final MemcachedClient memcachedClient = LocalMemcacheExtension.getClient(context);
30+
Preconditions.checkNotNull(memcachedClient, "This extension depends on " + LocalMemcacheExtension.class.getSimpleName());
2431

2532
ObjectifyService.init(new ObjectifyFactory(datastore, memcachedClient));
2633

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.googlecode.objectify.test.util;
2+
3+
import com.google.cloud.datastore.Datastore;
4+
import com.google.cloud.datastore.testing.LocalDatastoreHelper;
5+
import com.google.cloud.datastore.testing.RemoteDatastoreHelper;
6+
import com.google.common.base.Preconditions;
7+
import com.googlecode.objectify.ObjectifyFactory;
8+
import com.googlecode.objectify.ObjectifyService;
9+
import com.googlecode.objectify.util.Closeable;
10+
import net.spy.memcached.MemcachedClient;
11+
import org.junit.jupiter.api.extension.AfterEachCallback;
12+
import org.junit.jupiter.api.extension.BeforeEachCallback;
13+
import org.junit.jupiter.api.extension.ExtensionContext;
14+
import org.junit.jupiter.api.extension.ExtensionContext.Namespace;
15+
16+
/**
17+
* Sets up and tears down the RemoteTestHelper, initializes objectify.
18+
* To use this, you will need GOOGLE_APPLICATION_CREDENTIALS set to a relevant service account key file.
19+
*/
20+
public class RemoteObjectifyExtension implements BeforeEachCallback, AfterEachCallback {
21+
22+
private static final Namespace NAMESPACE = Namespace.create(RemoteObjectifyExtension.class);
23+
24+
@Override
25+
public void beforeEach(final ExtensionContext context) throws Exception {
26+
final MemcachedClient memcachedClient = LocalMemcacheExtension.getClient(context);
27+
Preconditions.checkNotNull(memcachedClient, "This extension depends on " + LocalMemcacheExtension.class.getSimpleName());
28+
29+
final RemoteDatastoreHelper helper = RemoteDatastoreHelper.create();
30+
context.getStore(NAMESPACE).put(RemoteDatastoreHelper.class, helper);
31+
32+
final Datastore datastore = helper.getOptions().getService();
33+
34+
ObjectifyService.init(new ObjectifyFactory(datastore, memcachedClient));
35+
36+
final Closeable rootService = ObjectifyService.begin();
37+
38+
context.getStore(NAMESPACE).put(Closeable.class, rootService);
39+
}
40+
41+
@Override
42+
public void afterEach(final ExtensionContext context) throws Exception {
43+
final Closeable rootService = context.getStore(NAMESPACE).get(Closeable.class, Closeable.class);
44+
rootService.close();
45+
46+
final RemoteDatastoreHelper helper = context.getStore(NAMESPACE).get(RemoteDatastoreHelper.class, RemoteDatastoreHelper.class);
47+
helper.deleteNamespace();
48+
}
49+
}

0 commit comments

Comments
 (0)