Skip to content

Stefano #31

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

Open
wants to merge 3 commits into
base: main
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
6 changes: 6 additions & 0 deletions TestAPI.sln
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
Readme.md = Readme.md
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnitTestAPI", "UnitTestAPI\UnitTestAPI.csproj", "{47102326-3669-4003-A5BD-8C025F25788F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -28,6 +30,10 @@ Global
{A720BF0D-868C-404E-9A87-410694A01C6F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A720BF0D-868C-404E-9A87-410694A01C6F}.Release|Any CPU.Build.0 = Release|Any CPU
{A720BF0D-868C-404E-9A87-410694A01C6F}.Release|Any CPU.Deploy.0 = Release|Any CPU
{47102326-3669-4003-A5BD-8C025F25788F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{47102326-3669-4003-A5BD-8C025F25788F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{47102326-3669-4003-A5BD-8C025F25788F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{47102326-3669-4003-A5BD-8C025F25788F}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
7 changes: 5 additions & 2 deletions TestAPI/Database/WeatherDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ public class WeatherDatabase : DbContext, IWeatherDatabase
public WeatherDatabase(DbContextOptions<WeatherDatabase> options) : base(options)
{
}

public virtual DbSet<Summary> Summaries { get; set; }
public static string ConnectionString
{
get { return "Data Source=SPOPYPC;Initial Catalog=Weather;Integrated Security=True;"; }
}
public virtual DbSet<Summary> Summaries { get; set; }

public virtual DbSet<Forecast> Forecasts { get; set; }

Expand Down
12 changes: 12 additions & 0 deletions TestAPI/Models/SummaryList.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Collections.Generic;
using System.Linq;
namespace TestAPI.Models
{
public class SummaryList : List<Summary>
{
public Summary GetSummaries(int celsius)
{
return this.Single(s => (!s.CelsiusLow.HasValue || celsius >= s.CelsiusLow.Value) && (!s.CelsiusHigh.HasValue || celsius < s.CelsiusHigh.Value));
}
}
}
78 changes: 42 additions & 36 deletions TestAPI/Services/WeatherForecastService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,46 +19,52 @@ public WeatherForecastService(IWeatherDatabase weatherDatabase)
_weatherDatabase = weatherDatabase;
}

public async IAsyncEnumerable<WeatherForecast> GetAsync(int number, [EnumeratorCancellation] CancellationToken token)
{
var startDate = DateTime.Today;
var endDate = startDate + TimeSpan.FromDays(number);
var forecasts = await _weatherDatabase.Forecasts.Include(x => x.Summary).Where(x => x.Id >= startDate && x.Id < endDate).ToDictionaryAsync(x => x.Id, token);
var dirty = false;
List<Summary> summaries = null;

for (var currentDate = startDate; currentDate < endDate; currentDate += TimeSpan.FromDays(1))
{
if (!forecasts.TryGetValue(currentDate, out var forecast))
public async IAsyncEnumerable<WeatherForecast> GetAsync(int number, [EnumeratorCancellation] CancellationToken token)
{
summaries ??= await _weatherDatabase.Summaries.AsQueryable().ToListAsync(token);
var celsius = _rng.Next(-20, 55);
var summary = summaries.Single(s => (!s.CelsiusLow.HasValue || celsius >= s.CelsiusLow.Value) && (!s.CelsiusHigh.HasValue || celsius < s.CelsiusHigh.Value));
var startDate = DateTime.Today;
var endDate = startDate + TimeSpan.FromDays(number);
var forecasts = await _weatherDatabase.Forecasts.Include(x => x.Summary).Where(x => x.Id >= startDate && x.Id < endDate).ToDictionaryAsync(x => x.Id, token);
var dirty = false;
SummaryList summaries = null;

forecast = new Forecast
{
Celsius = celsius,
Id = currentDate,
SummaryId = summary.Id,
Summary = summary
};
for (var currentDate = startDate; currentDate < endDate; currentDate += TimeSpan.FromDays(1))
{
if (!forecasts.TryGetValue(currentDate, out var forecast))
{
if (summaries == null)
{
summaries = new();
summaries.AddRange(await _weatherDatabase.Summaries.AsQueryable().ToListAsync(token));
}
var celsius = _rng.Next(-20, 55);

_weatherDatabase.Forecasts.Add(forecast);
dirty = true;
}
var summary = summaries.GetSummaries(celsius);
//var summary = summaries.Single(s => (!s.CelsiusLow.HasValue || celsius >= s.CelsiusLow.Value) && (!s.CelsiusHigh.HasValue || celsius < s.CelsiusHigh.Value));

yield return new WeatherForecast
{
Date = forecast.Id,
Summary = forecast.Summary.Id,
TemperatureC = forecast.Celsius
};
}
forecast = new Forecast
{
Celsius = celsius,
Id = currentDate,
SummaryId = summary.Id,
Summary = summary
};

if (dirty)
{
await _weatherDatabase.SaveChangesAsync(token);
}
_weatherDatabase.Forecasts.Add(forecast);
dirty = true;
}

yield return new WeatherForecast
{
Date = forecast.Id,
Summary = forecast.Summary.Id,
TemperatureC = forecast.Celsius
};
}

if (dirty)
{
await _weatherDatabase.SaveChangesAsync(token);
}
}
}
}
}
2 changes: 1 addition & 1 deletion TestAPI/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public Startup(IConfiguration configuration)
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<WeatherDatabase>(options => options.UseSqlServer("Data Source=(localdb)\\mssqllocaldb;Initial Catalog=Weather;Integrated Security=True;"));
services.AddDbContext<WeatherDatabase>(options => options.UseSqlServer(WeatherDatabase.ConnectionString));
services.AddScoped<IWeatherDatabase>(sp => sp.GetRequiredService<WeatherDatabase>());
services.AddControllers();
services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo {Title = "TestAPI", Version = "v1"}); });
Expand Down
4 changes: 2 additions & 2 deletions TestAPI/TestAPI.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
Expand Down
42 changes: 38 additions & 4 deletions TestDB/Stored Procedures/usp_movecase.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,45 @@ BEGIN

