Best Python code snippet using pytest-play_python
install.py
Source:install.py
1# vim:ts=4:sts=4:sw=4:expandtab2from django.db import models3from satori.core.models import Entity4from satori.core.dbev.events import registry5def install_versions_sql(model):6 qv = lambda x : '\''+str(x)+'\''7 tabs = []8 keys = []9 mod = model10 while issubclass(mod, models.Model):11 tabs.append(str(mod._meta.db_table))12 keys.append(str(mod._meta.pk.column))13 if len(mod._meta.parents.items()) > 0:14 mod = mod._meta.parents.items()[0][0]15 else:16 break17 return """18 SELECT install_versions({0}, {1}, {2}, {3}, 'satori');19 """.format(20 qv(model._meta.db_table),21 qv(model._meta.pk.column),22 'ARRAY[' + ','.join([qv(tab) for tab in tabs]) + ']',23 'ARRAY[' + ','.join([qv(key) for key in keys]) + ']',24 )25def install_rights_sql():26 qv = lambda x : '\''+str(x)+'\''27 dirty = {}28 sql = """29CREATE OR REPLACE FUNCTION right_inheritance_update(_id INTEGER) RETURNS VOID AS $$30BEGIN31 PERFORM right_inheritance_clear(_id);32 CASE (SELECT model FROM core_entity WHERE id=_id)"""33 for model in sorted(registry.keys(), key=lambda m: m._meta.db_table):34 if issubclass(model, Entity):35 model_name = model._meta.app_label + '.' + model._meta.module_name36 sql += """37 WHEN {0} THEN """.format(qv(model_name))38 for right, list in model.inherit_rights().items():39 for pent, pright, cfield, cvalue in list:40 and_statement = ''41 if cfield and cvalue:42 dirty_model = model._meta.get_field(cfield).model43 dirty_column = model._meta.get_field(cfield).column44 if dirty_model not in dirty:45 dirty[dirty_model] = set()46 dirty[dirty_model].add(dirty_column)47 and_statement = """ AND v.{0}={1}""".format(dirty_column, qv(cvalue))48 if pent:49 dirty_model = model._meta.get_field(pent).model50 dirty_column = model._meta.get_field(pent).column51 if dirty_model not in dirty:52 dirty[dirty_model] = set()53 dirty[dirty_model].add(dirty_column)54 sql += """55 PERFORM right_inheritance_add(_id, {0}, (SELECT {1} FROM {2} v WHERE v.id=_id {3}), {4});""".format(56 qv(right),57 dirty_column,58 model._meta.db_table + '__view',59 and_statement,60 qv(pright))61 else:62 sql += """63 PERFORM right_inheritance_add(_id, {0}, (SELECT g.id FROM core_global__view g, {1} v WHERE v.id=_id {2}), {3});""".format(64 qv(right),65 model._meta.db_table + '__view',66 and_statement,67 qv(pright))68 sql += """69 END CASE;70END;71$$ LANGUAGE plpgsql;72"""73 ret = []74 ret.append(sql)75 for model in sorted(registry.keys(), key=lambda m: m._meta.db_table):76 if issubclass(model, Entity):77 model_name = model._meta.app_label + '.' + model._meta.module_name78 trig = 'CREATE OR REPLACE FUNCTION ' + model._meta.db_table + '__after_insert_update_rights_trigger() RETURNS TRIGGER AS $$ '79 trig += 'BEGIN IF (SELECT model FROM core_entity WHERE id=new.' + model._meta.pk.column + ') = \'' + model_name + '\' THEN '80 trig += 'PERFORM right_inheritance_update(new.' + model._meta.pk.column + ');'81 trig += 'END IF;'82 trig += 'RETURN new; END; $$ LANGUAGE plpgsql;'83 trig += 'CREATE TRIGGER ' + model._meta.db_table + '__after_insert_update_rights AFTER INSERT ON ' + model._meta.db_table + ' FOR EACH ROW EXECUTE PROCEDURE ' + model._meta.db_table + '__after_insert_update_rights_trigger();'84 ret.append(trig)85 for model in dirty:86 trig = 'CREATE OR REPLACE FUNCTION ' + model._meta.db_table + '__after_update_update_rights_trigger() RETURNS TRIGGER AS $$ '87 trig += 'BEGIN IF '88 trig += ' OR '.join([ 'old.' + x + ' <> ' + 'new.' + x for x in dirty[model]])89 trig += ' THEN PERFORM right_inheritance_update(new.' + model._meta.pk.column + '); END IF;'90 trig += 'RETURN new; END; $$ LANGUAGE plpgsql;'91 trig += 'CREATE TRIGGER ' + model._meta.db_table + '__after_update_update_rights AFTER UPDATE ON ' + model._meta.db_table + ' FOR EACH ROW EXECUTE PROCEDURE ' + model._meta.db_table + '__after_update_update_rights_trigger();'92 ret.append(trig)93 ret.append('SELECT right_inheritance_update(id) FROM core_entity;')94 return ret95def install_dbev_sql():96 set_user_id_function = """97CREATE OR REPLACE FUNCTION set_user_id(arg INTEGER) RETURNS VOID AS $$98BEGIN99 DROP TABLE IF EXISTS user_id;100 CREATE TEMPORARY TABLE user_id (id) AS VALUES (arg);101 DROP TABLE IF EXISTS user_roles;102 CREATE TEMPORARY TABLE user_roles (role_id) 103 AS SELECT keyid AS role_id104 FROM connectby('core_rolemapping'::text, 'parent_id'::text, 'child_id'::text, arg::text, 0)105 AS t(keyid INT, parent_keyid INT, level INT);106 DROP TABLE IF EXISTS user_privs;107 CREATE TEMPORARY TABLE user_privs ("right", entity_id)108 AS SELECT cp.right, cp.entity_id109 FROM core_privilege AS cp110 WHERE 111 (EXISTS (SELECT * FROM user_roles AS ur WHERE ur.role_id = cp.role_id))112 AND (coalesce(cp.start_on, NOW()) <= NOW())113 AND (coalesce(cp.finish_on, NOW()) >= NOW());114END;115$$ LANGUAGE plpgsql;116"""117 update_user_rights_function = """118CREATE OR REPLACE FUNCTION update_user_rights() RETURNS VOID AS $$119DECLARE120 user_id INTEGER; 121BEGIN122 user_id := get_user_id();123 IF NOT (user_id IS NULL) THEN124 PERFORM set_user_id(user_id);125 END IF;126END;127$$ LANGUAGE plpgsql;128"""129 get_user_id_function = """130CREATE OR REPLACE FUNCTION get_user_id() RETURNS INTEGER AS $$131BEGIN132 RETURN (SELECT id FROM user_id);133EXCEPTION134 WHEN undefined_table THEN135 RETURN NULL;136END;137$$ LANGUAGE plpgsql;138"""139 transaction_id_seq = """140DROP SEQUENCE IF EXISTS transaction_id_seq;141CREATE SEQUENCE transaction_id_seq;142"""143 get_transaction_id_function = """144CREATE OR REPLACE FUNCTION get_transaction_id() RETURNS INTEGER AS $$145DECLARE146 _xid TEXT;147 _id INTEGER;148BEGIN149 _xid := (SELECT virtualxid FROM pg_locks WHERE locktype='virtualxid' and pid=pg_backend_pid());150 _id := (SELECT id FROM transaction_id WHERE xid=_xid);151 IF _id IS NULL THEN152 _id := nextval('transaction_id_seq');153 DELETE FROM transaction_id;154 INSERT INTO transaction_id VALUES(_xid, _id);155 RETURN _id;156 END IF;157 RETURN _id;158EXCEPTION159 WHEN undefined_table THEN160 CREATE TEMPORARY TABLE transaction_id(xid TEXT, id INTEGER);161 _id := nextval('transaction_id_seq');162 INSERT INTO transaction_id VALUES(_xid, _id);163 RETURN _id;164END;165$$ LANGUAGE plpgsql;166"""167 insert_rawevent_trigger = """168CREATE OR REPLACE FUNCTION core_rawevent__before_insert_trigger() RETURNS TRIGGER AS $$169BEGIN170 new.transaction := get_transaction_id();171 NOTIFY satori;172 RETURN new;173END;174$$ LANGUAGE plpgsql;175CREATE TRIGGER core_rawevent__before_insert BEFORE INSERT ON core_rawevent FOR EACH ROW EXECUTE PROCEDURE core_rawevent__before_insert_trigger();176"""177 create_version_table_function = """178CREATE OR REPLACE FUNCTION create_version_table(_table TEXT, _key TEXT) RETURNS TEXT AS $$179DECLARE180 _exec TEXT := '';181 _texec TEXT := '';182 _vtable TEXT;183 _cols TEXT[];184 _rec RECORD;185 i INTEGER;186 j INTEGER;187BEGIN188 _exec := 'ALTER TABLE ' || quote_ident(_table) || ' ADD COLUMN _version_transaction integer NOT NULL DEFAULT get_transaction_id();';189 _texec := _texec || _exec || ';';190 EXECUTE _exec;191 _vtable := _table || '__versions';192 _exec := 'DROP TABLE IF EXISTS ' || quote_ident(_vtable);193 _texec := _texec || _exec || ';';194 EXECUTE _exec;195 _exec := 'CREATE TABLE ' || quote_ident(_vtable) || ' (';196 FOR _rec in (197 SELECT a.attname, format_type(a.atttypid, a.atttypmod) AS type from pg_class c LEFT JOIN pg_attribute a ON a.attrelid = c.oid198 WHERE c.relname=_table AND a.attnum > 0 ORDER BY a.attnum199 ) LOOP200 IF _rec.attname <> '_version_transaction' THEN201 _exec := _exec || quote_ident(_rec.attname) || ' ' || _rec.type;202 IF _rec.attname = _key THEN203 _exec := _exec || ' NOT NULL';204 END IF;205 _exec := _exec || ',';206 END IF;207 END LOOP;208 _exec := _exec || '_version_transaction integer NOT NULL,';209 _exec := _exec || '_version_prev integer,';210 _exec := _exec || '_version_next integer,';211 _exec := _exec || '_version_user integer,';212 _exec := _exec || '_version_date timestamp with time zone default now(),';213 _exec := _exec || 'PRIMARY KEY(' || quote_ident(_key) || ',_version_transaction)';214 _exec := _exec || ')';215 _texec := _texec || _exec || ';';216 EXECUTE _exec;217 RETURN _texec;218END;219$$ LANGUAGE plpgsql;220"""221 create_full_view_function = """222CREATE OR REPLACE FUNCTION create_full_view(_tables TEXT[], _keys TEXT[]) RETURNS TEXT AS $$223DECLARE224 _exec TEXT := '';225 _name TEXT;226 _cols TEXT[];227 _vers TEXT[];228 _rec RECORD;229 i INTEGER;230 j INTEGER;231BEGIN232 i := array_lower(_tables, 1);233 _name := quote_ident(_tables[i] || '__view');234 _exec := 'CREATE OR REPLACE VIEW ' || quote_ident(_name) || ' AS SELECT ';235 _exec := _exec || (quote_ident('t' || i) || '.' || quote_ident(_keys[i])) || ' AS id, ';236 FOR i IN array_lower(_tables, 1)..array_upper(_tables, 1) LOOP237 FOR _rec in (238 SELECT a.attname from pg_class c LEFT JOIN pg_attribute a ON a.attrelid = c.oid WHERE c.relname=_tables[i] AND a.attnum > 0 ORDER BY a.attnum239 ) LOOP240 IF _rec.attname = '_version_transaction' THEN241 _vers := _vers || (quote_ident('t' || i) || '.' || quote_ident(_rec.attname));242 ELSIF _rec.attname <> _keys[i] THEN243 _cols := _cols || (quote_ident('t' || i) || '.' || quote_ident(_rec.attname));244 END IF;245 END LOOP;246 END LOOP;247 _exec := _exec || array_to_string(_cols, ', ');248 IF _vers <> ARRAY[]::TEXT[] THEN249 _exec := _exec || ', GREATEST(' || array_to_string(_vers, ', ') || ') AS _version_transaction';250 END IF;251 _exec := _exec || ' FROM ';252 j := array_lower(_tables, 1);253 _exec := _exec || quote_ident(_tables[j]) || ' ' || quote_ident('t' || j);254 FOR i IN (j+1)..array_upper(_tables, 1) LOOP255 _exec := _exec || ' LEFT JOIN ' || quote_ident(_tables[i]) || ' ' || quote_ident('t' || i);256 _exec := _exec || ' ON ' || quote_ident('t' || j) || '.' || quote_ident(_keys[j]) || ' = ' || quote_ident('t' || i) || '.' || quote_ident(_keys[i]);257 END LOOP;258 EXECUTE _exec;259 RETURN _exec;260END;261$$ LANGUAGE plpgsql;262"""263 create_version_function_function = """264CREATE OR REPLACE FUNCTION create_version_function(_tables TEXT[], _keys TEXT[]) RETURNS TEXT AS $$265DECLARE266 _exec TEXT := '';267 _texec TEXT := '';268 _name TEXT;269 _vtables TEXT[];270 _cols TEXT[];271 _vers TEXT[];272 _rec RECORD;273 i INTEGER;274 j INTEGER;275BEGIN276 i := array_lower(_tables, 1);277 _name := quote_ident(_tables[i] || '__version_view');278 FOR i IN array_lower(_tables, 1)..array_upper(_tables, 1) LOOP279 SELECT INTO j COUNT(*) FROM pg_class c LEFT JOIN pg_attribute a ON a.attrelid = c.oid280 WHERE c.relname=_tables[i] AND a.attnum > 0 AND a.attname = '_version_transaction';281 IF j > 0 THEN282 _exec := 'CREATE OR REPLACE FUNCTION ' || quote_ident(_tables[i] || '__version_id') || '(_id INTEGER, _ver INTEGER) RETURNS INTEGER AS ''';283 _exec := _exec || 'DECLARE _v INTEGER;';284 _exec := _exec || 'BEGIN SELECT INTO _v MAX(_version_transaction) FROM ' || quote_ident(_tables[i]) || '__versions';285 _exec := _exec || ' WHERE ' || quote_ident(_keys[i]) || '=_id AND _version_transaction<=_ver AND _version_next IS NULL OR _version_next>_ver;';286 _exec := _exec || 'RETURN _v; END; '' LANGUAGE plpgsql;';287 _texec := _texec || _exec || ';';288 EXECUTE _exec;289 _vtables := _vtables || (quote_ident(_tables[i] || '__versions'));290 ELSE291 _vtables := _vtables || (quote_ident(_tables[i]));292 END IF;293 END LOOP;294 _exec := 'CREATE OR REPLACE FUNCTION ' || quote_ident(_name) || '(_id INTEGER, _ver INTEGER) RETURNS TABLE(id integer';295 FOR i IN array_lower(_tables, 1)..array_upper(_tables, 1) LOOP296 FOR _rec in (297 SELECT a.attname, format_type(a.atttypid, a.atttypmod) AS type from pg_class c LEFT JOIN pg_attribute a ON a.attrelid = c.oid298 WHERE c.relname=_tables[i] AND a.attnum > 0 ORDER BY a.attnum299 ) LOOP300 IF _rec.attname <> '_version_transaction' AND _rec.attname <> _keys[i] THEN301 _exec := _exec || ',' || quote_ident(_rec.attname) || ' ' || _rec.type;302 END IF;303 END LOOP;304 END LOOP;305 _exec := _exec || ', _version_transaction integer) AS ''';306 _exec := _exec || 'BEGIN RETURN QUERY SELECT ';307 i := array_lower(_tables, 1);308 _exec := _exec || (quote_ident('t' || i) || '.' || quote_ident(_keys[i])) || ', ';309 FOR i IN array_lower(_tables, 1)..array_upper(_tables, 1) LOOP310 FOR _rec in (311 SELECT a.attname from pg_class c LEFT JOIN pg_attribute a ON a.attrelid = c.oid WHERE c.relname=_tables[i] AND a.attnum > 0 ORDER BY a.attnum312 ) LOOP313 IF _rec.attname = '_version_transaction' THEN314 _vers := _vers || (quote_ident('t' || i) || '.' || quote_ident(_rec.attname));315 ELSIF _rec.attname <> _keys[i] THEN316 _cols := _cols || (quote_ident('t' || i) || '.' || quote_ident(_rec.attname));317 END IF;318 END LOOP;319 END LOOP;320 _exec := _exec || array_to_string(_cols, ', ');321 IF _vers <> ARRAY[]::TEXT[] THEN322 _exec := _exec || ', GREATEST(' || array_to_string(_vers, ', ') || ')';323 END IF;324 _exec := _exec || ' FROM ';325 j := array_lower(_tables, 1);326 _exec := _exec || _vtables[j] || ' ' || quote_ident('t' || j);327 FOR i IN (j+1)..array_upper(_tables, 1) LOOP328 _exec := _exec || ' LEFT JOIN ' || _vtables[i] || ' ' || quote_ident('t' || i);329 _exec := _exec || ' ON ' || quote_ident('t' || j) || '.' || quote_ident(_keys[j]) || ' = ' || quote_ident('t' || i) || '.' || quote_ident(_keys[i]);330 END LOOP;331 _exec := _exec || ' WHERE ' || quote_ident('t' || j) || '.' || quote_ident(_keys[j]) || ' = _id';332 FOR i IN array_lower(_tables, 1)..array_upper(_tables, 1) LOOP333 SELECT INTO j COUNT(*) FROM pg_class c LEFT JOIN pg_attribute a ON a.attrelid = c.oid334 WHERE c.relname=_tables[i] AND a.attnum > 0 AND a.attname = '_version_transaction';335 IF j > 0 THEN336 _exec := _exec || ' AND ' || quote_ident('t' || i) || '._version_transaction = ' || quote_ident(_tables[i] || '__version_id') || '(_id,_ver)';337 END IF;338 END LOOP;339 _exec := _exec || '; END; '' LANGUAGE plpgsql;';340 _texec := _texec || _exec || ';';341 EXECUTE _exec;342 RETURN _texec;343END;344$$ LANGUAGE plpgsql;345"""346 create_triggers_function = """347CREATE OR REPLACE FUNCTION create_triggers(_table TEXT, _key TEXT, _notify TEXT) RETURNS TEXT AS $$348DECLARE349 _exec TEXT := '';350 _texec TEXT := '';351 _rec RECORD;352BEGIN353 _exec := 'CREATE OR REPLACE FUNCTION ' || quote_ident(_table || '__on_after_insert') || '() RETURNS TRIGGER AS $' || '$';354 _exec := _exec || 'BEGIN INSERT INTO ' || quote_ident(_table || '__versions') || '(';355 FOR _rec in (356 SELECT a.attname from pg_class c LEFT JOIN pg_attribute a ON a.attrelid = c.oid WHERE c.relname=_table AND a.attnum > 0 ORDER BY a.attnum357 ) LOOP358 IF _rec.attname <> '_version_transaction' THEN359 _exec := _exec || quote_ident(_rec.attname) || ', ';360 END IF;361 END LOOP;362 _exec := _exec || '_version_transaction, _version_prev, _version_user';363 _exec := _exec || ') VALUES(';364 FOR _rec in (365 SELECT a.attname from pg_class c LEFT JOIN pg_attribute a ON a.attrelid = c.oid WHERE c.relname=_table AND a.attnum > 0 ORDER BY a.attnum366 ) LOOP367 IF _rec.attname <> '_version_transaction' THEN368 _exec := _exec || 'new.' || quote_ident(_rec.attname) || ', ';369 END IF;370 END LOOP;371 _exec := _exec || 'get_transaction_id(), get_transaction_id(), get_user_id());';372 _exec := _exec || 'INSERT INTO core_notification(action, "table", object, transaction, previous, "user") VALUES(''I'', ''' || _table || ''', new.' || quote_ident(_key);373 _exec := _exec || ', get_transaction_id(), NULL, get_user_id());';374 _exec := _exec || 'NOTIFY ' || quote_ident(_notify) || ';';375 _exec := _exec || 'RETURN new; END; $' || '$ LANGUAGE plpgsql;';376 _texec := _texec || _exec || ';';377 EXECUTE _exec;378 _exec := 'CREATE TRIGGER ' || quote_ident(_table || '__after_insert') || ' AFTER INSERT ON ';379 _exec := _exec || quote_ident(_table) || ' FOR EACH ROW EXECUTE PROCEDURE ' || quote_ident(_table || '__on_after_insert') || '();';380 _texec := _texec || _exec || ';';381 EXECUTE _exec;382 _exec := 'CREATE OR REPLACE FUNCTION ' || quote_ident(_table || '__on_before_update') || '() RETURNS TRIGGER AS $' || '$';383 _exec := _exec || 'BEGIN IF old <> new THEN new._version_transaction = get_transaction_id(); END IF; RETURN new; END; $' || '$ LANGUAGE plpgsql;';384 _texec := _texec || _exec || ';';385 EXECUTE _exec;386 _exec := 'CREATE TRIGGER ' || quote_ident(_table || '__before_update') || ' BEFORE UPDATE ON ';387 _exec := _exec || quote_ident(_table) || ' FOR EACH ROW EXECUTE PROCEDURE ' || quote_ident(_table || '__on_before_update') || '();';388 _texec := _texec || _exec || ';';389 EXECUTE _exec;390 _exec := 'CREATE OR REPLACE FUNCTION ' || quote_ident(_table || '__on_after_update') || '() RETURNS TRIGGER AS $' || '$';391 _exec := _exec || 'BEGIN IF old = new THEN return new; END IF;';392 _exec := _exec || 'UPDATE ' || quote_ident(_table || '__versions') || ' SET _version_next = get_transaction_id() WHERE ';393 _exec := _exec || quote_ident(_key) || ' = new.' || quote_ident(_key) || ' AND _version_next IS NULL;';394 _exec := _exec || 'DELETE FROM ' || quote_ident(_table || '__versions') || ' WHERE _version_transaction = get_transaction_id() AND ';395 _exec := _exec || quote_ident(_key) || ' = new.' || quote_ident(_key) || ';';396 _exec := _exec || 'INSERT INTO ' || quote_ident(_table || '__versions') || '(';397 FOR _rec in (398 SELECT a.attname from pg_class c LEFT JOIN pg_attribute a ON a.attrelid = c.oid WHERE c.relname=_table AND a.attnum > 0 ORDER BY a.attnum399 ) LOOP400 IF _rec.attname <> '_version_transaction' THEN401 _exec := _exec || quote_ident(_rec.attname) || ', ';402 END IF;403 END LOOP;404 _exec := _exec || '_version_transaction, _version_prev, _version_user';405 _exec := _exec || ') VALUES(';406 FOR _rec in (407 SELECT a.attname from pg_class c LEFT JOIN pg_attribute a ON a.attrelid = c.oid WHERE c.relname=_table AND a.attnum > 0 ORDER BY a.attnum408 ) LOOP409 IF _rec.attname <> '_version_transaction' THEN410 _exec := _exec || 'new.' || quote_ident(_rec.attname) || ', ';411 END IF;412 END LOOP;413 _exec := _exec || 'get_transaction_id(), old._version_transaction, get_user_id());';414 _exec := _exec || 'UPDATE core_notification SET "user"=get_user_id() WHERE "table"=''' || _table || ''' AND object=new.' || quote_ident(_key);415 _exec := _exec || ' AND transaction=get_transaction_id(); IF NOT found THEN ';416 _exec := _exec || 'INSERT INTO core_notification(action, "table", object, transaction, previous, "user") VALUES(''U'', ''' || _table || ''', new.' || quote_ident(_key);417 _exec := _exec || ', get_transaction_id(), old._version_transaction, get_user_id()); END IF;';418 _exec := _exec || 'NOTIFY ' || quote_ident(_notify) || ';';419 _exec := _exec || 'RETURN new; END; $' || '$ LANGUAGE plpgsql;';420 _texec := _texec || _exec || ';';421 EXECUTE _exec;422 _exec := 'CREATE TRIGGER ' || quote_ident(_table || '__after_update') || ' AFTER UPDATE ON ';423 _exec := _exec || quote_ident(_table) || ' FOR EACH ROW EXECUTE PROCEDURE ' || quote_ident(_table || '__on_after_update') || '();';424 _texec := _texec || _exec || ';';425 EXECUTE _exec;426 _exec := 'CREATE OR REPLACE FUNCTION ' || quote_ident(_table || '__on_after_delete') || '() RETURNS TRIGGER AS $' || '$';427 _exec := _exec || 'BEGIN UPDATE ' || quote_ident(_table || '__versions') || ' SET _version_next = get_transaction_id() WHERE ';428 _exec := _exec || quote_ident(_key) || ' = old.' || quote_ident(_key) || ' AND _version_next IS NULL;';429 _exec := _exec || 'DELETE FROM ' || quote_ident(_table || '__versions') || ' WHERE _version_transaction = get_transaction_id() AND ';430 _exec := _exec || quote_ident(_key) || ' = old.' || quote_ident(_key) || ';';431 _exec := _exec || 'UPDATE core_notification SET action=''D'', "user"=get_user_id() WHERE "table"=''' || _table || ''' AND object=old.' || quote_ident(_key);432 _exec := _exec || ' AND transaction=get_transaction_id(); IF NOT found THEN ';433 _exec := _exec || 'INSERT INTO core_notification(action, "table", object, transaction, previous, "user") VALUES(''D'', ''' || _table || ''', old.' || quote_ident(_key);434 _exec := _exec || ', get_transaction_id(), old._version_transaction, get_user_id()); END IF;';435 _exec := _exec || 'NOTIFY ' || quote_ident(_notify) || ';';436 _exec := _exec || 'RETURN old; END; $' || '$ LANGUAGE plpgsql;';437 _texec := _texec || _exec || ';';438 EXECUTE _exec;439 _exec := 'CREATE TRIGGER ' || quote_ident(_table || '__after_delete') || ' AFTER DELETE ON ';440 _exec := _exec || quote_ident(_table) || ' FOR EACH ROW EXECUTE PROCEDURE ' || quote_ident(_table || '__on_after_delete') || '();';441 _texec := _texec || _exec || ';';442 EXECUTE _exec;443 RETURN _texec;444END;445$$ LANGUAGE plpgsql;446"""447 install_rights_inheritance = """448DROP TABLE IF EXISTS "right_dict";449CREATE TABLE "right_dict" (450 "id" serial NOT NULL PRIMARY KEY,451 "name" VARCHAR(32),452 UNIQUE("name")453);454CREATE OR REPLACE FUNCTION get_right_by_name(_right_name TEXT) RETURNS INT AS $$455DECLARE456 _ret INT := NULL;457BEGIN458 _ret := (SELECT "id" FROM "right_dict" WHERE "name" = _right_name);459 IF _ret IS NULL THEN460 INSERT INTO right_dict("name") VALUES(_right_name);461 _ret := (SELECT "id" FROM "right_dict" WHERE "name" = _right_name);462 END IF;463 RETURN _ret;464END;465$$ LANGUAGE plpgsql;466CREATE TYPE "right_pair" AS (467 "entity_id" INT,468 "right_id" INT469);470DROP TABLE IF EXISTS "right_inheritance";471CREATE TABLE "right_inheritance" (472 "parent" INT NOT NULL,473 "child" INT NOT NULL,474 "child_entity_id" INT NOT NULL475);476CREATE INDEX "right_inheritance_child_idx" ON "right_inheritance" ( "child" );477CREATE INDEX "right_inheritance_child_entity_id_idx" ON "right_inheritance" ( "child_entity_id" );478CREATE OR REPLACE FUNCTION right_inheritance_clear(_entity_id INT) RETURNS VOID AS $$479BEGIN480 DELETE FROM "right_inheritance" WHERE "child_entity_id" = _entity_id;481END;482$$ LANGUAGE plpgsql;483CREATE OR REPLACE FUNCTION right_inheritance_add(_child_entity_id INT, _child_right_name TEXT, _parent_entity_id INT, _parent_right_name TEXT) RETURNS VOID AS $$484BEGIN485 IF _child_entity_id IS NULL OR _child_right_name IS NULL OR _parent_entity_id IS NULL OR _parent_right_name IS NULL THEN486 RETURN;487 END IF;488 INSERT INTO "right_inheritance"("parent", "child", "child_entity_id") VALUES (489 _parent_entity_id*256 + (SELECT get_right_by_name(_parent_right_name)),490 _child_entity_id*256 + (SELECT get_right_by_name(_child_right_name)),491 _child_entity_id);492END;493$$ LANGUAGE plpgsql;494CREATE OR REPLACE FUNCTION right_check(_entity_id INT, _right_name TEXT) RETURNS BOOLEAN AS $$495DECLARE result BOOLEAN;496BEGIN497 SELECT EXISTS498 (SELECT * FROM499 user_roles500 JOIN core_privilege ON user_roles.role_id = core_privilege.role_id501 JOIN right_dict ON core_privilege.right = right_dict.name502 JOIN503 (SELECT keyid/256 as entity, keyid%256 as right 504 FROM connectby('right_inheritance'::text, 'parent'::text, 'child'::text, (_entity_id*256 + (SELECT get_right_by_name(_right_name)))::text, 0)505 AS t(keyid int, parent_keyid int, level int)506 ) AS inherited ON inherited.right = right_dict.id AND core_privilege.entity_id = inherited.entity507 WHERE coalesce(core_privilege.start_on, NOW()) <= NOW() AND coalesce(core_privilege.finish_on, NOW()) >= NOW()508 )509 INTO result;510 RETURN result;511EXCEPTION512 WHEN undefined_table THEN513 RETURN FALSE;514END;515$$ LANGUAGE plpgsql;516"""517 install_versions_function = """518CREATE OR REPLACE FUNCTION install_versions(_table TEXT, _key TEXT, _tables TEXT[], _keys TEXT[], _notify TEXT) RETURNS TEXT AS $$519DECLARE520 _texec TEXT := '';521 _exec TEXT := '';522BEGIN523 SELECT INTO _exec create_version_table(_table, _key);524 _texec := _texec || _exec;525 SELECT INTO _exec create_full_view(_tables, _keys);526 _texec := _texec || _exec;527 SELECT INTO _exec create_version_function(_tables, _keys);528 _texec := _texec || _exec;529 RETURN _texec;530END;531$$ LANGUAGE plpgsql;532"""533# SELECT INTO _exec create_triggers(_table, _key, _notify);534# _texec := _texec || _exec;535 ret = [set_user_id_function, update_user_rights_function, get_user_id_function, transaction_id_seq, get_transaction_id_function, insert_rawevent_trigger, create_version_table_function, create_full_view_function, create_version_function_function, create_triggers_function, install_versions_function, install_rights_inheritance]536 for model in sorted(registry.keys(), key=lambda m: m._meta.db_table):537 ret.append(install_versions_sql(model))...
test_commands.py
Source:test_commands.py
...9 self.assertFalse(FindOccurrences.modifying)10class FileHandlingCommandsTest(TestCaseCommandTest):11 def test_file_saving(self):12 self._file_saved = False13 self._exec(SaveFile())14 assert_true(self._file_saved)15 assert_false(self._ctrl.datafile_controller.dirty)16 def test_file_saving_purifies(self):17 self._add_empty_step_to_macro()18 other_name = self._ctrl.name + 'foo'19 self._copy_macro_as(other_name)20 self._exec(SaveFile())21 assert_equals(len(self._ctrl.steps), self._orig_number_of_steps+1)22 other = self._get_macro_by_name(other_name)23 assert_equals(len(other.steps), self._orig_number_of_steps+1)24 def test_undo_after_file_save_does_not_break(self):25 self._exec(SaveFile())26 self._exec(Undo())27 def _add_empty_step_to_macro(self):28 self._exec(ChangeCellValue(self._orig_number_of_steps+1, 10, 'A'))29 self._verify_step_number_change(2)30 def _copy_macro_as(self, name):31 self._exec(CopyMacroAs(name))32class MacroCopyingTest(TestCaseCommandTest):33 def test_copy_macro(self):34 new_name = self._ctrl.name + '1'35 original_macro_number = len(self._get_macros())36 self._exec(CopyMacroAs(new_name))37 macro_names = [m.name for m in self._get_macros()]38 assert_true(self._ctrl.name in macro_names)39 assert_true(new_name in macro_names)40 assert_equals(len(macro_names), original_macro_number+1)41 assert_equals(len(self._get_macro_by_name(new_name).steps), 42 len(self._ctrl.steps))43 def test_copy_does_not_change_original(self):44 new_name = self._ctrl.name + '2'45 self._exec(CopyMacroAs(new_name))46 copy = self._get_copy(new_name)47 copy.execute(ChangeCellValue(0, 0, 'Changed Step'))48 assert_equals(self._ctrl.steps[0].keyword, STEP1_KEYWORD)49 assert_equals(copy.steps[0].keyword, 'Changed Step')50 def _get_copy(self,name):51 copies = [m for m in self._get_macros() if m.name == name]52 assert_equals(len(copies), 1)53 return copies[0]54 def test_copy_macro_inherits_default_tag(self):55 suite = self._ctrl.datafile_controller56 tag_name = 'konsukiepre'57 suite.default_tags.add(DefaultTag(tag_name))58 assert_true(any(True for tag in self._ctrl.tags if tag.name == tag_name))59 new_name = self._ctrl.name + '3'60 self._exec(CopyMacroAs(new_name))61 assert_true(any(True for tag in self._get_copy(new_name).tags if tag.name == tag_name))62class TestCaseEditingTest(TestCaseCommandTest):63 def test_changing_one_cell(self):64 self._exec(ChangeCellValue(0, 0, 'Changed Step'))65 assert_equals(self._steps[0].keyword, 'Changed Step')66 def test_changing_first_cell_in_for_loop_step(self):67 step_index = self._data_row(FOR_LOOP_STEP1)68 value = 'Foo'69 self._exec(ChangeCellValue(step_index, 0, value))70 assert_equals(self._steps[step_index].as_list()[0], value)71 def test_empty_second_cell_in_for_loop_step(self):72 step_index = self._data_row(FOR_LOOP_STEP1)73 value = ''74 self._exec(ChangeCellValue(step_index, 1, value))75 assert_equals(self._steps[step_index].as_list()[1], value)76 def test_undo_redo(self):77 original_cell_value = self._data_step_as_list(STEP1)[0]78 changed_cell_value = 'Changed Step'79 self._exec(ChangeCellValue(0, 0, changed_cell_value))80 assert_equals(self._steps[0].keyword, changed_cell_value)81 self._exec(Undo())82 assert_equals(self._steps[0].keyword, original_cell_value)83 self._exec(Redo())84 assert_equals(self._steps[0].keyword, changed_cell_value)85 def test_undo_when_nothing_to_undo(self):86 self._exec(Undo())87 assert_equals(self._number_of_test_changes, 0)88 def test_redo_when_nothing_to_redo(self):89 self._exec(Redo())90 assert_equals(self._number_of_test_changes, 0)91 def test_undo_undo_redo_redo(self):92 original_cell_value = self._data_step_as_list(STEP1)[0]93 changed_cell_value_1 = 'Changed Step'94 changed_cell_value_2 = 'Again changed Step'95 self._exec(ChangeCellValue(0, 0, changed_cell_value_1))96 assert_equals(self._steps[0].keyword, changed_cell_value_1)97 self._exec(ChangeCellValue(0, 0, changed_cell_value_2))98 assert_equals(self._steps[0].keyword, changed_cell_value_2)99 self._exec(Undo())100 assert_equals(self._steps[0].keyword, changed_cell_value_1)101 self._exec(Undo())102 assert_equals(self._steps[0].keyword, original_cell_value)103 self._exec(Redo())104 assert_equals(self._steps[0].keyword, changed_cell_value_1)105 self._exec(Redo())106 assert_equals(self._steps[0].keyword, changed_cell_value_2)107 def test_redo_does_nothing_after_state_changing_command_that_is_not_undo(self):108 changed_cell_value_1 = 'Changed Step'109 changed_cell_value_2 = 'Changed Step again'110 self._exec(ChangeCellValue(0, 0, changed_cell_value_1))111 self._exec(Undo())112 self._exec(ChangeCellValue(0, 0, changed_cell_value_2))113 self._exec(Redo())114 assert_equals(self._steps[0].keyword, changed_cell_value_2)115 def test_changing_cell_value_after_last_column_adds_empty_columns(self):116 self._exec(ChangeCellValue(0, 2, 'Hello'))117 assert_equals(self._steps[0].args, ['arg', 'Hello'])118 def test_changing_cell_value_after_last_row_adds_empty_rows(self):119 self._exec(ChangeCellValue(len(self._data)+5, 0, 'Hello'))120 assert_equals(self._steps[len(self._data)+5].keyword, 'Hello')121 def test_changing_for_loop_header_value(self):122 self._exec(ChangeCellValue(self._data_row(FOR_LOOP_HEADER), 0, 'Keyword'))123 assert_equals(self._steps[self._data_row(FOR_LOOP_HEADER)].as_list(),124 ['Keyword'] + self._data_step_as_list(FOR_LOOP_HEADER)[1:])125 self._verify_step_unchanged(FOR_LOOP_STEP1)126 assert_equals(len(self._steps), self._orig_number_of_steps)127 def test_changing_for_loop_header_argument(self):128 self._exec(ChangeCellValue(self._data_row(FOR_LOOP_HEADER), 1, 'Keyword'))129 assert_equals(self._steps[self._data_row(FOR_LOOP_HEADER)].as_list(),130 [': FOR', 'Keyword'] + self._data_step_as_list(FOR_LOOP_HEADER)[2:])131 self._verify_step_unchanged(FOR_LOOP_STEP1)132 assert_equals(len(self._steps), self._orig_number_of_steps)133 def test_changing_for_loop_header_in_clause(self):134 self._exec(ChangeCellValue(self._data_row(FOR_LOOP_HEADER), 2, 'Keyword'))135 assert_equals(self._steps[self._data_row(FOR_LOOP_HEADER)].as_list(),136 [': FOR', '${i}', 'Keyword'] + self._data_step_as_list(FOR_LOOP_HEADER)[3:])137 assert_equals(self._steps[self._data_row(FOR_LOOP_STEP1)].as_list(),138 self._data_step_as_list(FOR_LOOP_STEP1))139 assert_equals(len(self._steps), self._orig_number_of_steps)140 def test_deleting_row(self):141 self._exec(DeleteRow(0))142 self._verify_step_number_change(-1)143 self._verify_row_does_not_exist(STEP1)144 def test_undoing_row_delete(self):145 self._exec(DeleteRow(0))146 self._exec(Undo())147 assert_equals(len(self._steps), self._orig_number_of_steps)148 self._verify_step(0, 'Step 1', ['arg'])149 def test_delete_row_inside_of_for_loop(self):150 self._exec(DeleteRow(self._data_row(FOR_LOOP_STEP1)))151 assert_equals(len(self._steps), self._orig_number_of_steps-1)152 self._verify_row_does_not_exist(FOR_LOOP_STEP1)153 def test_delete_for_loop_header_row(self):154 self._exec(DeleteRow(self._data_row(FOR_LOOP_HEADER)))155 assert_equals(len(self._steps), self._orig_number_of_steps-1)156 self._verify_row_does_not_exist(FOR_LOOP_HEADER)157 def test_adding_row_last(self):158 self._exec(AddRow(-1))159 assert_equals(len(self._steps), self._orig_number_of_steps+1)160 assert_equals(self._steps[self._orig_number_of_steps].as_list(), [])161 def test_adding_row_first(self):162 self._exec(AddRow(0))163 assert_equals(len(self._steps), self._orig_number_of_steps+1)164 assert_equals(self._steps[0].as_list(), [])165 def test_adding_row_middle(self):166 self._exec(AddRow(1))167 assert_equals(len(self._steps), self._orig_number_of_steps+1)168 assert_equals(self._steps[1].as_list(), [])169 def test_adding_row_in_for_loop_body(self):170 row_in_for_loop = self._data_row(FOR_LOOP_STEP2)171 self._exec(AddRow(row_in_for_loop))172 assert_equals(len(self._steps), self._orig_number_of_steps+1)173 assert_equals(self._steps[row_in_for_loop].as_list(), [''])174 def test_inserting_cell_when_for_loop_is_last(self):175 row_after_for_loop = self._data_row(STEP_AFTER_FOR_LOOP)176 self._exec(DeleteRow(row_after_for_loop))177 self._exec(DeleteRow(row_after_for_loop))178 assert_equals(self._steps[-1].as_list(), ['', 'No Operation'])179 self._exec(InsertCell(0,0))180 self._verify_step(0, '', ['Step 1', 'arg'])181 def test_add_multiple_rows(self):182 self._exec(AddRows([1,2]))183 self._verify_step_number_change(2)184 self._verify_step(0, 'Step 1', ['arg'])185 self._verify_step_is_empty(1)186 self._verify_step_is_empty(2)187 self._verify_step(3, 'Step 2', ['a1', 'a2', 'a3'])188 def test_undo_multiple_rows_add(self):189 self._exec(AddRows([3,2,1,4,5,6,9,8,7,10]))190 self._exec(Undo())191 self._verify_step(0, 'Step 1', ['arg'])192 self._verify_step(1, 'Step 2', ['a1', 'a2', 'a3'])193 def test_purify_removes_empty_rows(self):194 self._exec(AddRow(-1))195 self._exec(AddRow(1))196 self._exec(AddRow(2))197 assert_equals(len(self._steps), self._orig_number_of_steps+3)198 self._exec(Purify())199 assert_equals(len(self._steps), self._orig_number_of_steps)200 def test_purify_can_be_undone(self):201 self._exec(AddRow(1))202 self._exec(AddRow(2))203 assert_equals(len(self._steps), self._orig_number_of_steps+2)204 self._exec(Purify())205 assert_equals(len(self._steps), self._orig_number_of_steps)206 self._exec(Undo())207 assert_equals(len(self._steps), self._orig_number_of_steps+2)208 def test_purify_removes_rows_with_no_data(self):209 self._exec(ChangeCellValue(0,0, ''))210 self._exec(ChangeCellValue(0,1, ''))211 self._exec(Purify())212 assert_equals(len(self._steps), self._orig_number_of_steps-1)213 def test_can_add_values_to_empty_row(self):214 self._exec(AddRow(-1))215 self._exec(ChangeCellValue(0, 3, 'HELLO'))216 assert_equals(self._steps[0].args, ['arg', '', 'HELLO'])217 def test_only_comment_is_left(self):218 index = self._data_row(STEP_WITH_COMMENT)219 self._exec(ChangeCellValue(index, 0, ''))220 self._exec(Purify())221 assert_equals(self._steps[index].as_list(), ['# this is a comment'])222 def test_comment_is_changed(self):223 index = self._data_row(STEP_WITH_COMMENT)224 self._exec(ChangeCellValue(index, 1, '# new comment'))225 self._verify_step(index, 'Foo', [], '# new comment')226 def test_cell_value_after_comment_is_changed(self):227 index = self._data_row(STEP_WITH_COMMENT)228 self._exec(ChangeCellValue(index, 2, 'something'))229 assert_equals(self._steps[index].as_list(), ['Foo', '# this is a comment', 'something'])230 def test_change_keyword_value_in_indented_step(self):231 index = self._data_row(FOR_LOOP_STEP1)232 self._exec(ChangeCellValue(index, 1, 'Blog'))233 assert_equals(self._steps[index].keyword, 'Blog')234 assert_equals(len(self._steps), self._orig_number_of_steps)235 def test_delete_multiple_rows(self):236 self._exec(DeleteRows([2,0]))237 assert_equals(len(self._steps), self._orig_number_of_steps-2)238 self._verify_row_does_not_exist(STEP1)239 self._verify_row_does_not_exist(STEP_WITH_COMMENT)240 self._verify_number_of_test_changes(1)241 def test_deleting_rows_below_existing_steps_should_do_nothing(self):242 self._exec(DeleteRows([1000, 960]))243 self._verify_number_of_test_changes(0)244 def test_inserting_rows_below_existing_steps_should_do_nothing(self):245 self._exec(AddRows([1001, 1002]))246 self._verify_number_of_test_changes(0)247 def test_clear_area(self):248 self._exec(ClearArea((0,1), (1,2)))249 self._verify_step(0, 'Step 1')250 self._verify_step(1, 'Step 2', ['', '', 'a3'])251 def test_paste_area(self):252 self._exec(PasteArea((0, 0), [['Changed Step 1', '', ''],253 ['Changed Step 2', '', 'ca2']]))254 self._verify_step(0, 'Changed Step 1')255 self._verify_step(1, 'Changed Step 2', ['', 'ca2', 'a3'])256 def test_paste_area_different_length_rows(self):257 self._exec(PasteArea((0, 0), [['Changed Step 1', '', '', '', '\t'],258 ['Changed Step 2', '', 'ca2']]))259 self._verify_step(0, 'Changed Step 1')260 self._verify_step(1, 'Changed Step 2', ['', 'ca2', 'a3'])261 def test_insert_area_inserts_cells_before_selected_cell(self):262 self._exec(InsertArea((0, 0), [['Changed Step 1', '', ''],263 ['Changed Step 2', '', 'ca2']]))264 self._verify_step(0, 'Changed Step 1')265 self._verify_step(1, 'Changed Step 2', ['', 'ca2'])266 self._verify_step(2, 'Step 1', ['arg'])267 def test_insert_area_inserts_cells_before_selected_cell_different_length_rows(self):268 self._exec(InsertArea((0, 0), [['Changed Step 1', '', '', '\t'],269 ['Changed Step 2', '', 'ca2']]))270 self._verify_step(0, 'Changed Step 1')271 self._verify_step(1, 'Changed Step 2', ['', 'ca2'])272 self._verify_step(2, 'Step 1', ['arg'])273 def test_insert_cell(self):274 self._exec(InsertCells((0,1), (0,1)))275 self._verify_step(0, 'Step 1', ['', 'arg'])276 def test_inserting_cells_outside_step(self):277 self._exec(InsertCells((0,10), (0,10)))278 self._verify_step(0, 'Step 1', ['arg'])279 def test_insert_cell_before_comment(self):280 self._exec(InsertCells((2,1), (2,1)))281 self._verify_step(2, 'Foo', [''], exp_comment='# this is a comment')282 def test_inserting_many_cells(self):283 self._exec(InsertCells((0,1), (1,2)))284 self._verify_step(0, 'Step 1', ['', '', 'arg'])285 self._verify_step(1, 'Step 2', ['', '', 'a1', 'a2', 'a3'])286 def test_delete_many_cells(self):287 self._exec(DeleteCells((0,1), (1,2)))288 self._verify_step(0, 'Step 1', [])289 self._verify_step(1, 'Step 2', ['a3'])290 def test_delete_cells_in_for_loop_and_undo(self):291 start_row = self._data_row(FOR_LOOP_STEP1)292 end_row = self._data_row(FOR_LOOP_STEP2)293 self._exec(DeleteCells((start_row, 1), (end_row, 10)))294 assert_equals(self._steps[start_row].as_list(), [''])295 assert_equals(self._steps[end_row].as_list(), [''])296 self._exec(Undo())297 self._verify_steps_unchanged(FOR_LOOP_STEP1, FOR_LOOP_STEP2)298 def test_commenting(self):299 self._exec(CommentRows([0]))300 self._verify_step(0, 'Comment', ['Step 1', 'arg'])301 def test_commenting_many_rows(self):302 self._exec(CommentRows([1,2,3,4]))303 for row_data in [STEP2, STEP_WITH_COMMENT, FOR_LOOP_HEADER, FOR_LOOP_STEP1]:304 assert_equals(self._steps[self._data_row(row_data)].as_list(),305 ['Comment'] + self._data_step_as_list(row_data))306 def test_commenting_step_in_for_loop(self):307 row = self._data_row(FOR_LOOP_STEP1)308 self._exec(CommentRows([row]))309 assert_equals(self._steps[row].as_list(),310 ['', 'Comment'] + self._data_step_as_list(FOR_LOOP_STEP1)[1:])311 def test_uncommenting_single_row(self):312 self._exec(CommentRows([0]))313 self._exec(UncommentRows([0]))314 assert_equals(self._steps[0].as_list(), self._data_step_as_list(STEP1))315 def test_uncommenting_rows(self):316 self._exec(CommentRows([1,2,3,4]))317 self._exec(UncommentRows([1,2,3,4]))318 self._verify_steps_unchanged(STEP2, STEP_WITH_COMMENT, FOR_LOOP_HEADER, FOR_LOOP_STEP1)319 def test_uncommenting_commented_step_in_for_loop(self):320 row = self._data_row(FOR_LOOP_STEP1)321 self._exec(CommentRows([row]))322 self._exec(UncommentRows([row]))323 self._verify_step_unchanged(FOR_LOOP_STEP1)324 def test_uncommenting_does_nothing_if_not_commented(self):325 self._exec(UncommentRows([1,2,3,4]))326 self._verify_steps_unchanged(STEP2, STEP_WITH_COMMENT, FOR_LOOP_HEADER, FOR_LOOP_STEP1)327 def test_commenting_and_uncommenting_row_with_no_step(self):328 self._exec(CommentRows([1000]))329 self._verify_number_of_test_changes(0)330 self._exec(UncommentRows([10001]))331 self._verify_number_of_test_changes(0)332_TEST_WITH_TWO_FOR_LOOPS = ['Test With Two For Loops',333 ' : FOR ${i} IN 1 2',334 ' Log ${i}',335 ' : FOR ${j} IN 1 2',336 ' Log ${j}']337class ForLoopCases(TestCaseCommandTest):338 def _create_data(self):339 return _TEST_WITH_TWO_FOR_LOOPS[:]340 def test_remove_second_for_header(self):341 self._exec(DeleteCells((2,0), (2,0)))342 self._verify_step(2, '${j}', ['IN', '1', '2'])343 def test_remove_first_step_in_for_loop(self):344 self._exec(DeleteCells((1,1), (1,10)))345 self._verify_step_unchanged(' : FOR ${i} IN 1 2')346 self._verify_step(1, '')347 self._verify_step_unchanged(' : FOR ${j} IN 1 2')348class RowMovingTest(TestCaseCommandTest):349 def test_row_up(self):350 result = self._exec(MoveRowsUp([1]))351 assert_true(result)352 self._assert_step_order(STEP2, STEP1)353 def test_first_row_up_does_nothing(self):354 result = self._exec(MoveRowsUp([0]))355 assert_true(not result)356 assert_equals(self._number_of_test_changes, 0)357 self._exec(Undo())358 self._exec(Redo())359 def test_moving_block_containing_first_row_up_does_nothing(self):360 self._exec(MoveRowsUp([0,1,2]))361 assert_equals(self._number_of_test_changes, 0)362 def test_move_for_loop_header_up(self):363 self._exec(MoveRowsUp([self._data_row(FOR_LOOP_HEADER)]))364 self._assert_step_order(STEP1,365 STEP2,366 FOR_LOOP_HEADER,367 ' '+STEP_WITH_COMMENT,368 FOR_LOOP_STEP1,369 FOR_LOOP_STEP2,370 STEP_AFTER_FOR_LOOP)371 def test_move_step1_in_for_loop_header_up(self):372 self._exec(MoveRowsUp([self._data_row(FOR_LOOP_STEP1)]))373 self._assert_step_order(STEP1,374 STEP2,375 STEP_WITH_COMMENT,376 FOR_LOOP_STEP1[2:],377 FOR_LOOP_HEADER,378 FOR_LOOP_STEP2,379 STEP_AFTER_FOR_LOOP)380 def test_move_down_step_before_for_loop_header(self):381 self._exec(MoveRowsDown([self._data_row(STEP_WITH_COMMENT)]))382 self._assert_step_order(STEP1,383 STEP2,384 FOR_LOOP_HEADER,385 ' '+STEP_WITH_COMMENT,386 FOR_LOOP_STEP1,387 FOR_LOOP_STEP2,388 STEP_AFTER_FOR_LOOP)389 def test_move_up_step_after_for_loop(self):390 self._exec(MoveRowsUp([self._data_row(STEP_AFTER_FOR_LOOP)]))391 self._assert_step_order(STEP1,392 STEP2,393 STEP_WITH_COMMENT,394 FOR_LOOP_HEADER,395 FOR_LOOP_STEP1,396 ' '+STEP_AFTER_FOR_LOOP,397 FOR_LOOP_STEP2)398 def test_move_down_last_step_in_for_loop(self):399 self._exec(MoveRowsDown([self._data_row(FOR_LOOP_STEP2)]))400 self._assert_step_order(STEP1,401 STEP2,402 STEP_WITH_COMMENT,403 FOR_LOOP_HEADER,404 FOR_LOOP_STEP1,405 STEP_AFTER_FOR_LOOP,406 FOR_LOOP_STEP2[2:])407 def test_move_down_for_loop_header(self):408 self._exec(MoveRowsDown([self._data_row(FOR_LOOP_HEADER)]))409 self._assert_step_order(STEP1,410 STEP2,411 STEP_WITH_COMMENT,412 FOR_LOOP_STEP1[2:],413 FOR_LOOP_HEADER,414 FOR_LOOP_STEP2,415 STEP_AFTER_FOR_LOOP,)416 def test_undo_row_up(self):417 self._exec(MoveRowsUp([1]))418 self._exec(Undo())419 self._assert_step_order(STEP1, STEP2)420 def test_moving_rows(self):421 self._exec(MoveRowsUp([1, 2]))422 self._assert_step_order(STEP2, STEP_WITH_COMMENT, STEP1)423 def test_undoing_moving_rows(self):424 self._exec(MoveRowsUp([1, 2]))425 self._exec(Undo())426 self._assert_step_order(STEP1, STEP2, STEP_WITH_COMMENT)427 def test_move_row_down(self):428 self._exec(MoveRowsDown([0]))429 self._assert_step_order(STEP2, STEP1)430 def test_undo_move_row_down(self):431 self._exec(MoveRowsDown([0]))432 self._exec(Undo())433 self._assert_step_order(STEP1, STEP2)434 def test_move_rows_down(self):435 self._exec(MoveRowsDown([0,1]))436 self._assert_step_order(STEP_WITH_COMMENT, STEP1, STEP2)437 def _assert_step_order(self, *steps):438 for idx, step in enumerate(steps):439 assert_equals(self._steps[idx].as_list(),440 self._data_step_as_list(step))441 assert_true(self._ctrl.dirty)442if __name__ == "__main__":...
test_cli.py
Source:test_cli.py
2from subprocess import Popen, PIPE3import pytest4# import weblogo5from . import data_stream6def _exec(args, outputtext, returncode=0, stdin=None):7 if not stdin:8 stdin = data_stream("cap.fa")9 args = ["weblogo"] + args10 p = Popen(args, stdin=stdin, stdout=PIPE, stderr=PIPE)11 (out, err) = p.communicate()12 if returncode == 0 and p.returncode > 0:13 print(err)14 assert returncode == p.returncode15 if returncode == 0:16 assert len(err) == 017 if outputtext:18 out = out.decode()19 for item in outputtext:20 assert item in out21 stdin.close()22def test_malformed_options():23 _exec(["--notarealoption"], [], 2)24 _exec(["extrajunk"], [], 2)25 _exec(["-I"], [], 2)26def test_help_option():27 _exec(["-h"], ["options"])28 _exec(["--help"], ["options"])29# def test_version_option():30# _exec(['--version'], weblogo.__version__[0:5])31def test_default_build():32 _exec([], ["%%Title: Sequence Logo:"])33# Format options34def test_width():35 _exec(['-W', '1234'], ["/stack_width 1234"])36 _exec(['--stack-width', '1234'], ["/stack_width 1234"])37def test_height():38 _exec(['-W', '1000'], ["/stack_height 5000"])39 _exec(['-W', '1000', '--aspect-ratio', '2'], ["/stack_height 2000"])40def test_stacks_per_line():41 _exec(['-n', '7'], ["/stacks_per_line 7 def"])42 _exec(['--stacks-per-line', '7'], ["/stacks_per_line 7 def"])43def test_title():44 _exec(['-t', '3456'], ['/logo_title (3456) def', '/show_title True def'])45 _exec(['-t', ''], ['/logo_title () def', '/show_title False def'])46 _exec(['--title', '3456'], ['/logo_title (3456) def', '/show_title True def'])47def test_annotate():48 _exec(["--annotate", "1,2,3,4"], [], 2)49 _exec(["--annotate", "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,,"], [])50def test_color():51 _exec(['--color', 'black', 'AG', 'Purine'], [])52 _exec(['--color', 'not_a_color', 'AG', 'Purine'], [], 2)53def test_reverse_complement():54 _exec(['--complement'], [])55 _exec(['--reverse'], [])56 _exec(['--revcomp'], [])57def test_formats():58 _exec(['--format', 'eps'], [])59 _exec(['--format', 'png'], [])60 _exec(['--format', 'png_print'], [])61 _exec(['--format', 'pdf'], [])62 _exec(['--format', 'jpeg'], [])63 _exec(['--format', 'logodata'], [])64@pytest.mark.skipif(shutil.which('pdf2svg') is None,65 reason="requires pdf2svg")66def test_formats_svg():...
helpers.js
Source:helpers.js
...5 _exec (fn) {6 return this._layoutHelpers && fn()7 },8 getLayoutSidenav () {9 return this._exec(() => this._layoutHelpers.getLayoutSidenav()) || null10 },11 getSidenav () {12 return this._exec(() => this._layoutHelpers.getSidenav()) || null13 },14 getLayoutNavbar () {15 return this._exec(() => this._layoutHelpers.getLayoutNavbar()) || null16 },17 getLayoutFooter () {18 return this._exec(() => this._layoutHelpers.getLayoutFooter()) || null19 },20 getLayoutContainer () {21 return this._exec(() => this._layoutHelpers.getLayoutContainer()) || null22 },23 isSmallScreen () {24 return this._exec(() => this._layoutHelpers.isSmallScreen())25 },26 isLayout1 () {27 return this._exec(() => this._layoutHelpers.isLayout1())28 },29 isCollapsed () {30 return this._exec(() => this._layoutHelpers.isCollapsed())31 },32 isFixed () {33 return this._exec(() => this._layoutHelpers.isFixed())34 },35 isOffcanvas () {36 return this._exec(() => this._layoutHelpers.isOffcanvas())37 },38 isNavbarFixed () {39 return this._exec(() => this._layoutHelpers.isNavbarFixed())40 },41 isFooterFixed () {42 return this._exec(() => this._layoutHelpers.isFooterFixed())43 },44 isReversed () {45 return this._exec(() => this._layoutHelpers.isReversed())46 },47 setCollapsed (collapsed, animate = true) {48 this._exec(() => this._layoutHelpers.setCollapsed(collapsed, animate))49 },50 toggleCollapsed (animate = true) {51 this._exec(() => this._layoutHelpers.toggleCollapsed(animate))52 },53 setPosition (fixed, offcanvas) {54 this._exec(() => this._layoutHelpers.setPosition(fixed, offcanvas))55 },56 setNavbarFixed (fixed) {57 this._exec(() => this._layoutHelpers.setNavbarFixed(fixed))58 },59 setFooterFixed (fixed) {60 this._exec(() => this._layoutHelpers.setFooterFixed(fixed))61 },62 setReversed (reversed) {63 this._exec(() => this._layoutHelpers.setReversed(reversed))64 },65 update () {66 this._exec(() => this._layoutHelpers.update())67 },68 setAutoUpdate (enable) {69 this._exec(() => this._layoutHelpers.setAutoUpdate(enable))70 },71 on (event, callback) {72 this._exec(() => this._layoutHelpers.on(event, callback))73 },74 off (event) {75 this._exec(() => this._layoutHelpers.off(event))76 },77 init () {78 this._exec(() => this._layoutHelpers.init())79 },80 destroy () {81 this._exec(() => this._layoutHelpers.destroy())82 },83 // Internal84 //85 _removeClass (cls) {86 this._exec(() => this._layoutHelpers._removeClass(cls))87 }...
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!!