Best Python code snippet using autotest_python
test_xclusion_crits.py
Source:test_xclusion_crits.py  
1# ----------------------------------------------------------------------------2# Copyright (c) 2020, Franck Lejzerowicz.3#4# Distributed under the terms of the Modified BSD License.5#6# The full license is in the file LICENSE, distributed with this software.7# ----------------------------------------------------------------------------8import unittest9import pandas as pd10import pkg_resources11from pandas.testing import assert_frame_equal12from Xclusion_criteria.xclusion_crits import (13    check_in_md,14    check_factors,15    check_index,16    check_islist,17    check_key,18    check_numeric_indicator,19    check_var_in_md,20    get_criteria,21    do_filtering22)23ROOT = pkg_resources.resource_filename('Xclusion_criteria', 'tests')24class TestCrits(unittest.TestCase):25    def setUp(self):26        self.messages = []27        self.criteria = {}28        self.md = pd.DataFrame({29            'antibiotic_history': ['Yes', 'No', 'No'],30            'col2': [1., 2., 3.],31            'col3': [1.3, 1, 'missing']32        })33        self.nulls = ['missing', 'not applicable']34    def test_check_in_md(self):35        check_in_md(36            ['var_1', 'var_2', 'var_3'],37            ['var_1', 'var_2'],38            self.criteria,39            self.messages,40            ['missing', 'not applicable'],41            'init'42        )43        criteria = {'init': {44                ('var_1', '0'): ['missing', 'not applicable'],45                ('var_2', '0'): ['missing', 'not applicable']46            }47        }48        messages = ['Variable var_3 not in metadata (skipped)']49        self.assertEqual(self.criteria, criteria)50        self.assertEqual(self.messages, messages)51    def test_check_factors(self):52        test_message = []53        test_boolean, test_values = check_factors('col', '2', ['what', 'ever'], self.nulls, pd.DataFrame({'col': ['anything', 'else']}), test_message)54        self.assertEqual(test_boolean, False)55        self.assertEqual(test_values, ['what', 'ever'])56        self.assertEqual(test_message, [])57        test_message = []58        test_boolean, test_values = check_factors('col', '1', ['f4', 'f5'], self.nulls, pd.DataFrame({'col': ['f1', 'f2']}), test_message)59        self.assertEqual(test_boolean, True)60        self.assertEqual(test_values, [])61        self.assertEqual(test_message, ['Subset values for variable col not in table (skipped)'])62        test_message = []63        test_boolean, test_values = check_factors('col', '1', ['f1', 'f2', 'f3'], self.nulls, pd.DataFrame({'col': ['f1', 'f2']}), test_message)64        self.assertEqual(test_boolean, False)65        self.assertEqual(test_values, ['f1', 'f2'])66        self.assertEqual(test_message, ['[Warning] Subset values for variable col not in table\n - f3'])67    def test_check_index(self):68        test_boolean = check_index('0', ['f1', 'f2', 'f3'], [])69        self.assertEqual(test_boolean, False)70        test_boolean = check_index('1', ['f1', 'f2', 'f3'], [])71        self.assertEqual(test_boolean, False)72        test_message = []73        test_boolean = check_index('2', [None], test_message)74        self.assertEqual(test_boolean, True)75        test_message = []76        test_boolean = check_index('2', ['f1', 'f2', 'f3'], test_message)77        self.assertEqual(test_boolean, True)78        self.assertEqual(test_message,79                         ['For min-max subsetting, two-items list need: no min (or no max) should be "None"'])80    def check_is_list(self):81        test_messages = []82        test_boolean = check_islist('var', ['f1', 'f2', 'f3'], test_messages)83        self.assertEqual(test_boolean, False)84        self.assertEqual(test_messages, [])85        test_messages = []86        test_boolean = check_islist('var', 'f1', test_messages)87        self.assertEqual(test_boolean, True)88        self.assertEqual(test_messages, ['Values to subset for must be in a list format (var skipped)'])89        test_messages = []90        test_boolean = check_islist('var', False, test_messages)91        self.assertEqual(test_boolean, True)92        self.assertEqual(test_messages, ['Values to subset for must be in a list format (var skipped)'])93        test_messages = []94        test_boolean = check_islist('var', {'f1'}, test_messages)95        self.assertEqual(test_boolean, True)96        self.assertEqual(test_messages, ['Values to subset for must be in a list format (var skipped)'])97    def check_numeric_indicator(self):98        test_boolean = check_numeric_indicator('col', '0', [])99        self.assertEqual(test_boolean, False)100        test_boolean = check_numeric_indicator('col', '1', [])101        self.assertEqual(test_boolean, False)102        test_boolean = check_numeric_indicator('col', '2', [])103        self.assertEqual(test_boolean, False)104        test_messages = []105        test_boolean = check_numeric_indicator('col', 'x', test_messages)106        self.assertEqual(test_boolean, False)107        self.assertEqual(test_messages, ['Numeric indicator not "0", "1" or "2" (x) (col skipped)'])108    def check_var_in_md(self):109        test_messages = []110        test_boolean = check_var_in_md('col1', ['col1', 'col2'], test_messages)111        self.assertEqual(test_boolean, False)112        self.assertEqual(test_messages, [])113        test_boolean = check_var_in_md('col1', ['col2', 'col3'], test_messages)114        self.assertEqual(test_boolean, True)115        self.assertEqual(test_messages, ['Variable col1 not in metadata (skipped)'])116    def check_in_md(self):117        test_messages = []118        test_criteria = {}119        check_in_md(['col1', 'col2'], ['col1', 'col2', 'col3'],120                    test_criteria, test_messages, ['missing'], 'init')121        self.assertEqual(test_messages, [])122        self.assertEqual(test_criteria, {('col1', '0'): ['missing'], ('col2', '0'): ['missing']})123        test_messages = []124        test_criteria = {}125        check_in_md(['col1', 'col2'], ['col2', 'col3'],126                    test_criteria, test_messages, ['missing'], 'init')127        self.assertEqual(test_messages, ['Variable col1 not in metadata (skipped)'])128        self.assertEqual(test_criteria, {('col1', '0'): ['missing']})129        test_messages = []130        test_criteria = {}131        check_in_md(['col1', 'col2'], ['col3'],132                    test_criteria, test_messages, ['missing'], 'init')133        self.assertEqual(test_messages, ['Variable col1 not in metadata (skipped)',134                                         'Variable col2 not in metadata (skipped)'])135        self.assertEqual(test_criteria, {})136    def test_check_key(self):137        test_messages = []138        test_boolean = check_key('col,0', test_messages)139        self.assertEqual(test_boolean, False)140        self.assertEqual(test_messages, [])141        test_messages = []142        test_boolean = check_key('col+0', test_messages)143        self.assertEqual(test_boolean, True)144        self.assertEqual(test_messages, ['Must have a metadata variable and a numeric separated by a comma (",")'])145        test_messages = []146        test_boolean = check_key('col,0,', test_messages)147        self.assertEqual(test_boolean, True)148        self.assertEqual(test_messages, ['Must have a metadata variable and a numeric separated by a comma (",")'])149    def test_get_criteria(self):150        no_comma = '%s/criteria/criteria_no_comma.yml' % ROOT151        test_messages = []152        test_criteria = get_criteria(no_comma, self.md, self.nulls, test_messages)153        self.assertEqual(test_criteria, {})154        self.assertEqual(test_messages, ['Must have a metadata variable and a numeric separated by a comma (",")'])155        no_correct_index = '%s/criteria/criteria_no_correct_index.yml' % ROOT156        test_messages = []157        test_criteria = get_criteria(no_correct_index, self.md, self.nulls, test_messages)158        self.assertEqual(test_criteria, {})159        self.assertEqual(test_messages, ['Numeric indicator not "0", "1" or "2" (9) (antibiotic_history skipped)'])160        no_index = '%s/criteria/criteria_no_index.yml' % ROOT161        test_messages = []162        test_criteria = get_criteria(no_index, self.md, self.nulls, test_messages)163        self.assertEqual(test_criteria, {})164        self.assertEqual(test_messages, ['Must have a metadata variable and a numeric separated by a comma (",")'])165        var_not_in_md = '%s/criteria/criteria_var_not_in_md.yml' % ROOT166        test_messages = []167        test_criteria = get_criteria(var_not_in_md, self.md, self.nulls, test_messages)168        self.assertEqual(test_criteria, {})169        self.assertEqual(test_messages, ['Variable not_in_md not in metadata (skipped)'])170        is_not_list = '%s/criteria/criteria_is_not_list.yml' % ROOT171        test_messages = []172        test_criteria = get_criteria(is_not_list, self.md, self.nulls, test_messages)173        self.assertEqual(test_criteria, {})174        self.assertEqual(test_messages, ['Values to subset for must be in a list format (antibiotic_history skipped)'])175        wrong_minmax = '%s/criteria/criteria_wrong_minmax.yml' % ROOT176        test_messages = []177        test_criteria = get_criteria(wrong_minmax, self.md, self.nulls, test_messages)178        self.assertEqual(test_criteria, {})179        self.assertEqual(180            test_messages,181            ['For min-max subsetting, two-items list need: no min (or no max) should be "None"'])182    def test_do_filtering(self):183        md_abx_filt_y = pd.DataFrame({'antibiotic_history': ['Yes'], 'col2': [1.], 'col3': ['1.3']})184        test_name, test_boolean, test_md_abx_y = do_filtering(self.md, 'antibiotic_history', '1', ['Yes'], [], [])185        md_abx_filt_y.col3 = md_abx_filt_y.col3.astype('object')186        test_md_abx_y.col3 = md_abx_filt_y.col3.astype('object')187        self.assertEqual(test_name, 'antibiotic_history')188        self.assertEqual(test_boolean, False)189        assert_frame_equal(md_abx_filt_y, test_md_abx_y)190        md_abx_filt_n = pd.DataFrame({'antibiotic_history': ['No', 'No'], 'col2': [2., 3.], 'col3': [1, 'missing']})191        test_name, test_boolean, test_md_abx_x = do_filtering(self.md, 'antibiotic_history', '1', ['No'], [], [])192        md_abx_filt_n.index = range(md_abx_filt_n.shape[0])193        test_md_abx_x.index = range(test_md_abx_x.shape[0])194        self.assertEqual(test_name, 'antibiotic_history')195        self.assertEqual(test_boolean, False)196        assert_frame_equal(md_abx_filt_n, test_md_abx_x)197        md_abx_filt_n = pd.DataFrame({'antibiotic_history': ['No', 'No'], 'col2': [2., 3.], 'col3': [1, 'missing']})198        test_name, test_boolean, test_md_abx_x = do_filtering(self.md, 'antibiotic_history', '0', ['Yes'], [], [])199        md_abx_filt_n.index = range(md_abx_filt_n.shape[0])200        test_md_abx_x.index = range(test_md_abx_x.shape[0])201        self.assertEqual(test_name, 'No_antibiotic_history')202        self.assertEqual(test_boolean, False)203        assert_frame_equal(md_abx_filt_n, test_md_abx_x)204        md_abx_filt_mm = pd.DataFrame({'antibiotic_history': ['Yes', 'No'], 'col2': [1., 2.], 'col3': ['1.3', '1']})205        test_name, test_boolean, test_md_abx_mm = do_filtering(self.md, 'col2', '2', ['None', 3], ['col2'], [])206        # weirdly, if the two col3 contents are "object" and identical, the test fails, hence:207        md_abx_filt_mm.col3 = md_abx_filt_mm.col3.astype('float')208        test_md_abx_mm.col3 = test_md_abx_mm.col3.astype('float')209        md_abx_filt_mm.index = range(md_abx_filt_mm.shape[0])210        test_md_abx_mm.index = range(test_md_abx_mm.shape[0])211        self.assertEqual(test_name, 'Range_col2')212        self.assertEqual(test_boolean, False)213        assert_frame_equal(md_abx_filt_mm, test_md_abx_mm)214        md_abx_filt_mm = pd.DataFrame({'antibiotic_history': ['No'], 'col2': [2.], 'col3': ['1']})215        test_name, test_boolean, test_md_abx_mm = do_filtering(self.md, 'col2', '2', [1, 3], ['col2'], [])216        # weirdly, if the two col3 contents are "object" and identical, the test fails, hence:217        md_abx_filt_mm.col3 = md_abx_filt_mm.col3.astype('float')218        test_md_abx_mm.col3 = test_md_abx_mm.col3.astype('float')219        md_abx_filt_mm.index = range(md_abx_filt_mm.shape[0])220        test_md_abx_mm.index = range(test_md_abx_mm.shape[0])221        self.assertEqual(test_name, 'Range_col2')222        self.assertEqual(test_boolean, False)223        assert_frame_equal(md_abx_filt_mm, test_md_abx_mm)224        test_messages = []225        test_name, test_boolean, output_md = do_filtering(self.md, 'col2', '2', ['None', 3], ['col3'], test_messages)226        self.assertEqual(test_name, '')227        self.assertEqual(test_boolean, True)228        self.assertEqual(test_messages, ['Metadata variable col2 is not numerical (skipping)'])229        test_messages = []230        test_name, test_boolean, output_md = do_filtering(self.md, 'col2', '2', ['None', 'None'], ['col2'], test_messages)231        self.assertEqual(test_name, '')232        self.assertEqual(test_boolean, True)233        self.assertEqual(test_messages, ['[Warning] Both numerical bounds for col2 are "None" (skipping)'])234if __name__ == '__main__':...test_d04.py
Source:test_d04.py  
...31# ex0032print("\nTests for ex00:")33run_command("rm -f cook.txt")34exitcode, out, err = run_command("curl -v -c cook.txt '" + day_on_server_location + "/ex00/index.php' | grep 'login'")35test_boolean('name="login"' in out, "login named correctly")36test_boolean(("value" not in out) or ('value=""' in out), "login set correctly")37exitcode, out, err = run_command("curl -v -c cook.txt '" + day_on_server_location + "/ex00/index.php' | grep 'passwd'")38test_boolean('name="passwd"' in out, "passwd named correctly")39test_boolean(("value" not in out) or ('value=""' in out), "passwd set correctly")40exitcode, out, err = run_command("curl -v -c cook.txt '" + day_on_server_location + "/ex00/index.php' | grep 'submit'")41test_boolean('name="submit"' in out, "submit named correctly")42test_boolean(("value" not in out) or ('value="OK"' in out), "submit set correctly")43exitcode, out, err = run_command("curl -v -b cook.txt '" + day_on_server_location + "/ex00/index.php?login=sb&passwd=beeone' | grep 'login'")44test_boolean('name="login"' in out, "login named correctly after no ok")45test_boolean(("value" not in out) or ('value=""' in out), "login set correctly after no ok")46exitcode, out, err = run_command("curl -v -b cook.txt '" + day_on_server_location + "/ex00/index.php?login=sb&passwd=beeone' | grep 'passwd'")47test_boolean('name="passwd"' in out, "passwd named correctly after no ok")48test_boolean(("value" not in out) or ('value=""' in out), "passwd set correctly after no ok")49exitcode, out, err = run_command("curl -v -b cook.txt '" + day_on_server_location + "/ex00/index.php?login=sb&passwd=beeone' | grep 'submit'")50test_boolean('name="submit"' in out, "submit named correctly after no ok")51test_boolean(("value" not in out) or ('value="OK"' in out), "submit set correctly after no ok")52exitcode, out, err = run_command("curl -v -b cook.txt '" + day_on_server_location + "/ex00/index.php?login=sb&passwd=beeone&submit=OK' | grep 'login'")53test_boolean('name="login"' in out, "login named correctly after ok")54test_boolean('value="sb"' in out, "login set correctly after ok")55exitcode, out, err = run_command("curl -v -b cook.txt '" + day_on_server_location + "/ex00/index.php?login=sb&passwd=beeone' | grep 'passwd'")56test_boolean('name="passwd"' in out, "passwd named correctly after ok")57test_boolean('value="beeone"' in out, "passwd set correctly after ok")58exitcode, out, err = run_command("curl -v -b cook.txt '" + day_on_server_location + "/ex00/index.php?login=sb&passwd=beeone' | grep 'submit'")59test_boolean('name="submit"' in out, "submit named correctly after ok")60test_boolean('value="OK"' in out, "submit set correctly after ok")61exitcode, out, err = run_command("curl -v -b cook.txt '" + day_on_server_location + "/ex00/index.php' | grep 'login'")62test_boolean('name="login"' in out, "login named correctly after not passing stuff in url")63test_boolean('value="sb"' in out, "login set correctly after not passing stuff in url")64exitcode, out, err = run_command("curl -v -b cook.txt '" + day_on_server_location + "/ex00/index.php' | grep 'passwd'")65test_boolean('name="passwd"' in out, "passwd named correctly after not passing stuff in url")66test_boolean('value="beeone"' in out, "passwd set correctly after not passing stuff in url")67exitcode, out, err = run_command("curl -v -b cook.txt '" + day_on_server_location + "/ex00/index.php' | grep 'submit'")68test_boolean('name="submit"' in out, "submit named correctly after not passing stuff in url")69test_boolean('value="OK"' in out, "submit set correctly after not passing stuff in url")70exitcode, out, err = run_command("curl -v '" + day_on_server_location + "/ex00/index.php' | grep 'login'")71test_boolean('name="login"' in out, "login named correctly after removing cookie file")72test_boolean(("value" not in out) or ('value=""' in out), "login set correctly after removing cookie file")73exitcode, out, err = run_command("curl -v -c cook.txt '" + day_on_server_location + "/ex00/index.php' | grep 'passwd'")74test_boolean('name="passwd"' in out, "passwd named correctly after removing cookie file")75test_boolean(("value" not in out) or ('value=""' in out), "passwd set correctly after removing cookie file")76exitcode, out, err = run_command("curl -v -c cook.txt '" + day_on_server_location + "/ex00/index.php' | grep 'submit'")77test_boolean('name="submit"' in out, "submit named correctly after removing cookie file")78test_boolean(("value" not in out) or ('value="OK"' in out), "submit set correctly after removing cookie file")79run_command("rm -f cook.txt")80print("")81# ex0182print("Tests for ex01:")83run_command("rm -rf " + expanduser(location_of_private))84test_command("curl -d login=toto1 -d passwd=titi1 -d submit=OK '" + day_on_server_location + "/ex01/create.php'", "OK\n", 0)85password_file = expanduser(location_of_private + "/passwd")86test_boolean('a:1:{' == get_file_contents(password_file)[:5]87                  , "begin part of serialized file (" + password_file + ")")88test_command("curl -d login=toto1 -d passwd=titi1 -d submit=OK '" + day_on_server_location + "/ex01/create.php'", "ERROR\n", 0)89test_command("curl -d login=toto2 -d passwd= -d submit=OK '" + day_on_server_location + "/ex01/create.php'", "ERROR\n", 0)90test_command("curl -d login= -d passwd=hello -d submit=OK '" + day_on_server_location + "/ex01/create.php'", "ERROR\n", 0)91test_command("curl -d login=toto1 -d passwd=titi1 -d submit=OK '" + day_on_server_location + "/ex01/create.php'", "ERROR\n", 0)92test_command("curl -d login=toto1 -d passwd=titi1 -d submit=OKK '" + day_on_server_location + "/ex01/create.php'", "ERROR\n", 0)93test_command("curl -d login=login -d passwd=password '" + day_on_server_location + "/ex01/create.php'", "ERROR\n", 0)94exitcode, out, err = run_command("curl -v '" + day_on_server_location + "/ex01/index.html'")95test_boolean('method="POST"' in out or 'method="post"' in out, "correct method")96run_command("rm -rf " + expanduser(location_of_private))97print("")98# ex0299print("Tests for ex02:")100run_command("rm -rf " + expanduser(location_of_private))101test_command("curl -d login=x -d passwd=21 -d submit=OK '" + day_on_server_location + "/ex01/create.php'", "OK\n", 0)102password_file = location_of_private + "/passwd"103test_boolean('a:1:{' == get_file_contents(location_of_private + "/passwd")[:5]104                  , "begin part of serialized file (" + password_file + ")")105test_command("curl -d login=x -d oldpw=21 -d newpw=42 -d submit=OK '" + day_on_server_location + "/ex02/modif.php'", "OK\n", 0) # change to 42106test_command("curl -d login=x -d oldpw=42 -d newpw=hello -d submit=OK '" + day_on_server_location + "/ex02/modif.php'", "OK\n", 0) # change to hello107test_command("curl -d login=x -d oldpw=hello -d newpw=42 '" + day_on_server_location + "/ex02/modif.php'", "ERROR\n", 0) # no submit=OK108test_command("curl -d login=x -d oldpw=hello -d newpw=42 -d submit=OK '" + day_on_server_location + "/ex02/modif.php'", "OK\n", 0) # change to 42109test_command("curl -d login=x -d oldpw=21 -d newpw=42 -d submit=OK '" + day_on_server_location + "/ex02/modif.php'", "ERROR\n", 0) # wrong password110test_command("curl -d login=x -d oldpw=42 -d newpw= -d submit=OK '" + day_on_server_location + "/ex02/modif.php'", "ERROR\n", 0) # blank new password111exitcode, out, err = run_command("curl -v '" + day_on_server_location + "/ex02/index.html'")112test_boolean('method="POST"' in out or 'method="post"' in out, "correct method")113run_command("rm -rf " + expanduser(location_of_private))114print("")115# ex03116print("Tests for ex03:")117run_command("rm -rf " + expanduser(location_of_private))118test_command("curl -d login=toto -d passwd=titi -d submit=OK '" + day_on_server_location + "/ex01/create.php'", "OK\n", 0)119password_file = location_of_private + "/passwd"120test_boolean('a:1:{' == get_file_contents(location_of_private + "/passwd")[:5]121                  , "begin part of serialized file (" + password_file + ")")122test_command("curl '" + day_on_server_location + "/ex03/login.php?login=toto&passwd=titi'", "OK\n", 0) # check login.php: correct123run_command("rm -rf " + expanduser(location_of_private))124run_command("curl -d login=toto -d passwd=titi -d submit=OK '" + day_on_server_location + "/ex01/create.php'")125test_command("curl -c cook.txt '" + day_on_server_location + "/ex03/login.php?login=toto&passwd=titi'", "OK\n", 0)126test_command("curl -b cook.txt '" + day_on_server_location + "/ex03/whoami.php'", "toto\n", 0)127test_command("curl -b cook.txt '" + day_on_server_location + "/ex03/logout.php'", "", 0)128test_command("curl -b cook.txt '" + day_on_server_location + "/ex03/whoami.php'", "ERROR\n", 0)129run_command("rm -f cook.txt")130# END OF TESTS  END OF TESTS  END OF TESTS  END OF TESTS  END OF TESTS...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!!
