Skip to content

Commit

Permalink
Refactor test
Browse files Browse the repository at this point in the history
  • Loading branch information
AMagistroni committed Sep 10, 2021
1 parent 441db14 commit 861bd97
Show file tree
Hide file tree
Showing 16 changed files with 219 additions and 84 deletions.
1 change: 0 additions & 1 deletion SqlSchemaCompare.Core/Common/ISchemaBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using SqlSchemaCompare.Core.DbStructures;
using SqlSchemaCompare.Core.TSql;

namespace SqlSchemaCompare.Core.Common
{
Expand Down
28 changes: 28 additions & 0 deletions SqlSchemaCompare.Core/Common/RelatedDbObjects.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using SqlSchemaCompare.Core.DbStructures;
using System.Collections.Generic;
using System.Linq;

namespace SqlSchemaCompare.Core.Common
{
public class RelatedDbObjectsConfiguration
{
public static List<List<DbObjectType>> RelatedDbObjects = new List<List<DbObjectType>>
{
new List<DbObjectType> { DbObjectType.Function },
new List<DbObjectType> { DbObjectType.StoreProcedure },
new List<DbObjectType> { DbObjectType.Table, DbObjectType.TableContraint, DbObjectType.Column },
new List<DbObjectType> { DbObjectType.User, DbObjectType.Role, DbObjectType.Member },
new List<DbObjectType> { DbObjectType.View },
new List<DbObjectType> { DbObjectType.Schema },
new List<DbObjectType> { DbObjectType.Trigger, DbObjectType.EnableTrigger },
new List<DbObjectType> { DbObjectType.Type },
new List<DbObjectType> { DbObjectType.Other},
new List<DbObjectType> { DbObjectType.Index }
};

public List<DbObjectType> GetRelatedDbObjects(DbObjectType dbObject)
{
return RelatedDbObjects.Single(x => x.Contains(dbObject));
}
}
}
16 changes: 12 additions & 4 deletions SqlSchemaCompare.Test/TSql/TSqlFunctionTest.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
using Shouldly;
using SqlSchemaCompare.Core.Common;
using SqlSchemaCompare.Core.DbStructures;
using SqlSchemaCompare.Core.TSql;
using System.Collections.Generic;
using System.Linq;
using Xunit;

namespace SqlSchemaCompare.Test.TSql
{
public class TSqlFunctionTest
{
private IList<DbObjectType> SelectedObjects;
public TSqlFunctionTest()
{
RelatedDbObjectsConfiguration relatedDbObjectsConfiguration = new();
SelectedObjects = relatedDbObjectsConfiguration.GetRelatedDbObjects(DbObjectType.Function);
}
[Fact]
public void CreateFunction()
{
Expand Down Expand Up @@ -53,7 +61,7 @@ RETURN 3
END
GO";

(string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, new DbObjectType[] { DbObjectType.Function });
(string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, SelectedObjects);

updateSchema.ShouldBeEmpty();
errors.ShouldBeEmpty();
Expand All @@ -75,7 +83,7 @@ RETURN 3
GO";
const string destination = "";

(string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, new DbObjectType[] { DbObjectType.Function });
(string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, SelectedObjects);

updateSchema.ShouldBe(
@"CREATE FUNCTION [dbo].[func1] (@par int)
Expand Down Expand Up @@ -106,7 +114,7 @@ RETURN 3
END
GO";

(string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, new DbObjectType[] { DbObjectType.Function });
(string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, SelectedObjects);

updateSchema.ShouldBe(
@"DROP FUNCTION [dbo].[func1]
Expand Down Expand Up @@ -139,7 +147,7 @@ RETURN 10
END
GO";

(string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, new DbObjectType[] { DbObjectType.Function });
(string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, SelectedObjects);

updateSchema.ShouldBe(
@"ALTER FUNCTION [dbo].[func1] (@par int)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
using Shouldly;
using SqlSchemaCompare.Core.Common;
using SqlSchemaCompare.Core.DbStructures;
using SqlSchemaCompare.Core.TSql;
using System.Collections.Generic;
using System.Linq;
using Xunit;

namespace SqlSchemaCompare.Test.TSql
{
public class IndexTest
public class TSqlIndexTest
{
private IList<DbObjectType> SelectedObjects;
public TSqlIndexTest()
{
RelatedDbObjectsConfiguration relatedDbObjectsConfiguration = new();
SelectedObjects = relatedDbObjectsConfiguration.GetRelatedDbObjects(DbObjectType.Index);
}
[Fact]
public void CreateIndex()
{
Expand Down Expand Up @@ -60,7 +68,7 @@ [ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [indexTable]
GO";

(string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, new DbObjectType[] { DbObjectType.Index });
(string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, SelectedObjects);

updateSchema.ShouldBeEmpty();
errors.ShouldBeEmpty();
Expand All @@ -80,7 +88,7 @@ [ID] ASC
GO";
const string destination = "";

(string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, new DbObjectType[] { DbObjectType.Index });
(string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, SelectedObjects);

updateSchema.ShouldBe(
@"CREATE NONCLUSTERED INDEX [indexName] ON [dbo].[table]
Expand Down Expand Up @@ -110,7 +118,7 @@ [ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [indexTable]
GO";

(string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, new DbObjectType[] { DbObjectType.Index });
(string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, SelectedObjects);

updateSchema.ShouldBe(
@"USE [dbName]
Expand Down Expand Up @@ -143,7 +151,7 @@ [ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [indexTable]
GO";

(string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, new DbObjectType[] { DbObjectType.Index });
(string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, SelectedObjects);

updateSchema.ShouldBe(
@"DROP INDEX [indexName] ON [dbo].[table]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
using Shouldly;
using SqlSchemaCompare.Core.Common;
using SqlSchemaCompare.Core.DbStructures;
using SqlSchemaCompare.Core.TSql;
using System.Collections.Generic;
using System.Linq;
using Xunit;

namespace SqlSchemaCompare.Test.TSql
{
public class MemberTest
public class TSqlMemberTest
{
private IList<DbObjectType> SelectedObjects;
public TSqlMemberTest()
{
RelatedDbObjectsConfiguration relatedDbObjectsConfiguration = new();
SelectedObjects = relatedDbObjectsConfiguration.GetRelatedDbObjects(DbObjectType.Member);
}
[Fact]
public void CreateSchema()
{
Expand All @@ -33,7 +41,7 @@ public void UpdateSchemaEqualsDbObject()
const string origin = "ALTER ROLE [role] ADD MEMBER [member1]";
const string destination = "ALTER ROLE [role] ADD MEMBER [member1]";

(string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, new DbObjectType[] { DbObjectType.Member });
(string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, SelectedObjects);

updateSchema.ShouldBeEmpty();
errors.ShouldBeEmpty();
Expand All @@ -48,7 +56,7 @@ public void UpdateSchemaCreateDbObject()
const string origin = "ALTER ROLE [role] ADD MEMBER [member1]";
const string destination = "";

(string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, new DbObjectType[] { DbObjectType.Member });
(string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, SelectedObjects);

updateSchema.ShouldBe(
@"ALTER ROLE [role] ADD MEMBER [member1]
Expand All @@ -67,7 +75,7 @@ public void UpdateSchemaDropDbObject()
const string origin = "";
const string destination = "ALTER ROLE [role] ADD MEMBER [member1]";

(string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, new DbObjectType[] { DbObjectType.Member });
(string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, SelectedObjects);

updateSchema.ShouldBe(
@"ALTER ROLE [role] DROP MEMBER [member1]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
using Shouldly;
using SqlSchemaCompare.Core.Common;
using SqlSchemaCompare.Core.DbStructures;
using SqlSchemaCompare.Core.TSql;
using System.Collections.Generic;
using System.Linq;
using Xunit;

namespace SqlSchemaCompare.Test.TSql
{
public class RoleTest
public class TSqlRoleTest
{
private IList<DbObjectType> SelectedObjects;
public TSqlRoleTest()
{
RelatedDbObjectsConfiguration relatedDbObjectsConfiguration = new();
SelectedObjects = relatedDbObjectsConfiguration.GetRelatedDbObjects(DbObjectType.Role);
}
[Fact]
public void CreateRole()
{
Expand All @@ -32,7 +40,7 @@ public void UpdateSchemaEqualsDbObject()
const string origin = "CREATE ROLE [role]";
const string destination = "CREATE ROLE [role]";

(string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, new DbObjectType[] { DbObjectType.Role });
(string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, SelectedObjects);

updateSchema.ShouldBeEmpty();
errors.ShouldBeEmpty();
Expand All @@ -47,7 +55,7 @@ public void UpdateSchemaCreateDbObject()
const string origin = "CREATE ROLE [role]";
const string destination = "";

(string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, new DbObjectType[] { DbObjectType.Role });
(string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, SelectedObjects);

updateSchema.ShouldBe(
@"CREATE ROLE [role]
Expand All @@ -72,7 +80,7 @@ ALTER ROLE [role] ADD MEMBER [user]
GO
";

(string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, new DbObjectType[] { DbObjectType.Role });
(string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, SelectedObjects);

updateSchema.ShouldBe(
@"DROP ROLE [role]
Expand Down
14 changes: 11 additions & 3 deletions SqlSchemaCompare.Test/TSql/TSqlSchemaTest.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
using Shouldly;
using SqlSchemaCompare.Core.Common;
using SqlSchemaCompare.Core.DbStructures;
using SqlSchemaCompare.Core.TSql;
using System.Collections.Generic;
using System.Linq;
using Xunit;

namespace SqlSchemaCompare.Test.TSql
{
public class TSqlSchemaTest
{
private IList<DbObjectType> SelectedObjects;
public TSqlSchemaTest()
{
RelatedDbObjectsConfiguration relatedDbObjectsConfiguration = new();
SelectedObjects = relatedDbObjectsConfiguration.GetRelatedDbObjects(DbObjectType.Schema);
}
[Fact]
public void CreateSchema()
{
Expand All @@ -33,7 +41,7 @@ public void UpdateSchemaEqualsDbObject()
const string origin = "CREATE SCHEMA [sch1]";
const string destination = "CREATE SCHEMA [sch1]";

(string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, new DbObjectType[] { DbObjectType.Schema });
(string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, SelectedObjects);

updateSchema.ShouldBeEmpty();
}
Expand All @@ -47,7 +55,7 @@ public void UpdateSchemaCreateDbObject()
const string origin = "CREATE SCHEMA [sch1]";
const string destination = "";

(string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, new DbObjectType[] { DbObjectType.Schema });
(string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, SelectedObjects);

updateSchema.ShouldBe(
@"CREATE SCHEMA [sch1]
Expand All @@ -66,7 +74,7 @@ public void UpdateSchemaDropDbObject()
const string origin = "";
const string destination = "CREATE SCHEMA [sch1]";

(string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, new DbObjectType[] { DbObjectType.Schema });
(string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, SelectedObjects);

updateSchema.ShouldBe(
@"DROP SCHEMA [sch1]
Expand Down
16 changes: 12 additions & 4 deletions SqlSchemaCompare.Test/TSql/TSqlStoreProcedureTest.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
using Shouldly;
using SqlSchemaCompare.Core.Common;
using SqlSchemaCompare.Core.DbStructures;
using SqlSchemaCompare.Core.TSql;
using System.Collections.Generic;
using System.Linq;
using Xunit;

namespace SqlSchemaCompare.Test.TSql
{
public class TSqlStoreProcedureTest
{
private IList<DbObjectType> SelectedObjects;
public TSqlStoreProcedureTest()
{
RelatedDbObjectsConfiguration relatedDbObjectsConfiguration = new();
SelectedObjects = relatedDbObjectsConfiguration.GetRelatedDbObjects(DbObjectType.StoreProcedure);
}
[Fact]
public void CreateStoreProcedure()
{
Expand Down Expand Up @@ -51,7 +59,7 @@ public void UpdateSchemaEqualsDbObject()
END
GO";

(string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, new DbObjectType[] { DbObjectType.StoreProcedure });
(string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, SelectedObjects);

updateSchema.ShouldBeEmpty();
errors.ShouldBeEmpty();
Expand All @@ -73,7 +81,7 @@ public void UpdateSchemaCreateDbObject()
GO";
const string destination = "";

(string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, new DbObjectType[] { DbObjectType.StoreProcedure });
(string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, SelectedObjects);

updateSchema.ShouldBe(
@"CREATE PROCEDURE [dbo].[proc]
Expand Down Expand Up @@ -104,7 +112,7 @@ public void UpdateSchemaDropDbObject()
END
GO";

(string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, new DbObjectType[] { DbObjectType.StoreProcedure });
(string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, SelectedObjects);

updateSchema.ShouldBe(
@"DROP PROCEDURE [dbo].[proc]
Expand Down Expand Up @@ -137,7 +145,7 @@ public void UpdateSchemaAlterDbObject()
END
GO";

(string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, new DbObjectType[] { DbObjectType.StoreProcedure });
(string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, SelectedObjects);

updateSchema.ShouldBe(
@"ALTER PROCEDURE [dbo].[proc]
Expand Down
Loading

0 comments on commit 861bd97

Please sign in to comment.