Skip to content

Commit

Permalink
test: Add tests to snippets
Browse files Browse the repository at this point in the history
  • Loading branch information
HomelessDinosaur committed Aug 16, 2021
1 parent 4fada7d commit c483ca6
Show file tree
Hide file tree
Showing 12 changed files with 346 additions and 6 deletions.
18 changes: 18 additions & 0 deletions examples/documents/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#
# Copyright (c) 2021 Nitric Technologies Pty Ltd.
#
# This file is part of Nitric Python 3 SDK.
# See https://github.com/nitrictech/python-sdk for further info.
#
# 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.
#
120 changes: 120 additions & 0 deletions examples/documents/documents_examples_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
from nitric.proto.nitric.document.v1 import Collection, DocumentGetResponse, DocumentQueryStreamResponse, Document, Key
from examples.documents.set import documents_set
from examples.documents.get import documents_get
from examples.documents.delete import documents_delete
from examples.documents.paged_results import documents_paged_results
from examples.documents.query import documents_query
from examples.documents.query_filter import documents_query_filter
from examples.documents.query_limits import documents_query_limits
from examples.documents.refs import documents_refs
from examples.documents.streamed import documents_streamed
from examples.documents.sub_doc_query import documents_sub_doc_query

import pytest
from unittest import IsolatedAsyncioTestCase
from unittest.mock import patch, AsyncMock
from betterproto.lib.google.protobuf import Struct, Value

class DocumentsExamplesTest(IsolatedAsyncioTestCase):
async def test_set_document(self):
mock_set = AsyncMock()

with patch("nitric.proto.nitric.document.v1.DocumentServiceStub.set", mock_set):
await documents_set()

mock_set.assert_called_once()

async def test_get_document(self):
mock_get = AsyncMock()
mock_get.return_value = DocumentGetResponse(
document=Document(
key=Key(id="nitric", collection=Collection(name="products")),
content=Struct(
fields={
"nitric": Value(number_value=1.0),
},
),
),
)

with patch("nitric.proto.nitric.document.v1.DocumentServiceStub.get", mock_get):
await documents_get()

mock_get.assert_called_once_with(
key=Key(
collection=Collection(name="products"),
id="nitric",
)
)
async def test_delete_document(self):
mock_delete = AsyncMock()

with patch("nitric.proto.nitric.document.v1.DocumentServiceStub.delete", mock_delete):
await documents_delete()

mock_delete.assert_called_once()

async def test_query_document(self):
mock_query = AsyncMock()

with patch("nitric.proto.nitric.document.v1.DocumentServiceStub.query", mock_query):
await documents_query()

mock_query.assert_called_once()

async def test_paged_results_document(self):
mock_query = AsyncMock()

with patch("nitric.proto.nitric.document.v1.DocumentServiceStub.query", mock_query):
await documents_paged_results()

mock_query.assert_called()

async def test_query_filter_document(self):
mock_query = AsyncMock()

with patch("nitric.proto.nitric.document.v1.DocumentServiceStub.query", mock_query):
await documents_query_filter()

mock_query.assert_called_once()

async def test_query_limits_document(self):
mock_query = AsyncMock()

with patch("nitric.proto.nitric.document.v1.DocumentServiceStub.query", mock_query):
await documents_query_limits()

mock_query.assert_called_once()

async def test_sub_doc_query_document(self):
mock_query = AsyncMock()

with patch("nitric.proto.nitric.document.v1.DocumentServiceStub.query", mock_query):
await documents_sub_doc_query()

mock_query.assert_called_once()

async def test_streamed_document(self):
stream_calls = 0
call_args = {}

async def mock_stream(self, **kwargs):
nonlocal call_args
nonlocal stream_calls
call_args = kwargs
for i in range(3):
stream_calls += 1
yield DocumentQueryStreamResponse(
document=Document(content=Struct(fields={"a": Value(number_value=i)}))
)

with patch("nitric.proto.nitric.document.v1.DocumentServiceStub.query_stream", mock_stream):
await documents_streamed()

self.assertEqual(3, stream_calls)

def test_refs_document(self):
try:
documents_refs()
except:
pytest.fail()
2 changes: 1 addition & 1 deletion examples/documents/query_limits.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ async def documents_query_limits():
# [START snippet]
docs = Documents()

query = docs.collection("Customers").collection("Orders").query().limit(1000)
query = docs.collection("Customers").query().limit(1000)

results = await query.fetch()
# [END snippet]
11 changes: 6 additions & 5 deletions examples/documents/refs.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
# [START import]
from nitric.api import Documents
# [END import]
def documents_refs():
# [START snippet]
docs = Documents()
docs = Documents()

# create a reference to a collection named 'products'
products = docs.collection("products")
# create a reference to a collection named 'products'
products = docs.collection("products")

# create a reference to a document with the id 'nitric'
nitric = products.doc("nitric")
# create a reference to a document with the id 'nitric'
nitric = products.doc("nitric")
# [END snippet]
18 changes: 18 additions & 0 deletions examples/events/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#
# Copyright (c) 2021 Nitric Technologies Pty Ltd.
#
# This file is part of Nitric Python 3 SDK.
# See https://github.com/nitrictech/python-sdk for further info.
#
# 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.
#
22 changes: 22 additions & 0 deletions examples/events/events_examples_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from examples.events.publish import events_publish
from examples.events.event_ids import events_event_ids

