How to use _isidentifier method in autotest

Best Python code snippet using autotest_python

create.py

Source:create.py Github

copy

Full Screen

1import sqlparse2import logging3import datetime4formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')5mode = logging.DEBUG6class SQLCreateTable():7 """ https://docs.microsoft.com/en-us/sql/t-sql/statements/create-table-transact-sql?view=sql-server-ver15 """8 def __init__(self, tokens):9 self.index = 210 self.tokens = tokens11 self.tableName = ''12 self.columns = {}13 self.multiForeignKeys = []14 # Set up logger15 self.logger = logging.getLogger()16 self.logger.setLevel(level=mode)17 fh = logging.FileHandler(f'logs/create-table-{datetime.datetime.now()}.log')18 fh.setLevel(level=mode)19 fh.setFormatter(formatter)20 self.logger.addHandler(fh)21 self.logger.info(' '.join([token.value for token in self.tokens]))22 # Generate the metadata23 self._getMetaData()24 fh.close()25 self.logger.removeHandler(fh)26 def __repr__(self):27 return str((f"CREATE TABLE {self.tableName}", self.getColumns()))28 def getColumns(self):29 return str(self.columns)30 def _nextToken(self):31 """ Allows you to go to the next token """32 self.index += 133 self.logger.debug("Next token is {}".format(self.tokens[self.index]))34 return self.tokens[self.index]35 36 def _currentToken(self):37 self.logger.debug("Current token is {}".format(self.tokens[self.index]))38 return self.tokens[self.index]39 def _rewindToken(self):40 """ Allows you to rewind to the previous token """41 self.index -= 142 self.logger.debug("Previous token is {}".format(self.tokens[self.index]))43 def _isDataType(self, token):44 """ https://docs.microsoft.com/en-us/sql/t-sql/data-types/data-types-transact-sql?view=sql-server-ver15 """45 dtypes = {46 # Exact Numerics47 "BIGINT", "NUMERIC", "BIT", "SMALLINT", "DECIMAL", "SMALLMONEY", "INT", "TINYINT", "MONEY",48 # Approximate Numerics49 "FLOAT", "REAL",50 # Date and Time51 "DATE", "DATETIMEOFFSET", "DATETIME2", "SMALLDATETIME", "DATETIME", "TIME",52 # Character Strings53 "CHAR", "VARCHAR", "TEXT",54 # Unicode Character strings55 "NCHAR", "NVARCHAR", "NTEXT",56 # Binary Strings57 "BINARY", "VARBINARY", "IMAGE",58 # Other59 "ROWVERSION", "UNIQUEIDENTIFIER"60 }61 return token.upper() in dtypes62 def _isPunctuation(self, token):63 grammar = {64 ',', "'", '"', ';',65 '$', '@', '+', '-',66 '_', '=', '(', ')',67 '{', '[', ']', '}',68 ':', '<', '>', '/'69 }70 return token in grammar71 def _isKeyword(self, token):72 keywords = {73 # Constraint Keys74 'PRIMARY', 'FOREIGN', 'KEY', 'REFERENCES',75 # DDL76 'CREATE', 'DROP', 'ALTER',77 'TABLE', 'IDENTITY', 'NOT', 'NULL', 'ON',78 'SPARSE', 'CLUSTERED', 'NONCLUSTERED',79 'DEFAULT', 'ADD',80 # SQL DML81 'DELETE', 'UPDATE', 'INSERT', 'SELECT'82 }83 return token.upper() in keywords84 def _isIdentifier(self, token):85 return not (self._isKeyword(token) or self._isDataType(token) or self._isPunctuation(token))86 def _getNumberSizes(self, token):87 return [int(tkn.value) for tkn in self._filterTokens(token.tokens) if tkn.value.isnumeric()]88 def _getKeys(self, tokens):89 res = []90 for token in tokens:91 if type(token) == sqlparse.sql.Identifier:92 res.append(token.value.upper())93 return tuple(res)94 def _validForeignReference(self, data):95 return data['table_reference'] in self.metaData or data['delete_action'] != 'NO_ACTION'96 def _getForiegnKey(self, tableMode=False):97 """ Parses the PRIMARY KEY attributes """98 self.logger.debug("Begin _getForiegnKey")99 res = {}100 token = self._nextToken()101 if token.value.upper() != 'KEY':102 raise Exception("Expected keyword KEY")103 # For table constraint, get mulitple columns104 if tableMode:105 token = self._nextToken()106 if token.value != '(':107 raise Exception("Expected (")108 res['columns'] = []109 while True:110 token = self._nextToken()111 if not self._isIdentifier(token.value):112 raise Exception("Expected identifier")113 res['columns'].append(token.value)114 token = self._nextToken()115 if token.value == ')':116 break117 elif token.value != ',':118 raise Exception("Expected ,")119 token = self._nextToken()120 if token.value.upper() != 'REFERENCES':121 raise Exception("Expected keyword REFERENCES")122 token = self._nextToken()123 if not self._isIdentifier(token.value):124 raise Exception("Expected identifier")125 res['ref_table'] = token.value126 token = self._nextToken()127 if token.value != '(':128 raise Exception("Expected (")129 130 if tableMode:131 res['ref_column'] = []132 while True:133 token = self._nextToken()134 if not self._isIdentifier(token.value):135 raise Exception("Expected identifier")136 res['ref_column'].append(token.value)137 token = self._nextToken()138 if token.value == ')':139 self._rewindToken()140 break141 elif token.value != ',':142 raise Exception("Expected ,")143 else:144 token = self._nextToken()145 if not self._isIdentifier(token.value):146 raise Exception("Expected identifier")147 res['ref_column'] = token.value148 token = self._nextToken()149 if token.value != ')':150 raise Exception("Expected )")151 152 token = self._nextToken()153 if token.value.upper() == 'ON':154 while True:155 token = self._nextToken()156 print(token)157 if token.value.upper() not in {'DELETE', 'UPDATE'}:158 break159 action = token.value160 token = self._nextToken()161 if token.value.upper() == 'CASCADE':162 res[action] = 'CASCADE'163 elif token.value.upper() =='SET':164 token = self._nextToken()165 res[action] = token.value.upper()166 elif token.value.upper() == 'NO':167 self._nextToken()168 res[action] = 'NONE'169 self._rewindToken()170 elif token.value.upper() == 'NOT':171 pass172 else:173 self._rewindToken()174 self.logger.debug("End _getForiegnKey")175 return res176 def _getPrimaryKeyColumn(self):177 """ Parses the PRIMARY KEY attributes """178 res = {}179 token = self._nextToken()180 if token.value.upper() != 'KEY':181 raise Exception("Expected keyword KEY")182 res['primary_key'] = True183 token = self._nextToken()184 # Check for clustered indexing185 if token.value.upper() == 'NONCLUSTERED':186 res['clustered'] = False187 token = self._nextToken()188 elif token.value.upper() == 'CLUSTERED':189 res['clustered'] = True190 token = self._nextToken()191 token = self._rewindToken()192 return res193 def _getPrimaryKeyTable(self):194 """ Parses the PRIMARY KEY attributes """195 res = {}196 column = ''197 token = self._nextToken()198 if token.value.upper() != 'KEY':199 raise Exception("Expected keyword KEY")200 res['primary_key'] = True201 token = self._nextToken()202 # Check for clustered indexing203 if token.value.upper() == 'NONCLUSTERED':204 res['clustered'] = False205 token = self._nextToken()206 elif token.value.upper() == 'CLUSTERED':207 res['clustered'] = True208 token = self._nextToken()209 210 if token.value == '(':211 token = self._nextToken()212 if token.value not in self.columns:213 raise Exception(f"The column {token.value} doesn't exist")214 column = token.value215 token = self._nextToken()216 if token.value != ')':217 raise Exception(f"Expected )")218 token = self._nextToken()219 else:220 raise Exception(f"Expected (")221 222 token = self._rewindToken()223 self.columns[column] |= res224 def _getIdentityInfo(self):225 """ Parses the IDENTITY attributes """226 self.logger.debug("Begin _getIdentityInfo")227 info = None228 token = self._nextToken()229 if token.value == '(':230 seed = self._nextToken()231 self.logger.debug(seed.value)232 if not seed.value.isnumeric():233 raise Exception("Seed value is not numeric")234 token = self._nextToken()235 if not token.value == ',':236 raise Exception("Expected ,")237 increment = self._nextToken()238 if not increment.value.isnumeric():239 raise Exception("Increment value is not numeric")240 token = self._nextToken()241 if token.value != ')':242 raise Exception("Expected )")243 info = (int(seed.value), int(increment.value))244 else:245 token = self._rewindToken()246 info = (1, 1)247 self.logger.debug("End _getIdentityInfo")248 return info249 def _getDecimalInfo(self):250 """ Parses the DECIMAL/NUMERIC attributes """251 self.logger.debug("Begin _getDecimalInfo")252 res = { 'precision': 8, 'scale': 0 }253 token = self._nextToken()254 if token.value == '(':255 precision = self._nextToken()256 if not precision.value.isnumeric():257 raise Exception("Precision value is not numeric")258 res['precision'] = int(precision.value)259 token = self._nextToken()260 # Check if there's a scale to evaluate261 if token.value == ')':262 return res263 elif not token.value == ',':264 raise Exception("Expected ,")265 266 scale = self._nextToken()267 if not scale.value.isnumeric():268 raise Exception("Scale value is not numeric")269 res['scale'] = int(scale.value)270 token = self._nextToken()271 if token.value != ')':272 raise Exception("Expected )")273 else:274 token = self._rewindToken()275 self.logger.debug("End _getDecimalInfo")276 return res277 def _getDataSize(self, dtype):278 """ Parses the FLOAT, VARCHAR, NCHAR, NVARCHAR, CHAR, BINARY, VARBINARY attributes """279 self.logger.debug("Begin _getDataSize")280 res = { 'size': 1 }281 token = self._nextToken()282 if token.value == '(':283 size = self._nextToken()284 if not size.value.isnumeric():285 raise Exception("Precision value is not numeric")286 res['size'] = int(size.value)287 token = self._nextToken()288 # Check if there's a scale to evaluate289 if not token.value == ')':290 raise Exception("Expected ,")291 else:292 if dtype == 'FLOAT':293 res['size'] = 53294 token = self._rewindToken()295 return res296 def _processDataType(self):297 """ Process the data type column """298 res = {}299 token = self._currentToken()300 res['data_type'] = token.value.upper()301 if token.value.upper() in { 'DECIMAL', 'NUMERIC' }:302 res |= self._getDecimalInfo()303 elif token.value.upper() in { 'FLOAT', 'VARCHAR', 'NCHAR', 'NVARCHAR', 'CHAR', 'BINARY', 'VARBINARY' }:304 res |= self._getDataSize(token.value.upper())305 self._nextToken()306 return res307 def _parseColumn(self, column):308 self.logger.debug("Begin _parseColumn")309 self.columns[column] = {}310 token = self._nextToken()311 self.logger.debug(token.value)312 if self._isDataType(token.value):313 self.columns[column] = self._processDataType() 314 token = self._currentToken()315 while token.value not in { ',', ')' }:316 if token.value.upper() == 'IDENTITY':317 self.columns[column]['identity'] = self._getIdentityInfo()318 #elif token.value.upper() == 'NOT':319 # token = self._nextToken()320 # if token.value.upper() == 'NULL':321 # self.columns[column]['nullable'] = False322 elif token.value.upper() == 'NOT NULL':323 self.columns[column]['nullable'] = False324 elif token.value.upper() == 'NULL':325 self.columns[column]['nullable'] = True326 elif token.value.upper() == 'DEFAULT':327 token = self._nextToken()328 self.columns[column]['default_value'] = token.value329 elif token.value.upper() == 'PRIMARY':330 self.columns[column] |= self._getPrimaryKeyColumn()331 elif token.value.upper() == 'FOREIGN':332 self.columns[column]['foreign_key'] = self._getForiegnKeyColumn()333 token = self._nextToken()334 self._rewindToken() # If we hit a ',' or ')', go back one token335 self.logger.debug("End _parseColumn")336 return # Go to the next column or we reached the end of the table337 elif token.value.upper() == 'AS':338 pass339 pass340 def _parseColumns(self):341 self.logger.debug("Begin _parseColumns")342 while True:343 token = self._nextToken() # Should be either identitiy token, ',', or ')'344 #print(f'Next token for column: {token}')345 #print(self.columns)346 #self.logger.debug("Look for Identifier: ", token, type(token), type(token) == sqlparse.sql.Identifier)347 if self._isIdentifier(token.value.upper()):348 self._parseColumn(token.value)349 elif token.value.upper() == 'PRIMARY':350 self._getPrimaryKeyTable()351 elif token.value.upper() == 'FOREIGN':352 obj = self._getForiegnKey(tableMode=True)353 if len(obj['columns']) == 1:354 obj['ref_column'] = obj['ref_column'][0]355 column = obj['columns'][0]356 del obj['columns']357 self.columns[column]['foreign_key'] = obj358 else:359 self.multiForeignKeys.append(obj)360 elif token.value == ',':361 continue362 elif token.value == ')':363 break364 self.logger.debug("End _parseColumns")365 def _getMetaData(self):366 tableName = self._currentToken()367 if self._isIdentifier(tableName.value.upper()):368 self.tableName = tableName.value369 else:370 raise Exception("Table name expected")371 token = self._nextToken() # Should be either ',' or AS372 if token.value.upper() == 'AS':373 pass374 if token.value == '(':375 self._parseColumns()376 else:...

Full Screen

Full Screen

runtime.py

Source:runtime.py Github

copy

Full Screen

1# -*- coding: utf-8 -*-2"""Tests for the run-time object."""3import unittest4from dtfabric.runtime import runtime5from tests import test_lib6class StructureValuesClassFactoryTest(test_lib.BaseTestCase):7 """Structure values class factory tests."""8 # pylint: disable=protected-access9 def testCreateClassTemplate(self):10 """Tests the _CreateClassTemplate function."""11 definitions_file = self._GetTestFilePath(['structure.yaml'])12 definitions_registry = self._CreateDefinitionRegistryFromFile(13 definitions_file)14 data_type_definition = definitions_registry.GetDefinitionByName('point3d')15 class_template = runtime.StructureValuesClassFactory._CreateClassTemplate(16 data_type_definition)17 self.assertIsNotNone(class_template)18 # TODO: implement error conditions.19 def testIsIdentifier(self):20 """Tests the _IsIdentifier function."""21 result = runtime.StructureValuesClassFactory._IsIdentifier('valid')22 self.assertTrue(result)23 result = runtime.StructureValuesClassFactory._IsIdentifier('_valid')24 self.assertTrue(result)25 result = runtime.StructureValuesClassFactory._IsIdentifier('valid1')26 self.assertTrue(result)27 result = runtime.StructureValuesClassFactory._IsIdentifier('')28 self.assertFalse(result)29 result = runtime.StructureValuesClassFactory._IsIdentifier('0invalid')30 self.assertFalse(result)31 result = runtime.StructureValuesClassFactory._IsIdentifier('in-valid')32 self.assertFalse(result)33 def testValidateDataTypeDefinition(self):34 """Tests the _ValidateDataTypeDefinition function."""35 definitions_file = self._GetTestFilePath(['structure.yaml'])36 definitions_registry = self._CreateDefinitionRegistryFromFile(37 definitions_file)38 data_type_definition = definitions_registry.GetDefinitionByName('point3d')39 runtime.StructureValuesClassFactory._ValidateDataTypeDefinition(40 data_type_definition)41 # TODO: implement error conditions.42 def testCreateClass(self):43 """Tests the CreateClass function."""44 definitions_file = self._GetTestFilePath(['structure.yaml'])45 definitions_registry = self._CreateDefinitionRegistryFromFile(46 definitions_file)47 data_type_definition = definitions_registry.GetDefinitionByName('point3d')48 structure_values_class = runtime.StructureValuesClassFactory.CreateClass(49 data_type_definition)50 self.assertIsNotNone(structure_values_class)51if __name__ == '__main__':...

Full Screen

Full Screen

check_identifiers.py

Source:check_identifiers.py Github

copy

Full Screen

2# All rights reserved. This work is under a BSD license, see LICENSE.TXT.3import re4import keyword5from .config_errors import ConfigDefinitionException6def _isidentifier(name):7 if name in keyword.kwlist:8 return False9 return re.match(r'^[a-z_][a-z0-9_]*$', name, re.I) is not None10def check_valid_identifier(name):11 if not _isidentifier(name):12 raise ConfigDefinitionException(repr(name) + " is not a valid identifier.")13def check_valid_identifiers(names):14 invalid = []15 for name in names:16 if not _isidentifier(name):17 invalid.append(name)18 if not invalid:19 return20 if len(invalid) == 1:21 raise ConfigDefinitionException(repr(invalid[0]) + " is not a valid identifier.")...

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run autotest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful