Best Python code snippet using molecule_python
commands.js
Source:commands.js
...85importClass(org.ow2.proactive_grid_cloud_portal.cli.cmd.AbstractCommand);86importClass(com.google.common.collect.ObjectArrays);87var currentContext = ApplicationContextImpl.currentContext();88function url(url) {89 execute(new SetUrlCommand('' + url));90}91function silent() {92 execute(new SetSilentCommand());93}94function exit() {95 currentContext.setProperty(AbstractIModeCommand.TERMINATE, true);96}97function printWelcomeMsg() {98 print('Type help() for interactive help \r\n');99 if (getUser(currentContext) == null && getCredFile(currentContext) == null) {100 print('Warning: You are not currently logged in.\r\n');101 }102}103function prints() {104 execute(new PrintSessionCommand());105}106function printError(error, currentContext) {107 print("An error occurred while executing the command:\r\n" + error.message + "\r\n");108 if (AbstractCommand.isDebugModeEnabled(currentContext)) {109 if (error.javaException != null) {110 error.javaException.printStackTrace();111 } else {112 error.printStackTrace(); // if executed with JDK8113 }114 }115}116function reconnect() {117 if (getCredFile(currentContext) != null) {118 loginwithcredentials(getCredFile(currentContext));119 } else if (getUser(currentContext) != null) {120 login(getUser(currentContext));121 } else {122 print('use either login(username) or loginwithcredentials(cred-file) function\r\n')123 }124}125function debug(flag) {126 execute(new SetDebugModeCommand(string(flag)));127}128function string(obj) {129 return '' + obj;130}131function getUser(context) {132 return context.getProperty(LoginCommand.USERNAME, stringClass);133}134function getCredFile(context) {135 return context.getProperty(LoginWithCredentialsCommand.CRED_FILE, stringClass);136}137function execute(cmd) {138 var tryAfterReLogin = false;139 try {140 cmd.execute(currentContext);141 } catch (e) {142 if (e.javaException instanceof CLIException143 && (e.javaException.reason() == CLIException.REASON_UNAUTHORIZED_ACCESS)144 && currentContext.getProperty(AbstractLoginCommand.PROP_PERSISTED_SESSION, java.lang.Boolean.TYPE, false)) {145 tryAfterReLogin = true;146 } else {147 printError(e, currentContext);148 }149 }150 if (tryAfterReLogin) {151 currentContext.setProperty(AbstractLoginCommand.PROP_RENEW_SESSION, java.lang.Boolean.TRUE);152 try {153 if (getCredFile(currentContext) != null) {154 execute(new LoginWithCredentialsCommand(getCredFile(currentContext)));155 } else if (getUser(currentContext) != null) {156 execute(new LoginCommand(getUser(currentContext)));157 }158 cmd.execute(currentContext);159 } catch (e) {160 printError(e, currentContext);161 }162 }163}164function schedulerlogin(user) {165 currentContext.setSessionId('');166 execute(new LoginSchedCommand('' + user));167}168function login(user) {169 currentContext.setProperty(AbstractLoginCommand.PROP_RENEW_SESSION, true);170 execute(new LoginCommand('' + user));171}172function loginwithcredentials(pathname) {173 currentContext.setProperty(AbstractLoginCommand.PROP_RENEW_SESSION, true);174 execute(new LoginWithCredentialsCommand('' + pathname));175}176// scheduler177function schedulerhelp() {178 execute(new SchedJsHelpCommand());179}180function putcredential(key, value) {181 execute(new PutThirdPartyCredentialCommand(string(key), string(value)));182}183function removecredential(key) {184 execute(new RemoveThirdPartyCredentialCommand(string(key)));185}186function listcredentials() {187 execute(new ThirdPartyCredentialKeySetCommand());188}189function submit(pathname, variables, genericInfos) {190 if (typeof genericInfos == 'undefined') {191 if (typeof variables == 'undefined') {192 execute(new SubmitJobCommand('' + pathname));193 } else {194 execute(new SubmitJobCommand('' + pathname, '' + variables));195 }196 } else {197 execute(new SubmitJobCommand('' + pathname, '' + variables, '' + genericInfos));198 }199}200function resubmit(pathname, variables, genericInfos) {201 if (typeof variables == 'undefined') {202 execute(new ReSubmitJobCommand([string(pathname)]));203 } else {204 execute(new ReSubmitJobCommand(new Array([string(pathname)], variables, genericInfos)));205 }206}207function submitarchive(pathname, variables) {208 if (typeof variables == 'undefined') {209 execute(new SubmitJobCommand([string(pathname)]));210 } else {211 execute(new SubmitJobCommand(ObjectArrays.concat([string(pathname)], variables, java.lang.String)));212 }213}214function jobpriority(jobId, priority) {215 execute(new ChangeJobPriorityCommand('' + jobId, '' + priority));216}217function pausejob(jobId) {218 execute(new PauseJobCommand('' + jobId));219}220function resumejob(jobId) {221 execute(new ResumeJobCommand('' + jobId));222}223function resumeAllPausedTasks(jobId) {224 execute(new ResumeAllPausedTasksCommand('' + jobId));225}226function restartAllInErrorTasks(jobId) {227 execute(new RestartAllInErrorTasksCommand('' + jobId));228}229function resumeAllPausedTasksAndRestartAllInErrorTasks(jobId) {230 execute(new ResumeAllPausedTasksAndRestartAllInErrorTasksCommand('' + jobId));231}232function killjob(jobId) {233 execute(new KillJobCommand('' + jobId));234}235function removejob(jobId) {236 execute(new RemoveJobCommand('' + jobId));237}238function jobstate(jobId) {239 execute(new GetJobStateCommand('' + jobId));240}241function listjobs(x, y) {242 if (typeof x == 'undefined') {243 execute(new ListJobCommand());244 } else if (typeof y == 'undefined') {245 execute(new ListJobCommand('latest=' + x));246 } else {247 execute(new ListJobCommand(['from=' + x, 'limit=' + y]));248 }249}250/*251 * The following parameters have different meaning whether252 * listtasks is called with 1,2,3 or 4 parameters.253 * 1 parameter : arg1 is the jobId254 * 2 parameters : arg1 is the jobId255 * arg2 is the tag256 * 3 parameters : arg1 is the jobId257 * arg2 is the offset258 * arg3 is the limit259 * 4 parameters : arg1 is the jobId260 * arg2 is the tag261 * arg3 is the offset262 * arg4 is the limit263 * 264 */265function listtasks(arg1, arg2, arg3, arg4) {266 // There is only one parameter, the jobId267 if (arguments.length == 1 && typeof arg1 != 'undefined') {268 execute(ListJobTasksCommand.LJTCommandBuilder.newInstance().jobId(arg1).instance());269 }270 // Two arguments: jobId and tag271 if (arguments.length == 2 && typeof arg1 != 'undefined'272 && typeof arg2 != 'undefined') {273 execute(ListJobTasksCommand.LJTCommandBuilder.newInstance().jobId(arg1).tag(arg2).instance());274 }275 // Three parameters: jobId, offset and limit276 if (arguments.length == 3 && typeof arg1 != 'undefined'277 && typeof arg2 != 'undefined' && typeof arg3 != 'undefined') {278 execute(ListJobTasksCommand.LJTCommandBuilder.newInstance().jobId(arg1).offset(arg2).limit(arg3).instance());279 }280 // Four parameters: jobId, tag, offset and limit281 // We don't parse any parameters after the fourth one282 if (arguments.length >= 4 && typeof arg1 != 'undefined'283 && typeof arg2 != 'undefined' && typeof arg3 != 'undefined'284 && typeof arg4 != 'undefined') {285 execute(ListJobTasksCommand.LJTCommandBuilder.newInstance().jobId(arg1).tag(arg2).offset(arg3).limit(arg4).instance());286 }287}288function schedulerstats() {289 execute(new SchedStatsCommand());290}291function jobresult(jobId, tag) {292 if(typeof tag == 'undefined'){293 execute(new GetJobResultCommand('' + jobId));294 }295 else{296 execute(new GetJobResultCommand('' + jobId, tag));297 }298}299function joboutput(jobId, tag) {300 if(typeof tag == 'undefined'){301 execute(new GetJobOutputCommand('' + jobId));302 }303 else{304 execute(new GetJobOutputCommand('' + jobId, tag));305 }306}307function jobcontent(jobId) {308 execute(new GetJobContentCommand('' + jobId))309}310function taskresult(jobId, taskId) {311 execute(new GetTaskResultCommand('' + jobId, '' + taskId));312}313function taskoutput(jobId, taskId) {314 execute(new GetTaskOutputCommand('' + jobId, '' + taskId));315}316/*317 * The following parameters have different meaning whether318 * taskstates is called with 1,2,3 or 4 parameters.319 * 1 parameter : arg1 is the jobId320 * 2 parameters : arg1 is the jobId321 * arg2 is the tag322 * 3 parameters : arg1 is the jobId323 * arg2 is the offset324 * arg3 is the limit325 * 4 parameters : arg1 is the jobId326 * arg2 is the tag327 * arg3 is the offset328 * arg4 is the limit329 * 330 */331function taskstates(arg1, arg2, arg3, arg4){332 // There is only one parameter, the jobId333 if (arguments.length == 1 && typeof arg1 != 'undefined') {334 execute(new ListTaskStatesCommand(arg1));335 }336 // Two arguments: jobId and tag337 if (arguments.length == 2 && typeof arg1 != 'undefined'338 && typeof arg2 != 'undefined') {339 execute(new ListTaskStatesCommand(arg1, arg2));340 }341 // Three parameters: jobId, offset and limit342 if (arguments.length == 3 && typeof arg1 != 'undefined'343 && typeof arg2 != 'undefined' && typeof arg3 != 'undefined') {344 execute(new ListTaskStatesCommand(arg1, arg2, arg3));345 }346 // Four parameters: jobId, tag, offset and limit347 // We don't parse any parameters after the fourth one348 if (arguments.length >= 4 && typeof arg1 != 'undefined'349 && typeof arg2 != 'undefined' && typeof arg3 != 'undefined'350 && typeof arg4 != 'undefined') {351 execute(new ListTaskStatesCommand(arg1, arg2, arg3, arg4));352 }353}354function preempttask(jobId, taskId) {355 execute(new PreemptTaskCommand('' + jobId, '' + taskId));356}357function restarttask(jobId, taskId) {358 execute(new RestartTaskCommand('' + jobId, '' + taskId));359}360function restartInErrorTask(jobId, taskId) {361 execute(new RestartInErrorTaskCommand('' + jobId, '' + taskId));362}363function restartRunningTask(jobId, taskId) {364 execute(new RestartRunningTaskCommand('' + jobId, '' + taskId));365}366function uploadfile(spaceName, filePath, fileName, localFile) {367 execute(new UploadFileCommand(string(spaceName), string(filePath),string(fileName), string(localFile)));368}369function downloadfile(spaceName, pathName, localFile) {370 execute(new DownloadFileCommand(string(spaceName), string(pathName), string(localFile))) ;371}372function livelog(jobId) {373 execute(new LiveLogCommand(string(jobId)));374}375function installpackage(packagePath) {376 execute(new InstallPackageCommand(string(packagePath)));377}378function start() {379 execute(new StartCommand());380}381function stop() {382 execute(new StopCommand());383}384function pause() {385 execute(new PauseCommand());386}387function resume() {388 execute(new ResumeCommand());389}390function freeze() {391 execute(new FreezeCommand());392}393function kill() {394 execute(new KillCommand());395}396function script(path, args) {397 execute(new EvalScriptCommand('' + path, '' + args));398}399function linkrm(rmUrl) {400 execute(new LinkRmCommand('' + rmUrl));401}402// rm403function rmhelp() {404 execute(new RmJsHelpCommand());405}406function addnode(nodeUrl, nodeSource) {407 if (nodeSource) {408 execute(new AddNodeCommand('' + nodeUrl, '' + nodeSource));409 } else {410 execute(new AddNodeCommand('' + nodeUrl));411 }412}413function removenode(nodeUrl, preempt) {414 execute(new RemoveNodeCommand('' + nodeUrl, preempt));415}416function definens(nodeSource, infrastructure, policy, nodesRecoverable) {417 execute(new SetInfrastructureCommand(infrastructure));418 execute(new SetPolicyCommand(policy));419 if(typeof nodesRecoverable == 'undefined'){420 execute(new DefineNodeSourceCommand('' + nodeSource));421 }422 else{423 execute(new DefineNodeSourceCommand('' + nodeSource, '' + nodesRecoverable));424 }425}426function editns(nodeSource, infrastructure, policy, nodesRecoverable) {427 execute(new SetInfrastructureCommand(infrastructure));428 execute(new SetPolicyCommand(policy));429 execute(new EditNodeSourceCommand('' + nodeSource, '' + nodesRecoverable));430}431function updatensparam(nodeSource, infrastructure, policy) {432 execute(new SetInfrastructureCommand(infrastructure));433 execute(new SetPolicyCommand(policy));434 execute(new UpdateDynamicParametersCommand('' + nodeSource));435}436function createns(nodeSource, infrastructure, policy, nodesRecoverable) {437 execute(new SetInfrastructureCommand(infrastructure));438 execute(new SetPolicyCommand(policy));439 if(typeof nodesRecoverable == 'undefined'){440 execute(new CreateNodeSourceCommand('' + nodeSource));441 }442 else{443 execute(new CreateNodeSourceCommand('' + nodeSource, '' + nodesRecoverable));444 }445}446function deployns(nodeSourceName) {447 execute(new DeployNodeSourceCommand('' + nodeSourceName));448}449function undeployns(nodeSourceName, preempt) {450 if(typeof preempt == 'undefined'){451 execute(new UndeployNodeSourceCommand('' + nodeSourceName));452 } else {453 execute(new UndeployNodeSourceCommand('' + nodeSourceName, preempt));454 }455}456function removens(nodeSource, preempt) {457 execute(new RemoveNodeSourceCommand('' + nodeSource, preempt));458}459function locknodes(nodeUrl) {460 var result = [];461 for (var i = 0; i < arguments.length; i++) {462 result[i] = arguments[i];463 }464 execute(new LockNodeCommand(result));465}466function unlocknodes(nodeUrl) {467 var result = [];468 for (var i = 0; i < arguments.length; i++) {469 result[i] = arguments[i];470 }471 execute(new UnlockNodeCommand(result));472}473function listnodes(nodeSource) {474 if (nodeSource) {475 execute(new ListNodeCommand('' + nodeSource));476 } else {477 execute(new ListNodeCommand());478 }479}480function listns() {481 execute(new ListNodeSourceCommand());482}483function nodeinfo(nodeUrl) {484 execute(new GetNodeInfoCommand('' + nodeUrl));485}486function rmthreaddump() {487 execute(new GetRMThreadDumpCommand());488}489function nodethreaddump(nodeUrl) {490 execute(new GetNodeThreadDumpCommand('' + nodeUrl));491}492function listinfrastructures() {493 execute(new ListInfrastructureCommand());494}495function listpolicies() {496 execute(new ListPolicyCommand());497}498function topology() {499 execute(new GetTopologyCommand());500}501function rmstats() {502 execute(new RmStatsCommand());503}504function shutdown() {505 execute(new ShutdownCommand());506}507function help() {508 execute(new JsHelpCommand());509}510function version() {511 execute(new JsVersionCommand())512}...
JobSchedulerSpec.js
Source:JobSchedulerSpec.js
...37 var js = new JobScheduler([2.0, // JobType.TEXTURE38 0.0, // JobType.PROGRAM39 0.0]); // JobType.BUFFER40 var job = new MockJob();41 var executed = js.execute(job, JobType.TEXTURE);42 expect(executed).toEqual(true);43 expect(job.executed).toEqual(true);44 expect(js._totalUsedThisFrame).toEqual(1.0);45 expect(js._budgets[JobType.TEXTURE].total).toEqual(2.0);46 expect(js._budgets[JobType.TEXTURE].usedThisFrame).toEqual(1.0);47 });48 it('disableThisFrame does not execute a job', function() {49 var js = new JobScheduler([2.0, 0.0, 0.0]);50 expect(js.execute(new MockJob(), JobType.TEXTURE)).toEqual(true);51 js.disableThisFrame();52 expect(js.execute(new MockJob(), JobType.TEXTURE)).toEqual(false);53 });54 it('executes different job types', function() {55 var js = new JobScheduler([1.0, 1.0, 1.0]);56 expect(js.execute(new MockJob(), JobType.TEXTURE)).toEqual(true);57 expect(js.execute(new MockJob(), JobType.PROGRAM)).toEqual(true);58 expect(js.execute(new MockJob(), JobType.BUFFER)).toEqual(true);59 expect(js._totalUsedThisFrame).toEqual(3.0);60 var budgets = js._budgets;61 expect(budgets[JobType.TEXTURE].usedThisFrame).toEqual(1.0);62 expect(budgets[JobType.PROGRAM].usedThisFrame).toEqual(1.0);63 expect(budgets[JobType.BUFFER].usedThisFrame).toEqual(1.0);64 });65 it('executes a second job', function() {66 var js = new JobScheduler([2.0, 0.0, 0.0]);67 expect(js.execute(new MockJob(), JobType.TEXTURE)).toEqual(true);68 expect(js.execute(new MockJob(), JobType.TEXTURE)).toEqual(true);69 expect(js._totalUsedThisFrame).toEqual(2.0);70 expect(js._budgets[JobType.TEXTURE].usedThisFrame).toEqual(2.0);71 });72 it('does not execute second job (exceeds total time)', function() {73 var js = new JobScheduler([1.0, 0.0, 0.0]);74 expect(js.execute(new MockJob(), JobType.TEXTURE)).toEqual(true);75 expect(js.execute(new MockJob(), JobType.TEXTURE)).toEqual(false);76 expect(js._budgets[JobType.TEXTURE].starvedThisFrame).toEqual(true);77 });78 it('executes a second job (TEXTURE steals PROGRAM budget)', function() {79 var js = new JobScheduler([1.0, 1.0, 0.0]);80 expect(js.execute(new MockJob(), JobType.TEXTURE)).toEqual(true);81 expect(js.execute(new MockJob(), JobType.TEXTURE)).toEqual(true);82 expect(js._totalUsedThisFrame).toEqual(2.0);83 var budgets = js._budgets;84 expect(budgets[JobType.TEXTURE].usedThisFrame).toEqual(1.0);85 expect(budgets[JobType.TEXTURE].starvedThisFrame).toEqual(true);86 expect(budgets[JobType.PROGRAM].usedThisFrame).toEqual(0.0);87 expect(budgets[JobType.PROGRAM].stolenFromMeThisFrame).toEqual(1.0);88 expect(budgets[JobType.PROGRAM].starvedThisFrame).toEqual(false);89 // There are no budgets left to steal from90 expect(js.execute(new MockJob(), JobType.TEXTURE)).toEqual(false);91 expect(js.execute(new MockJob(), JobType.PROGRAM)).toEqual(true); // Allowed once per frame92 expect(js.execute(new MockJob(), JobType.PROGRAM)).toEqual(false);93 expect(budgets[JobType.PROGRAM].starvedThisFrame).toEqual(true);94 });95 it('does not steal in the same frame', function() {96 var js = new JobScheduler([1.0, 1.0, 1.0]);97 expect(js.execute(new MockJob(), JobType.TEXTURE)).toEqual(true);98 expect(js.execute(new MockJob(), JobType.PROGRAM)).toEqual(true);99 expect(js.execute(new MockJob(), JobType.BUFFER)).toEqual(true);100 // Exhaust budget for all job types in the first frame101 expect(js.execute(new MockJob(), JobType.TEXTURE)).toEqual(false);102 expect(js.execute(new MockJob(), JobType.PROGRAM)).toEqual(false);103 expect(js.execute(new MockJob(), JobType.BUFFER)).toEqual(false);104 // In this next frame, no job type can steal from another since105 // they all exhausted their budgets in the previous frame106 js.resetBudgets();107 expect(js.execute(new MockJob(), JobType.TEXTURE)).toEqual(true);108 expect(js.execute(new MockJob(), JobType.TEXTURE)).toEqual(false);109 expect(js.execute(new MockJob(), JobType.PROGRAM)).toEqual(true);110 expect(js.execute(new MockJob(), JobType.PROGRAM)).toEqual(false);111 expect(js.execute(new MockJob(), JobType.BUFFER)).toEqual(true);112 expect(js.execute(new MockJob(), JobType.BUFFER)).toEqual(false);113 });114 it('does not steal from starving job types over multiple frames', function() {115 var js = new JobScheduler([1.0, 1.0, 0.0]);116 // Exhaust in first frame117 expect(js.execute(new MockJob(), JobType.TEXTURE)).toEqual(true);118 expect(js.execute(new MockJob(), JobType.TEXTURE)).toEqual(true); // Stolen from PROGRAM119 expect(js.execute(new MockJob(), JobType.TEXTURE)).toEqual(false);120 js.resetBudgets();121 expect(js.execute(new MockJob(), JobType.PROGRAM)).toEqual(true);122 expect(js.execute(new MockJob(), JobType.PROGRAM)).toEqual(false); // Can't steal from TEXTURE123 expect(js.execute(new MockJob(), JobType.TEXTURE)).toEqual(true);124 expect(js.execute(new MockJob(), JobType.TEXTURE)).toEqual(false);125 js.resetBudgets();126 expect(js.execute(new MockJob(), JobType.PROGRAM)).toEqual(true);127 expect(js.execute(new MockJob(), JobType.PROGRAM)).toEqual(false); // Can't steal from TEXTURE yet128 js.resetBudgets();129 expect(js.execute(new MockJob(), JobType.PROGRAM)).toEqual(true);130 expect(js.execute(new MockJob(), JobType.PROGRAM)).toEqual(true); // Can steal from TEXTURE since it wasn't exhausted last frame131 });132 it('Allows progress on all job types once per frame', function() {133 var js = new JobScheduler([1.0, 1.0, 1.0]);134 expect(js.execute(new MockJob(), JobType.TEXTURE)).toEqual(true);135 expect(js.execute(new MockJob(), JobType.TEXTURE)).toEqual(true); // Steal from PROGRAM136 expect(js.execute(new MockJob(), JobType.TEXTURE)).toEqual(true); // Steal from BUFFER137 expect(js.execute(new MockJob(), JobType.TEXTURE)).toEqual(false);138 expect(js.execute(new MockJob(), JobType.PROGRAM)).toEqual(true); // Still gets to make progress once this frame139 expect(js.execute(new MockJob(), JobType.PROGRAM)).toEqual(false);140 expect(js.execute(new MockJob(), JobType.BUFFER)).toEqual(true); // Still gets to make progress once this frame141 expect(js.execute(new MockJob(), JobType.BUFFER)).toEqual(false);142 });143 it('Long job still allows progress on other job types once per frame', function() {144 // Job duration is always 1.0 in the tests so shorten budget145 var js = new JobScheduler([0.5, 0.2, 0.2]);146 expect(js.execute(new MockJob(), JobType.TEXTURE)).toEqual(true); // Goes over total budget147 expect(js.execute(new MockJob(), JobType.PROGRAM)).toEqual(true); // Still gets to make progress once this frame148 expect(js.execute(new MockJob(), JobType.BUFFER)).toEqual(true); // Still gets to make progress once this frame149 });150 it('constructor throws when budgets.length is not JobType.NUMBER_OF_JOB_TYPES', function() {151 expect(function() {152 return new JobScheduler([1.0]);153 }).toThrowDeveloperError();154 });...
test-authorizer.js
Source:test-authorizer.js
1function terminateTest()2{3 if (window.layoutTestController)4 layoutTestController.notifyDone();5}6function logAndTerminateTest(message, error)7{8 log(message + ": " + error.message);9 terminateTest();10}11function cleanup(db)12{13 db.transaction(function(tx) {14 tx.executeSql("DROP TABLE IF EXISTS Test;");15 tx.executeSql("DROP INDEX IF EXISTS TestIndex;");16 tx.executeSql("DROP VIEW IF EXISTS TestView;");17 tx.executeSql("DROP TRIGGER IF EXISTS TestTrigger;");18 }, function(error) { logAndTerminateTest("Cleanup failed", error); });19}20function statementSuccessCallback(statementType)21{22 log(statementType + " statement succeeded.");23}24function statementErrorCallback(statementType, error)25{26 log(statementType + " statement failed: " + error.message);27 return false;28}29function executeStatement(tx, statement, operation)30{31 tx.executeSql(statement, [],32 function(result) { statementSuccessCallback(operation); },33 function(tx, error) { return statementErrorCallback(operation, error); });34}35function createTableCallback(tx)36{37 executeStatement(tx, "CREATE TABLE Test (Foo int);", "SQLITE_CREATE_TABLE");38}39function createStatementsCallback(tx)40{41 executeStatement(tx, "CREATE INDEX TestIndex ON Test (Foo);", "SQLITE_CREATE_INDEX");42 // Even though the following query should trigger a SQLITE_CREATE_TEMP_INDEX operation43 // (according to http://www.sqlite.org/tempfiles.html), it doesn't, and I'm not aware44 // of any other way to trigger this operation. So we'll skip it for now.45 //executeStatement(tx, "SELECT * FROM Test WHERE Foo IN (1, 2, 3);", "SQLITE_CREATE_TEMP_INDEX");46 executeStatement(tx, "CREATE TEMP TABLE TestTempTable (Foo int);", "SQLITE_CREATE_TEMP_TABLE");47 executeStatement(tx, "CREATE TEMP TRIGGER TestTempTrigger INSERT ON Test BEGIN SELECT COUNT(*) FROM Test; END;", "SQLITE_CREATE_TEMP_TRIGGER");48 executeStatement(tx, "CREATE TEMP VIEW TestTempView AS SELECT COUNT(*) FROM Test;", "SQLITE_CREATE_TEMP_VIEW");49 executeStatement(tx, "CREATE TRIGGER TestTrigger INSERT ON Test BEGIN SELECT COUNT(*) FROM Test; END;", "SQLITE_CREATE_TRIGGER");50 executeStatement(tx, "CREATE VIEW TestView AS SELECT COUNT(*) FROM Test;", "SQLITE_CREATE_VIEW");51 executeStatement(tx, "CREATE VIRTUAL TABLE TestVirtualTable USING MissingModule;", "SQLITE_CREATE_VTABLE");52}53function otherStatementsCallback(tx)54{55 executeStatement(tx, "SELECT COUNT(*) FROM Test;", "SQLITE_READ");56 executeStatement(tx, "SELECT COUNT(*) FROM Test;", "SQLITE_SELECT");57 executeStatement(tx, "DELETE FROM Test;", "SQLITE_DELETE");58 executeStatement(tx, "INSERT INTO Test VALUES (1);", "SQLITE_INSERT");59 executeStatement(tx, "UPDATE Test SET Foo = 2 WHERE Foo = 1;", "SQLITE_UPDATE");60 executeStatement(tx, "PRAGMA cache_size;", "SQLITE_PRAGMA");61 executeStatement(tx, "ALTER TABLE Test RENAME TO TestTable;", "SQLITE_ALTER_TABLE");62 // Rename the table back to its original name63 executeStatement(tx, "ALTER TABLE TestTable RENAME To Test;", "SQLITE_ALTER_TABLE");64 executeStatement(tx, "BEGIN TRANSACTION;", "SQLITE_TRANSACTION");65 executeStatement(tx, "ATTACH main AS TestMain;", "SQLITE_ATTACH");66 executeStatement(tx, "DETACH TestMain;", "SQLITE_DETACH");67 executeStatement(tx, "REINDEX;", "SQLITE_REINDEX");68 executeStatement(tx, "ANALYZE;", "SQLITE_ANALYZE");69 // SQLITE_FUNCTION: allowed write mode70 // There is no SQL/Javascript API to add user-defined functions to SQLite,71 // so we cannot test this operation72}73function dropStatementsCallback(tx)74{75 executeStatement(tx, "DROP INDEX TestIndex;", "SQLITE_DROP_INDEX");76 // SQLITE_DROP_TEMP_INDEX: allowed in write mode77 // Not sure how to test this: temp indexes are automatically dropped when78 // the database is closed, but HTML5 doesn't specify a closeDatabase() call.79 executeStatement(tx, "DROP TABLE TestTempTable;", "SQLITE_DROP_TEMP_TABLE");80 executeStatement(tx, "DROP TRIGGER TestTempTrigger;", "SQLITE_DROP_TEMP_TRIGGER");81 executeStatement(tx, "DROP VIEW TestTempView;", "SQLITE_DROP_TEMP_VIEW");82 executeStatement(tx, "DROP TRIGGER TestTrigger;", "SQLITE_DROP_TRIGGER");83 executeStatement(tx, "DROP VIEW TestView;", "SQLITE_DROP_VIEW");84 // SQLITE_DROP_VTABLE: allowed in write mode85 // Not sure how to test this: we cannot create a virtual table because we do not86 // have SQL/Javascript APIs to register a module that implements a virtual table.87 // Therefore, trying to drop a virtual table will always fail (no such table)88 // before even getting to the authorizer.89 executeStatement(tx, "DROP TABLE Test;", "SQLITE_DROP_TABLE");90}91function testReadWriteMode(db)92{93 db.transaction(function(tx) {94 createTableCallback(tx);95 createStatementsCallback(tx);96 otherStatementsCallback(tx);97 dropStatementsCallback(tx);98 },99 function(error) { logAndTerminateTest("Write transaction failed", error); },100 function() { log("Write transaction succeeded."); });101}102function testReadOnlyMode(db)103{104 // Test the 'CREATE TABLE' operation; it should be disallowed105 db.readTransaction(createTableCallback,106 function(error) { logAndTerminateTest("Read 'CREATE TABLE' transaction failed", error); });107 // In order to test all other 'CREATE' operations, we must create the table first108 db.transaction(createTableCallback,109 function(error) { logAndTerminateTest("Write 'CREATE TABLE' transaction failed", error); });110 db.readTransaction(createStatementsCallback,111 function(error) { logAndTerminateTest("Read 'CREATE' transaction failed", error); });112 // In order to test the 'DROP' and 'other' operations, we need to first create the respective entities113 db.transaction(createStatementsCallback,114 function(error) { logAndTerminateTest("Write 'CREATE' transaction failed", error); });115 db.readTransaction(otherStatementsCallback,116 function(error) { logAndTerminateTest("Read 'other' transaction failed", error); });117 // Hack: insert an empty write transaction to guaratee that these transactions are executed sequentially118 db.transaction(function(tx) { });119 db.readTransaction(dropStatementsCallback,120 function(error) { logAndTerminateTest("Read 'DROP' transaction failed", error); },121 function() { log("Read transactions succeeded."); terminateTest(); });122}123function runTest()124{125 var db = openDatabaseWithSuffix("AuthorizerTest", "1.0", "Tests the database authorizer.", 32768);126 cleanup(db);127 testReadWriteMode(db);128 testReadOnlyMode(db);...
test-authorizer-sync.js
Source:test-authorizer-sync.js
1function cleanup(db)2{3 db.transaction(function(tx) {4 tx.executeSql("DROP TABLE IF EXISTS Test");5 tx.executeSql("DROP INDEX IF EXISTS TestIndex");6 tx.executeSql("DROP VIEW IF EXISTS TestView");7 tx.executeSql("DROP TRIGGER IF EXISTS TestTrigger");8 });9}10function executeStatement(tx, statement, operation)11{12 try {13 tx.executeSql(statement);14 postMessage(operation + " allowed.");15 } catch (err) {16 postMessage(operation + " not allowed: " + err + " (" + err.code + ")");17 }18}19function createTableCallback(tx)20{21 executeStatement(tx, "CREATE TABLE Test (Foo int)", "SQLITE_CREATE_TABLE");22}23function createStatementsCallback(tx)24{25 executeStatement(tx, "CREATE INDEX TestIndex ON Test (Foo)", "SQLITE_CREATE_INDEX");26 // Even though the following query should trigger a SQLITE_CREATE_TEMP_INDEX operation27 // (according to http://www.sqlite.org/tempfiles.html), it doesn't, and I'm not aware28 // of any other way to trigger this operation. So we'll skip it for now.29 //executeStatement(tx, "SELECT * FROM Test WHERE Foo IN (1, 2, 3)", "SQLITE_CREATE_TEMP_INDEX");30 executeStatement(tx, "CREATE TEMP TABLE TestTempTable (Foo int)", "SQLITE_CREATE_TEMP_TABLE");31 executeStatement(tx, "CREATE TEMP TRIGGER TestTempTrigger INSERT ON Test BEGIN SELECT COUNT(*) FROM Test; END", "SQLITE_CREATE_TEMP_TRIGGER");32 executeStatement(tx, "CREATE TEMP VIEW TestTempView AS SELECT COUNT(*) FROM Test", "SQLITE_CREATE_TEMP_VIEW");33 executeStatement(tx, "CREATE TRIGGER TestTrigger INSERT ON Test BEGIN SELECT COUNT(*) FROM Test; END", "SQLITE_CREATE_TRIGGER");34 executeStatement(tx, "CREATE VIEW TestView AS SELECT COUNT(*) FROM Test", "SQLITE_CREATE_VIEW");35 // We should try to create a virtual table using fts3, when WebKit's sqlite library supports it.36 executeStatement(tx, "CREATE VIRTUAL TABLE TestVirtualTable USING UnsupportedModule", "SQLITE_CREATE_VTABLE");37}38function otherStatementsCallback(tx)39{40 executeStatement(tx, "SELECT COUNT(*) FROM Test", "SQLITE_READ");41 executeStatement(tx, "SELECT COUNT(*) FROM Test", "SQLITE_SELECT");42 executeStatement(tx, "DELETE FROM Test", "SQLITE_DELETE");43 executeStatement(tx, "INSERT INTO Test VALUES (1)", "SQLITE_INSERT");44 executeStatement(tx, "UPDATE Test SET Foo = 2 WHERE Foo = 1", "SQLITE_UPDATE");45 executeStatement(tx, "PRAGMA cache_size", "SQLITE_PRAGMA");46 executeStatement(tx, "ALTER TABLE Test RENAME TO TestTable", "SQLITE_ALTER_TABLE");47 // Rename the table back to its original name48 executeStatement(tx, "ALTER TABLE TestTable RENAME To Test", "SQLITE_ALTER_TABLE");49 executeStatement(tx, "BEGIN TRANSACTION", "SQLITE_TRANSACTION");50 executeStatement(tx, "ATTACH main AS TestMain", "SQLITE_ATTACH");51 executeStatement(tx, "DETACH TestMain", "SQLITE_DETACH");52 executeStatement(tx, "REINDEX", "SQLITE_REINDEX");53 executeStatement(tx, "ANALYZE", "SQLITE_ANALYZE");54 // SQLITE_FUNCTION: allowed in write mode55 // There is no SQL/Javascript API to add user-defined functions to SQLite,56 // so we cannot test this operation57}58function dropStatementsCallback(tx)59{60 executeStatement(tx, "DROP INDEX TestIndex", "SQLITE_DROP_INDEX");61 // SQLITE_DROP_TEMP_INDEX: allowed in write mode62 // Not sure how to test this: temp indexes are automatically dropped when63 // the database is closed, but HTML5 doesn't specify a closeDatabase() call.64 executeStatement(tx, "DROP TABLE TestTempTable", "SQLITE_DROP_TEMP_TABLE");65 executeStatement(tx, "DROP TRIGGER TestTempTrigger", "SQLITE_DROP_TEMP_TRIGGER");66 executeStatement(tx, "DROP VIEW TestTempView", "SQLITE_DROP_TEMP_VIEW");67 executeStatement(tx, "DROP TRIGGER TestTrigger", "SQLITE_DROP_TRIGGER");68 executeStatement(tx, "DROP VIEW TestView", "SQLITE_DROP_VIEW");69 // SQLITE_DROP_VTABLE: allowed in write mode70 // Not sure how to test this: we cannot create a virtual table because we do not71 // have SQL/Javascript APIs to register a module that implements a virtual table.72 // Therefore, trying to drop a virtual table will always fail (no such table)73 // before even getting to the authorizer.74 executeStatement(tx, "DROP TABLE Test", "SQLITE_DROP_TABLE");75}76function testReadWriteMode(db)77{78 db.transaction(function(tx) {79 postMessage("Beginning write transaction:");80 createTableCallback(tx);81 createStatementsCallback(tx);82 otherStatementsCallback(tx);83 dropStatementsCallback(tx);84 postMessage("Write transaction succeeded.\n");85 });86}87function testReadOnlyMode(db)88{89 postMessage("Beginning read transactions:");90 // Test the 'CREATE TABLE' operation; it should be disallowed91 db.readTransaction(createTableCallback);92 // In order to test all other 'CREATE' operations, we must create the table first93 db.transaction(createTableCallback);94 db.readTransaction(createStatementsCallback);95 // In order to test the 'DROP' and 'other' operations, we need to first create the respective entities96 db.transaction(createStatementsCallback);97 db.readTransaction(otherStatementsCallback);98 db.readTransaction(dropStatementsCallback);99 postMessage("Read transactions succeeded.");100}101var db = openDatabaseSync("TestAuthorizerTest", "1.0", "Test the database authorizer.", 1);102cleanup(db);103testReadWriteMode(db);104testReadOnlyMode(db);...
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!!