Skip to content

Commit

Permalink
test: add tests for generating schema (#11)
Browse files Browse the repository at this point in the history
Co-authored-by: Giau. Tran Minh <[email protected]>
  • Loading branch information
luantranminh and giautm authored Jul 31, 2024
1 parent e434ac6 commit 8d5ad94
Show file tree
Hide file tree
Showing 11 changed files with 225 additions and 10 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ jobs:
- name: Dotnet Cake Build
shell: pwsh
run: dotnet cake --target=Build
- name: Dotnet Cake Test
shell: pwsh
run: dotnet cake --target=Test
- name: Dotnet Cake Pack
shell: pwsh
run: dotnet cake --target=Pack
Expand Down
9 changes: 9 additions & 0 deletions AtlasProvider.sln
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Atlas.Provider.Loader", "sr
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Atlas.Provider.Core", "src\Atlas.Provider.Core\Atlas.Provider.Core.csproj", "{FAEB024F-B0AA-4B7C-A820-68E497657D0D}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{A0F935A4-52B6-4790-AA08-A86E0CB562E9}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Atlas.Provider.Test", "test\Atlas.Provider.Test\Atlas.Provider.Test.csproj", "{D34350D9-C254-4187-A908-00EA03DDE2B5}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -26,9 +30,14 @@ Global
{FAEB024F-B0AA-4B7C-A820-68E497657D0D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FAEB024F-B0AA-4B7C-A820-68E497657D0D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FAEB024F-B0AA-4B7C-A820-68E497657D0D}.Release|Any CPU.Build.0 = Release|Any CPU
{D34350D9-C254-4187-A908-00EA03DDE2B5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D34350D9-C254-4187-A908-00EA03DDE2B5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D34350D9-C254-4187-A908-00EA03DDE2B5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D34350D9-C254-4187-A908-00EA03DDE2B5}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{003BD0CC-7AF6-408C-A40D-3FE8F6C73537} = {C41D6C7A-456B-4A0B-ACE1-9931DA092288}
{FAEB024F-B0AA-4B7C-A820-68E497657D0D} = {C41D6C7A-456B-4A0B-ACE1-9931DA092288}
{D34350D9-C254-4187-A908-00EA03DDE2B5} = {A0F935A4-52B6-4790-AA08-A86E0CB562E9}
EndGlobalSection
EndGlobal
12 changes: 12 additions & 0 deletions build.cake
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,18 @@ Task("Build")
});
});

Task("Test")
.Description("Runs unit tests")
.DoesForEach(
GetFiles("./test/**/*.csproj"),
project =>
{
DotNetTest(project.ToString(),new DotNetTestSettings()
{
Configuration = configuration,
});
}).DeferOnError();

Task("Pack")
.Description("Creates NuGet packages and outputs them to the artifacts directory.")
.Does(() =>
Expand Down
8 changes: 4 additions & 4 deletions src/Atlas.Provider.Core/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ static void Main(
continue;
}
var sql = executor.ScriptDbContext(name);
if (ctxInfo["ProviderName"]!.ToString()!.EndsWith("SqlServer"))
{
sql = "-- atlas:delimiter GO \n" + sql;
}
if (!string.IsNullOrEmpty(sql))
{
if (ctxInfo["ProviderName"]!.ToString()!.EndsWith("SqlServer"))
{
Console.WriteLine("-- atlas:delimiter GO");
}
Console.WriteLine(sql);
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/Atlas.Provider.Demo/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ public BloggingContext CreateDbContext(string[] args)
{
var provider = args.FirstOrDefault();

return new BloggingContext(provider);
return new BloggingContext(provider!);
}
}
public class BloggingContext : DbContext
{
public DbSet<Blog> Blogs { get; set; }
public DbSet<Blog>? Blogs { get; set; }

private readonly string _provider;
public BloggingContext(string provider = "SqlServer")
Expand Down Expand Up @@ -79,14 +79,14 @@ public class Blog
public int BlogId { get; set; }

[Column(TypeName = "varchar(200)")]
public string Url { get; set; }
public string? Url { get; set; }

[Column(TypeName = "decimal(5, 2)")]
public decimal Rating { get; set; }
public string Title { get; set; } = string.Empty;
public string Content { get; set; } = string.Empty;
public string Author { get; set; } = string.Empty;
public List<Post> Posts { get; set; }
public List<Post>? Posts { get; set; }
}

public class Post
Expand All @@ -95,7 +95,7 @@ public class Post
public int PostId { get; set; }
public string Title { get; set; } = string.Empty;
public string Content { get; set; } = string.Empty;
public string BlogUrl { get; set; }
public Blog Blog { get; set; }
public string? BlogUrl { get; set; }
public Blog? Blog { get; set; }
}
}
36 changes: 36 additions & 0 deletions test/Atlas.Provider.Test/Atlas.Provider.Test.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="6.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.0" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Atlas.Provider.Demo\Atlas.Provider.Demo.csproj" />
<ProjectReference Include="..\..\src\Atlas.Provider.Core\Atlas.Provider.Core.csproj" />
<ProjectReference Include="..\..\src\Atlas.Provider.Loader\Atlas.Provider.Loader.csproj" />
</ItemGroup>

