diff --git a/TestAPI.sln b/TestAPI.sln index 19a1031..3755108 100644 --- a/TestAPI.sln +++ b/TestAPI.sln @@ -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 @@ -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 diff --git a/TestAPI/Database/WeatherDatabase.cs b/TestAPI/Database/WeatherDatabase.cs index 6fce733..850eb79 100644 --- a/TestAPI/Database/WeatherDatabase.cs +++ b/TestAPI/Database/WeatherDatabase.cs @@ -8,8 +8,11 @@ public class WeatherDatabase : DbContext, IWeatherDatabase public WeatherDatabase(DbContextOptions options) : base(options) { } - - public virtual DbSet Summaries { get; set; } + public static string ConnectionString + { + get { return "Data Source=SPOPYPC;Initial Catalog=Weather;Integrated Security=True;"; } + } + public virtual DbSet Summaries { get; set; } public virtual DbSet Forecasts { get; set; } diff --git a/TestAPI/Models/SummaryList.cs b/TestAPI/Models/SummaryList.cs new file mode 100644 index 0000000..b669047 --- /dev/null +++ b/TestAPI/Models/SummaryList.cs @@ -0,0 +1,12 @@ +using System.Collections.Generic; +using System.Linq; +namespace TestAPI.Models +{ + public class SummaryList : List + { + public Summary GetSummaries(int celsius) + { + return this.Single(s => (!s.CelsiusLow.HasValue || celsius >= s.CelsiusLow.Value) && (!s.CelsiusHigh.HasValue || celsius < s.CelsiusHigh.Value)); + } + } +} diff --git a/TestAPI/Services/WeatherForecastService.cs b/TestAPI/Services/WeatherForecastService.cs index 54b9e8d..29002e1 100644 --- a/TestAPI/Services/WeatherForecastService.cs +++ b/TestAPI/Services/WeatherForecastService.cs @@ -19,46 +19,52 @@ public WeatherForecastService(IWeatherDatabase weatherDatabase) _weatherDatabase = weatherDatabase; } - public async IAsyncEnumerable 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 summaries = null; - - for (var currentDate = startDate; currentDate < endDate; currentDate += TimeSpan.FromDays(1)) - { - if (!forecasts.TryGetValue(currentDate, out var forecast)) + public async IAsyncEnumerable 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); + } + } } - } } \ No newline at end of file diff --git a/TestAPI/Startup.cs b/TestAPI/Startup.cs index fbaa11e..edae55b 100644 --- a/TestAPI/Startup.cs +++ b/TestAPI/Startup.cs @@ -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(options => options.UseSqlServer("Data Source=(localdb)\\mssqllocaldb;Initial Catalog=Weather;Integrated Security=True;")); + services.AddDbContext(options => options.UseSqlServer(WeatherDatabase.ConnectionString)); services.AddScoped(sp => sp.GetRequiredService()); services.AddControllers(); services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo {Title = "TestAPI", Version = "v1"}); }); diff --git a/TestAPI/TestAPI.csproj b/TestAPI/TestAPI.csproj index 9b3812e..32588c6 100644 --- a/TestAPI/TestAPI.csproj +++ b/TestAPI/TestAPI.csproj @@ -1,7 +1,7 @@ - + - net5.0 + net6.0 diff --git a/TestDB/Stored Procedures/usp_movecase.sql b/TestDB/Stored Procedures/usp_movecase.sql index ea3452f..78202c4 100644 --- a/TestDB/Stored Procedures/usp_movecase.sql +++ b/TestDB/Stored Procedures/usp_movecase.sql @@ -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 @@ -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 diff --git a/TestUI/src/app/app.component.html b/TestUI/src/app/app.component.html index 56bba0c..d6082d4 100644 --- a/TestUI/src/app/app.component.html +++ b/TestUI/src/app/app.component.html @@ -3,10 +3,10 @@
-
{{weather.date}}
-
{{weather.temperatureF}} °F
+
{{weather.date | date:'dd/MM/yyyy'}}
+
{{weather.temperatureF}} °F
{{weather.temperatureC}} °C
-

{{weather.summary}}

+

{{weather.summary}}

diff --git a/TestUI/src/app/app.component.scss b/TestUI/src/app/app.component.scss index 64a2c06..0264295 100644 --- a/TestUI/src/app/app.component.scss +++ b/TestUI/src/app/app.component.scss @@ -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; +} \ No newline at end of file diff --git a/TestUI/src/environments/environment.ts b/TestUI/src/environments/environment.ts index 12ba3e4..05727ae 100644 --- a/TestUI/src/environments/environment.ts +++ b/TestUI/src/environments/environment.ts @@ -4,7 +4,7 @@ export const environment = { production: false, - url:'https://localhost:5001' + url:'http://localhost:5100' }; /* diff --git a/UnitTestAPI/GlobalUsings.cs b/UnitTestAPI/GlobalUsings.cs new file mode 100644 index 0000000..cefced4 --- /dev/null +++ b/UnitTestAPI/GlobalUsings.cs @@ -0,0 +1 @@ +global using NUnit.Framework; \ No newline at end of file diff --git a/UnitTestAPI/UnitTest1.cs b/UnitTestAPI/UnitTest1.cs new file mode 100644 index 0000000..c51ada4 --- /dev/null +++ b/UnitTestAPI/UnitTest1.cs @@ -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(); + 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... + } + } + } \ No newline at end of file diff --git a/UnitTestAPI/UnitTestAPI.csproj b/UnitTestAPI/UnitTestAPI.csproj new file mode 100644 index 0000000..1083fdc --- /dev/null +++ b/UnitTestAPI/UnitTestAPI.csproj @@ -0,0 +1,24 @@ + + + + net8.0 + enable + enable + + false + true + + + + + + + + + + + + + + +