def db_client(*args, **kwargs): client = MongoClient() db = client.get_database("conversation") conversations = db.get_collection("conversations") history, _ = self.history_conversations() conversations.insert_many(history) return client, "conversation", "conversations", None
def db_client(*args, **kwargs): client = MongoClient(Utility.environment['tracker']['url']) db = client.get_database("conversation") conversations = db.get_collection("conversations") history, _ = self.history_conversations() conversations.insert_many(history) return client, 'Loading host:mongodb://test_kairon:27016, db:conversation, collection:conversations'
def new_log(self, update, funcname): client = MongoClient("mongodb://localhost") database = client.get_database("test-db") database.create_collection("unittest") database["unittest"].insert_one({ 'user': update.effective_user.first_name, 'user id': update.message.chat.id, 'function': funcname.name, 'message': update.message.text, 'time': datetime.now().strftime("%Y-%m-%d %H.%M") }) result = database["unittest"].find_one() del result['_id'] # print(result) return result
class TestHandlerInMemory(unittest.TestCase): def setUp(self): self.conn = boto.resource("s3", region_name="eu-central-1") self.conn.create_bucket(Bucket="mongo-lambda-backup-2") self.client = MockMongoClient("mongodb://localhost") database = self.client.get_database("test-db") database.create_collection("unittest") database["unittest"].insert_one({"this": "is", "data": ["some"]}) database["unittest"].insert_one({"this": "is", "data": ["other"]}) database.create_collection("skip") database["skip"].insert_one({"key": 1}) database["skip"].insert_one({"key": 2}) @patch("mongo_lambda_backup.handler.MongoClient") def test_handler(self, mock_constructor): mock_constructor.return_value = self.client handler(None, None) body = ( self.conn.Object("mongo-lambda-backup-2", "backups/unittest.json") .get()["Body"] .read() .decode("utf-8") ) for line in body.split("\n"): if line: doc = loads(line) assert doc.get("_id") assert doc.get("this") assert doc.get("data") try: self.conn.Object("mongo-lambda-backup-2", "backups/skip.json").get() except ClientError: pass else: assert False