<ItemGroup>
<None Remove="data\*" />
</ItemGroup>

<ItemGroup>
<Content Include="data\*">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>

</Project>
46 changes: 46 additions & 0 deletions test/Atlas.Provider.Test/GenerateSchemaTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using Xunit;
using System.Diagnostics;
using System.Reflection;

public class GenerateSchemaTest
{
[Theory]
[InlineData("SqlServer", "data/sqlserver_default")]
[InlineData("Postgres", "data/postgres_default")]
[InlineData("MySql", "data/mysql_default")]
[InlineData("Sqlite", "data/sqlite_default")]
public void Can_generate_script(string providerName, string expectedFile)
{
var dllFileName = Assembly.Load(new AssemblyName("Atlas.Provider.Loader")).Location;

ProcessStartInfo startInfo = new ProcessStartInfo
{
WorkingDirectory = Path.GetFullPath("../../../../../src/Atlas.Provider.Demo"),
FileName = "dotnet",
Arguments = $"exec {dllFileName} -- {providerName}",
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true
};

using Process? process = Process.Start(startInfo);
Assert.NotNull(process);
string output = process.StandardOutput.ReadToEnd();
string error = process.StandardError.ReadToEnd();
process.WaitForExit();
Assert.Equal(FileReader.Read(expectedFile), output);
Assert.Equal("", error);
}
}
internal static class FileReader
{
public static string Read(string filePath)
{
return File.ReadAllText(
Path.Combine(
Directory.GetCurrentDirectory(),
filePath
));
}
}
29 changes: 29 additions & 0 deletions test/Atlas.Provider.Test/data/mysql_default
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
ALTER DATABASE CHARACTER SET utf8mb4;


CREATE TABLE `Blogs` (
`BlogId` int NOT NULL AUTO_INCREMENT,
`Url` varchar(200) CHARACTER SET utf8mb4 NOT NULL,
`Rating` decimal(5,2) NOT NULL,
`Title` longtext CHARACTER SET utf8mb4 NOT NULL,
`Content` longtext CHARACTER SET utf8mb4 NOT NULL,
`Author` varchar(200) CHARACTER SET utf8mb4 NOT NULL DEFAULT 'Anonymous',
CONSTRAINT `PK_Blogs` PRIMARY KEY (`BlogId`),
CONSTRAINT `AK_Blogs_Url` UNIQUE (`Url`)
) CHARACTER SET=utf8mb4;


CREATE TABLE `Post` (
`PostId` int NOT NULL AUTO_INCREMENT,
`Title` longtext CHARACTER SET utf8mb4 NOT NULL,
`Content` longtext CHARACTER SET utf8mb4 NOT NULL,
`BlogUrl` varchar(200) CHARACTER SET utf8mb4 NULL,
CONSTRAINT `PK_Post` PRIMARY KEY (`PostId`),
CONSTRAINT `FK_Post_Blogs_BlogUrl` FOREIGN KEY (`BlogUrl`) REFERENCES `Blogs` (`Url`)
) CHARACTER SET=utf8mb4;