DECLARE @Result AS INT = 0
DECLARE @_errormessage NVARCHAR(MAX)

Declare @Pcategory uniqueidentifier
Declare @Ppallet uniqueidentifier
Declare @Newpallet uniqueidentifier
BEGIN TRANSACTION
BEGIN TRY

PRINT 'your code goes here'
Select @Ppallet = C.Palletguid,
@Pcategory = Prd.Productcategoryguid
From [Case] C
Inner Join Product Prd on ( Prd.guid = C.Productguid)
where c.guid = @casetomove

if (@Ppallet is null Or @Pcategory is null) Throw 50000,'Case not found',1

Select Top 1 @Newpallet = P.guid
From Pallet P
Inner join [Case] C on (P.guid = C.Palletguid)
Inner Join Product Prd on ( Prd.guid = C.Productguid)
Where P.guid <> @Ppallet
And Prd.productCategoryguid = @PCategory

print 'Check found pallet'
if ( @Newpallet is null)
Begin
print 'Search empty pallet'

Select Top 1 @NewPallet = P.guid
From Pallet P
Where Not Exists ( Select 1 From [Case] C Where C.Palletguid = p.guid)

End

if ( @Newpallet is null) Throw 50000,'No pallet avaiable',1

Update [Case]
set PalletGuid = @NewPallet,
modifieddate = GetDate()
Where guid = @casetomove


END TRY
BEGIN CATCH
Expand All @@ -26,12 +60,12 @@ BEGIN
IF (@Result >= 0)
BEGIN
COMMIT TRANSACTION
PRINT 'Commited Transaction at ' + GETDATE()
PRINT 'Commited Transaction at ' + Convert(Varchar(20), GETDATE(),120)
END
ELSE
BEGIN
ROLLBACK TRANSACTION
PRINT 'Rolled back Transaction at ' + GETDATE() + 'because of error: ' + @_errormessage
PRINT 'Rolled back Transaction at ' + Convert(Varchar(20), GETDATE(),120) + 'because of error: ' + @_errormessage
END
END

