Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ jobs:
- checkout
- run: dotnet tool install -g dotnet-reportgenerator-globaltool
- run: dotnet build -c Debug
- run: dotnet test -c Debug --no-build -p:CollectCoverage=true -p:CoverletOutputFormat=opencover --test-adapter-path:. --logger:"xunit;LogFilePath=results.xml" < /dev/null
- run: dotnet test -c Debug --no-build -p:CollectCoverage=true -p:CoverletOutputFormat=opencover -p:ExcludeByAttribute="GeneratedCodeAttribute" --test-adapter-path:. --logger:"xunit;LogFilePath=results.xml" < /dev/null
- run: curl -s https://codecov.io/bash > codecov
- run: chmod +x codecov
- run: ./codecov -f ./src/MySQLToCsharp.Tests/coverage.netcoreapp3.1.opencover.xml
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -352,5 +352,5 @@ MigrationBackup/

# custom
launchSettings.json
coverage.opencover.xml
coverage.*.opencover.xml
results.xml
76 changes: 76 additions & 0 deletions src/MySQLToCsharp.Tests/CommandLineDirTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using Cocona;
using FluentAssertions;
using Microsoft.Extensions.Logging;
using MySQLToCsharp.Tests.Helper;
using System.Linq;
using Xunit;

namespace MySQLToCsharp.Tests
{
// collection test will execute serial
[Collection("cli")]
public class CommandLineDirTest
{
[Fact]
public void DirExecutionTest()
{
var args = new[] { "dir", "-i", "test_data/simple/", "-o", "hoge", "-n", "Fuga" };
CoconaApp.Create().ConfigureLogging(logging =>
{
logging.ReplaceToTraceLogger();
logging.SetMinimumLevel(LogLevel.Trace);
})
.Run<QueryToCSharp>(args);
var expected = @"// Code generated by SqlToCsharp

using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace Fuga
{
public partial class ships_gun
{
public int guns_id { get; set; }
public int ship_id { get; set; }
}
}
";
var (logLevel, msg) = TraceLogger.Stack.First(x => x.logLevel == LogLevel.Trace);
msg.Should().Be(expected);
}

[Fact]
public void DirExecutionAnnotationTest()
{
var args = new[] { "dir", "-i", "test_data/simple_annotation/", "-o", "hoge", "-n", "Fuga" };
CoconaApp.Create().ConfigureLogging(logging =>
{
logging.ReplaceToTraceLogger();
logging.SetMinimumLevel(LogLevel.Trace);
})
.Run<QueryToCSharp>(args);
var expected = @"// Code generated by SqlToCsharp

using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace Fuga
{
public partial class quengine
{
public int id { get; set; }
[Required]
[StringLength(10)]
public string class { get; set; }
[Required]
public byte[] data { get; set; }
}
}
";
var (logLevel, msg) = TraceLogger.Stack.First(x => x.logLevel == LogLevel.Trace);
msg.Should().Be(expected);
}
}
}
76 changes: 76 additions & 0 deletions src/MySQLToCsharp.Tests/CommandLineFileTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using Cocona;
using FluentAssertions;
using Microsoft.Extensions.Logging;
using MySQLToCsharp.Tests.Helper;
using System.Linq;
using Xunit;

namespace MySQLToCsharp.Tests
{
// collection test will execute serial
[Collection("cli")]
public class CommandLineFileTest
{
[Fact]
public void FileExecutionTest()
{
var args = new[] { "file", "-i", "test_data/simple/create_tables_simple.sql", "-o", "hoge", "-n", "Fuga" };
CoconaApp.Create().ConfigureLogging(logging =>
{
logging.ReplaceToTraceLogger();
logging.SetMinimumLevel(LogLevel.Trace);
})
.Run<QueryToCSharp>(args);
var expected = @"// Code generated by SqlToCsharp

using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace Fuga
{
public partial class ships_gun
{
public int guns_id { get; set; }
public int ship_id { get; set; }
}
}
";
var (logLevel, msg) = TraceLogger.Stack.First(x => x.logLevel == LogLevel.Trace);
msg.Should().Be(expected);
}

[Fact]
public void FileExecutionAnnotationTest()
{
var args = new[] { "file", "-i", "test_data/simple_annotation/create_tables_simple_annotation.sql", "-o", "hoge", "-n", "Fuga" };
CoconaApp.Create().ConfigureLogging(logging =>
{
logging.ReplaceToTraceLogger();
logging.SetMinimumLevel(LogLevel.Trace);
})
.Run<QueryToCSharp>(args);
var expected = @"// Code generated by SqlToCsharp

using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace Fuga
{
public partial class quengine
{
public int id { get; set; }
[Required]
[StringLength(10)]
public string class { get; set; }
[Required]
public byte[] data { get; set; }
}
}
";
var (logLevel, msg) = TraceLogger.Stack.First(x => x.logLevel == LogLevel.Trace);
msg.Should().Be(expected);
}
}
}
76 changes: 76 additions & 0 deletions src/MySQLToCsharp.Tests/CommandLineQueryTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using Cocona;
using FluentAssertions;
using Microsoft.Extensions.Logging;
using MySQLToCsharp.Tests.Helper;
using System.Linq;
using Xunit;

