Best Python code snippet using assertpy_python
testRemoveMembersFromGroups.py
Source:testRemoveMembersFromGroups.py
1"""2"""3# Created on 2016.04.174#5# Author: Giovanni Cannata6#7# Copyright 2016 - 2020 Giovanni Cannata8#9# This file is part of ldap3.10#11# ldap3 is free software: you can redistribute it and/or modify12# it under the terms of the GNU Lesser General Public License as published13# by the Free Software Foundation, either version 3 of the License, or14# (at your option) any later version.15#16# ldap3 is distributed in the hope that it will be useful,17# but WITHOUT ANY WARRANTY; without even the implied warranty of18# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the19# GNU Lesser General Public License for more details.20#21# You should have received a copy of the GNU Lesser General Public License22# along with ldap3 in the COPYING and COPYING.LESSER files.23# If not, see <http://www.gnu.org/licenses/>.24import unittest25from test.config import test_base, add_user, add_group, get_connection, drop_connection, random_id, test_server_type, get_response_values26testcase_id = ''27class Test(unittest.TestCase):28 def setUp(self):29 global testcase_id30 testcase_id = random_id()31 self.connection = get_connection()32 self.delete_at_teardown = []33 def tearDown(self):34 drop_connection(self.connection, self.delete_at_teardown)35 self.assertFalse(self.connection.bound)36 def test_remove_member_from_group(self):37 if test_server_type == 'EDIR' and not self.connection.strategy.pooled and not self.connection.strategy.no_real_dsa:38 self.delete_at_teardown.append(add_user(self.connection, testcase_id, 'user-1'))39 self.delete_at_teardown.append(add_group(self.connection, testcase_id, 'group-1'))40 self.delete_at_teardown.append(add_group(self.connection, testcase_id, 'group-1b', [self.delete_at_teardown[0]]))41 self.connection.extend.novell.add_members_to_groups(self.delete_at_teardown[0][0],42 [self.delete_at_teardown[1][0],43 self.delete_at_teardown[2][0]],44 fix=True,45 transaction=False)46 # verifies user in group-147 status, result, response, request = get_response_values(self.connection.search(self.delete_at_teardown[0][0], '(objectclass=*)', attributes=['securityEquals', 'groupMembership']), self.connection)48 if response:49 self.assertTrue(self.delete_at_teardown[1][0] in (response[0]['attributes']['securityEquals'] if 'securityEquals' in response[0]['attributes'] else []))50 self.assertTrue(self.delete_at_teardown[1][0] in (response[0]['attributes']['groupMembership'] if 'groupMembership' in response[0]['attributes'] else []))51 else:52 self.assertFalse(True, self.delete_at_teardown[1][0] + ' not found')53 status, result, response, request = get_response_values(self.connection.search(self.delete_at_teardown[1][0], '(objectclass=*)', attributes=['member', 'equivalentToMe']), self.connection)54 if response:55 self.assertTrue(self.delete_at_teardown[0][0] in (response[0]['attributes']['member'] if 'member' in response[0]['attributes'] else []))56 self.assertTrue(self.delete_at_teardown[0][0] in (response[0]['attributes']['equivalentToMe'] if 'equivalentToMe' in response[0]['attributes'] else []))57 else:58 self.assertFalse(True, self.delete_at_teardown[0][0] + ' not found')59 # verifies user in group-1b60 status, result, response, request = get_response_values(self.connection.search(self.delete_at_teardown[0][0], '(objectclass=*)', attributes=['securityEquals', 'groupMembership']), self.connection)61 if response:62 self.assertTrue(self.delete_at_teardown[2][0] in (response[0]['attributes']['securityEquals'] if 'securityEquals' in response[0]['attributes'] else []))63 self.assertTrue(self.delete_at_teardown[2][0] in (response[0]['attributes']['groupMembership'] if 'groupMembership' in response[0]['attributes'] else []))64 else:65 self.assertFalse(True, self.delete_at_teardown[2][0] + ' not found')66 status, result, response, request = get_response_values(self.connection.search(self.delete_at_teardown[2][0], '(objectclass=*)', attributes=['member', 'equivalentToMe']), self.connection)67 if response:68 self.assertTrue(self.delete_at_teardown[0][0] in (response[0]['attributes']['member'] if 'member' in response[0]['attributes'] else []))69 self.assertTrue(self.delete_at_teardown[0][0] in (response[0]['attributes']['equivalentToMe'] if 'equivalentToMe' in response[0]['attributes'] else []))70 else:71 self.assertFalse(True, self.delete_at_teardown[0][0] + ' not found')72 self.connection.extend.novell.remove_members_from_groups(self.delete_at_teardown[0][0],73 self.delete_at_teardown[1][0],74 fix=False,75 transaction=False)76 # verifies users not in group-177 status, result, response, request = get_response_values(self.connection.search(self.delete_at_teardown[0][0], '(objectclass=*)', attributes=['securityEquals', 'groupMembership']), self.connection)78 if response:79 self.assertTrue(self.delete_at_teardown[1][0] not in (response[0]['attributes']['securityEquals'] if 'securityEquals' in response[0]['attributes'] else []))80 self.assertTrue(self.delete_at_teardown[1][0] not in (response[0]['attributes']['groupMembership'] if 'groupMembership' in response[0]['attributes'] else []))81 else:82 self.assertFalse(True, self.delete_at_teardown[1][0] + ' not found')83 status, result, response, request = get_response_values(self.connection.search(self.delete_at_teardown[1][0], '(objectclass=*)', attributes=['member', 'equivalentToMe']), self.connection)84 if response:85 self.assertTrue(self.delete_at_teardown[0][0] not in (response[0]['attributes']['member'] if 'member' in response[0]['attributes'] else []))86 self.assertTrue(self.delete_at_teardown[0][0] not in (response[0]['attributes']['equivalentToMe'] if 'equivalentToMe' in response[0]['attributes'] else []))87 else:88 self.assertFalse(True, self.delete_at_teardown[0][0] + ' not found')89 # verifies user still in group-1b90 status, result, response, request = get_response_values(self.connection.search(self.delete_at_teardown[0][0], '(objectclass=*)', attributes=['securityEquals', 'groupMembership']), self.connection)91 if response:92 self.assertTrue(self.delete_at_teardown[2][0] in (response[0]['attributes']['securityEquals'] if 'securityEquals' in response[0]['attributes'] else []))93 self.assertTrue(self.delete_at_teardown[2][0] in (response[0]['attributes']['groupMembership'] if 'groupMembership' in response[0]['attributes'] else []))94 else:95 self.assertFalse(True, self.delete_at_teardown[2][0] + ' not found')96 status, result, response, request = get_response_values(self.connection.search(self.delete_at_teardown[2][0], '(objectclass=*)', attributes=['member', 'equivalentToMe']), self.connection)97 if response:98 self.assertTrue(self.delete_at_teardown[0][0] in (response[0]['attributes']['member'] if 'member' in response[0]['attributes'] else []))99 self.assertTrue(self.delete_at_teardown[0][0] in (response[0]['attributes']['equivalentToMe'] if 'equivalentToMe' in response[0]['attributes'] else []))100 else:101 self.assertFalse(True, self.delete_at_teardown[0][0] + ' not found')102 def test_remove_members_from_groups(self):103 if test_server_type == 'EDIR' and not self.connection.strategy.pooled and not self.connection.strategy.no_real_dsa:104 self.delete_at_teardown.append(add_user(self.connection, testcase_id, 'user-2'))105 self.delete_at_teardown.append(add_user(self.connection, testcase_id, 'user-3'))106 self.delete_at_teardown.append(add_user(self.connection, testcase_id, 'user-4'))107 self.delete_at_teardown.append(add_group(self.connection, testcase_id, 'group-2', self.delete_at_teardown))108 self.delete_at_teardown.append(add_group(self.connection, testcase_id, 'group-3'))109 self.delete_at_teardown.append(add_group(self.connection, testcase_id, 'group-4'))110 self.connection.extend.novell.add_members_to_groups([self.delete_at_teardown[0][0],111 self.delete_at_teardown[1][0],112 self.delete_at_teardown[2][0]],113 [self.delete_at_teardown[3][0],114 self.delete_at_teardown[4][0],115 self.delete_at_teardown[5][0]],116 fix=True,117 transaction=False118 )119 for i in range(0, 2):120 status, result, response, request = get_response_values(self.connection.search(self.delete_at_teardown[i][0], '(objectclass=*)', attributes=['securityEquals', 'groupMembership']), self.connection)121 if response:122 for j in range(3, 5):123 self.assertTrue(self.delete_at_teardown[j][0] in (response[0]['attributes']['securityEquals'] if 'securityEquals' in response[0]['attributes'] else []))124 self.assertTrue(self.delete_at_teardown[j][0] in (response[0]['attributes']['groupMembership'] if 'groupMembership' in response[0]['attributes'] else []))125 else:126 self.assertFalse(True, self.delete_at_teardown[i][0] + ' not found')127 for j in range(3, 5):128 status, result, response, request = get_response_values(self.connection.search(self.delete_at_teardown[j][0], '(objectclass=*)', attributes=['member', 'equivalentToMe']), self.connection)129 if response:130 for i in range(0, 2):131 self.assertTrue(self.delete_at_teardown[i][0] in (response[0]['attributes']['member'] if 'member' in response[0]['attributes'] else []))132 self.assertTrue(self.delete_at_teardown[i][0] in (response[0]['attributes']['equivalentToMe'] if 'equivalentToMe' in response[0]['attributes'] else []))133 else:134 self.assertFalse(True, self.delete_at_teardown[j][0] + ' not found')135 self.connection.extend.novell.remove_members_from_groups([self.delete_at_teardown[0][0],136 self.delete_at_teardown[1][0],137 self.delete_at_teardown[2][0]],138 [self.delete_at_teardown[3][0],139 self.delete_at_teardown[4][0],140 self.delete_at_teardown[5][0]],141 fix=True,142 transaction=False143 )144 for i in range(0, 2):145 status, result, response, request = get_response_values(self.connection.search(self.delete_at_teardown[i][0], '(objectclass=*)', attributes=['securityEquals', 'groupMembership']), self.connection)146 if response:147 for j in range(3, 5):148 self.assertTrue(self.delete_at_teardown[j][0] not in (response[0]['attributes']['securityEquals'] if 'securityEquals' in response[0]['attributes'] else []))149 self.assertTrue(self.delete_at_teardown[j][0] not in (response[0]['attributes']['groupMembership'] if 'groupMembership' in response[0]['attributes'] else []))150 else:151 self.assertFalse(True, self.delete_at_teardown[i][0] + ' not found')152 for j in range(3, 5):153 status, result, response, request = get_response_values(self.connection.search(self.delete_at_teardown[j][0], '(objectclass=*)', attributes=['member', 'equivalentToMe']), self.connection)154 if response:155 for i in range(0, 2):156 self.assertTrue(self.delete_at_teardown[i][0] not in (response[0]['attributes']['member'] if 'member' in response[0]['attributes'] else []))157 self.assertTrue(self.delete_at_teardown[i][0] not in (response[0]['attributes']['equivalentToMe'] if 'equivalentToMe' in response[0]['attributes'] else []))158 else:159 self.assertFalse(True, self.delete_at_teardown[j][0] + ' not found')160 def test_remove_member_from_group_transactional(self):161 if test_server_type == 'EDIR' and not self.connection.strategy.pooled and not self.connection.strategy.no_real_dsa:162 self.delete_at_teardown.append(add_user(self.connection, testcase_id, 'user-5'))163 self.delete_at_teardown.append(add_group(self.connection, testcase_id, 'group-5', self.delete_at_teardown))164 self.connection.extend.novell.add_members_to_groups(self.delete_at_teardown[0][0],165 self.delete_at_teardown[1][0],166 fix=True,167 transaction=True)168 status, result, response, request = get_response_values(self.connection.search(self.delete_at_teardown[0][0], '(objectclass=*)', attributes=['securityEquals', 'groupMembership']), self.connection)169 if response:170 self.assertTrue(self.delete_at_teardown[1][0] in (response[0]['attributes']['securityEquals'] if 'securityEquals' in response[0]['attributes'] else []))171 self.assertTrue(self.delete_at_teardown[1][0] in (response[0]['attributes']['groupMembership'] if 'groupMembership' in response[0]['attributes'] else []))172 else:173 self.assertFalse(True, self.delete_at_teardown[1][0] + ' not found')174 status, result, response, request = get_response_values(self.connection.search(self.delete_at_teardown[1][0], '(objectclass=*)', attributes=['member', 'equivalentToMe']), self.connection)175 if response:176 self.assertTrue(self.delete_at_teardown[0][0] in (response[0]['attributes']['member'] if 'member' in response[0]['attributes'] else []))177 self.assertTrue(self.delete_at_teardown[0][0] in (response[0]['attributes']['equivalentToMe'] if 'equivalentToMe' in response[0]['attributes'] else []))178 else:179 self.assertFalse(True, self.delete_at_teardown[0][0] + ' not found')180 self.connection.extend.novell.remove_members_from_groups(self.delete_at_teardown[0][0],181 self.delete_at_teardown[1][0],182 fix=False,183 transaction=False)184 status, result, response, request = get_response_values(self.connection.search(self.delete_at_teardown[0][0], '(objectclass=*)', attributes=['securityEquals', 'groupMembership']), self.connection)185 if response:186 self.assertTrue(self.delete_at_teardown[1][0] not in (response[0]['attributes']['securityEquals'] if 'securityEquals' in response[0]['attributes'] else []))187 self.assertTrue(self.delete_at_teardown[1][0] not in (response[0]['attributes']['groupMembership'] if 'groupMembership' in response[0]['attributes'] else []))188 else:189 self.assertFalse(True, self.delete_at_teardown[1][0] + ' not found')190 status, result, response, request = get_response_values(self.connection.search(self.delete_at_teardown[1][0], '(objectclass=*)', attributes=['member', 'equivalentToMe']), self.connection)191 if response:192 self.assertTrue(self.delete_at_teardown[0][0] not in (response[0]['attributes']['member'] if 'member' in response[0]['attributes'] else []))193 self.assertTrue(self.delete_at_teardown[0][0] not in (response[0]['attributes']['equivalentToMe'] if 'equivalentToMe' in response[0]['attributes'] else []))194 else:195 self.assertFalse(True, self.delete_at_teardown[0][0] + ' not found')196 def test_remove_members_from_groups_transactional(self):197 if test_server_type == 'EDIR' and not self.connection.strategy.pooled and not self.connection.strategy.no_real_dsa:198 self.delete_at_teardown.append(add_user(self.connection, testcase_id, 'user-6'))199 self.delete_at_teardown.append(add_user(self.connection, testcase_id, 'user-7'))200 self.delete_at_teardown.append(add_user(self.connection, testcase_id, 'user-8'))201 self.delete_at_teardown.append(add_group(self.connection, testcase_id, 'group-6', self.delete_at_teardown)) # this group has members but other attributes are not set202 self.delete_at_teardown.append(add_group(self.connection, testcase_id, 'group-7'))203 self.delete_at_teardown.append(add_group(self.connection, testcase_id, 'group-8'))204 self.connection.extend.novell.add_members_to_groups([self.delete_at_teardown[0][0],205 self.delete_at_teardown[1][0],206 self.delete_at_teardown[2][0]],207 [self.delete_at_teardown[3][0],208 self.delete_at_teardown[4][0],209 self.delete_at_teardown[5][0]],210 fix=True,211 transaction=True212 )213 for i in range(0, 2):214 status, result, response, request = get_response_values(self.connection.search(self.delete_at_teardown[i][0], '(objectclass=*)', attributes=['securityEquals', 'groupMembership']), self.connection)215 if response:216 for j in range(3, 5):217 self.assertTrue(self.delete_at_teardown[j][0] in (response[0]['attributes']['securityEquals'] if 'securityEquals' in response[0]['attributes'] else []))218 self.assertTrue(self.delete_at_teardown[j][0] in (response[0]['attributes']['groupMembership'] if 'groupMembership' in response[0]['attributes'] else []))219 else:220 self.assertFalse(True, self.delete_at_teardown[i][0] + ' not found')221 for j in range(3, 5):222 status, result, response, request = get_response_values(self.connection.search(self.delete_at_teardown[j][0], '(objectclass=*)', attributes=['member', 'equivalentToMe']), self.connection)223 if response:224 for i in range(0, 2):225 self.assertTrue(self.delete_at_teardown[i][0] in (response[0]['attributes']['member'] if 'member' in response[0]['attributes'] else []))226 self.assertTrue(self.delete_at_teardown[i][0] in (response[0]['attributes']['equivalentToMe'] if 'equivalentToMe' in response[0]['attributes'] else []))227 else:228 self.assertFalse(True, self.delete_at_teardown[j][0] + ' not found')229 self.connection.extend.novell.remove_members_from_groups([self.delete_at_teardown[0][0],230 self.delete_at_teardown[1][0],231 self.delete_at_teardown[2][0]],232 [self.delete_at_teardown[3][0],233 self.delete_at_teardown[4][0],234 self.delete_at_teardown[5][0]],235 fix=True,236 transaction=False237 )238 for i in range(0, 2):239 status, result, response, request = get_response_values(self.connection.search(self.delete_at_teardown[i][0], '(objectclass=*)', attributes=['securityEquals', 'groupMembership']), self.connection)240 if response:241 for j in range(3, 5):242 self.assertTrue(self.delete_at_teardown[j][0] not in (response[0]['attributes']['securityEquals'] if 'securityEquals' in response[0]['attributes'] else []))243 self.assertTrue(self.delete_at_teardown[j][0] not in (response[0]['attributes']['groupMembership'] if 'groupMembership' in response[0]['attributes'] else []))244 else:245 self.assertFalse(True, self.delete_at_teardown[i][0] + ' not found')246 for j in range(3, 5):247 status, result, response, request = get_response_values(self.connection.search(self.delete_at_teardown[j][0], '(objectclass=*)', attributes=['member', 'equivalentToMe']), self.connection)248 if response:249 for i in range(0, 2):250 self.assertTrue(self.delete_at_teardown[i][0] not in (response[0]['attributes']['member'] if 'member' in response[0]['attributes'] else []))251 self.assertTrue(self.delete_at_teardown[i][0] not in (response[0]['attributes']['equivalentToMe'] if 'equivalentToMe' in response[0]['attributes'] else []))252 else:...
test_suite.py
Source:test_suite.py
...99 del self.state100 def test_setup_none(self):101 self.assertTrue(self.state.setup(None))102 def test_teardown_no_prior_setup_does_not_raise(self):103 self.state.teardown()104 @contextmanager105 def _temporary_module(self, klass, module):106 module_name = 'haas_test_module_{0}'.format(next(self._name_count))107 self.assertNotIn(module_name, sys.modules)108 sys.modules[module_name] = module109 old_module = klass.__module__110 klass.__module__ = module_name111 try:112 yield113 finally:114 klass.__module__ = old_module115 del sys.modules[module_name]116 def _prepare_test(self, klass, module_factory, setup_raise,117 teardown_raise):118 klass.setup_raise = setup_raise119 klass.teardown_raise = teardown_raise120 test = klass()121 if module_factory is not None:122 module = module_factory(123 setup_raise=setup_raise,124 teardown_raise=teardown_raise,125 )126 else:127 module = None128 return test, module129 @contextmanager130 def _run_test_context(self, klass, module_factory, setup_raise,131 teardown_raise, class_setup, class_teardown,132 module_setup, module_teardown):133 test, module = self._prepare_test(134 klass, module_factory, setup_raise, teardown_raise)135 with self._temporary_module(klass, module):136 self.assertEqual(self.state.setup(test), not setup_raise)137 self.assertEqual(klass.setup, class_setup)138 self.assertFalse(klass.teardown)139 if module is not None:140 self.assertEqual(module.setup, module_setup)141 self.assertFalse(module.teardown)142 yield test143 self.state.teardown()144 self.assertEqual(klass.setup, class_setup)145 self.assertEqual(klass.teardown, class_teardown)146 if module is not None:147 self.assertEqual(module.setup, module_setup)148 self.assertEqual(module.teardown, module_teardown)149 def _run_test(self, klass, module_factory, setup_raise, teardown_raise,150 class_setup, class_teardown, module_setup, module_teardown):151 with self._run_test_context(152 klass, module_factory, setup_raise, teardown_raise,153 class_setup, class_teardown, module_setup, module_teardown):154 pass155 def test_call_setup_without_setup_or_teardown(self):156 self._run_test(157 klass=MockTestCase,158 module_factory=MockModule,159 setup_raise=False,160 teardown_raise=False,161 class_setup=False,162 class_teardown=False,163 module_setup=False,164 module_teardown=False,165 )166 def test_setup_skip(self):167 MockTestCaseSetupTeardown.__unittest_skip__ = True168 self._run_test(169 klass=MockTestCaseSetupTeardown,170 module_factory=MockModule,171 setup_raise=False,172 teardown_raise=False,173 class_setup=False,174 class_teardown=False,175 module_setup=False,176 module_teardown=False,177 )178 def test_setup_case_setup(self):179 self._run_test(180 klass=MockTestCaseSetup,181 module_factory=MockModule,182 setup_raise=False,183 teardown_raise=False,184 class_setup=True,185 class_teardown=False,186 module_setup=False,187 module_teardown=False,188 )189 def test_setup_case_teardown(self):190 self._run_test(191 klass=MockTestCaseTeardown,192 module_factory=MockModule,193 setup_raise=False,194 teardown_raise=False,195 class_setup=False,196 class_teardown=True,197 module_setup=False,198 module_teardown=False,199 )200 def test_setup_case_setup_and_teardown(self):201 self._run_test(202 klass=MockTestCaseSetupTeardown,203 module_factory=MockModule,204 setup_raise=False,205 teardown_raise=False,206 class_setup=True,207 class_teardown=True,208 module_setup=False,209 module_teardown=False,210 )211 def test_setup_case_setup_raises_and_teardown(self):212 self._run_test(213 klass=MockTestCaseSetupTeardown,214 module_factory=MockModule,215 setup_raise=True,216 teardown_raise=False,217 class_setup=True,218 class_teardown=False,219 module_setup=False,220 module_teardown=False,221 )222 def test_setup_case_setup_and_teardown_raises(self):223 self._run_test(224 klass=MockTestCaseSetupTeardown,225 module_factory=MockModule,226 setup_raise=False,227 teardown_raise=True,228 class_setup=True,229 class_teardown=True,230 module_setup=False,231 module_teardown=False,232 )233 def test_setup_module_setup(self):234 self._run_test(235 klass=MockTestCase,236 module_factory=MockModuleSetup,237 setup_raise=False,238 teardown_raise=False,239 class_setup=False,240 class_teardown=False,241 module_setup=True,242 module_teardown=False,243 )244 def test_setup_module_teardown(self):245 self._run_test(246 klass=MockTestCase,247 module_factory=MockModuleTeardown,248 setup_raise=False,249 teardown_raise=False,250 class_setup=False,251 class_teardown=False,252 module_setup=False,253 module_teardown=True,254 )255 def test_setup_module_setup_and_teardown(self):256 self._run_test(257 klass=MockTestCase,258 module_factory=MockModuleSetupTeardown,259 setup_raise=False,260 teardown_raise=False,261 class_setup=False,262 class_teardown=False,263 module_setup=True,264 module_teardown=True,265 )266 def test_setup_module_setup_raises_and_teardown(self):267 self._run_test(268 klass=MockTestCase,269 module_factory=MockModuleSetupTeardown,270 setup_raise=True,271 teardown_raise=False,272 class_setup=False,273 class_teardown=False,274 module_setup=True,275 module_teardown=False,276 )277 def test_setup_module_setup_and_teardown_raises(self):278 self._run_test(279 klass=MockTestCase,280 module_factory=MockModuleSetupTeardown,...
test_file.py
Source:test_file.py
1# This file is part of Exdir, the Experimental Directory Structure.2#3# Copyright 2017 Simen Tennøe4#5# License: MIT, see "LICENSE" file for the full license terms.6#7# This file contains code from h5py, a Python interface to the HDF5 library,8# licensed under a standard 3-clause BSD license9# with copyright Andrew Collette and contributors.10# See http://www.h5py.org and the "3rdparty/h5py-LICENSE" file for details.11import pytest12import os13import pathlib14from exdir.core import File, Group15from exdir.core.exdir_object import _create_object_directory, is_nonraw_object_directory, DATASET_TYPENAME, FILE_TYPENAME16import exdir.core.filename_validation as fv17import numpy as np18from conftest import remove19def test_file_init(setup_teardown_folder):20 no_exdir = setup_teardown_folder[0] / "no_exdir"21 f = File(no_exdir, mode="w")22 f.close()23 assert is_nonraw_object_directory(no_exdir.with_suffix(".exdir"))24 remove(setup_teardown_folder[1])25 f = File(setup_teardown_folder[1], mode="w")26 f.close()27 assert is_nonraw_object_directory(setup_teardown_folder[1])28 remove(setup_teardown_folder[1])29 f = File(setup_teardown_folder[1], mode="a")30 f.close()31 assert is_nonraw_object_directory(setup_teardown_folder[1])32 remove(setup_teardown_folder[1])33 f = File(setup_teardown_folder[1], mode="a")34 f.close()35 assert is_nonraw_object_directory(setup_teardown_folder[1])36 remove(setup_teardown_folder[1])37 setup_teardown_folder[1].mkdir(parents=True)38 with pytest.raises(FileExistsError):39 f = File(setup_teardown_folder[1], mode="w")40 remove(setup_teardown_folder[1])41 _create_object_directory(pathlib.Path(setup_teardown_folder[1]), DATASET_TYPENAME)42 with pytest.raises(FileExistsError):43 f = File(setup_teardown_folder[1], mode="w")44 remove(setup_teardown_folder[1])45 with pytest.raises(IOError):46 f = File(setup_teardown_folder[1], mode="r")47 with pytest.raises(IOError):48 f = File(setup_teardown_folder[1], mode="r+")49 _create_object_directory(pathlib.Path(setup_teardown_folder[1]), FILE_TYPENAME)50 with pytest.raises(FileExistsError):51 f = File(setup_teardown_folder[1], mode="w")52 remove(setup_teardown_folder[1])53 _create_object_directory(pathlib.Path(setup_teardown_folder[1]), FILE_TYPENAME)54 f = File(setup_teardown_folder[1], mode="w", allow_remove=True)55 remove(setup_teardown_folder[1])56 _create_object_directory(pathlib.Path(setup_teardown_folder[1]), FILE_TYPENAME)57 with pytest.raises(IOError):58 f = File(setup_teardown_folder[1], mode="w-")59 with pytest.raises(IOError):60 f = File(setup_teardown_folder[1], mode="x")61 with pytest.raises(ValueError):62 f = File(setup_teardown_folder[1], mode="not existing")63def test_create(setup_teardown_folder):64 """Mode 'w' opens file in overwrite mode."""65 f = File(setup_teardown_folder[1], 'w')66 assert f67 f.create_group('foo')68 f.close()69 f = File(setup_teardown_folder[1], 'w', allow_remove=True)70 assert 'foo' not in f71 f.close()72 with pytest.raises(FileExistsError):73 f = File(setup_teardown_folder[1], 'w')74def test_create_exclusive(setup_teardown_folder):75 """Mode 'w-' opens file in exclusive mode."""76 f = File(setup_teardown_folder[1], 'w-')77 assert f78 f.close()79 with pytest.raises(IOError):80 File(setup_teardown_folder[1], 'w-')81def test_append(setup_teardown_folder):82 """Mode 'a' opens file in append/readwrite mode, creating if necessary."""83 f = File(setup_teardown_folder[1], 'a')84 assert f85 f.create_group('foo')86 assert 'foo' in f87 f = File(setup_teardown_folder[1], 'a')88 assert 'foo' in f89 f.create_group('bar')90 assert 'bar' in f91def test_readonly(setup_teardown_folder):92 """Mode 'r' opens file in readonly mode."""93 f = File(setup_teardown_folder[1], 'w')94 f.close()95 # TODO comment in when close is implemented96 # assert not f97 f = File(setup_teardown_folder[1], 'r')98 assert f99 with pytest.raises(IOError):100 f.create_group('foo')101 f.create_dataset("bar", (2))102 f.close()103def test_readwrite(setup_teardown_folder):104 """Mode 'r+' opens existing file in readwrite mode."""105 f = File(setup_teardown_folder[1], 'w')106 f.create_group('foo')107 f.close()108 f = File(setup_teardown_folder[1], 'r+')109 assert 'foo' in f110 f.create_group('bar')111 assert 'bar' in f112 f.close()113def test_nonexistent_file(setup_teardown_folder):114 """Modes 'r' and 'r+' do not create files."""115 with pytest.raises(IOError):116 File(setup_teardown_folder[1], 'r')117 with pytest.raises(IOError):118 File(setup_teardown_folder[1], 'r+')119def test_invalid_mode(setup_teardown_folder):120 """Invalid modes raise ValueError."""121 with pytest.raises(ValueError):122 File(setup_teardown_folder[1], 'Error mode')123def test_file_close(setup_teardown_folder):124 """Closing a file."""125 f = File(setup_teardown_folder[1], mode="w")126 f.close()127def test_validate_name_thorough(setup_teardown_folder):128 """Test naming rule thorough."""129 f = File(setup_teardown_folder[0] / "test.exdir", validate_name=fv.thorough)130 f.close()131 with pytest.raises(FileExistsError):132 File(setup_teardown_folder[0] / "Test.exdir", validate_name=fv.thorough)133 with pytest.raises(NameError):134 File(setup_teardown_folder[0] / "tes#.exdir", validate_name=fv.thorough)135def test_validate_name_strict(setup_teardown_folder):136 """Test naming rule strict."""137 f = File(setup_teardown_folder[1], validate_name=fv.strict)138 f.close()139 with pytest.raises(NameError):140 File(setup_teardown_folder[1].with_suffix(".exdirA"), validate_name=fv.strict)141def test_validate_name_error(setup_teardown_folder):142 """Test naming rule with error."""143 with pytest.raises(ValueError):144 File(setup_teardown_folder[1], validate_name='Error rule')145def test_validate_name_none(setup_teardown_folder):146 """Test naming rule with error."""147 File(setup_teardown_folder[1].with_name("test&().exdir"), validate_name=fv.none)148def test_opening_with_different_validate_name(setup_teardown_folder):149 """Test opening with wrong naming rule."""150 f = File(setup_teardown_folder[1], "w", validate_name=fv.none)151 f.create_group("AAA")152 f.close()153 # TODO changing name validation should result in warning/error154 f = File(setup_teardown_folder[1], "a", validate_name=fv.thorough)155 with pytest.raises(FileExistsError):156 f.create_group("aaa")157 f.close()158def test_contains(setup_teardown_file):159 """Root group (by itself) is contained."""160 f = setup_teardown_file[3]161 f.create_group("test")162 assert "/" in f163 assert "/test" in f164def test_create_group(setup_teardown_file):165 """Root group (by itself) is contained."""166 f = setup_teardown_file[3]167 grp = f.create_group("/test")168 assert isinstance(grp, Group)169def test_require_group(setup_teardown_file):170 """Root group (by itself) is contained."""171 f = setup_teardown_file[3]172 grp = f.require_group("/foo")173 assert isinstance(grp, Group)174def test_open(setup_teardown_file):175 """thorough obj[name] opening."""176 f = setup_teardown_file[3]177 grp = f.create_group("foo")178 grp2 = f["foo"]179 grp3 = f["/foo"]180 f = f["/"]181 assert grp == grp2182 assert grp2 == grp3183 assert f == f184def test_open_mode(setup_teardown_folder):185 # must exist186 for mode in ["r+", "r"]:187 with pytest.raises(IOError):188 f = File(setup_teardown_folder[1], mode)189 # create if not exist190 for mode in ["a", "w", "w-"]:191 remove(setup_teardown_folder[1])192 f = File(setup_teardown_folder[1], mode)193 f.require_dataset('dset', np.arange(10))194 f.attrs['can_overwrite'] = 42195 f.attrs['can_overwrite'] = 14196 f.require_group('mygroup')197 remove(setup_teardown_folder[1])198 f = File(setup_teardown_folder[1], 'w')199 f.close() # dummy close200 # read write if exist201 f = File(setup_teardown_folder[1], "r+")202 f.require_group('mygroup')203 f.require_dataset('dset', np.arange(10))204 f.attrs['can_overwrite'] = 42205 f.attrs['can_overwrite'] = 14206 # read only, can not write207 f = File(setup_teardown_folder[1], 'r')208 with pytest.raises(IOError):209 f.require_dataset('dset', np.arange(10))210 f.attrs['can_not_write'] = 42211 f.create_group('mygroup')212def test_open_two_attrs(setup_teardown_file):213 f = setup_teardown_file[3]214 f.attrs['can_overwrite'] = 42215 f.attrs['another_atribute'] = 14216# TODO uncomment when enter and exit has been implemented217# # Feature: File objects can be used as context managers218# def test_context_manager(setup_teardown_folder):219# """File objects can be used in with statements."""220# no_exdir = pytest.TESTPATH / "no_exdir"221# with File(no_exdir, mode="w") as f:222# assert f...
test_statuses.py
Source:test_statuses.py
1import os2import sys3from avocado import Test, skip4class SkipSetup(Test):5 @skip("from setUp()")6 def setUp(self):7 self.log.info('setup pre')8 self.log.info('setup post')9 def test(self):10 self.log.info('test pre')11 self.log.info('test post')12 def tearDown(self):13 self.log.info('teardown pre')14 self.log.info('teardown status: %s', self.status)15 self.log.info('teardown post')16class SkipTest(Test):17 def setUp(self):18 self.log.info('setup pre')19 self.log.info('setup post')20 @skip("from test()")21 def test(self):22 self.log.info('test pre')23 self.log.info('test post')24 def tearDown(self):25 self.log.info('teardown pre')26 self.log.info('teardown status: %s', self.status)27 self.log.info('teardown post')28class SkipTeardown(Test):29 def setUp(self):30 self.log.info('setup pre')31 self.log.info('setup post')32 def test(self):33 self.log.info('test pre')34 self.log.info('test post')35 @skip("from tearDown()")36 def tearDown(self):37 self.log.info('teardown pre')38 self.log.info('teardown status: %s', self.status)39 self.log.info('teardown post')40class CancelSetup(Test):41 def setUp(self):42 self.log.info('setup pre')43 self.cancel()44 self.log.info('setup post')45 def test(self):46 self.log.info('test pre')47 self.log.info('test post')48 def tearDown(self):49 self.log.info('teardown pre')50 self.log.info('teardown status: %s', self.status)51 self.log.info('teardown post')52class CancelTest(Test):53 def setUp(self):54 self.log.info('setup pre')55 self.log.info('setup post')56 def test(self):57 self.log.info('test pre')58 self.cancel()59 self.log.info('test post')60 def tearDown(self):61 self.log.info('teardown pre')62 self.log.info('teardown status: %s', self.status)63 self.log.info('teardown post')64class CancelTeardown(Test):65 def setUp(self):66 self.log.info('setup pre')67 self.log.info('setup post')68 def test(self):69 self.log.info('test pre')70 self.log.info('test post')71 def tearDown(self):72 self.log.info('teardown pre')73 self.log.info('teardown status: %s', self.status)74 self.cancel()75 self.log.info('teardown post')76class FailSetup(Test):77 def setUp(self):78 self.log.info('setup pre')79 self.fail()80 self.log.info('setup post')81 def test(self):82 self.log.info('test pre')83 self.log.info('test post')84 def tearDown(self):85 self.log.info('teardown pre')86 self.log.info('teardown status: %s', self.status)87 self.log.info('teardown post')88class FailTest(Test):89 def setUp(self):90 self.log.info('setup pre')91 self.log.info('setup post')92 def test(self):93 self.log.info('test pre')94 self.fail()95 self.log.info('test post')96 def tearDown(self):97 self.log.info('teardown pre')98 self.log.info('teardown status: %s', self.status)99 self.log.info('teardown post')100class FailTeardown(Test):101 def setUp(self):102 self.log.info('setup pre')103 self.log.info('setup post')104 def test(self):105 self.log.info('test pre')106 self.log.info('test post')107 def tearDown(self):108 self.log.info('teardown pre')109 self.log.info('teardown status: %s', self.status)110 self.fail()111 self.log.info('teardown post')112class WarnSetup(Test):113 def setUp(self):114 self.log.info('setup pre')115 self.log.warn('')116 self.log.info('setup post')117 def test(self):118 self.log.info('test pre')119 self.log.info('test post')120 def tearDown(self):121 self.log.info('teardown pre')122 self.log.info('teardown status: %s', self.status)123 self.log.info('teardown post')124class WarnTest(Test):125 def setUp(self):126 self.log.info('setup pre')127 self.log.info('setup post')128 def test(self):129 self.log.info('test pre')130 self.log.warn('')131 self.log.info('test post')132 def tearDown(self):133 self.log.info('teardown pre')134 self.log.info('teardown status: %s', self.status)135 self.log.info('teardown post')136class WarnTeardown(Test):137 def setUp(self):138 self.log.info('setup pre')139 self.log.info('setup post')140 def test(self):141 self.log.info('test pre')142 self.log.info('test post')143 def tearDown(self):144 self.log.info('teardown pre')145 self.log.info('teardown status: %s', self.status)146 self.log.warn('')147 self.log.info('teardown post')148class ExitSetup(Test):149 def setUp(self):150 self.log.info('setup pre')151 sys.exit(-1)152 self.log.info('setup post')153 def test(self):154 self.log.info('test pre')155 self.log.info('test post')156 def tearDown(self):157 self.log.info('teardown pre')158 self.log.info('teardown status: %s', self.status)159 self.log.info('teardown post')160class ExitTest(Test):161 def setUp(self):162 self.log.info('setup pre')163 self.log.info('setup post')164 def test(self):165 self.log.info('test pre')166 sys.exit(-1)167 self.log.info('test post')168 def tearDown(self):169 self.log.info('teardown pre')170 self.log.info('teardown status: %s', self.status)171 self.log.info('teardown post')172class ExitTeardown(Test):173 def setUp(self):174 self.log.info('setup pre')175 self.log.info('setup post')176 def test(self):177 self.log.info('test pre')178 self.log.info('test post')179 def tearDown(self):180 self.log.info('teardown pre')181 self.log.info('teardown status: %s', self.status)182 sys.exit(-1)183 self.log.info('teardown post')184class ExceptionSetup(Test):185 def setUp(self):186 self.log.info('setup pre')187 raise ValueError188 # pylint: disable=W0101189 self.log.info('setup post')190 def test(self):191 self.log.info('test pre')192 self.log.info('test post')193 def tearDown(self):194 self.log.info('teardown pre')195 self.log.info('teardown status: %s', self.status)196 self.log.info('teardown post')197class ExceptionTest(Test):198 def setUp(self):199 self.log.info('setup pre')200 self.log.info('setup post')201 def test(self):202 self.log.info('test pre')203 raise ValueError204 # pylint: disable=W0101205 self.log.info('test post')206 def tearDown(self):207 self.log.info('teardown pre')208 self.log.info('teardown status: %s', self.status)209 self.log.info('teardown post')210class ExceptionTeardown(Test):211 def setUp(self):212 self.log.info('setup pre')213 self.log.info('setup post')214 def test(self):215 self.log.info('test pre')216 self.log.info('test post')217 def tearDown(self):218 self.log.info('teardown pre')219 self.log.info('teardown status: %s', self.status)220 raise ValueError221 # pylint: disable=W0101222 self.log.info('teardown post')223class KillTest(Test):224 def setUp(self):225 self.log.info('setup pre')226 self.log.info('setup post')227 def test(self):228 self.log.info('test pre')229 os.kill(os.getpid(), 9)230 self.log.info('test post')231 def tearDown(self):232 self.log.info('teardown pre')233 self.log.info('teardown status: %s', self.status)...
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!!