-
Notifications
You must be signed in to change notification settings - Fork 43
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
chore: added regional endpoint sample for datastore #1043
base: main
Are you sure you want to change the base?
Changes from 7 commits
f8f37af
3c3b655
117c621
0bcafad
e0eda8e
ec72ff5
2cdaf5d
eeff0ae
dc41325
52c15d5
c446023
66fd56a
99f1d99
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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Copyright 2023 Google Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.example.datastore; | ||
|
||
// Imports the Google Cloud client library | ||
|
||
import com.google.cloud.datastore.Datastore; | ||
import com.google.cloud.datastore.DatastoreOptions; | ||
|
||
public class RegionalEndpoint { | ||
|
||
public Datastore createClient() throws Exception { | ||
// Instantiates a client | ||
// [START datastore_regional_endpoint] | ||
DatastoreOptions options = | ||
DatastoreOptions.newBuilder().setHost("https://nam5-datastore.googleapis.com").build(); | ||
Datastore datastore = options.getService(); | ||
// [END datastore_regional_endpoint] | ||
return datastore; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* Copyright 2023 Google Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.example.datastore; | ||
|
||
import com.google.cloud.datastore.Datastore; | ||
import com.google.cloud.datastore.Entity; | ||
import com.google.cloud.datastore.Key; | ||
import com.rule.SystemsOutRule; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.JUnit4; | ||
|
||
/** Tests for quickstart sample. */ | ||
@RunWith(JUnit4.class) | ||
@SuppressWarnings("checkstyle:abbreviationaswordinname") | ||
public class RegionalEndpointIT { | ||
|
||
private static RegionalEndpoint regionalEndpoint; | ||
@Rule public final SystemsOutRule systemsOutRule = new SystemsOutRule(); | ||
|
||
private static final void deleteTestEntity(Datastore datastore) { | ||
String kind = "Task"; | ||
String name = "sampletask1"; | ||
Key taskKey = datastore.newKeyFactory().setKind(kind).newKey(name); | ||
datastore.delete(taskKey); | ||
} | ||
|
||
@Before | ||
public void setUp() { | ||
regionalEndpoint = new RegionalEndpoint(); | ||
} | ||
|
||
@After | ||
public void tearDown() { | ||
System.setOut(null); | ||
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. issue: Remove this. If you use the 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. |
||
} | ||
|
||
@Test | ||
public void testRegionalEndpoint() throws Exception { | ||
Datastore datastoreWithEndpoint = regionalEndpoint.createClient(); | ||
|
||
// run a few operations with the client | ||
deleteTestEntity(datastoreWithEndpoint); | ||
// The kind for the new entity | ||
String kind = "Task"; | ||
// The name/ID for the new entity | ||
String name = "sampletask1"; | ||
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. These are created in a common project, and many of these tests can be run at least 4x simultaneously. You should strongly consider using a UUID as name, possibly with a common prefix to tell you which test created, but failed before deleting the object. 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. Updated to an identifiable ID. Thank you. |
||
// The Cloud Datastore key for the new entity | ||
Key taskKey = datastoreWithEndpoint.newKeyFactory().setKind(kind).newKey(name); | ||
|
||
// Prepares the new entity | ||
Entity task = Entity.newBuilder(taskKey).set("description", "Buy milk").build(); | ||
|
||
// Saves the entity | ||
datastoreWithEndpoint.put(task); | ||
|
||
System.out.printf("Saved %s: %s%n", task.getKey().getName(), task.getString("description")); | ||
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. nit: Remove print statements. Tests are most commonly run in a CI/CD environment where output is written to logs. 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. |
||
|
||
// Retrieve entity | ||
Entity retrieved = datastoreWithEndpoint.get(taskKey); | ||
|
||
System.out.printf("Retrieved %s: %s%n", taskKey.getName(), retrieved.getString("description")); | ||
|
||
systemsOutRule.assertContains("Saved sampletask1: Buy milk"); | ||
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. issue: it looks like this test is upserting an entity and then querying the Datastore for the same entity. You don't need to do a string comparison here. Instead just compare the 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. |
||
systemsOutRule.assertContains("Retrieved sampletask1: Buy milk"); | ||
deleteTestEntity(datastoreWithEndpoint); | ||
} | ||
} |
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.
nit: current best practice is to include
import
statements and the sample class inside the region tags.See:
https://googlecloudplatform.github.io/samples-style-guide/#region-tags
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.