-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create integrations tests for pessoa fisica controller
- Loading branch information
Showing
19 changed files
with
389 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
Demo.GestaoEscolar.WebApplication.Test/Fakes/MessageBusFake.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using CrossCutting; | ||
using MassTransit; | ||
using NLog; | ||
|
||
namespace Demo.GestaoEscolar.WebApplication.Test | ||
{ | ||
public class MessageBusFake : IMessageBus | ||
{ | ||
public Task<bool> IsAliveAsync() | ||
{ | ||
return Task.FromResult(true); | ||
} | ||
|
||
public Task PublishAsync<T>(T e) | ||
{ | ||
return Task.CompletedTask; | ||
} | ||
} | ||
} |
26 changes: 0 additions & 26 deletions
26
Demo.GestaoEscolar.WebApplication.Test/Fakes/PessaoFisicaFinderFake.cs
This file was deleted.
Oops, something went wrong.
9 changes: 9 additions & 0 deletions
9
Demo.GestaoEscolar.WebApplication.Test/Infraestructure/IntegrationTestCollection.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using Xunit; | ||
|
||
namespace Demo.GestaoEscolar.WebApplication.Test | ||
{ | ||
[CollectionDefinition("Integration test collection")] | ||
public class IntegrationTestCollection : ICollectionFixture<TestSetup> | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
131 changes: 131 additions & 0 deletions
131
Demo.GestaoEscolar.WebApplication.Test/Infraestructure/TestSetup.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Data.SqlClient; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Reflection; | ||
|
||
namespace Demo.GestaoEscolar.WebApplication.Test | ||
{ | ||
public class TestSetup : IDisposable | ||
{ | ||
public TestSetup() | ||
{ | ||
DestroyDatabase(); | ||
CreateDatabase(); | ||
CreateSchema(); | ||
CreateTablePessoaFisica(); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
DestroyDatabase(); | ||
} | ||
|
||
private static void CreateDatabase() | ||
{ | ||
ExecuteSqlCommand(Master, $@" | ||
CREATE DATABASE [GestaoEscolar] | ||
ON (NAME = 'GestaoEscolar', | ||
FILENAME = '{Filename}')"); | ||
} | ||
|
||
private static void CreateSchema() | ||
{ | ||
ExecuteSqlCommand(GestaoEscolar, $@"CREATE SCHEMA GES;"); | ||
} | ||
|
||
private static void CreateTablePessoaFisica() | ||
{ | ||
ExecuteSqlCommand(GestaoEscolar, $@"CREATE TABLE GES.PessoaFisica( | ||
Id INT IDENTITY NOT NULL, | ||
EntityId UNIQUEIDENTIFIER NOT NULL, | ||
DataCriacao DATETIME NOT NULL, | ||
Nome VARCHAR(400) NOT NULL, | ||
Cpf VARCHAR(11) NOT NULL, | ||
NomeSocial VARCHAR(400) NULL, | ||
Sexo CHAR(1) NOT NULL, | ||
DataNascimento DATETIME NOT NULL | ||
); | ||
ALTER TABLE GES.PessoaFisica ADD CONSTRAINT PK_PessoaFisica PRIMARY KEY (Id); | ||
ALTER TABLE GES.PessoaFisica ADD CONSTRAINT UC_PessoaFisica_Cpf UNIQUE(Cpf);"); | ||
} | ||
|
||
private static void DestroyDatabase() | ||
{ | ||
var fileNames = ExecuteSqlQuery(Master, @" | ||
SELECT [physical_name] FROM [sys].[master_files] | ||
WHERE [database_id] = DB_ID('GestaoEscolar')", | ||
row => (string)row["physical_name"]); | ||
|
||
if (fileNames.Any()) | ||
{ | ||
ExecuteSqlCommand(Master, @" | ||
ALTER DATABASE [GestaoEscolar] SET SINGLE_USER WITH ROLLBACK IMMEDIATE; | ||
EXEC sp_detach_db 'GestaoEscolar'"); | ||
|
||
fileNames.ForEach(File.Delete); | ||
} | ||
} | ||
|
||
private static void ExecuteSqlCommand( | ||
SqlConnectionStringBuilder connectionStringBuilder, | ||
string commandText) | ||
{ | ||
using (var connection = new SqlConnection(connectionStringBuilder.ConnectionString)) | ||
{ | ||
connection.Open(); | ||
using (var command = connection.CreateCommand()) | ||
{ | ||
command.CommandText = commandText; | ||
command.ExecuteNonQuery(); | ||
} | ||
} | ||
} | ||
|
||
private static List<T> ExecuteSqlQuery<T>( | ||
SqlConnectionStringBuilder connectionStringBuilder, | ||
string queryText, | ||
Func<SqlDataReader, T> read) | ||
{ | ||
var result = new List<T>(); | ||
using (var connection = new SqlConnection(connectionStringBuilder.ConnectionString)) | ||
{ | ||
connection.Open(); | ||
using (var command = connection.CreateCommand()) | ||
{ | ||
command.CommandText = queryText; | ||
using (var reader = command.ExecuteReader()) | ||
{ | ||
while (reader.Read()) | ||
{ | ||
result.Add(read(reader)); | ||
} | ||
} | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
private static SqlConnectionStringBuilder Master => | ||
new SqlConnectionStringBuilder | ||
{ | ||
DataSource = @"(LocalDB)\MSSQLLocalDB", | ||
InitialCatalog = "master", | ||
IntegratedSecurity = true | ||
}; | ||
|
||
private static SqlConnectionStringBuilder GestaoEscolar => | ||
new SqlConnectionStringBuilder | ||
{ | ||
DataSource = @"(LocalDB)\MSSQLLocalDB", | ||
InitialCatalog = "GestaoEscolar", | ||
IntegratedSecurity = true | ||
}; | ||
|
||
private static string Filename => Path.Combine( | ||
Path.GetDirectoryName( | ||
typeof(TestSetup).GetTypeInfo().Assembly.Location), | ||
"GestaoEscolar.mdf"); | ||
} | ||
} |
43 changes: 0 additions & 43 deletions
43
Demo.GestaoEscolar.WebApplication.Test/PessoaFisicaControllerTests.cs
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.