| 1 |
from unittest.mock import Mock, call, patch |
| 2 |
|
| 3 |
import pytest |
| 4 |
|
| 5 |
from celery import states |
| 6 |
from celery.backends import cosmosdbsql |
| 7 |
from celery.backends.cosmosdbsql import CosmosDBSQLBackend |
| 8 |
from celery.exceptions import ImproperlyConfigured |
| 9 |
|
| 10 |
MODULE_TO_MOCK = "celery.backends.cosmosdbsql" |
| 11 |
|
| 12 |
pytest.importorskip('pydocumentdb') |
| 13 |
|
| 14 |
|
| 15 |
class test_DocumentDBBackend: |
| 16 |
def setup_method(self): |
| 17 |
self.url = "cosmosdbsql://:key@endpoint" |
| 18 |
self.backend = CosmosDBSQLBackend(app=self.app, url=self.url) |
| 19 |
|
| 20 |
def test_missing_third_party_sdk(self): |
| 21 |
pydocumentdb = cosmosdbsql.pydocumentdb |
| 22 |
try: |
| 23 |
cosmosdbsql.pydocumentdb = None |
| 24 |
with pytest.raises(ImproperlyConfigured): |
| 25 |
CosmosDBSQLBackend(app=self.app, url=self.url) |
| 26 |
finally: |
| 27 |
cosmosdbsql.pydocumentdb = pydocumentdb |
| 28 |
|
| 29 |
def test_bad_connection_url(self): |
| 30 |
with pytest.raises(ImproperlyConfigured): |
| 31 |
CosmosDBSQLBackend._parse_url( |
| 32 |
"cosmosdbsql://:key@") |
| 33 |
|
| 34 |
with pytest.raises(ImproperlyConfigured): |
| 35 |
CosmosDBSQLBackend._parse_url( |
| 36 |
"cosmosdbsql://:@host") |
| 37 |
|
| 38 |
with pytest.raises(ImproperlyConfigured): |
| 39 |
CosmosDBSQLBackend._parse_url( |
| 40 |
"cosmosdbsql://corrupted") |
| 41 |
|
| 42 |
def test_default_connection_url(self): |
| 43 |
endpoint, password = CosmosDBSQLBackend._parse_url( |
| 44 |
"cosmosdbsql://:key@host") |
| 45 |
|
| 46 |
assert password == "key" |
| 47 |
assert endpoint == "https://host:443" |
| 48 |
|
| 49 |
endpoint, password = CosmosDBSQLBackend._parse_url( |
| 50 |
"cosmosdbsql://:key@host:443") |
| 51 |
|
| 52 |
assert password == "key" |
| 53 |
assert endpoint == "https://host:443" |
| 54 |
|
| 55 |
endpoint, password = CosmosDBSQLBackend._parse_url( |
| 56 |
"cosmosdbsql://:key@host:8080") |
| 57 |
|
| 58 |
assert password == "key" |
| 59 |
assert endpoint == "http://host:8080" |
| 60 |
|
| 61 |
def test_bad_partition_key(self): |
| 62 |
with pytest.raises(ValueError): |
| 63 |
CosmosDBSQLBackend._get_partition_key("") |
| 64 |
|
| 65 |
with pytest.raises(ValueError): |
| 66 |
CosmosDBSQLBackend._get_partition_key(" ") |
| 67 |
|
| 68 |
with pytest.raises(ValueError): |
| 69 |
CosmosDBSQLBackend._get_partition_key(None) |
| 70 |
|
| 71 |
def test_bad_consistency_level(self): |
| 72 |
with pytest.raises(ImproperlyConfigured): |
| 73 |
CosmosDBSQLBackend(app=self.app, url=self.url, |
| 74 |
consistency_level="DoesNotExist") |
| 75 |
|
| 76 |
@patch(MODULE_TO_MOCK + ".DocumentClient") |
| 77 |
def test_create_client(self, mock_factory): |
| 78 |
mock_instance = Mock() |
| 79 |
mock_factory.return_value = mock_instance |
| 80 |
backend = CosmosDBSQLBackend(app=self.app, url=self.url) |
| 81 |
|
| 82 |
# ensure database and collection get created on client access... |
| 83 |
assert mock_instance.CreateDatabase.call_count == 0 |
| 84 |
assert mock_instance.CreateCollection.call_count == 0 |
| 85 |
assert backend._client is not None |
| 86 |
assert mock_instance.CreateDatabase.call_count == 1 |
| 87 |
assert mock_instance.CreateCollection.call_count == 1 |
| 88 |
|
| 89 |
# ...but only once per backend instance |
| 90 |
assert backend._client is not None |
| 91 |
assert mock_instance.CreateDatabase.call_count == 1 |
| 92 |
assert mock_instance.CreateCollection.call_count == 1 |
| 93 |
|
| 94 |
@patch(MODULE_TO_MOCK + ".CosmosDBSQLBackend._client") |
| 95 |
def test_get(self, mock_client): |
| 96 |
self.backend.get(b"mykey") |
| 97 |
|
| 98 |
mock_client.ReadDocument.assert_has_calls( |
| 99 |
[call("dbs/celerydb/colls/celerycol/docs/mykey", |
| 100 |
{"partitionKey": "mykey"}), |
| 101 |
call().get("value")]) |
| 102 |
|
| 103 |
@patch(MODULE_TO_MOCK + ".CosmosDBSQLBackend._client") |
| 104 |
def test_get_missing(self, mock_client): |
| 105 |
mock_client.ReadDocument.side_effect = \ |
| 106 |
cosmosdbsql.HTTPFailure(cosmosdbsql.ERROR_NOT_FOUND) |
| 107 |
|
| 108 |
assert self.backend.get(b"mykey") is None |
| 109 |
|
| 110 |
@patch(MODULE_TO_MOCK + ".CosmosDBSQLBackend._client") |
| 111 |
def test_set(self, mock_client): |
| 112 |
self.backend._set_with_state(b"mykey", "myvalue", states.SUCCESS) |
| 113 |
|
| 114 |
mock_client.CreateDocument.assert_called_once_with( |
| 115 |
"dbs/celerydb/colls/celerycol", |
| 116 |
{"id": "mykey", "value": "myvalue"}, |
| 117 |
{"partitionKey": "mykey"}) |
| 118 |
|
| 119 |
@patch(MODULE_TO_MOCK + ".CosmosDBSQLBackend._client") |
| 120 |
def test_mget(self, mock_client): |
| 121 |
keys = [b"mykey1", b"mykey2"] |
| 122 |
|
| 123 |
self.backend.mget(keys) |
| 124 |
|
| 125 |
mock_client.ReadDocument.assert_has_calls( |
| 126 |
[call("dbs/celerydb/colls/celerycol/docs/mykey1", |
| 127 |
{"partitionKey": "mykey1"}), |
| 128 |
call().get("value"), |
| 129 |
call("dbs/celerydb/colls/celerycol/docs/mykey2", |
| 130 |
{"partitionKey": "mykey2"}), |
| 131 |
call().get("value")]) |
| 132 |
|
| 133 |
@patch(MODULE_TO_MOCK + ".CosmosDBSQLBackend._client") |
| 134 |
def test_delete(self, mock_client): |
| 135 |
self.backend.delete(b"mykey") |
| 136 |
|
| 137 |
mock_client.DeleteDocument.assert_called_once_with( |
| 138 |
"dbs/celerydb/colls/celerycol/docs/mykey", |
| 139 |
{"partitionKey": "mykey"}) |