-- Regression fixtures are always rolled back. Requires a migrated database and active project user. SET XACT_ABORT ON; BEGIN TRY BEGIN TRANSACTION; DECLARE @ProjectID int,@UserID int,@Source int,@Parent int,@Child int,@Other int; SELECT TOP (1) @ProjectID=ProjectID,@UserID=UserID FROM dbo.ProjectUserAccess WHERE IsActive=1; INSERT dbo.Locations(ProjectID,LocationName,Description) VALUES (@ProjectID,N'Reparent test room',N'Preserve details'); SET @Source=SCOPE_IDENTITY(); INSERT dbo.Locations(ProjectID,LocationName) VALUES (@ProjectID,N'Reparent test building'); SET @Parent=SCOPE_IDENTITY(); INSERT dbo.Locations(ProjectID,LocationName,ParentLocationID) VALUES (@ProjectID,N'Reparent test cupboard',@Source); SET @Child=SCOPE_IDENTITY(); INSERT dbo.Locations(ProjectID,LocationName) VALUES (@ProjectID,N'Reparent test other building'); SET @Other=SCOPE_IDENTITY(); INSERT dbo.LocationAliases(LocationID,Alias) VALUES (@Source,N'Room alias'); EXEC dbo.Location_Reparent @ProjectID,@Source,@Parent,NULL,@UserID; IF NOT EXISTS (SELECT 1 FROM dbo.Locations WHERE LocationID=@Source AND ParentLocationID=@Parent) THROW 51001,'Initial parent not assigned.',1; EXEC dbo.Location_Reparent @ProjectID,@Source,@Other,@Parent,@UserID; IF NOT EXISTS (SELECT 1 FROM dbo.Locations WHERE LocationID=@Source AND ParentLocationID=@Other AND Description=N'Preserve details') THROW 51001,'Reassignment changed details or failed.',1; IF NOT EXISTS (SELECT 1 FROM dbo.Locations WHERE LocationID=@Child AND ParentLocationID=@Source) THROW 51001,'Subtree changed.',1; IF NOT EXISTS (SELECT 1 FROM dbo.LocationAliases WHERE LocationID=@Source AND Alias=N'Room alias') THROW 51001,'Alias changed.',1; IF (SELECT COUNT(*) FROM dbo.ProjectActivity WHERE EntityType=N'Location' AND EntityID=@Source AND ActivityType=N'Updated')<>2 THROW 51001,'Audit entries missing.',1; ROLLBACK TRANSACTION; SELECT N'PASS: assign, reassign, preserve subtree/details/aliases, audit' AS Result; END TRY BEGIN CATCH IF XACT_STATE()<>0 ROLLBACK TRANSACTION; THROW; END CATCH; GO BEGIN TRY BEGIN TRANSACTION; DECLARE @ProjectID int,@UserID int,@Source int,@Parent int,@Child int,@Other int; SELECT TOP (1) @ProjectID=ProjectID,@UserID=UserID FROM dbo.ProjectUserAccess WHERE IsActive=1; INSERT dbo.Locations(ProjectID,LocationName,Description) VALUES (@ProjectID,N'Reparent test room',N'Preserve details'); SET @Source=SCOPE_IDENTITY(); INSERT dbo.Locations(ProjectID,LocationName) VALUES (@ProjectID,N'Reparent test building'); SET @Parent=SCOPE_IDENTITY(); INSERT dbo.Locations(ProjectID,LocationName,ParentLocationID) VALUES (@ProjectID,N'Reparent test cupboard',@Source); SET @Child=SCOPE_IDENTITY(); INSERT dbo.Locations(ProjectID,LocationName) VALUES (@ProjectID,N'Reparent test other building'); SET @Other=SCOPE_IDENTITY(); INSERT dbo.LocationAliases(LocationID,Alias) VALUES (@Source,N'Room alias'); EXEC dbo.Location_Reparent @ProjectID,@Source,@Source,NULL,@UserID; THROW 51001,'Invalid move was accepted.',1; END TRY BEGIN CATCH IF XACT_STATE()<>0 ROLLBACK TRANSACTION; IF ERROR_NUMBER()<>51000 THROW; SELECT N'PASS: reject self; fixtures rolled back' AS Result; END CATCH; GO BEGIN TRY BEGIN TRANSACTION; DECLARE @ProjectID int,@UserID int,@Source int,@Parent int,@Child int,@Other int; SELECT TOP (1) @ProjectID=ProjectID,@UserID=UserID FROM dbo.ProjectUserAccess WHERE IsActive=1; INSERT dbo.Locations(ProjectID,LocationName,Description) VALUES (@ProjectID,N'Reparent test room',N'Preserve details'); SET @Source=SCOPE_IDENTITY(); INSERT dbo.Locations(ProjectID,LocationName) VALUES (@ProjectID,N'Reparent test building'); SET @Parent=SCOPE_IDENTITY(); INSERT dbo.Locations(ProjectID,LocationName,ParentLocationID) VALUES (@ProjectID,N'Reparent test cupboard',@Source); SET @Child=SCOPE_IDENTITY(); INSERT dbo.Locations(ProjectID,LocationName) VALUES (@ProjectID,N'Reparent test other building'); SET @Other=SCOPE_IDENTITY(); INSERT dbo.LocationAliases(LocationID,Alias) VALUES (@Source,N'Room alias'); EXEC dbo.Location_Reparent @ProjectID,@Source,@Child,NULL,@UserID; THROW 51001,'Invalid move was accepted.',1; END TRY BEGIN CATCH IF XACT_STATE()<>0 ROLLBACK TRANSACTION; IF ERROR_NUMBER()<>51000 THROW; SELECT N'PASS: reject descendant; fixtures rolled back' AS Result; END CATCH; GO BEGIN TRY BEGIN TRANSACTION; DECLARE @ProjectID int,@UserID int,@Source int,@Parent int,@Child int,@Other int; SELECT TOP (1) @ProjectID=ProjectID,@UserID=UserID FROM dbo.ProjectUserAccess WHERE IsActive=1; INSERT dbo.Locations(ProjectID,LocationName,Description) VALUES (@ProjectID,N'Reparent test room',N'Preserve details'); SET @Source=SCOPE_IDENTITY(); INSERT dbo.Locations(ProjectID,LocationName) VALUES (@ProjectID,N'Reparent test building'); SET @Parent=SCOPE_IDENTITY(); INSERT dbo.Locations(ProjectID,LocationName,ParentLocationID) VALUES (@ProjectID,N'Reparent test cupboard',@Source); SET @Child=SCOPE_IDENTITY(); INSERT dbo.Locations(ProjectID,LocationName) VALUES (@ProjectID,N'Reparent test other building'); SET @Other=SCOPE_IDENTITY(); INSERT dbo.LocationAliases(LocationID,Alias) VALUES (@Source,N'Room alias'); EXEC dbo.Location_Reparent @ProjectID,@Source,@Parent,@Other,@UserID; THROW 51001,'Invalid move was accepted.',1; END TRY BEGIN CATCH IF XACT_STATE()<>0 ROLLBACK TRANSACTION; IF ERROR_NUMBER()<>51000 THROW; SELECT N'PASS: reject stale parent; fixtures rolled back' AS Result; END CATCH; GO BEGIN TRY BEGIN TRANSACTION; DECLARE @ProjectID int,@UserID int,@Source int,@Parent int,@Child int,@Other int; SELECT TOP (1) @ProjectID=ProjectID,@UserID=UserID FROM dbo.ProjectUserAccess WHERE IsActive=1; INSERT dbo.Locations(ProjectID,LocationName,Description) VALUES (@ProjectID,N'Reparent test room',N'Preserve details'); SET @Source=SCOPE_IDENTITY(); INSERT dbo.Locations(ProjectID,LocationName) VALUES (@ProjectID,N'Reparent test building'); SET @Parent=SCOPE_IDENTITY(); INSERT dbo.Locations(ProjectID,LocationName,ParentLocationID) VALUES (@ProjectID,N'Reparent test cupboard',@Source); SET @Child=SCOPE_IDENTITY(); INSERT dbo.Locations(ProjectID,LocationName) VALUES (@ProjectID,N'Reparent test other building'); SET @Other=SCOPE_IDENTITY(); INSERT dbo.LocationAliases(LocationID,Alias) VALUES (@Source,N'Room alias'); UPDATE dbo.Locations SET ParentLocationID=@Parent WHERE LocationID=@Source; EXEC dbo.Location_Reparent @ProjectID,@Source,@Parent,@Parent,@UserID; THROW 51001,'Invalid move was accepted.',1; END TRY BEGIN CATCH IF XACT_STATE()<>0 ROLLBACK TRANSACTION; IF ERROR_NUMBER()<>51000 THROW; SELECT N'PASS: reject same parent; fixtures rolled back' AS Result; END CATCH; GO BEGIN TRY BEGIN TRANSACTION; DECLARE @ProjectID int,@UserID int,@Source int,@Parent int,@Child int,@Other int; SELECT TOP (1) @ProjectID=ProjectID,@UserID=UserID FROM dbo.ProjectUserAccess WHERE IsActive=1; INSERT dbo.Locations(ProjectID,LocationName,Description) VALUES (@ProjectID,N'Reparent test room',N'Preserve details'); SET @Source=SCOPE_IDENTITY(); INSERT dbo.Locations(ProjectID,LocationName) VALUES (@ProjectID,N'Reparent test building'); SET @Parent=SCOPE_IDENTITY(); INSERT dbo.Locations(ProjectID,LocationName,ParentLocationID) VALUES (@ProjectID,N'Reparent test cupboard',@Source); SET @Child=SCOPE_IDENTITY(); INSERT dbo.Locations(ProjectID,LocationName) VALUES (@ProjectID,N'Reparent test other building'); SET @Other=SCOPE_IDENTITY(); INSERT dbo.LocationAliases(LocationID,Alias) VALUES (@Source,N'Room alias'); UPDATE dbo.Locations SET IsArchived=1 WHERE LocationID=@Parent; EXEC dbo.Location_Reparent @ProjectID,@Source,@Parent,NULL,@UserID; THROW 51001,'Invalid move was accepted.',1; END TRY BEGIN CATCH IF XACT_STATE()<>0 ROLLBACK TRANSACTION; IF ERROR_NUMBER()<>51000 THROW; SELECT N'PASS: reject archived parent; fixtures rolled back' AS Result; END CATCH; GO BEGIN TRY BEGIN TRANSACTION; DECLARE @ProjectID int,@UserID int,@Source int,@Parent int,@Child int,@Other int; SELECT TOP (1) @ProjectID=ProjectID,@UserID=UserID FROM dbo.ProjectUserAccess WHERE IsActive=1; INSERT dbo.Locations(ProjectID,LocationName,Description) VALUES (@ProjectID,N'Reparent test room',N'Preserve details'); SET @Source=SCOPE_IDENTITY(); INSERT dbo.Locations(ProjectID,LocationName) VALUES (@ProjectID,N'Reparent test building'); SET @Parent=SCOPE_IDENTITY(); INSERT dbo.Locations(ProjectID,LocationName,ParentLocationID) VALUES (@ProjectID,N'Reparent test cupboard',@Source); SET @Child=SCOPE_IDENTITY(); INSERT dbo.Locations(ProjectID,LocationName) VALUES (@ProjectID,N'Reparent test other building'); SET @Other=SCOPE_IDENTITY(); INSERT dbo.LocationAliases(LocationID,Alias) VALUES (@Source,N'Room alias'); INSERT dbo.Projects(ProjectName) VALUES (N'Reparent other project'); DECLARE @OtherProject int=SCOPE_IDENTITY(); UPDATE dbo.Locations SET ProjectID=@OtherProject WHERE LocationID=@Parent; EXEC dbo.Location_Reparent @ProjectID,@Source,@Parent,NULL,@UserID; THROW 51001,'Invalid move was accepted.',1; END TRY BEGIN CATCH IF XACT_STATE()<>0 ROLLBACK TRANSACTION; IF ERROR_NUMBER()<>51000 THROW; SELECT N'PASS: reject cross-project; fixtures rolled back' AS Result; END CATCH; GO BEGIN TRY BEGIN TRANSACTION; DECLARE @ProjectID int,@UserID int,@Source int,@Parent int,@Child int,@Other int; SELECT TOP (1) @ProjectID=ProjectID,@UserID=UserID FROM dbo.ProjectUserAccess WHERE IsActive=1; INSERT dbo.Locations(ProjectID,LocationName,Description) VALUES (@ProjectID,N'Reparent test room',N'Preserve details'); SET @Source=SCOPE_IDENTITY(); INSERT dbo.Locations(ProjectID,LocationName) VALUES (@ProjectID,N'Reparent test building'); SET @Parent=SCOPE_IDENTITY(); INSERT dbo.Locations(ProjectID,LocationName,ParentLocationID) VALUES (@ProjectID,N'Reparent test cupboard',@Source); SET @Child=SCOPE_IDENTITY(); INSERT dbo.Locations(ProjectID,LocationName) VALUES (@ProjectID,N'Reparent test other building'); SET @Other=SCOPE_IDENTITY(); INSERT dbo.LocationAliases(LocationID,Alias) VALUES (@Source,N'Room alias'); SET @UserID=NULL; EXEC dbo.Location_Reparent @ProjectID,@Source,@Parent,NULL,@UserID; THROW 51001,'Invalid move was accepted.',1; END TRY BEGIN CATCH IF XACT_STATE()<>0 ROLLBACK TRANSACTION; IF ERROR_NUMBER()<>51000 THROW; SELECT N'PASS: reject unauthorized; fixtures rolled back' AS Result; END CATCH; GO