CREATE INDEX `IX_Post_BlogUrl` ON `Post` (`BlogUrl`);



26 changes: 26 additions & 0 deletions test/Atlas.Provider.Test/data/postgres_default
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
CREATE TABLE "Blogs" (
"BlogId" integer GENERATED BY DEFAULT AS IDENTITY,
"Url" varchar(200) NOT NULL,
"Rating" numeric(5,2) NOT NULL,
"Title" text NOT NULL,
"Content" text NOT NULL,
"Author" character varying(200) NOT NULL DEFAULT 'Anonymous',
CONSTRAINT "PK_Blogs" PRIMARY KEY ("BlogId"),
CONSTRAINT "AK_Blogs_Url" UNIQUE ("Url")
);


CREATE TABLE "Post" (
"PostId" integer GENERATED BY DEFAULT AS IDENTITY,
"Title" text NOT NULL,
"Content" text NOT NULL,
"BlogUrl" varchar(200) NULL,
CONSTRAINT "PK_Post" PRIMARY KEY ("PostId"),
CONSTRAINT "FK_Post_Blogs_BlogUrl" FOREIGN KEY ("BlogUrl") REFERENCES "Blogs" ("Url")
);


CREATE INDEX "IX_Post_BlogUrl" ON "Post" ("BlogUrl");



24 changes: 24 additions & 0 deletions test/Atlas.Provider.Test/data/sqlite_default
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
CREATE TABLE "Blogs" (
"BlogId" INTEGER NOT NULL CONSTRAINT "PK_Blogs" PRIMARY KEY AUTOINCREMENT,
"Url" varchar(200) NOT NULL,
"Rating" decimal(5, 2) NOT NULL,
"Title" TEXT NOT NULL,
"Content" TEXT NOT NULL,
"Author" TEXT NOT NULL DEFAULT 'Anonymous',
CONSTRAINT "AK_Blogs_Url" UNIQUE ("Url")
);


CREATE TABLE "Post" (
"PostId" INTEGER NOT NULL CONSTRAINT "PK_Post" PRIMARY KEY AUTOINCREMENT,
"Title" TEXT NOT NULL,
"Content" TEXT NOT NULL,
"BlogUrl" varchar(200) NULL,
CONSTRAINT "FK_Post_Blogs_BlogUrl" FOREIGN KEY ("BlogUrl") REFERENCES "Blogs" ("Url")
);


CREATE INDEX "IX_Post_BlogUrl" ON "Post" ("BlogUrl");



30 changes: 30 additions & 0 deletions test/Atlas.Provider.Test/data/sqlserver_default
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
-- atlas:delimiter GO
CREATE TABLE [Blogs] (
[BlogId] int NOT NULL IDENTITY,
[Url] varchar(200) NOT NULL,
[Rating] decimal(5,2) NOT NULL,
[Title] nvarchar(max) NOT NULL,
[Content] nvarchar(max) NOT NULL,
[Author] nvarchar(200) NOT NULL DEFAULT N'Anonymous',
CONSTRAINT [PK_Blogs] PRIMARY KEY ([BlogId]),
CONSTRAINT [AK_Blogs_Url] UNIQUE ([Url])
);
GO


CREATE TABLE [Post] (
[PostId] int NOT NULL IDENTITY,
[Title] nvarchar(max) NOT NULL,
[Content] nvarchar(max) NOT NULL,
[BlogUrl] varchar(200) NULL,
CONSTRAINT [PK_Post] PRIMARY KEY ([PostId]),
CONSTRAINT [FK_Post_Blogs_BlogUrl] FOREIGN KEY ([BlogUrl]) REFERENCES [Blogs] ([Url])
);
GO


CREATE INDEX [IX_Post_BlogUrl] ON [Post] ([BlogUrl]);
GO



0 comments on commit 8d5ad94

Please sign in to comment.