def test_schemaanalyzer_existing_schema_same_owner(mockdbcontext): mockdbcontext.get_schema_owner = lambda x: ROLES[0] schemaconf = own.SchemaAnalyzer(ROLES[0], schema=SCHEMAS[0], dbcontext=mockdbcontext) changes = schemaconf.analyze() assert changes == []
def test_schemaanalyzer_get_improperly_owned_objects(mockdbcontext): mockdbcontext.get_schema_owner = lambda x: ROLES[0] mockdbcontext.get_schema_objects = lambda x: [ # Properly owned ObjectInfo('tables', quoted_object(ROLES[0], TABLES[0]), ROLES[0], False), ObjectInfo('sequences', quoted_object(ROLES[0], SEQUENCES[0]), ROLES[ 0], False), # Improperly owned ObjectInfo('tables', quoted_object(ROLES[0], TABLES[1]), ROLES[1], False), ObjectInfo('sequences', quoted_object(ROLES[0], SEQUENCES[1]), ROLES[ 1], False), # Improperly owned but dependent (i.e. should be skipped) ObjectInfo('sequences', quoted_object(ROLES[0], SEQUENCES[2]), ROLES[ 1], True), ] schema = ROLES[0] schemaconf = own.SchemaAnalyzer(rolename=ROLES[0], schema=schema, dbcontext=mockdbcontext, is_personal_schema=True) actual = schemaconf.get_improperly_owned_objects() expected = [('tables', quoted_object(schema, TABLES[1]), ROLES[1]), ('sequences', quoted_object(schema, SEQUENCES[1]), ROLES[1])] assert set(actual) == set(expected)
def test_schemaanalyzer_existing_personal_schema_change_object_owners( mockdbcontext): mockdbcontext.get_schema_owner = lambda x: ROLES[0] mockdbcontext.get_schema_objects = lambda x: [ ObjectInfo('tables', quoted_object(ROLES[0], TABLES[0]), ROLES[0], False), ObjectInfo('sequences', quoted_object(ROLES[0], SEQUENCES[0]), ROLES[ 0], False), ObjectInfo('tables', quoted_object(ROLES[0], TABLES[1]), ROLES[1], False), ObjectInfo('sequences', quoted_object(ROLES[0], SEQUENCES[1]), ROLES[ 1], False), ] schema = ROLES[0] schemaconf = own.SchemaAnalyzer(ROLES[0], schema=schema, dbcontext=mockdbcontext, is_personal_schema=True) actual = schemaconf.analyze() expected = [ own.Q_SET_OBJECT_OWNER.format('TABLE', quoted_object(ROLES[0], TABLES[1]), ROLES[0], ROLES[1]), own.Q_SET_OBJECT_OWNER.format('SEQUENCE', quoted_object(ROLES[0], SEQUENCES[1]), ROLES[0], ROLES[1]), ] assert actual == expected
def test_schemaanalyzer_analyzer_creates_schema(mockdbcontext): schemaconf = own.SchemaAnalyzer(ROLES[0], schema=SCHEMAS[0], dbcontext=mockdbcontext) actual = schemaconf.analyze() expected = [own.Q_CREATE_SCHEMA.format(SCHEMAS[0], ROLES[0])] assert actual == expected
def test_schemaanalyzer_get_improperly_owned_objects(mockdbcontext): owner = ROLES[0] wrong_owner = ROLES[1] mockdbcontext.get_schema_owner = lambda x: owner mockdbcontext.get_schema_objects = lambda x: [ # Properly owned ObjectInfo('tables', ObjectName(owner, TABLES[0]), owner, False), ObjectInfo('sequences', ObjectName(owner, SEQUENCES[0]), owner, False), # Improperly owned ObjectInfo('tables', ObjectName(owner, TABLES[1]), wrong_owner, False), ObjectInfo('sequences', ObjectName(owner, SEQUENCES[1]), wrong_owner, False), # Improperly owned but dependent (i.e. should be skipped) ObjectInfo('sequences', ObjectName(owner, SEQUENCES[2]), wrong_owner, True), ] schemaconf = own.SchemaAnalyzer(rolename=owner, objname=ObjectName(owner), dbcontext=mockdbcontext, is_personal_schema=True) actual = schemaconf.get_improperly_owned_objects() expected = [('tables', ObjectName(owner, TABLES[1]), wrong_owner), ('sequences', ObjectName(owner, SEQUENCES[1]), wrong_owner)] assert set(actual) == set(expected)
def test_schemaanalyzer_create_schema(mockdbcontext): schemaconf = own.SchemaAnalyzer(ROLES[0], schema=SCHEMAS[0], dbcontext=mockdbcontext) schemaconf.create_schema() assert schemaconf.sql_to_run == [ own.Q_CREATE_SCHEMA.format(SCHEMAS[0], ROLES[0]) ]
def test_set_owner(mockdbcontext): previous_owner = ROLES[1] mockdbcontext.get_schema_owner = lambda x: previous_owner schemaconf = own.SchemaAnalyzer(ROLES[0], schema=SCHEMAS[0], dbcontext=mockdbcontext) schemaconf.set_owner() expected = [own.Q_SET_SCHEMA_OWNER.format(SCHEMAS[0], ROLES[0], previous_owner)] assert schemaconf.sql_to_run == expected
def test_schemaanalyzer_existing_schema_owner_change(mockdbcontext): mockdbcontext.get_schema_owner = lambda x: ROLES[1] schemaconf = own.SchemaAnalyzer(ROLES[0], schema=SCHEMAS[0], dbcontext=mockdbcontext) changes = schemaconf.analyze() assert changes == [ own.Q_SET_SCHEMA_OWNER.format(SCHEMAS[0], ROLES[0], ROLES[1]) ]
def test_init(mockdbcontext): mockdbcontext.get_schema_owner = lambda x: 'foo' mockdbcontext.get_schema_objects = lambda x: 'bar' schemaconf = own.SchemaAnalyzer(rolename=ROLES[0], schema=SCHEMAS[0], dbcontext=mockdbcontext) assert schemaconf.rolename == ROLES[0] assert schemaconf.schema == SCHEMAS[0] assert schemaconf.current_owner == 'foo' assert schemaconf.exists is True assert schemaconf.schema_objects == 'bar'
def test_alter_object_owner(mockdbcontext): previous_owner = ROLES[1] owner = ROLES[0] schema = SCHEMAS[0] table_name = quoted_object(schema, TABLES[0]) mockdbcontext.get_schema_owner = lambda x: owner schemaconf = own.SchemaAnalyzer(owner, schema=schema, dbcontext=mockdbcontext) schemaconf.alter_object_owner('tables', table_name, previous_owner) assert schemaconf.sql_to_run == [own.Q_SET_OBJECT_OWNER.format('TABLE', table_name, owner, previous_owner)]
def test_schemaanalyzer_init(mockdbcontext): mockdbcontext.get_schema_owner = lambda x: 'foo' mockdbcontext.get_schema_objects = lambda x: 'bar' schemaconf = own.SchemaAnalyzer(rolename=ROLES[0], objname=ObjectName(SCHEMAS[0]), dbcontext=mockdbcontext) assert schemaconf.rolename == ROLES[0] assert isinstance(schemaconf.objname, ObjectName) assert schemaconf.objname.schema == SCHEMAS[0] assert schemaconf.current_owner == 'foo' assert schemaconf.exists is True assert schemaconf.schema_objects == 'bar'
def test_schemaanalyzer_alter_object_owner(mockdbcontext): previous_owner = ROLES[1] owner = ROLES[0] schema = SCHEMAS[0] objname = ObjectName(schema, TABLES[0]) mockdbcontext.get_schema_owner = lambda x: owner schemaconf = own.SchemaAnalyzer(owner, objname=ObjectName(schema), dbcontext=mockdbcontext) schemaconf.alter_object_owner('tables', objname, previous_owner) assert schemaconf.sql_to_run == [ own.Q_SET_OBJECT_OWNER.format('TABLE', objname.qualified_name, owner, previous_owner) ]