Skip to content

Commit f86cb2f

Browse files
committed
session 1 -donet
1 parent 1313f94 commit f86cb2f

File tree

181 files changed

+4166
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

181 files changed

+4166
-0
lines changed

.github/workflows/dotnet-ci.yaml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
name: .NET 8 CI
2+
3+
on:
4+
push:
5+
branches: [ "main" ]
6+
pull_request:
7+
branches: [ "main" ]
8+
9+
jobs:
10+
build-and-test:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- name: Checkout code
14+
uses: actions/checkout@v3
15+
16+
- name: Setup .NET 8
17+
uses: actions/setup-dotnet@v3
18+
with:
19+
dotnet-version: '8.0.x'
20+
21+
- name: Restore dependencies
22+
run: dotnet restore
23+
24+
- name: Build
25+
run: dotnet build --no-restore --configuration Release
26+
27+
- name: Test
28+
run: dotnet test --no-build --configuration Release

ghcp-course.sln

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
Microsoft Visual Studio Solution File, Format Version 12.00
2+
# Visual Studio Version 17
3+
VisualStudioVersion = 17.5.2.0
4+
MinimumVisualStudioVersion = 10.0.40219.1
5+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "session1", "session1", "{42E5AB8A-C2B5-5F6A-F78D-D028ECFDC319}"
6+
EndProject
7+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "dotnet", "dotnet", "{6CC7315A-4A4C-9920-263C-879A3F2A10B1}"
8+
EndProject
9+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ProductService", "session1\dotnet\ProductService\ProductService.csproj", "{24CBE407-66A3-D971-5A1E-B53D2253AB74}"
10+
EndProject
11+
Global
12+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
13+
Debug|Any CPU = Debug|Any CPU
14+
Release|Any CPU = Release|Any CPU
15+
EndGlobalSection
16+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
17+
{24CBE407-66A3-D971-5A1E-B53D2253AB74}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
18+
{24CBE407-66A3-D971-5A1E-B53D2253AB74}.Debug|Any CPU.Build.0 = Debug|Any CPU
19+
{24CBE407-66A3-D971-5A1E-B53D2253AB74}.Release|Any CPU.ActiveCfg = Release|Any CPU
20+
{24CBE407-66A3-D971-5A1E-B53D2253AB74}.Release|Any CPU.Build.0 = Release|Any CPU
21+
EndGlobalSection
22+
GlobalSection(SolutionProperties) = preSolution
23+
HideSolutionNode = FALSE
24+
EndGlobalSection
25+
GlobalSection(NestedProjects) = preSolution
26+
{6CC7315A-4A4C-9920-263C-879A3F2A10B1} = {42E5AB8A-C2B5-5F6A-F78D-D028ECFDC319}
27+
{24CBE407-66A3-D971-5A1E-B53D2253AB74} = {6CC7315A-4A4C-9920-263C-879A3F2A10B1}
28+
EndGlobalSection
29+
GlobalSection(ExtensibilityGlobals) = postSolution
30+
SolutionGuid = {EB14142E-BACF-4296-A94E-FFD36E21D7B9}
31+
EndGlobalSection
32+
EndGlobal
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
<IsPackable>false</IsPackable>
8+
<IsTestProject>true</IsTestProject>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Bogus" Version="35.6.3" />
13+
<PackageReference Include="coverlet.collector" Version="6.0.0" />
14+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
15+
<PackageReference Include="xunit" Version="2.9.3" />
16+
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3" />
17+
</ItemGroup>
18+
19+
<ItemGroup>
20+
<ProjectReference Include="..\ProductService\ProductService.csproj" />
21+
</ItemGroup>
22+
23+
</Project>
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
using Xunit;
2+
using ProductService.Services;
3+
using ProductService.Models;
4+
using Bogus;
5+
using System.Linq;
6+
7+
namespace ProductService.Tests
8+
{
9+
public class ProductServiceTests
10+
{
11+
private readonly ProductService.Services.ProductService _service;
12+
13+
public ProductServiceTests()
14+
{
15+
_service = new ProductService.Services.ProductService();
16+
}
17+
18+
[Fact]
19+
public void GetAll_ReturnsNonEmptyList()
20+
{
21+
var products = _service.GetAll();
22+
Assert.NotEmpty(products);
23+
}
24+
25+
[Fact]
26+
public void GetById_ReturnsCorrectProduct()
27+
{
28+
var first = _service.GetAll().First();
29+
var result = _service.GetById(first.Id);
30+
Assert.NotNull(result);
31+
Assert.Equal(first.Id, result.Id);
32+
Assert.Equal(first.Name, result.Name);
33+
}
34+
35+
[Fact]
36+
public void GetById_ReturnsNullForUnknownId()
37+
{
38+
var result = _service.GetById(-999);
39+
Assert.Null(result);
40+
}
41+
42+
[Fact]
43+
public void Add_InsertsProduct()
44+
{
45+
var countBefore = _service.GetAll().Count;
46+
var newProduct = new Product(0, "Tablet", 199.99m);
47+
var added = _service.Add(newProduct);
48+
var all = _service.GetAll();
49+
Assert.Equal(countBefore + 1, all.Count);
50+
Assert.Contains(all, p => p.Id == added.Id && p.Name == "Tablet");
51+
}
52+
53+
[Fact]
54+
public void Add_WithBogusProduct()
55+
{
56+
var faker = new Faker<Product>()
57+
.CustomInstantiator(f =>
58+
new Product(0, f.Commerce.ProductName(), f.Random.Decimal(1, 1000))
59+
);
60+
var fakeProduct = faker.Generate();
61+
var added = _service.Add(fakeProduct);
62+
Assert.True(added.Id > 0);
63+
Assert.Equal(fakeProduct.Name, added.Name);
64+
}
65+
66+
[Fact]
67+
public void Add_WithNegativePriceOrEmptyName()
68+
{
69+
var badProduct1 = new Product(0, "", 100m);
70+
var badProduct2 = new Product(0, "Bad", -50m);
71+
72+
var added1 = _service.Add(badProduct1);
73+
var added2 = _service.Add(badProduct2);
74+
75+
Assert.Equal("", added1.Name);
76+
Assert.Equal(-50m, added2.Price);
77+
}
78+
}
79+
}
2.32 MB
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)