namespace MySQLToCsharp.Tests
{
// collection test will execute serial
[Collection("cli")]
public class CommandLineQueryTest
{
[Fact]
public void QueryExecutionTest()
{
var args = new[] { "query", "-i", "create table ships_guns(guns_id int, ship_id int);", "-o", "hoge", "-n", "Fuga" };
CoconaApp.Create().ConfigureLogging(logging =>
{
logging.ReplaceToTraceLogger();
logging.SetMinimumLevel(LogLevel.Trace);
})
.Run<QueryToCSharp>(args);
var expected = @"// Code generated by SqlToCsharp

using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace Fuga
{
public partial class ships_gun
{
public int guns_id { get; set; }
public int ship_id { get; set; }
}
}
";
var (logLevel, msg) = TraceLogger.Stack.First(x => x.logLevel == LogLevel.Trace);
msg.Should().Be(expected);
}

[Fact]
public void QueryExecutionAnnotationTest()
{
var args = new[] { "query", "-i", "create table quengine(id int auto_increment key, class varchar(10), data binary) engine='InnoDB';", "-o", "hoge", "-n", "Fuga" };
CoconaApp.Create().ConfigureLogging(logging =>
{
logging.ReplaceToTraceLogger();
logging.SetMinimumLevel(LogLevel.Trace);
})
.Run<QueryToCSharp>(args);
var expected = @"// Code generated by SqlToCsharp

using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace Fuga
{
public partial class quengine
{
public int id { get; set; }
[Required]
[StringLength(10)]
public string class { get; set; }
[Required]
public byte[] data { get; set; }
}
}
";
var (logLevel, msg) = TraceLogger.Stack.First(x => x.logLevel == LogLevel.Trace);
msg.Should().Be(expected);
}
}
}
4 changes: 0 additions & 4 deletions src/MySQLToCsharp.Tests/CreateTableParseTableUnitTest.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
using FluentAssertions;
using MySQLToCsharp.Listeners;
using MySQLToCsharp.Parsers;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Xunit;

namespace MySQLToCsharp.Tests
Expand Down
85 changes: 85 additions & 0 deletions src/MySQLToCsharp.Tests/Helper/TraceLogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;

namespace MySQLToCsharp.Tests.Helper
{
public class TraceLoggerProvider : ILoggerProvider
{
readonly TraceLogger loggerDefault;

public TraceLoggerProvider() => loggerDefault = new TraceLogger(LogLevel.Trace);
public ILogger CreateLogger(string categoryName) => loggerDefault;
public void Dispose() { }
}
public class TraceLogger : ILogger
{
public static Stack<(LogLevel logLevel, string msg)> Stack = new Stack<(LogLevel, string)>();
readonly LogLevel minimumLogLevel;
public TraceLogger(LogLevel minimumLogLevel)
{
Stack.Clear();
this.minimumLogLevel = minimumLogLevel;
}
public IDisposable BeginScope<TState>(TState state) => NullDisposable.Instance;
public bool IsEnabled(LogLevel logLevel) => minimumLogLevel <= logLevel;

public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
{
if (formatter == null) throw new ArgumentNullException(nameof(formatter));
if (minimumLogLevel > logLevel) return;
var msg = formatter(state, exception);

if (!string.IsNullOrEmpty(msg))
{
Stack.Push((logLevel, msg));
}

if (exception != null)
{
Stack.Push((logLevel, msg));
}
}

class NullDisposable : IDisposable
{
public static readonly IDisposable Instance = new NullDisposable();
public void Dispose()
{
return;
}
}
}
public static class TraceLoggerExtensions
{
/// <summary>
/// use ConsoleAppFramework.Logging.SimpleConsoleLogger.
/// </summary>
/// <param name="builder"></param>
/// <returns></returns>
public static ILoggingBuilder AddTraceLogger(this ILoggingBuilder builder)
{
builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton<ILoggerProvider, TraceLoggerProvider>());
return builder;
}

/// <summary>
/// Remove default ConsoleLoggerProvider and replace to SimpleConsoleLogger.
/// </summary>
public static ILoggingBuilder ReplaceToTraceLogger(this ILoggingBuilder builder)
{
// Use SimpleConsoleLogger instead of the default ConsoleLogger.
var consoleLogger = builder.Services.FirstOrDefault(x => x.ImplementationType?.FullName == "Microsoft.Extensions.Logging.Console.ConsoleLoggerProvider");
if (consoleLogger != null)
{
builder.Services.Remove(consoleLogger);
}

builder.AddTraceLogger();
return builder;
}
}
}
2 changes: 1 addition & 1 deletion src/MySQLToCsharp.Tests/MySQLToCsharp.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
</ItemGroup>

<ItemGroup>
<None Update="test_data\*.sql">
<None Update="test_data/**/*.sql">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
create table ships_guns(guns_id int, ship_id int);
create table quengine(id int auto_increment key, class varchar(10), data binary) engine='InnoDB';
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
create table quengine(id int auto_increment key, class varchar(10), data binary) engine='InnoDB';
Loading