-
Notifications
You must be signed in to change notification settings - Fork 373
Expand file tree
/
Copy pathtest_google.py
More file actions
104 lines (78 loc) · 3.96 KB
/
test_google.py
File metadata and controls
104 lines (78 loc) · 3.96 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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, GoogleCloudStorageContainer, BigQueryContainer
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."
def test_gcs_container():
from google.cloud import storage
gcs: GoogleCloudStorageContainer
with GoogleCloudStorageContainer() as gcs:
wait_for_logs(gcs, 'level=INFO msg="server started at', timeout=60)
# Create a new topic
client: storage.Client = gcs.get_storage_client()
client.create_bucket("test-bucket")
buckets = list(client.list_buckets())
assert any(bucket.name == "test-bucket" for bucket in buckets)
def test_bigquery_container():
from google.cloud import bigquery
bigquery: BigQueryContainer
with BigQueryContainer() as bigquery:
wait_for_logs(bigquery, r"[bigquery-emulator] REST server listening at", timeout=60)
# Create a new topic
client: bigquery.Client = bigquery.get_bigquery_client()
datasets = client.list_datasets(project="test-project")
assert any(dataset.dataset_id == "test-containers" for dataset in datasets)