Best Python code snippet using lemoncheesecake
test_watch_directory.py
Source:test_watch_directory.py
...3from os.path import isfile, join4from os import chmod, remove, stat as do_stat5from stat import S_IEXEC6from tempfile import TemporaryDirectory as tempdir7def get_fixtures(td):8 log_path = join(td, 'log.txt')9 script_path = join(td, 'script.sh')10 make_test_script(script_path, log_path)11 return script_path, log_path12def make_test_file(path, content):13 with open(path, 'w') as f:14 f.write(content)15def make_test_script(script_path, log_path):16 with open(script_path, 'w') as f:17 content = '#!/bin/bash\n echo "${1}" >> "%s"' % log_path18 f.write(content)19 st = do_stat(script_path)20 chmod(script_path, st.st_mode | S_IEXEC)21class WatchDirectoryArgumentTests(common.TestCase, metaclass=common.LoggableTestCase):22 def setUp(self):23 self.mod = common.load('watch_directory', common.TOOLS_DIR + '/scripts/files/watch_directory.py')24 self.mod._enable_colours(False)25 self.mod._logger = common.logging.getLogger(common.LABEL_TEST_LOGGER)26 def check_error(self, prefix, content=None):27 errors = self.getLogs('error')28 self.assertEqual(2, len(errors))29 self.assertStartsWith('Usage:', errors[1])30 self.assertStartsWith(prefix, errors[0])31 if content:32 self.assertContains(content, errors[0])33 def run_fail(self, cmd):34 with self.assertRaises(SystemExit) as ex:35 self.mod._parse_args(cmd)36 self.assertEqual(1, ex.exception.code)37 self.assertEmpty(self.getLogs('info'))38 def test_main_error_bad_args(self):39 self.run_fail(['--bad-args'])40 self.check_error('Error parsing arguments:')41 def test_fail_bad_number(self):42 with tempdir() as management_dir:43 script_path, log_path = get_fixtures(management_dir)44 with tempdir() as file_dir:45 self.run_fail(['-s', script_path, file_dir, '-w', 'z'])46 self.check_error('Invalid number: ', 'z')47 def test_fail_no_script(self):48 with tempdir() as file_dir:49 script_path = join(file_dir, 'a')50 self.run_fail(['-s', script_path, file_dir])51 self.check_error('Invalid script path: ', script_path)52 def test_run_fail_no_dir_exist(self):53 with tempdir() as management_dir:54 script_path, log_path = get_fixtures(management_dir)55 with tempdir() as file_dir:56 bad_path = join(file_dir, 'a')57 self.run_fail(['-s', script_path, bad_path])58 self.check_error('Invalid target directory path: ', bad_path)59 def test_fail_no_arguments(self):60 self.run_fail([])61 self.assertEmpty(self.getLogs('info'))62 errors = self.getLogs('error')63 self.assertEqual(3, len(errors))64 self.assertStartsWith('Usage:', errors[-1])65 self.assertContains('No script provided.', errors)66 self.assertContains('No directory provided.', errors)67 def test_fail_no_dir_provided(self):68 with tempdir() as management_dir:69 script_path, log_path = get_fixtures(management_dir)70 self.run_fail(['-s', script_path])71 self.check_error('No directory provided.')72 def test_fail_no_script_provided(self):73 with tempdir() as file_dir:74 self.run_fail([file_dir])75 self.check_error('No script provided.')76 def test_fail_script_not_executable(self):77 with tempdir() as file_dir:78 script_path = join(file_dir, 'a')79 make_test_file(script_path, 'moot')80 self.run_fail(['-s', script_path, file_dir])81 self.check_error('Invalid script path: ', script_path)82 def test_hexit(self):83 with self.assertRaises(SystemExit) as ex:84 self.mod._parse_args(['-h'])85 self.assertEqual(0, ex.exception.code)86 msg = self.assertSingle(self.getLogs('error'))87 self.assertStartsWith('Usage: ', msg)88 def test_pass(self):89 with tempdir() as management_dir:90 script_path, log_path = get_fixtures(management_dir)91 with tempdir() as file_dir:92 data = self.mod._parse_args(['-s', script_path, file_dir])93 self.assertEmpty(self.getLogs('info'))94 self.assertEmpty(self.getLogs('error'))95 self.assertFalse(data.get('recursive', False))96 self.assertNone(data.get('workers'))97 def test_pass_recursive(self):98 with tempdir() as management_dir:99 script_path, log_path = get_fixtures(management_dir)100 with tempdir() as file_dir:101 data = self.mod._parse_args(['-s', script_path, file_dir, '-r'])102 self.assertEmpty(self.getLogs('info'))103 self.assertEmpty(self.getLogs('error'))104 self.assertTrue(data.get('recursive', False))105 self.assertNone(data.get('workers'))106 def test_pass_workers(self):107 with tempdir() as management_dir:108 script_path, log_path = get_fixtures(management_dir)109 with tempdir() as file_dir:110 data = self.mod._parse_args(['-s', script_path, file_dir, '-w', '5'])111 self.assertEmpty(self.getLogs('info'))112 self.assertEmpty(self.getLogs('error'))113 self.assertFalse(data.get('recursive', False))114 self.assertEqual(5, data.get('workers'))115class WatchDirectoryRunTests(common.TestCase, metaclass=common.LoggableTestCase):116 def setUp(self):117 self.mod = common.load('watch_directory', common.TOOLS_DIR + '/scripts/files/watch_directory.py')118 self.mod._enable_colours(False)119 self.mod._logger = common.logging.getLogger(common.LABEL_TEST_LOGGER)120 def run_cmd(self, cmd, execute_only=True):121 if execute_only:122 cmd.append('--execute-only')123 exit_code = self.mod._main(cmd)124 self.assertEqual(0, exit_code)125 def test_run_executeonly_content(self):126 with tempdir() as management_dir:127 script_path, log_path = get_fixtures(management_dir)128 with tempdir() as file_dir:129 file_1 = join(file_dir, 'a')130 file_2 = join(file_dir, 'b')131 file_3 = join(file_dir, 'c')132 make_test_file(file_1, 'abc')133 make_test_file(file_2, '123')134 # Make file_3 EMPTY, so that it should not be run.135 make_test_file(file_3, '')136 import time137 self.run_cmd(['-s', script_path, file_dir])138 # Expected to have run the script twice.139 self.assertTrue(isfile(log_path))140 with open(log_path) as f:141 lines = [l.strip() for l in f.readlines()]142 self.assertContains(file_1, lines)143 self.assertContains(file_2, lines)144 self.assertDoesNotContain(file_3, lines)145 info = self.getLogs('info')146 for path in [file_1, file_2]:147 self.assertContains(f'Handling file: {path}', info)148 self.assertContains(f'Finished with file: {path}', info)149 def test_run_executeonly_none(self):150 with tempdir() as management_dir:151 script_path, log_path = get_fixtures(management_dir)152 with tempdir() as file_dir:153 self.run_cmd(['-s', script_path, file_dir])154 # No files in this directory, so nothing got run....
test_echarts.py
Source:test_echarts.py
...3RANGE_COLOR = ['#313695', '#4575b4', '#74add1', '#abd9e9',4 '#e0f3f8', '#ffffbf',5 '#fee090', '#fdae61', '#f46d43', '#d73027', '#a50026']6def test_pie_chart():7 s = p.get_sheet(file_name=get_fixtures('pie.csv'))8 s.save_as('pie.echarts.html', chart_type='pie')9def test_kline_chart():10 s = p.get_sheet(file_name=get_fixtures('kline.csv'))11 s.save_as('kline.echarts.html', chart_type='kline', legend='daily k')12def test_radar_chart():13 s = p.get_sheet(file_name=get_fixtures('radar.csv'))14 s.save_as('radar.echarts.html', chart_type='radar')15def test_bar_chart():16 s = p.get_sheet(file_name=get_fixtures('bar.csv'))17 s.save_as('bar.echarts.html', chart_type='bar')18def test_scatter3d_chart():19 s = p.get_sheet(file_name=get_fixtures('scatter_3d.csv'))20 s.save_as('scatter3d.echarts.html', chart_type='scatter3d',21 is_visualmap=True,22 visual_range_color=RANGE_COLOR)23def test_bar3d_chart():24 s = p.get_sheet(file_name=get_fixtures('bar3d.csv'))25 s.save_as('bar3d.echarts.html', chart_type='bar3d',26 is_visualmap=True, visual_range_color=RANGE_COLOR,27 visual_range=[0, 20],28 grid3D_width=200, grid3D_depth=80)29def test_heatmap_chart():30 s = p.get_sheet(file_name=get_fixtures('bar3d.csv'))31 s.save_as('heatmap.echarts.html', chart_type='heatmap',32 is_visualmap=True, visual_range_color=RANGE_COLOR,33 visual_range=[0, 20],34 visual_text_color="#000", visual_orient='horizontal')35def test_effectscatter_chart():36 s = p.get_sheet(file_name=get_fixtures('effectscatter.csv'))37 s.save_as('effectscatter.echarts.html', chart_type='effectscatter')38def test_funnel_chart():39 s = p.get_sheet(file_name=get_fixtures('funnel.csv'))40 s.save_as('funnel.echarts.html', chart_type='funnel')41def test_line_chart():42 s = p.get_sheet(file_name=get_fixtures('line.csv'))43 s.save_as('line.echarts.html', chart_type='line')44def test_gauge_chart():45 s = p.get_sheet(file_name=get_fixtures('gauge.csv'))...
fixtures_service.py
Source:fixtures_service.py
1from src import get_fixtures2def get_team_home_fixtures(team_id, starting_gw, ending_gw):3 fixtures = get_fixtures()4 home_fixtures = []5 for fixture in fixtures:6 if fixture.get("team_h") == team_id and starting_gw <= fixture.get("event") <= ending_gw:7 home_fixtures.append(fixture)8 return home_fixtures9def get_team_away_fixtures(team_id, starting_gw, ending_gw):10 fixtures = get_fixtures()11 away_fixtures = []12 for fixture in fixtures:13 if fixture.get("team_a") == team_id and starting_gw <= fixture.get("event") <= ending_gw:14 away_fixtures.append(fixture)15 return away_fixtures16def get_team_fixtures(team_id, starting_gw, ending_gw):17 fixtures = get_fixtures()18 team_fixtures = []19 for fixture in fixtures:20 if (fixture.get("team_h") == team_id or fixture.get("team_a") == team_id) \21 and starting_gw <= fixture.get("event") <= ending_gw:22 team_fixtures.append(fixture)...
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!!