def execute(self, context: Dict[Any, Any]) -> None: # Create the hook hook = AzureCosmosDBHook(azure_cosmos_conn_id=self.azure_cosmos_conn_id) # Create the DB if it doesn't already exist if not hook.does_database_exist(self.database_name): hook.create_database(self.database_name) # Create the collection as well if not hook.does_collection_exist(self.collection_name, self.database_name): hook.create_collection(self.collection_name, self.database_name) # finally insert the document hook.upsert_document(self.document, self.database_name, self.collection_name)
def test_upsert_document(self, mock_cosmos): test_id = str(uuid.uuid4()) mock_cosmos.return_value.CreateItem.return_value = {'id': test_id} hook = AzureCosmosDBHook( azure_cosmos_conn_id='azure_cosmos_test_key_id') returned_item = hook.upsert_document( {'data1': 'somedata'}, database_name=self.test_database_name, collection_name=self.test_collection_name, document_id=test_id) expected_calls = [ mock.call().CreateItem( 'dbs/' + self.test_database_name + '/colls/' + self.test_collection_name, { 'data1': 'somedata', 'id': test_id }) ] mock_cosmos.assert_any_call(self.test_end_point, {'masterKey': self.test_master_key}) mock_cosmos.assert_has_calls(expected_calls) logging.getLogger().info(returned_item) self.assertEqual(returned_item['id'], test_id)
def test_upsert_document_default(self, mock_cosmos): test_id = str(uuid.uuid4()) mock_cosmos.return_value.CreateItem.return_value = {'id': test_id} hook = AzureCosmosDBHook( azure_cosmos_conn_id='azure_cosmos_test_key_id') returned_item = hook.upsert_document({'id': test_id}) expected_calls = [ mock.call().CreateItem( 'dbs/' + self.test_database_default + '/colls/' + self.test_collection_default, {'id': test_id}, ) ] mock_cosmos.assert_any_call(self.test_end_point, {'masterKey': self.test_master_key}) mock_cosmos.assert_has_calls(expected_calls) logging.getLogger().info(returned_item) assert returned_item['id'] == test_id