Best Python code snippet using tox_python
test_io.py
Source:test_io.py
1import os2import sys3import shutil4import pytest5import numpy as np6from imageio import imread7from dmtools.io import (Metadata, read, write_netpbm, write_png, write_ascii,8 recreate_script_from_png, _get_next_version)9RESOURCES_PATH = os.path.join(os.path.dirname(__file__), 'resources/io_tests')10def test_get_next_version():11 image = np.array([[[1,1,1]]]) # white pixel image12 # test PNG versioning13 for i in range(1,10):14 expected = f"white_pixel_{i:04}.png"15 assert expected == _get_next_version("white_pixel.png")16 write_png(image, "white_pixel.png", versioning=True)17 for i in range(1,10):18 os.remove(f"white_pixel_{i:04}.png")19 # test Netpbm versioning20 for i in range(1,10):21 expected = f"white_pixel_{i:04}.ppm"22 assert expected == _get_next_version("white_pixel.ppm")23 write_netpbm(image, 255, "white_pixel.ppm", versioning=True)24 for i in range(1,10):25 os.remove(f"white_pixel_{i:04}.ppm")26 # test directory prefix27 dir_name = "dir_for_version_testing"28 os.makedirs(dir_name, exist_ok=True)29 for i in range(1,10):30 expected = f"{dir_name}/white_pixel_{i:04}.png"31 assert expected == _get_next_version(f"{dir_name}/white_pixel.png")32 write_png(image, f"{dir_name}/white_pixel.png", versioning=True)33 shutil.rmtree(dir_name)34@pytest.mark.parametrize("name",[35 ('color_matrix.png')])36def test_png_io(name):37 # read image38 ext = name.split('.')[-1]39 src = read(os.path.join(RESOURCES_PATH, name))40 file_name = 'text.%s' % ext41 write_png(src, file_name)42 image = read(file_name)43 os.remove(file_name)44 assert np.array_equal(src, image)45@pytest.mark.parametrize("name,k,new_ext",[46 ('color_matrix_ascii.pbm', 1, 'pbm'),47 ('color_matrix_ascii.pbm', 255, 'pgm'),48 ('color_matrix_ascii.pgm', 255, 'pgm'),49 ('color_matrix_ascii.ppm', 255, 'ppm'),50 ('color_matrix_raw.pbm', 1, 'pbm'),51 ('color_matrix_raw.pbm', 255, 'pgm'),52 ('color_matrix_raw.pgm', 255, 'pgm'),53 ('color_matrix_raw.ppm', 255, 'ppm')])54def test_netpbm_io(name, k, new_ext):55 # read image56 src = read(os.path.join(RESOURCES_PATH, name))57 file_name = 'test.%s' % new_ext58 write_netpbm(src, k, file_name)59 image = read(file_name)60 os.remove(file_name)61 assert np.array_equal(src, image)62@pytest.mark.parametrize("src,txt_expected_path,png_expected_path",[63 ('12_gradient.pgm', '12_gradient.txt', '12_gradient.png')])64def test_ascii_io(src, txt_expected_path, png_expected_path):65 image = read(os.path.join(RESOURCES_PATH, src))66 # test writing to ASCII txt67 write_ascii(image, 'test.txt', txt=True)68 txt_actual = ""69 with open('test.txt', mode='r') as f:70 txt_actual = f.read()71 txt_expected = ""72 with open(os.path.join(RESOURCES_PATH, txt_expected_path), mode='r') as f:73 txt_expected = f.read()74 os.remove('test.txt')75 assert txt_actual == txt_expected76 # test writing to ASCII PNG77 write_ascii(image, 'test.png')78 png_actual = read('test.png')79 png_expected = read(os.path.join(RESOURCES_PATH, png_expected_path))80 os.remove('test.png')81 assert np.array_equal(png_actual, png_expected)82def test_metadata_io():83 metadata = Metadata()84 src = read(os.path.join(RESOURCES_PATH, "color_matrix.png"))85 write_png(src, "color_matrix_metadata.png", metadata=metadata)86 write_netpbm(src, 255, "color_matrix_metadata.ppm", metadata=metadata)87 png_metadata = imread("color_matrix_metadata.png").meta88 os.remove("color_matrix_metadata.png")89 os.remove("color_matrix_metadata.ppm")90 assert png_metadata['Creation Time'] == metadata.creation_time91 assert png_metadata['Software'] == metadata.software92 assert png_metadata['Source'] == metadata.source93def test_recreate_script_from_png():94 source = open(sys.argv[0]).read()95 image = read(os.path.join(RESOURCES_PATH, "color_matrix.png"))96 write_png(image, "color_matrix_metadata.png")97 recreate_script_from_png("color_matrix_metadata.png", "test_recreate.py")98 source_from_file = open("test_recreate.py").read()99 os.remove("color_matrix_metadata.png")100 os.remove("test_recreate.py")...
test_schemas.py
Source:test_schemas.py
...10def test_destroy():11 for store in stores.known_stores.values():12 store.destroy()13 env.context.pgcursor.connection.commit()14def test_recreate():15 for store in stores.known_stores.values():16 store.setup()...
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!!