|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one or more |
| 2 | +# contributor license agreements. See the NOTICE file distributed with |
| 3 | +# this work for additional information regarding copyright ownership. |
| 4 | +# The ASF licenses this file to You under the Apache License, Version 2.0 |
| 5 | +# (the "License"); you may not use this file except in compliance with |
| 6 | +# the License. You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +import logging |
| 16 | +import json |
| 17 | +from couchbase.cluster import Cluster |
| 18 | +from couchbase.options import ClusterOptions |
| 19 | +from couchbase.auth import PasswordAuthenticator |
| 20 | +from couchbase.transcoder import RawBinaryTranscoder, RawStringTranscoder |
| 21 | + |
| 22 | + |
| 23 | +class CouchbaseChecker: |
| 24 | + def is_data_present_in_couchbase(self, doc_id: str, bucket_name: str, expected_data: str, expected_data_type: str): |
| 25 | + try: |
| 26 | + cluster = Cluster('couchbase://localhost', ClusterOptions( |
| 27 | + PasswordAuthenticator('Administrator', 'password123'))) |
| 28 | + |
| 29 | + bucket = cluster.bucket(bucket_name) |
| 30 | + collection = bucket.default_collection() |
| 31 | + |
| 32 | + if expected_data_type.lower() == "binary": |
| 33 | + binary_flag = 0x03 << 24 |
| 34 | + result = collection.get(doc_id, transcoder=RawBinaryTranscoder()) |
| 35 | + flags = result.flags |
| 36 | + if not flags & binary_flag: |
| 37 | + logging.error(f"Expected binary data for document '{doc_id}' but no binary flags were found.") |
| 38 | + return False |
| 39 | + |
| 40 | + content = result.content_as[bytes] |
| 41 | + return content.decode('utf-8') == expected_data |
| 42 | + |
| 43 | + if expected_data_type.lower() == "json": |
| 44 | + json_flag = 0x02 << 24 |
| 45 | + result = collection.get(doc_id) |
| 46 | + flags = result.flags |
| 47 | + if not flags & json_flag: |
| 48 | + logging.error(f"Expected JSON data for document '{doc_id}' but no JSON flags were found.") |
| 49 | + return False |
| 50 | + |
| 51 | + content = result.content_as[dict] |
| 52 | + return content == json.loads(expected_data) |
| 53 | + |
| 54 | + if expected_data_type.lower() == "string": |
| 55 | + string_flag = 0x04 << 24 |
| 56 | + result = collection.get(doc_id, transcoder=RawStringTranscoder()) |
| 57 | + flags = result.flags |
| 58 | + if not flags & string_flag: |
| 59 | + logging.error(f"Expected string data for document '{doc_id}' but no string flags were found.") |
| 60 | + return False |
| 61 | + |
| 62 | + content = result.content_as[str] |
| 63 | + return content == expected_data |
| 64 | + |
| 65 | + logging.error(f"Unsupported data type '{expected_data_type}'") |
| 66 | + return False |
| 67 | + except Exception as e: |
| 68 | + logging.error(f"Error while fetching document '{doc_id}' from bucket '{bucket_name}': {e}") |
| 69 | + return False |
0 commit comments