|
| 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() |
0 commit comments