from unittest import IsolatedAsyncioTestCase
from unittest.mock import patch, AsyncMock

class EventsExamplesTest(IsolatedAsyncioTestCase):
async def test_publish_topic(self):
mock_publish = AsyncMock()

with patch("nitric.proto.nitric.event.v1.EventServiceStub.publish", mock_publish):
await events_publish()

mock_publish.assert_called_once()

async def test_event_id_publish(self):
mock_publish = AsyncMock()

with patch("nitric.proto.nitric.event.v1.EventServiceStub.publish", mock_publish):
await events_event_ids()

mock_publish.assert_called_once()
18 changes: 18 additions & 0 deletions examples/queues/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#
# Copyright (c) 2021 Nitric Technologies Pty Ltd.
#
# This file is part of Nitric Python 3 SDK.
# See https://github.com/nitrictech/python-sdk for further info.
#
# 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.
#
22 changes: 22 additions & 0 deletions examples/queues/queues_examples_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from examples.queues.receive import queues_receive
from examples.queues.send import queues_send

from unittest import IsolatedAsyncioTestCase
from unittest.mock import patch, AsyncMock

class QueuesExamplesTest(IsolatedAsyncioTestCase):
async def test_receive_queue(self):
mock_receive = AsyncMock()

with patch("nitric.proto.nitric.queue.v1.QueueServiceStub.receive", mock_receive):
await queues_receive()

mock_receive.assert_called_once()

async def test_send_queue(self):
mock_send = AsyncMock()

with patch("nitric.proto.nitric.queue.v1.QueueServiceStub.send", mock_send):
await queues_send()

mock_send.assert_called_once()
18 changes: 18 additions & 0 deletions examples/secrets/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#
# Copyright (c) 2021 Nitric Technologies Pty Ltd.
#
# This file is part of Nitric Python 3 SDK.
# See https://github.com/nitrictech/python-sdk for further info.
#
# 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.
#
54 changes: 54 additions & 0 deletions examples/secrets/secrets_examples_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
from nitric.proto.nitric.secret.v1 import Secret, SecretVersion, SecretAccessResponse, SecretPutResponse
from examples.secrets.access import secret_access
from examples.secrets.put import secret_put
from examples.secrets.latest import secret_latest

from unittest import IsolatedAsyncioTestCase
from unittest.mock import patch, AsyncMock

class SecretsExamplesTest(IsolatedAsyncioTestCase):
async def test_latest_secret(self):
mock_latest = AsyncMock()
mock_response = SecretAccessResponse(
secret_version=SecretVersion(secret=Secret(name="test-secret"), version="response-version"),
value=b"super secret value",
)
mock_latest.return_value = mock_response

with patch("nitric.proto.nitric.secret.v1.SecretServiceStub.access", mock_latest):
await secret_latest()

mock_latest.assert_called_once()

async def test_put_secret(self):
mock_put = AsyncMock()
mock_response = SecretPutResponse(
secret_version=SecretVersion(secret=Secret(name="test-secret"), version="test-version")
)
mock_put.return_value = mock_response

mock_access = AsyncMock()
mock_response = SecretAccessResponse(
secret_version=SecretVersion(secret=Secret(name="test-secret"), version="response-version"),
value=b"super secret value",
)
mock_access.return_value = mock_response

with patch("nitric.proto.nitric.secret.v1.SecretServiceStub.put", mock_put):
with patch("nitric.proto.nitric.secret.v1.SecretServiceStub.access", mock_access):
await secret_put()

mock_put.assert_called_once()
mock_access.assert_called_once()

async def test_access_secret(self):
mock_access = AsyncMock()
mock_response = SecretAccessResponse(
secret_version=SecretVersion(secret=Secret(name="test-secret"), version="response-version"),
value=b"super secret value",
)
mock_access.return_value = mock_response
with patch("nitric.proto.nitric.secret.v1.SecretServiceStub.access", mock_access):
await secret_access()

mock_access.assert_called_once()
18 changes: 18 additions & 0 deletions examples/storage/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#
# Copyright (c) 2021 Nitric Technologies Pty Ltd.
#
# This file is part of Nitric Python 3 SDK.
# See https://github.com/nitrictech/python-sdk for further info.
#
# 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.
#
31 changes: 31 additions & 0 deletions examples/storage/storage_examples_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from examples.storage.read import storage_read
from examples.storage.delete import storage_delete
from examples.storage.write import storage_write

from unittest import IsolatedAsyncioTestCase
from unittest.mock import patch, AsyncMock

class StorageExamplesTest(IsolatedAsyncioTestCase):
async def test_read_storage(self):
mock_read = AsyncMock()

with patch("nitric.proto.nitric.storage.v1.StorageServiceStub.read", mock_read):
await storage_read()

mock_read.assert_called_once()

async def test_write_storage(self):
mock_write = AsyncMock()

with patch("nitric.proto.nitric.storage.v1.StorageServiceStub.write", mock_write):
await storage_write()

mock_write.assert_called_once()

async def test_delete_storage(self):
mock_delete = AsyncMock()

with patch("nitric.proto.nitric.storage.v1.StorageServiceStub.delete", mock_delete):
await storage_delete()

mock_delete.assert_called_once()

0 comments on commit c483ca6

Please sign in to comment.