Best Python code snippet using localstack_python
test_attributes.py
Source:test_attributes.py
1import copy2import datetime3import json4import numbers5import os6import pickle7import tempfile8import types9import unittest10from lib.attributes import Attributes11from lib.database import Database12class AttributesTestCase(unittest.TestCase):13 def setUp(self):14 parentpath = (15 os.path.abspath(16 os.path.join(17 os.path.dirname(os.path.realpath(__file__)),18 os.pardir19 )20 )21 )22 manifestpath = os.path.join(parentpath, 'manifest.json')23 self.rawattributes = None24 with open(manifestpath, 'r') as file_:25 self.rawattributes = json.load(file_)['attributes']26 configpath = os.path.join(parentpath, 'config.json')27 self.rawsettings = None28 with open(configpath, 'r') as file_:29 contents = json.load(file_)30 self.rawsettings = contents['options']['datasource']31 self.rawgoptions = {32 'today': contents['options']['datasource']33 }34 def test_init(self):35 # Arrange36 expected = len(self.rawattributes)37 # Act38 attributes = Attributes(39 self.rawattributes, database=None, goptions=self.rawgoptions40 )41 # Assert42 self.assertEqual(expected, len(attributes.attributes))43 for attribute in attributes.attributes:44 self.assertNotEqual('', attribute.name)45 self.assertNotEqual('', attribute.initial)46 self.assertIsInstance(attribute.weight, numbers.Number)47 self.assertIsInstance(attribute.enabled, bool)48 self.assertIsInstance(attribute.essential, bool)49 self.assertIsInstance(attribute.persist, bool)50 self.assertIsInstance(attribute.dependencies, list)51 self.assertIsInstance(attribute.options, dict)52 self.assertIsInstance(attribute.reference, types.ModuleType)53 def test_pickling(self):54 # Arrange55 attributes = Attributes(56 self.rawattributes, database=None, goptions=self.rawgoptions57 )58 expected = len(attributes.attributes)59 # Act60 pickled = pickle.dumps(attributes)61 unpickled = pickle.loads(pickled)62 # Assert63 self.assertIsInstance(unpickled, Attributes)64 self.assertEqual(expected, len(unpickled.attributes))65 for attribute in unpickled.attributes:66 self.assertNotEqual('', attribute.name)67 self.assertNotEqual('', attribute.initial)68 self.assertIsInstance(attribute.weight, numbers.Number)69 self.assertIsInstance(attribute.enabled, bool)70 self.assertIsInstance(attribute.essential, bool)71 self.assertIsInstance(attribute.persist, bool)72 self.assertIsInstance(attribute.dependencies, list)73 self.assertIsInstance(attribute.options, dict)74 self.assertIsInstance(attribute.reference, types.ModuleType)75 def test_keystring(self):76 # Arrange77 keystring = 'DuAlIcH'78 # Act79 attributes = Attributes(80 self.rawattributes, database=None, keystring=keystring,81 goptions=self.rawgoptions82 )83 # Assert84 for attribute in attributes.attributes:85 index = str.find(keystring.lower(), attribute.initial)86 if index == -1:87 self.assertFalse(attribute.enabled)88 else:89 self.assertTrue(attribute.enabled)90 if keystring[index].isupper():91 self.assertTrue(attribute.persist)92 else:93 self.assertFalse(attribute.persist)94 def test_is_persitence_enabled(self):95 # Arrange96 keystring = 'dualmichs'97 # Act98 attributes = Attributes(99 self.rawattributes, database=None, keystring=keystring,100 goptions=self.rawgoptions101 )102 # Assert103 self.assertFalse(attributes.is_persistence_enabled)104 # Arrange105 keystring = 'dualMichs'106 # Act107 attributes = Attributes(108 self.rawattributes, database=None, keystring=keystring109 )110 # Assert111 self.assertTrue(attributes.is_persistence_enabled)112 def test_init_repository(self):113 with tempfile.TemporaryDirectory() as directory:114 # Arrange115 project_id = 10868464116 repository_path = os.path.join(directory, str(project_id))117 expected = os.path.join(118 directory, str(project_id), 'squib'119 )120 # Act121 attributes = Attributes(122 self.rawattributes, database=Database(self.rawsettings),123 goptions=self.rawgoptions124 )125 try:126 attributes.database.connect()127 actual = attributes._init_repository(128 project_id, repository_path129 )130 # Assert131 self.assertTrue(len(os.listdir(repository_path)) > 0)132 self.assertTrue(expected in actual)133 finally:134 attributes.database.disconnect()135 def test_cleanup(self):136 with tempfile.TemporaryDirectory() as directory:137 # Arrange138 project_id = 10868464139 repository_home = os.path.join(directory, str(project_id))140 attributes = Attributes(141 self.rawattributes, database=Database(self.rawsettings),142 goptions=self.rawgoptions143 )144 try:145 attributes.database.connect()146 attributes._init_repository(project_id, repository_home)147 # Act148 attributes._cleanup(repository_home)149 # Assert150 self.assertFalse(os.path.exists(repository_home))151 finally:152 attributes.database.disconnect()153 def test_run_timeout(self):154 with tempfile.TemporaryDirectory() as directory:155 # Arrange156 project_id = 10868464157 repository_path = directory158 rawattributes = copy.deepcopy(self.rawattributes)159 for attribute in rawattributes:160 if 'architecture' in attribute['name']:161 attribute['options']['timeout'] = '1S' # Sabotage162 expected = (0, {'architecture': None})163 # Act164 attributes = Attributes(165 rawattributes,166 database=Database(self.rawsettings),167 keystring='a',168 goptions=self.rawgoptions169 )170 try:171 attributes.database.connect()172 actual = attributes.run(project_id, repository_path)173 # Assert174 self.assertEqual(expected, actual)175 finally:176 attributes.database.disconnect()177 def test_score(self):178 # Global Arrange179 attributes = Attributes(180 self.rawattributes, database=None, goptions=self.rawgoptions181 )182 # Arrange183 rresults = {184 'architecture': 9.00,185 'community': 9.00,186 'continuous_integration': True,187 'documentation': 9.00,188 'history': 9.00,189 'license': True,190 'management': 9.00,191 'state': 'active',192 'unit_test': 9.00,193 }194 expected = 100.00195 # Act196 actual = attributes.score(rresults)197 # Assert198 self.assertEqual(expected, actual)199 # Arrange200 rresults = {201 'architecture': 9.00,202 'community': 9.00,203 'continuous_integration': True,204 'documentation': 0,205 'history': 9.00,206 'license': True,207 'management': 9.00,208 'state': 'active',209 'unit_test': 9.00,210 }211 expected = 80.00212 # Act213 actual = attributes.score(rresults)214 # Assert215 self.assertEqual(expected, actual)216 # Arrange217 rresults = {218 'architecture': 9.00,219 'community': 9.00,220 'continuous_integration': True,221 'documentation': 9.00,222 'history': 9.00,223 'license': False,224 'management': 9.00,225 'state': 'active',226 'unit_test': 9.00,227 }228 expected = 0229 # Act230 actual = attributes.score(rresults)231 # Assert232 self.assertEqual(expected, actual)233 def test_requires_source(self):234 # Arrange235 keystring = 'mchs'236 # Act237 attributes = Attributes(238 self.rawattributes, database=None, keystring=keystring,239 goptions=self.rawgoptions240 )241 # Assert242 self.assertFalse(attributes.requires_source)243 # Arrange244 keystring = 'dualmichs'245 # Act246 attributes = Attributes(247 self.rawattributes, database=None, keystring=keystring248 )249 # Assert...
persistence.py
Source:persistence.py
...8from localstack.utils.files import chmod_r9STARTUP_INFO_FILE = "startup_info.json"10# set up logger11LOG = logging.getLogger(__name__)12def is_persistence_enabled():13 return config.PERSISTENCE and config.dirs.data14def is_persistence_restored():15 return not is_persistence_enabled()16class StartupInfo(NamedTuple):17 timestamp: str18 localstack_version: str19 localstack_ext_version: str20 pro_activated: bool21def save_startup_info():22 from localstack_ext import __version__ as localstack_ext_version23 file_path = os.path.join(config.dirs.data, STARTUP_INFO_FILE)24 info = StartupInfo(25 timestamp=datetime.datetime.now().isoformat(),26 localstack_version=constants.VERSION,27 localstack_ext_version=localstack_ext_version,28 pro_activated=is_env_true(constants.ENV_PRO_ACTIVATED),29 )...
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!!