Skip to content

Commit 439c13e

Browse files
authored
Merge pull request #36 from nitrictech/feature/snippets
test: Add tests to snippets
2 parents 4fada7d + 3440b5a commit 439c13e

8 files changed

Lines changed: 280 additions & 6 deletions

File tree

examples/documents/query_limits.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ async def documents_query_limits():
55
# [START snippet]
66
docs = Documents()
77

8-
query = docs.collection("Customers").collection("Orders").query().limit(1000)
8+
query = docs.collection("Customers").query().limit(1000)
99

1010
results = await query.fetch()
1111
# [END snippet]

examples/documents/refs.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
# [START import]
22
from nitric.api import Documents
33
# [END import]
4+
def documents_refs():
45
# [START snippet]
5-
docs = Documents()
6+
docs = Documents()
67

7-
# create a reference to a collection named 'products'
8-
products = docs.collection("products")
8+
# create a reference to a collection named 'products'
9+
products = docs.collection("products")
910

10-
# create a reference to a document with the id 'nitric'
11-
nitric = products.doc("nitric")
11+
# create a reference to a document with the id 'nitric'
12+
nitric = products.doc("nitric")
1213
# [END snippet]

tests/examples/__init__.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#
2+
# Copyright (c) 2021 Nitric Technologies Pty Ltd.
3+
#
4+
# This file is part of Nitric Python 3 SDK.
5+
# See https://github.com/nitrictech/python-sdk for further info.
6+
#
7+
# Licensed under the Apache License, Version 2.0 (the "License");
8+
# you may not use this file except in compliance with the License.
9+
# You may obtain a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing, software
14+
# distributed under the License is distributed on an "AS IS" BASIS,
15+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
# See the License for the specific language governing permissions and
17+
# limitations under the License.
18+
#
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
from nitric.proto.nitric.document.v1 import Collection, DocumentGetResponse, DocumentQueryStreamResponse, Document, Key
2+
from examples.documents.set import documents_set
3+
from examples.documents.get import documents_get
4+
from examples.documents.delete import documents_delete
5+
from examples.documents.paged_results import documents_paged_results
6+
from examples.documents.query import documents_query
7+
from examples.documents.query_filter import documents_query_filter
8+
from examples.documents.query_limits import documents_query_limits
9+
from examples.documents.refs import documents_refs
10+
from examples.documents.streamed import documents_streamed
11+
from examples.documents.sub_doc_query import documents_sub_doc_query
12+
13+
import pytest
14+
from unittest import IsolatedAsyncioTestCase
15+
from unittest.mock import patch, AsyncMock
16+
from betterproto.lib.google.protobuf import Struct, Value
17+
18+
19+
class DocumentsExamplesTest(IsolatedAsyncioTestCase):
20+
async def test_set_document(self):
21+
mock_set = AsyncMock()
22+
23+
with patch("nitric.proto.nitric.document.v1.DocumentServiceStub.set", mock_set):
24+
await documents_set()
25+
26+
mock_set.assert_called_once()
27+
28+
async def test_get_document(self):
29+
mock_get = AsyncMock()
30+
mock_get.return_value = DocumentGetResponse(
31+
document=Document(
32+
key=Key(id="nitric", collection=Collection(name="products")),
33+
content=Struct(
34+
fields={
35+
"nitric": Value(number_value=1.0),
36+
},
37+
),
38+
),
39+
)
40+
41+
with patch("nitric.proto.nitric.document.v1.DocumentServiceStub.get", mock_get):
42+
await documents_get()
43+
44+
mock_get.assert_called_once_with(
45+
key=Key(
46+
collection=Collection(name="products"),
47+
id="nitric",
48+
)
49+
)
50+
51+
async def test_delete_document(self):
52+
mock_delete = AsyncMock()
53+
54+
with patch("nitric.proto.nitric.document.v1.DocumentServiceStub.delete", mock_delete):
55+
await documents_delete()
56+
57+
mock_delete.assert_called_once()
58+
59+
async def test_query_document(self):
60+
mock_query = AsyncMock()
61+
62+
with patch("nitric.proto.nitric.document.v1.DocumentServiceStub.query", mock_query):
63+
await documents_query()
64+
65+
mock_query.assert_called_once()
66+
67+
async def test_paged_results_document(self):
68+
mock_query = AsyncMock()
69+
70+
with patch("nitric.proto.nitric.document.v1.DocumentServiceStub.query", mock_query):
71+
await documents_paged_results()
72+
73+
mock_query.assert_called()
74+
75+
async def test_query_filter_document(self):
76+
mock_query = AsyncMock()
77+
78+
with patch("nitric.proto.nitric.document.v1.DocumentServiceStub.query", mock_query):
79+
await documents_query_filter()
80+
81+
mock_query.assert_called_once()
82+
83+
async def test_query_limits_document(self):
84+
mock_query = AsyncMock()
85+
86+
with patch("nitric.proto.nitric.document.v1.DocumentServiceStub.query", mock_query):
87+
await documents_query_limits()
88+
89+
mock_query.assert_called_once()
90+
91+
async def test_sub_doc_query_document(self):
92+
mock_query = AsyncMock()
93+
94+
with patch("nitric.proto.nitric.document.v1.DocumentServiceStub.query", mock_query):
95+
await documents_sub_doc_query()
96+
97+
mock_query.assert_called_once()
98+
99+
async def test_streamed_document(self):
100+
stream_calls = 0
101+
call_args = {}
102+
103+
async def mock_stream(self, **kwargs):
104+
nonlocal call_args
105+
nonlocal stream_calls
106+
call_args = kwargs
107+
for i in range(3):
108+
stream_calls += 1
109+
yield DocumentQueryStreamResponse(
110+
document=Document(content=Struct(fields={"a": Value(number_value=i)}))
111+
)
112+
113+
with patch("nitric.proto.nitric.document.v1.DocumentServiceStub.query_stream", mock_stream):
114+
await documents_streamed()
115+
116+
self.assertEqual(3, stream_calls)
117+
118+
def test_refs_document(self):
119+
try:
120+
documents_refs()
121+
except:
122+
pytest.fail()
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from examples.events.publish import events_publish
2+
from examples.events.event_ids import events_event_ids
3+
4+
from unittest import IsolatedAsyncioTestCase
5+
from unittest.mock import patch, AsyncMock
6+
7+
8+
class EventsExamplesTest(IsolatedAsyncioTestCase):
9+
async def test_publish_topic(self):
10+
mock_publish = AsyncMock()
11+
12+
with patch("nitric.proto.nitric.event.v1.EventServiceStub.publish", mock_publish):
13+
await events_publish()
14+
15+
mock_publish.assert_called_once()
16+
17+
async def test_event_id_publish(self):
18+
mock_publish = AsyncMock()
19+
20+
with patch("nitric.proto.nitric.event.v1.EventServiceStub.publish", mock_publish):
21+
await events_event_ids()
22+
23+
mock_publish.assert_called_once()
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from examples.queues.receive import queues_receive
2+
from examples.queues.send import queues_send
3+
4+
from unittest import IsolatedAsyncioTestCase
5+
from unittest.mock import patch, AsyncMock
6+
7+
8+
class QueuesExamplesTest(IsolatedAsyncioTestCase):
9+
async def test_receive_queue(self):
10+
mock_receive = AsyncMock()
11+
12+
with patch("nitric.proto.nitric.queue.v1.QueueServiceStub.receive", mock_receive):
13+
await queues_receive()
14+
15+
mock_receive.assert_called_once()
16+
17+
async def test_send_queue(self):
18+
mock_send = AsyncMock()
19+
20+
with patch("nitric.proto.nitric.queue.v1.QueueServiceStub.send", mock_send):
21+
await queues_send()
22+
23+
mock_send.assert_called_once()
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
from nitric.proto.nitric.secret.v1 import Secret, SecretVersion, SecretAccessResponse, SecretPutResponse
2+
from examples.secrets.access import secret_access
3+
from examples.secrets.put import secret_put
4+
from examples.secrets.latest import secret_latest
5+
6+
from unittest import IsolatedAsyncioTestCase
7+
from unittest.mock import patch, AsyncMock
8+
9+
10+
class SecretsExamplesTest(IsolatedAsyncioTestCase):
11+
async def test_latest_secret(self):
12+
mock_latest = AsyncMock()
13+
mock_response = SecretAccessResponse(
14+
secret_version=SecretVersion(secret=Secret(name="test-secret"), version="response-version"),
15+
value=b"super secret value",
16+
)
17+
mock_latest.return_value = mock_response
18+
19+
with patch("nitric.proto.nitric.secret.v1.SecretServiceStub.access", mock_latest):
20+
await secret_latest()
21+
22+
mock_latest.assert_called_once()
23+
24+
async def test_put_secret(self):
25+
mock_put = AsyncMock()
26+
mock_response = SecretPutResponse(
27+
secret_version=SecretVersion(secret=Secret(name="test-secret"), version="test-version")
28+
)
29+
mock_put.return_value = mock_response
30+
31+
mock_access = AsyncMock()
32+
mock_response = SecretAccessResponse(
33+
secret_version=SecretVersion(secret=Secret(name="test-secret"), version="response-version"),
34+
value=b"super secret value",
35+
)
36+
mock_access.return_value = mock_response
37+
38+
with patch("nitric.proto.nitric.secret.v1.SecretServiceStub.put", mock_put):
39+
with patch("nitric.proto.nitric.secret.v1.SecretServiceStub.access", mock_access):
40+
await secret_put()
41+
42+
mock_put.assert_called_once()
43+
mock_access.assert_called_once()
44+
45+
async def test_access_secret(self):
46+
mock_access = AsyncMock()
47+
mock_response = SecretAccessResponse(
48+
secret_version=SecretVersion(secret=Secret(name="test-secret"), version="response-version"),
49+
value=b"super secret value",
50+
)
51+
mock_access.return_value = mock_response
52+
with patch("nitric.proto.nitric.secret.v1.SecretServiceStub.access", mock_access):
53+
await secret_access()
54+
55+
mock_access.assert_called_once()
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from examples.storage.read import storage_read
2+
from examples.storage.delete import storage_delete
3+
from examples.storage.write import storage_write
4+
5+
from unittest import IsolatedAsyncioTestCase
6+
from unittest.mock import patch, AsyncMock
7+
8+
9+
class StorageExamplesTest(IsolatedAsyncioTestCase):
10+
async def test_read_storage(self):
11+
mock_read = AsyncMock()
12+
13+
with patch("nitric.proto.nitric.storage.v1.StorageServiceStub.read", mock_read):
14+
await storage_read()
15+
16+
mock_read.assert_called_once()
17+
18+
async def test_write_storage(self):
19+
mock_write = AsyncMock()
20+
21+
with patch("nitric.proto.nitric.storage.v1.StorageServiceStub.write", mock_write):
22+
await storage_write()
23+
24+
mock_write.assert_called_once()
25+
26+
async def test_delete_storage(self):
27+
mock_delete = AsyncMock()
28+
29+
with patch("nitric.proto.nitric.storage.v1.StorageServiceStub.delete", mock_delete):
30+
await storage_delete()
31+
32+
mock_delete.assert_called_once()

0 commit comments

Comments
 (0)