Skip to content

Commit cb45448

Browse files
committed
Removed additional add_document function from couchbaseserver.py and modified overwritten changes to non-edge server code
1 parent 0915f0a commit cb45448

File tree

9 files changed

+161
-106
lines changed

9 files changed

+161
-106
lines changed

client/src/cbltest/api/couchbaseserver.py

Lines changed: 148 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
2020
ScopeAlreadyExistsException,
2121
)
2222
from couchbase.management.buckets import CreateBucketSettings
23-
from couchbase.management.collections import CollectionSpec
2423
from couchbase.management.options import CreatePrimaryQueryIndexOptions
2524
from couchbase.options import ClusterOptions
25+
from couchbase.subdocument import upsert
2626
from opentelemetry.trace import get_tracer
2727

2828
from cbltest.api.error import CblTestError
@@ -87,7 +87,7 @@ def create_collections(self, bucket: str, scope: str, names: list[str]) -> None:
8787
):
8888
try:
8989
if name != "_default":
90-
c.create_collection(CollectionSpec(name, scope))
90+
c.create_collection(scope_name=scope, collection_name=name)
9191
except CollectionAlreadyExistsException:
9292
pass
9393

@@ -294,6 +294,151 @@ def upsert_document(
294294
f"Failed to insert document '{doc_id}' into {bucket}.{scope}.{collection}: {e}"
295295
)
296296

