如何为 Google App Engine 单元测试设置已经一致的数据?
How to set up already consistent data for Google App Engine unit tests?
我知道 App Engine 数据存储查询只是最终一致的。但是,对于我的一些测试,我想用已经一致的数据来播种数据存储(即它在测试发生之前保存了很长时间并且现在是全局一致的)。
如何确保 运行 测试前的初始数据是一致的?我希望仍然能够要求被测操作不需要立即一致性。
我正在为 Java 使用 Google Cloud Endpoints,但我在这里没有任何特定于 Endpoints 的内容。
我最终创建了以下 BaseTest class 并让我的所有测试都继承了它:
package com.example;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.dev.HighRepJobPolicy;
import com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig;
import com.google.appengine.tools.development.testing.LocalServiceTestHelper;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
public class BaseTest {
public static final class Policy implements HighRepJobPolicy {
static boolean shouldApply = false;
public static void applyAll() {
shouldApply = true;
}
public static void applyNone() {
shouldApply = false;
}
@Override
public boolean shouldApplyNewJob(Key entityGroup) {
return shouldApply;
}
@Override
public boolean shouldRollForwardExistingJob(Key entityGroup) {
return shouldApply;
}
}
public final LocalServiceTestHelper helper =
new LocalServiceTestHelper(new LocalDatastoreServiceTestConfig()
.setAlternateHighRepJobPolicyClass(Policy.class));
@Before
public void setUp() {
helper.setUp();
}
@After
public void tearDown() {
helper.tearDown();
}
}
然后测试可以是以下形式:
@Test
public void thingShouldDoX() throws Exception {
Policy.applyAll();
// Do setup here, everything inside will be consistent.
Policy.applyNone();
// Run code under test here, without consistency.
}
我知道 App Engine 数据存储查询只是最终一致的。但是,对于我的一些测试,我想用已经一致的数据来播种数据存储(即它在测试发生之前保存了很长时间并且现在是全局一致的)。
如何确保 运行 测试前的初始数据是一致的?我希望仍然能够要求被测操作不需要立即一致性。
我正在为 Java 使用 Google Cloud Endpoints,但我在这里没有任何特定于 Endpoints 的内容。
我最终创建了以下 BaseTest class 并让我的所有测试都继承了它:
package com.example;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.dev.HighRepJobPolicy;
import com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig;
import com.google.appengine.tools.development.testing.LocalServiceTestHelper;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
public class BaseTest {
public static final class Policy implements HighRepJobPolicy {
static boolean shouldApply = false;
public static void applyAll() {
shouldApply = true;
}
public static void applyNone() {
shouldApply = false;
}
@Override
public boolean shouldApplyNewJob(Key entityGroup) {
return shouldApply;
}
@Override
public boolean shouldRollForwardExistingJob(Key entityGroup) {
return shouldApply;
}
}
public final LocalServiceTestHelper helper =
new LocalServiceTestHelper(new LocalDatastoreServiceTestConfig()
.setAlternateHighRepJobPolicyClass(Policy.class));
@Before
public void setUp() {
helper.setUp();
}
@After
public void tearDown() {
helper.tearDown();
}
}
然后测试可以是以下形式:
@Test
public void thingShouldDoX() throws Exception {
Policy.applyAll();
// Do setup here, everything inside will be consistent.
Policy.applyNone();
// Run code under test here, without consistency.
}