Best Python code snippet using testcontainers-python_python
mongodb.py
Source:mongodb.py
...41 ("samples.json", "Sample"),42 ]43 with MongoDbContainer() as mongodb:44 connection_url = mongodb.get_connection_url()45 db_client = mongodb.get_connection_client()46 config = Config(db_url=connection_url, db_name="test")47 for filename, collection_name in json_files:48 file_path = BASE_DIR / "test_data" / "basic_example" / filename49 with open(file_path, "r", encoding="utf8") as file:50 file_content = json.load(file)51 objects = file_content[os.path.splitext(filename)[0]]52 db_client[config.db_name][collection_name].insert_many(objects)53 app.dependency_overrides[get_config] = lambda: config54 app_client = TestClient(app)55 yield MongoAppFixture(app_client=app_client, config=config)56@pytest.fixture(scope="function")57def mongo_app_fixture2():58 """59 Setup a MongoDB database with metadata for testing of creation of a Dataset.60 """61 json_files = [62 ("biospecimens.json", "Biospecimen"),63 ("experiments.json", "Experiment"),64 ("files.json", "File"),65 ("individuals.json", "Individual"),66 ("projects.json", "Project"),67 ("samples.json", "Sample"),68 ("studies.json", "Study"),69 # ("technologies.json", "Technology"),70 ]71 with MongoDbContainer() as mongodb:72 connection_url = mongodb.get_connection_url()73 db_client = mongodb.get_connection_client()74 config = Config(db_url=connection_url, db_name="test")75 for filename, collection_name in json_files:76 file_path = BASE_DIR / "test_data" / "create_dataset_example" / filename77 with open(file_path, "r", encoding="utf8") as file:78 file_content = json.load(file)79 objects = file_content[os.path.splitext(filename)[0]]80 db_client[config.db_name][collection_name].insert_many(objects)81 app.dependency_overrides[get_config] = lambda: config82 app_client = TestClient(app)83 yield MongoAppFixture(app_client=app_client, config=config)84@pytest.fixture(scope="function")85def mongo_app_fixture3():86 """87 Setup an empty MongoDB database....
test_coffee_information_repository.py
Source:test_coffee_information_repository.py
...9COLLECTION = "coffee_information"10def describe_get_coffee_information():11 def test_should_return_coffee_information(database_mock, coffee_drinks):12 with MongoDbContainer() as mongodb:13 mongo_client = mongodb.get_connection_client()14 mongo_client[DATABASE][COLLECTION].insert_many(coffee_drinks)15 database_mock.client = mongo_client16 coffee_information_repository = CoffeeInformationRepository(database_mock)17 result = coffee_information_repository.get_coffee_information()18 expect(result).to(be_a(CoffeeInformation))19 expect(result.coffee_drinks).not_to(be_empty)20 def test_should_raise_no_data_exception_when_coffee_information_has_no_data(21 database_mock,22 ):23 with MongoDbContainer() as mongodb:24 database_mock.client = mongodb.get_connection_client()25 coffee_information_repository = CoffeeInformationRepository(database_mock)26 with pytest.raises(Exception) as exception:27 coffee_information_repository.get_coffee_information()28 expect(exception.value).to(be_a(NoDataException))29@pytest.fixture30def database_mock(mocker):31 return mocker.Mock(client=None)32@pytest.fixture33def coffee_drinks():34 return [35 {36 "_id": "209f4328-001c-48ff-925a-bc4319443340",37 "title": "Black",38 "description": "Coffee served as a beverage without cream or milk.",...
parse_log.py
Source:parse_log.py
2import re3def auth_data(ip):4 if ip == '127.0.0.1':5 return 'login', 'password'6def get_connection_client(host='127.0.0.1', connection_type='ssh'):7 if connection_type == 'ssh':8 ssh_client = paramiko.SSHClient()9 ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())10 user, psw = auth_data(host)11 ssh_client.connect(hostname=host_address, username=user, password=psw)12 transport = ssh_client.get_transport()13 sftp_client = transport.open_sftp_client()14 return sftp_client15 else:16 print('This connection is not supported: %s' % connection_type)17 return None18def get_logs(log_name, client, ID):19 """20 This function looks for log_name file,21 opens it, finds line with ID and prints22 this line and +-lines23 :param log_name: log file name24 :param client: server with logs directory25 :param ID: line id to look for26 :return: none27 """28 for name in client.listdir(path='/var/log/'):29 if re.match('%s*.*.log' % log_name, name):30 log_name = name31 break32 line_num = 033 lines = list()34 with client.open('/var/log/%s' % log_name) as log_file:35 for num, line in enumerate(log_file):36 lines.append((num, line))37 if len(lines) > 100:38 del lines[:1]39 if ID in line and not line_num:40 print(num, line)41 line_num = num42 if line_num and (line_num + 100 == num):43 break44 for num, line in lines:45 print(num, line.strip())46file_name = input('Enter name of log file: ')47host_address = input('Enter IP address: ')48ID = input('Enter ID for search: ')...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!