New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add support to customize gcloud command of LocalDatastoreHelper #137
Changes from 7 commits
15587f4
8654955
f49a94c
098a97b
0ffd3f2
d8b9176
1ffa1f0
13f7c7f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,8 +30,12 @@ | |
import com.google.cloud.datastore.Entity; | ||
import com.google.cloud.datastore.Key; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.concurrent.TimeoutException; | ||
import org.junit.After; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.JUnit4; | ||
|
@@ -43,6 +47,17 @@ public class ITLocalDatastoreHelperTest { | |
private static final double TOLERANCE = 0.00001; | ||
private static final String PROJECT_ID_PREFIX = "test-project-"; | ||
private static final String NAMESPACE = "namespace"; | ||
private Path dataDir; | ||
|
||
@Before | ||
public void setUp() throws IOException { | ||
dataDir = Files.createTempDirectory("gcd"); | ||
} | ||
|
||
@After | ||
public void tearDown() throws IOException { | ||
Files.delete(dataDir); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Calling Instead you can use the helper method com.google.cloud.datastore.testing.LocalDatastoreHelper#deleteRecursively by making it package-private. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done. PTAL |
||
} | ||
|
||
@Test | ||
public void testCreate() { | ||
|
@@ -54,6 +69,57 @@ public void testCreate() { | |
assertTrue(helper.getProjectId().startsWith(PROJECT_ID_PREFIX)); | ||
} | ||
|
||
@Test | ||
public void testCreateWithBuilder() { | ||
LocalDatastoreHelper helper = | ||
LocalDatastoreHelper.newBuilder() | ||
.setConsistency(0.75) | ||
.setPort(8081) | ||
.setStoreOnDisk(false) | ||
.setDataDir(dataDir) | ||
.build(); | ||
assertTrue(Math.abs(0.75 - helper.getConsistency()) < TOLERANCE); | ||
assertTrue(helper.getProjectId().startsWith(PROJECT_ID_PREFIX)); | ||
assertFalse(helper.isStoreOnDisk()); | ||
assertEquals(8081, helper.getPort()); | ||
assertEquals(dataDir, helper.getGcdPath()); | ||
LocalDatastoreHelper incompleteHelper = LocalDatastoreHelper.newBuilder().build(); | ||
assertTrue(Math.abs(0.9 - incompleteHelper.getConsistency()) < TOLERANCE); | ||
assertTrue(incompleteHelper.getProjectId().startsWith(PROJECT_ID_PREFIX)); | ||
} | ||
|
||
@Test | ||
public void testCreateWithToBuilder() throws IOException { | ||
LocalDatastoreHelper helper = | ||
LocalDatastoreHelper.newBuilder() | ||
.setConsistency(0.75) | ||
.setPort(8081) | ||
.setStoreOnDisk(false) | ||
.setDataDir(dataDir) | ||
.build(); | ||
assertTrue(Math.abs(0.75 - helper.getConsistency()) < TOLERANCE); | ||
assertTrue(helper.getProjectId().startsWith(PROJECT_ID_PREFIX)); | ||
assertFalse(helper.isStoreOnDisk()); | ||
assertEquals(8081, helper.getPort()); | ||
assertEquals(dataDir, helper.getGcdPath()); | ||
LocalDatastoreHelper actualHelper = helper.toBuilder().build(); | ||
assertLocalDatastoreHelpersEquivelent(helper, actualHelper); | ||
Path dataDir = Files.createTempDirectory("gcd_data_dir"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cleanup dir like above There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
actualHelper = | ||
helper | ||
.toBuilder() | ||
.setConsistency(0.85) | ||
.setPort(9091) | ||
.setStoreOnDisk(true) | ||
.setDataDir(dataDir) | ||
.build(); | ||
assertTrue(Math.abs(0.85 - actualHelper.getConsistency()) < TOLERANCE); | ||
assertTrue(actualHelper.isStoreOnDisk()); | ||
assertEquals(9091, actualHelper.getPort()); | ||
assertEquals(dataDir, actualHelper.getGcdPath()); | ||
Files.delete(dataDir); | ||
} | ||
|
||
@Test | ||
public void testCreatePort() { | ||
LocalDatastoreHelper helper = LocalDatastoreHelper.create(0.75, 8888); | ||
|
@@ -103,4 +169,31 @@ public void testStartStopReset() throws IOException, InterruptedException, Timeo | |
assertNotNull(ex.getMessage()); | ||
} | ||
} | ||
|
||
@Test | ||
public void testStartStopResetWithBuilder() | ||
throws IOException, InterruptedException, TimeoutException { | ||
try { | ||
LocalDatastoreHelper helper = LocalDatastoreHelper.newBuilder().build(); | ||
helper.start(); | ||
Datastore datastore = helper.getOptions().getService(); | ||
Key key = datastore.newKeyFactory().setKind("kind").newKey("name"); | ||
datastore.put(Entity.newBuilder(key).build()); | ||
assertNotNull(datastore.get(key)); | ||
helper.reset(); | ||
assertNull(datastore.get(key)); | ||
helper.stop(Duration.ofMinutes(1)); | ||
datastore.get(key); | ||
Assert.fail(); | ||
} catch (DatastoreException ex) { | ||
assertNotNull(ex.getMessage()); | ||
} | ||
} | ||
|
||
public void assertLocalDatastoreHelpersEquivelent( | ||
LocalDatastoreHelper expected, LocalDatastoreHelper actual) { | ||
assertEquals(expected.getConsistency(), actual.getConsistency(), 0); | ||
assertEquals(expected.isStoreOnDisk(), actual.isStoreOnDisk()); | ||
assertEquals(expected.getGcdPath(), actual.getGcdPath()); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, can you also add a delete in an
@After
so we're not leaving lots of directories around?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done