Expand Down
6 changes: 3 additions & 3 deletions TestUI/src/app/app.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
<div class="col-6 offset-3" *ngFor="let weather of weatherData">
<div class="card">
<div class="card-body">
<h5 class="card-title">{{weather.date}}</h5>
<h6 class="card-subtitle mb-2 text-muted">{{weather.temperatureF}} &#176;F </h6>
<h5 class="card-title">{{weather.date | date:'dd/MM/yyyy'}}</h5>
<h6 class="card-subtitle mb-2 text-muted ">{{weather.temperatureF}} &#176;F </h6>
<h6 class="card-subtitle mb-2 text-muted">{{weather.temperatureC}} &#176;C</h6>
<p class="card-text">{{weather.summary}}</p>
<p class="card-text {{ weather.summary}}">{{weather.summary}}</p>
</div>
</div>
</div>
Expand Down
13 changes: 13 additions & 0 deletions TestUI/src/app/app.component.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
.card {
top: 1rem;
}
.Freezing, .Bracing, .Chilly{
color:cyan;
}
.Mild, .Balmy, .Cool{
color:green;
}

.Warm, .Hot{
color:orange;
}
.Sweltering, .Scorching{
color: red;
}
2 changes: 1 addition & 1 deletion TestUI/src/environments/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

export const environment = {
production: false,
url:'https://localhost:5001'
url:'http://localhost:5100'
};

/*
Expand Down
1 change: 1 addition & 0 deletions UnitTestAPI/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global using NUnit.Framework;
98 changes: 98 additions & 0 deletions UnitTestAPI/UnitTest1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
using TestAPI.Database;
using TestAPI.Models;
namespace UnitTestAPI
{
public class DbServices
{
private readonly WeatherDatabase _weatherDatabase;
public DbServices(WeatherDatabase weatherDatabase)
{
_weatherDatabase = weatherDatabase;
}
}
public class Tests
{
private WeatherDatabase myDb;
private SummaryList summaries;
[SetUp]
public void Setup()
{
var optionsBuilder = new DbContextOptionsBuilder<WeatherDatabase>();
optionsBuilder.UseSqlServer(WeatherDatabase.ConnectionString);

myDb = new WeatherDatabase(optionsBuilder.Options);

summaries = new();
summaries.AddRange(myDb.Summaries.AsQueryable().ToList());
}

[Test]
public void TestConnection()
{
try
{
SqlConnection conn = new SqlConnection(TestAPI.Database.WeatherDatabase.ConnectionString);
conn.Open();
conn.Close();
}
catch (Exception Ex)
{
Assert.Fail(Ex.Message);
}

}
[Test]
public void TestSummary()
{


var summary = summaries.GetSummaries(-5);

Assert.IsTrue(summary.Id.Equals("Freezing"));
summary = summaries.GetSummaries(15);

Assert.IsTrue(summary.Id.Equals("Mild"));

//Etc...
}
[Test]
public void TestInsert()
{
DateTime DtTest = new DateTime(1900, 1, 1);


var forecast = myDb.Forecasts.Find(DtTest);

if (forecast != null)
{
myDb.Forecasts.Remove(forecast);
myDb.SaveChanges();
}
Summary summary = summaries.GetSummaries(15);
forecast = new Forecast
{
Celsius = 15,
Id = DtTest,
SummaryId = summary.Id,
Summary = summary
};

myDb.Forecasts.Add(forecast);
myDb.SaveChanges();

forecast = myDb.Forecasts.Find(DtTest);
Assert.IsTrue(forecast != null);

myDb.Forecasts.Remove(forecast);
myDb.SaveChanges();

forecast = myDb.Forecasts.Find(DtTest);
Assert.IsTrue(forecast == null);


//Etc...
}
}
}
24 changes: 24 additions & 0 deletions UnitTestAPI/UnitTestAPI.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1" />
<PackageReference Include="NUnit.Analyzers" Version="3.6.1" />
<PackageReference Include="coverlet.collector" Version="6.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\TestAPI\TestAPI.csproj" />
</ItemGroup>

</Project>