forked from testcontainers/testcontainers-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_google.py
More file actions
76 lines (59 loc) · 2.98 KB
/
test_google.py
File metadata and controls
76 lines (59 loc) · 2.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
from queue import Queue
from google.cloud.datastore import Entity
from testcontainers.core.waiting_utils import wait_for_logs
from testcontainers.google import PubSubContainer, DatastoreContainer
def test_pubsub_container():
pubsub: PubSubContainer
with PubSubContainer() as pubsub:
wait_for_logs(pubsub, r"Server started, listening on \d+", timeout=60)
# Create a new topic
publisher = pubsub.get_publisher_client()
topic_path = publisher.topic_path(pubsub.project, "my-topic")
publisher.create_topic(name=topic_path)
# Create a subscription
subscriber = pubsub.get_subscriber_client()
subscription_path = subscriber.subscription_path(pubsub.project, "my-subscription")
subscriber.create_subscription(name=subscription_path, topic=topic_path)
# Publish a message
publisher.publish(topic_path, b"Hello world!")
# Receive the message
queue = Queue()
subscriber.subscribe(subscription_path, queue.put)
message = queue.get(timeout=1)
assert message.data == b"Hello world!"
message.ack()
def test_datastore_container_creation():
# Initialize the Datastore emulator container
with DatastoreContainer() as datastore:
# Obtain a datastore client configured to connect to the emulator
client = datastore.get_datastore_client()
# Define a unique key for a test entity to ensure test isolation
key = client.key("TestKind", "test_id_1")
# Create and insert a new entity
entity = Entity(key=key)
entity.update({"foo": "bar"})
client.put(entity)
# Fetch the just-inserted entity directly
fetched_entity = client.get(key)
# Assert that the fetched entity matches what was inserted
assert fetched_entity is not None, "Entity was not found in the datastore."
assert fetched_entity["foo"] == "bar", "Entity attribute 'foo' did not match expected value 'bar'."
def test_datastore_container_isolation():
# Initialize the Datastore emulator container
with DatastoreContainer() as datastore:
# Obtain a datastore client configured to connect to the emulator
client = datastore.get_datastore_client()
# Define a unique key for a test entity to ensure test isolation
key = client.key("TestKind", "test_id_1")
# Create and insert a new entity
entity = Entity(key=key)
entity.update({"foo": "bar"})
client.put(entity)
# Create a second container and try to fetch the entity to makesure its a different container
with DatastoreContainer() as datastore2:
assert (
datastore.get_datastore_emulator_host() != datastore2.get_datastore_emulator_host()
), "Datastore containers use the same port."
client2 = datastore2.get_datastore_client()
fetched_entity2 = client2.get(key)
assert fetched_entity2 is None, "Entity was found in the datastore."