297+
def delete_document(
298+
self,
299+
bucket: str,
300+
doc_id: str,
301+
scope: str = "_default",
302+
collection: str = "_default",
303+
) -> None:
304+
"""
305+
Deletes a document from the specified bucket.scope.collection.
306+
"""
307+
with self.__tracer.start_as_current_span(
308+
"delete_document",
309+
attributes={
310+
"cbl.bucket.name": bucket,
311+
"cbl.scope.name": scope,
312+
"cbl.collection.name": collection,
313+
"cbl.document.id": doc_id,
314+
},
315+
):
316+
try:
317+
bucket_obj = _try_n_times(10, 1, False, self.__cluster.bucket, bucket)
318+
coll = bucket_obj.scope(scope).collection(collection)
319+
coll.remove(doc_id)
320+
except DocumentNotFoundException:
321+
pass
322+
except Exception as e:
323+
raise CblTestError(
324+
f"Failed to delete document '{doc_id}' from {bucket}.{scope}.{collection}: {e}"
325+
)
326+
327+
def get_document(
328+
self,
329+
bucket: str,
330+
doc_id: str,
331+
scope: str = "_default",
332+
collection: str = "_default",
333+
) -> dict | None:
334+
"""
335+
Gets a document from the specified bucket.scope.collection.
336+
337+
:param bucket: The bucket name.
338+
:param doc_id: The document ID.
339+
:param scope: The scope name.
340+
:param collection: The collection name.
341+
:return: The document content as a dictionary, or None if not found.
342+
"""
343+
with self.__tracer.start_as_current_span(
344+
"get_document",
345+
attributes={
346+
"cbl.bucket.name": bucket,
347+
"cbl.scope.name": scope,
348+
"cbl.collection.name": collection,
349+
"cbl.document.id": doc_id,
350+
},
351+
):
352+
try:
353+
bucket_obj = _try_n_times(10, 1, False, self.__cluster.bucket, bucket)
354+
coll = bucket_obj.scope(scope).collection(collection)
355+
result = coll.get(doc_id)
356+
return result.content_as[dict] if result else None
357+
except DocumentNotFoundException:
358+
return None
359+
except Exception as e:
360+
raise CblTestError(
361+
f"Failed to get document '{doc_id}' from {bucket}.{scope}.{collection}: {e}"
362+
)
363+
364+
def upsert_document_xattr(
365+
self,
366+
bucket: str,
367+
doc_id: str,
368+
xattr_key: str,
369+
xattr_value: str,
370+
scope: str = "_default",
371+
collection: str = "_default",
372+
) -> None:
373+
"""
374+
Upserts an xattr on a document using subdocument operations
375+
376+
:param bucket: The bucket containing the document
377+
:param doc_id: The ID of the document to update
378+
:param xattr_key: The xattr key to upsert
379+
:param xattr_value: The value to set for the xattr
380+
:param scope: The scope containing the document (default '_default')
381+
:param collection: The collection containing the document (default '_default')
382+
"""
383+
with self.__tracer.start_as_current_span(
384+
"upsert_document_xattr",
385+
attributes={
386+
"cbl.bucket": bucket,
387+
"cbl.scope": scope,
388+
"cbl.collection": collection,
389+
"cbl.document.id": doc_id,
390+
"cbl.xattr.key": xattr_key,
391+
},
392+
):
393+
try:
394+
col = self.__cluster.bucket(bucket).scope(scope).collection(collection)
395+
col.mutate_in(
396+
doc_id,
397+
[upsert(xattr_key, xattr_value, xattr=True, create_parents=True)],
398+
)
399+
except Exception as e:
400+
raise CblTestError(
401+
f"Failed to upsert xattr '{xattr_key}' on document '{doc_id}' in {bucket}.{scope}.{collection}: {e}"
402+
)
403+
404+
def delete_document_xattr(
405+
self,
406+
bucket: str,
407+
doc_id: str,
408+
xattr_key: str,
409+
scope: str = "_default",
410+
collection: str = "_default",
411+
) -> None:
412+
"""
413+
Deletes an xattr from a document using subdocument operations
414+
415+
:param bucket: The bucket containing the document
416+
:param doc_id: The ID of the document
417+
:param xattr_key: The xattr key to delete
418+
:param scope: The scope containing the document (default '_default')
419+
:param collection: The collection containing the document (default '_default')
420+
"""
421+
with self.__tracer.start_as_current_span(
422+
"delete_document_xattr",
423+
attributes={
424+
"cbl.bucket": bucket,
425+
"cbl.scope": scope,
426+
"cbl.collection": collection,
427+
"cbl.document.id": doc_id,
428+
"cbl.xattr.key": xattr_key,
429+
},
430+
):
431+
try:
432+
from couchbase.subdocument import remove
433+
434+
col = self.__cluster.bucket(bucket).scope(scope).collection(collection)
435+
col.mutate_in(
436+
doc_id,
437+
[remove(xattr_key, xattr=True)],
438+
)
439+
except Exception:
440+
pass
441+
297442
def start_xdcr(self, target: "CouchbaseServer", bucket_name: str) -> None:
298443
"""
299444
Starts an XDCR replication from this cluster to the target cluster
@@ -449,4 +594,4 @@ def stop_xcdr(self, target: "CouchbaseServer", bucket_name: str) -> None:
449594
resp = session.delete(
450595
f"http://{self.__hostname}:8091/controller/cancelXDCR/{encoded}",
451596
)
452-
resp.raise_for_status()
597+
resp.raise_for_status()

client/src/cbltest/api/jsongenerator.py

Lines changed: 0 additions & 90 deletions
This file was deleted.

tests/QE/edge_server/test_blobs.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ async def test_blobs_create_delete(
3434
"channels": ["public"],
3535
"timestamp": datetime.utcnow().isoformat(),
3636
}
37-
server.add_document(bucket_name, doc["id"], doc)
37+
server.upsert_document(bucket_name, doc["id"], doc)
3838
logger.info("2 documents created in Couchbase Server.")
3939

4040
self.mark_test_step(
@@ -753,7 +753,7 @@ async def test_blob_special_characters(
753753
"channels": ["public"],
754754
"timestamp": datetime.utcnow().isoformat(),
755755
}
756-
server.add_document(bucket_name, doc["id"], doc)
756+
server.upsert_document(bucket_name, doc["id"], doc)
757757
logger.info("2 documents created in Couchbase Server.")
758758

759759
self.mark_test_step(

tests/QE/edge_server/test_changes_feed.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ async def test_changes_feed_longpoll(
3333
"channels": ["public"],
3434
"timestamp": datetime.utcnow().isoformat(),
3535
}
36-
server.add_document(bucket_name, doc["id"], doc)
36+
server.upsert_document(bucket_name, doc["id"], doc)
3737
logger.info("5 documents created in Couchbase Server.")
3838

3939
self.mark_test_step(

tests/QE/edge_server/test_chaos_scenarios.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from cbltest.api.cloud import CouchbaseCloud
1212
from cbltest.api.edgeserver import BulkDocOperation
1313
from cbltest.api.httpclient import ClientFactory, HTTPClient
14-
from cbltest.api.jsongenerator import JSONGenerator
14+
from cbltest.api.json_generator import JSONGenerator
1515

1616

1717
class TestEdgeServerChaos(CBLTestClass):

tests/QE/edge_server/test_conflicts.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ async def test_conflicts(self, cblpytest: CBLPyTest, dataset_path: Path) -> None
3232
"channels": ["public"],
3333
"timestamp": datetime.utcnow().isoformat(),
3434
}
35-
server.add_document(bucket_name, doc["id"], doc)
35+
server.upsert_document(bucket_name, doc["id"], doc)
3636
logger.info("10 documents created in Couchbase Server.")
3737

3838
self.mark_test_step(

tests/QE/edge_server/test_end_to_end.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ async def test_end_to_end_sgw(
4040
"channels": ["public"],
4141
"timestamp": datetime.utcnow().isoformat(),
4242
}
43-
server.add_document(bucket_name, doc["id"], doc)
43+
server.upsert_document(bucket_name, doc["id"], doc)
4444
logger.info("10 documents created in Couchbase Server.")
4545

4646
self.mark_test_step(

tests/QE/edge_server/test_logging.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ async def test_audit_logging_default(
3333
"channels": ["public"],
3434
"timestamp": datetime.utcnow().isoformat(),
3535
}
36-
server.add_document(bucket_name, doc["id"], doc)
36+
server.upsert_document(bucket_name, doc["id"], doc)
3737
logger.info("5 documents created in Couchbase Server.")
3838

3939
self.mark_test_step(
@@ -170,7 +170,7 @@ async def test_audit_logging_disabled(
170170
"channels": ["public"],
171171
"timestamp": datetime.utcnow().isoformat(),
172172
}
173-
server.add_document(bucket_name, doc["id"], doc)
173+
server.upsert_document(bucket_name, doc["id"], doc)
174174
logger.info("5 documents created in Couchbase Server.")
175175

176176
self.mark_test_step(
@@ -288,7 +288,7 @@ async def test_audit_logging_enabled(
288288
"channels": ["public"],
289289
"timestamp": datetime.utcnow().isoformat(),
290290
}
291-
server.add_document(bucket_name, doc["id"], doc)
291+
server.upsert_document(bucket_name, doc["id"], doc)
292292
logger.info("5 documents created in Couchbase Server.")
293293

294294
self.mark_test_step(

tests/QE/edge_server/test_system.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ async def test_system_one_client_l(
4444
"channels": ["public"],
4545
"timestamp": datetime.utcnow().isoformat(),
4646
}
47-
server.add_document(bucket_name, doc["id"], doc)
47+
server.upsert_document(bucket_name, doc["id"], doc)
4848
logger.info("10 documents created in Couchbase Server.")
4949

5050
self.mark_test_step(
@@ -376,7 +376,7 @@ async def test_system_one_client_chaos(
376376
"channels": ["public"],
377377
"timestamp": datetime.utcnow().isoformat(),
378378
}
379-
server.add_document(bucket_name, doc["id"], doc)
379+
server.upsert_document(bucket_name, doc["id"], doc)
380380
logger.info("10 documents created in Couchbase Server.")
381381

382382
self.mark_test_step(
@@ -759,7 +759,7 @@ async def test_system_multiple_clients_l(
759759
"channels": ["public"],
760760
"timestamp": datetime.utcnow().isoformat(),
761761
}
762-
server.add_document(bucket_name, doc["id"], doc)
762+
server.upsert_document(bucket_name, doc["id"], doc)
763763
logger.info("10 documents created in Couchbase Server.")
764764

765765
self.mark_test_step(
@@ -1203,7 +1203,7 @@ async def test_system_multiple_clients_chaos(
12031203
"channels": ["public"],
12041204
"timestamp": datetime.utcnow().isoformat(),
12051205
}
1206-
server.add_document(bucket_name, doc["id"], doc)
1206+
server.upsert_document(bucket_name, doc["id"], doc)
12071207
logger.info("10 documents created in Couchbase Server.")
12081208

12091209
self.mark_test_step(

0 commit comments

Comments
 (0)