Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hotfix/issues23 #25

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
45 changes: 45 additions & 0 deletions StrangerData.UnitTests/AnyTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@

using System.Globalization;
using Xunit;

namespace StrangerData.UnitTests
{
public class AnyTests
{
[Fact]
public void Decimal1x2Test()
{
decimal value = Any.Decimal(1,2);
string[] v = value.ToString().Split(CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator[0]);
Assert.True(v[0].Length <= 2, "Erro na quantidade de digitos.");
Assert.True(v[1].Length <= 5, "Erro na quantidade de decimais.");
}

[Fact]
public void Decimal5x4Test()
{
decimal value = Any.Decimal(5, 4);
string[] v = value.ToString().Split(CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator[0]);
Assert.True(v[0].Length <= 5, "Erro na quantidade de digitos.");
Assert.True(v[1].Length <= 4, "Erro na quantidade de decimais.");
}

[Fact]
public void Decimal10x8Test()
{
decimal value = Any.Decimal(10, 8);
string[] v = value.ToString().Split(CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator[0]);
Assert.True(v[0].Length <= 10, "Erro na quantidade de digitos.");
Assert.True(v[1].Length <= 8, "Erro na quantidade de decimais.");
}

[Fact]
public void Decimal4x0Test()
{
decimal value = Any.Decimal(4, 0);
string[] v = value.ToString().Split(CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator[0]);
Assert.True(v.Length == 1, "Erro, foi gerado valores em decimal");
Assert.True(v[0].Length <= 4, "Erro na quantidade de digitos.");
}
}
}
3 changes: 3 additions & 0 deletions StrangerData.UnitTests/StrangerData.UnitTests.csproj
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="..\..\authorization-message-service\packages\xunit.runner.visualstudio.2.1.0\build\net20\xunit.runner.visualstudio.props" Condition="Exists('..\..\authorization-message-service\packages\xunit.runner.visualstudio.2.1.0\build\net20\xunit.runner.visualstudio.props')" />
<Import Project="..\src\packages\xunit.runner.visualstudio.2.1.0\build\net20\xunit.runner.visualstudio.props" Condition="Exists('..\src\packages\xunit.runner.visualstudio.2.1.0\build\net20\xunit.runner.visualstudio.props')" />
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
Expand Down Expand Up @@ -75,6 +76,7 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="AnyTests.cs" />
<Compile Include="DataFactoryTests.cs" />
<Compile Include="Lib\FakeDbDialect.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand All @@ -97,6 +99,7 @@
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\src\packages\xunit.runner.visualstudio.2.1.0\build\net20\xunit.runner.visualstudio.props')" Text="$([System.String]::Format('$(ErrorText)', '..\src\packages\xunit.runner.visualstudio.2.1.0\build\net20\xunit.runner.visualstudio.props'))" />
<Error Condition="!Exists('..\..\authorization-message-service\packages\xunit.runner.visualstudio.2.1.0\build\net20\xunit.runner.visualstudio.props')" Text="$([System.String]::Format('$(ErrorText)', '..\..\authorization-message-service\packages\xunit.runner.visualstudio.2.1.0\build\net20\xunit.runner.visualstudio.props'))" />
</Target>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
9 changes: 9 additions & 0 deletions src/Any.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,15 @@ public static decimal Decimal()
return (decimal)(integerPart + random.NextDouble());
}

public static decimal Decimal(int digits, int decimals = 2)
{

int integerPart = Int(0, (int) Math.Pow(10, digits) - 1);
int integerPartDescimal = Int(0, (int) Math.Pow(10, decimals) - 1);

return (decimal)(integerPart + (integerPartDescimal/ Math.Pow(10, decimals)));
}

/// <summary>
/// Generates a random double-precision floating-point number.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion src/DataFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ private object GenerateValueForColumn(TableColumnInfo column)
}
return Any.Int(1, 10 ^ column.Precision - 2);
case ColumnType.Decimal:
return Any.Decimal();
return Any.Decimal(column.Precision, column.Scale);
case ColumnType.Double:
return Any.Double();
case ColumnType.Long:
Expand Down
25 changes: 19 additions & 6 deletions src/StrangerData.SqlServer/SqlServerDialect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ ORDER BY OUTCOLUMNS.column_id
case "TEXT":
case "NTEXT":
case "VARCHAR":
case "CHAR":
tableColumnInfo.ColumnType = ColumnType.String;
break;
case "ROWVERSION":
Expand All @@ -129,8 +130,6 @@ ORDER BY OUTCOLUMNS.column_id
case "BIT":
tableColumnInfo.ColumnType = ColumnType.Boolean;
break;
case "CHAR":
break;
case "TINYINT":
case "INT":
case "SMALLINT":
Expand Down Expand Up @@ -170,10 +169,24 @@ public override IDictionary<string, object> Insert(string tableName, IEnumerable
{
bool hasIdentity = tableSchemaInfo.Any(t => t.IsIdentity);

StringBuilder insertStatementBuilder = new StringBuilder()
.AppendFormat("INSERT INTO {0}", SanitizeTableName(tableName))
.AppendFormat("({0})", string.Join(",", values.Keys))
.AppendFormat(" VALUES ({0});", string.Join(",", values.Keys.Select(c => "@" + c)));
StringBuilder insertStatementBuilder;

if(hasIdentity == true && tableSchemaInfo.Count() == 1)
{
insertStatementBuilder = new StringBuilder()
.AppendFormat("INSERT INTO {0}", SanitizeTableName(tableName))
.Append("DEFAULT VALUES");

}
else
{
insertStatementBuilder = new StringBuilder()
.AppendFormat("INSERT INTO {0}", SanitizeTableName(tableName))
.AppendFormat("({0})", string.Join(",", values.Keys))
.AppendFormat(" VALUES ({0});", string.Join(",", values.Keys.Select(c => "@" + c)));

}

// .AppendFormat(" SELECT SCOPE_IDENTITY();")
// .ToString();

Expand Down