Skip to content

Commit

Permalink
Backup Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
neojarvis committed Sep 13, 2023
1 parent fc14aab commit b386bab
Show file tree
Hide file tree
Showing 5 changed files with 239 additions and 12 deletions.
128 changes: 118 additions & 10 deletions dotnetproject/TestProject/UnitTest1.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,123 @@
namespace TestProject;
using NUnit.Framework;
using Microsoft.EntityFrameworkCore;
using dotnetapiapp.Controllers;
using dotnetapiapp.Models;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

public class Tests
namespace dotnetapiapp.Tests
{
[SetUp]
public void Setup()
[TestFixture]
public class BookControllerTests
{
}
private BookController _bookController;
private BookStoreContext _context;

[SetUp]
public void Setup()
{
// Initialize an in-memory database for testing
var options = new DbContextOptionsBuilder<BookStoreContext>()
.UseInMemoryDatabase(Guid.NewGuid().ToString())
.Options;

_context = new BookStoreContext(options);
_context.Database.EnsureCreated(); // Create the database

// Seed the database with sample data
_context.Books.AddRange(new List<Book>
{
new Book { Id = 1, Title = "Book 1", Author = "Author 1", Price = 10.99m, Quantity = 5 },
new Book { Id = 2, Title = "Book 2", Author = "Author 2", Price = 12.99m, Quantity = 7 },
new Book { Id = 3, Title = "Book 3", Author = "Author 3", Price = 9.99m, Quantity = 3 },
});
_context.SaveChanges();

_bookController = new BookController(_context);
}

[TearDown]
public void TearDown()
{
_context.Database.EnsureDeleted(); // Delete the in-memory database after each test
_context.Dispose();
}

[Test]
public async Task GetAllBooks_ReturnsOkResult()
{
// Act
var result = await _bookController.GetAllBooks();

// Assert
Assert.IsInstanceOf<OkObjectResult>(result.Result);
}

[Test]
public async Task GetAllBooks_ReturnsAllBooks()
{
// Act
var result = await _bookController.GetAllBooks();

// Assert
Assert.IsInstanceOf<OkObjectResult>(result.Result);
var okResult = result.Result as OkObjectResult;

Assert.IsInstanceOf<IEnumerable<Book>>(okResult.Value);
var books = okResult.Value as IEnumerable<Book>;

var bookCount = books.Count();
Assert.AreEqual(3, bookCount); // Assuming you have 3 books in the seeded data
}

[Test]
public async Task GetBookById_ExistingId_ReturnsOkResult()
{
// Arrange
var existingId = 1;

// Act
var result = await _bookController.GetBookById(existingId);

// Assert
Assert.IsInstanceOf<OkObjectResult>(result.Result);
}

[Test]
public async Task GetBookById_ExistingId_ReturnsBook()
{
// Arrange
var existingId = 1;

// Act
var result = await _bookController.GetBookById(existingId);

// Assert
Assert.IsNotNull(result);

Assert.IsInstanceOf<OkObjectResult>(result.Result);
var okResult = result.Result as OkObjectResult;

var book = okResult.Value as Book;
Assert.IsNotNull(book);
Assert.AreEqual(existingId, book.Id);
}

[Test]
public async Task GetBookById_NonExistingId_ReturnsNotFound()
{
// Arrange
var nonExistingId = 99; // Assuming this ID does not exist in the seeded data

// Act
var result = await _bookController.GetBookById(nonExistingId);

// Assert
Assert.IsInstanceOf<NotFoundResult>(result.Result);
}

[Test]
public void Test1()
{
Assert.Pass();
}
}
}
118 changes: 118 additions & 0 deletions dotnetproject/TestProject/UnitTest2.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using dotnetmvcapp.Controllers;
using dotnetmvcapp.Models;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using NUnit.Framework;

namespace dotnetmvcapp.Tests
{
[TestFixture]
public class BookControllerTests
{
private BookController _bookController;
private HttpClient _httpClient;

[SetUp]
public void Setup()
{
// Create an instance of the BookController
_bookController = new BookController();

// Create an HttpClient instance with custom handler for bypassing certificate validation
var httpClientHandler = new HttpClientHandler();
httpClientHandler.ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => true;
_httpClient = new HttpClient(httpClientHandler);
}

[TearDown]
public void TearDown()
{
// Dispose of the HttpClient
_httpClient.Dispose();
}

[Test]
public void Index_ReturnsViewWithListOfBooks()
{
// Act
var result = _bookController.Index();

// Assert
Assert.IsInstanceOf<ViewResult>(result);

var viewResult = (ViewResult)result;
Assert.IsInstanceOf<List<Book>>(viewResult.Model);
}

[Test]
public void Search_ValidId_ReturnsViewWithBook()
{
// Arrange
int validId = 1; // Replace with a valid book ID

// Act
var result = _bookController.Search(validId);

// Assert
Assert.IsInstanceOf<ViewResult>(result);

var viewResult = (ViewResult)result;
Assert.IsInstanceOf<List<Book>>(viewResult.Model);
Assert.IsTrue(((List<Book>)viewResult.Model).Count > 0);
}

[Test]
public void Search_InvalidId_ReturnsViewWithEmptyList()
{
// Arrange
int invalidId = -1; // Replace with an invalid book ID

// Act
var result = _bookController.Search(invalidId);

// Assert
Assert.IsInstanceOf<ViewResult>(result);

var viewResult = (ViewResult)result;
Assert.IsInstanceOf<List<Book>>(viewResult.Model);
Assert.AreEqual(0, ((List<Book>)viewResult.Model).Count);
}
[Test]
public void Search_NonexistentId_ReturnsNotFound()
{
// Arrange
int nonexistentId = 9999; // Replace with a non-existent book ID

// Act
var result = _bookController.Search(nonexistentId);
Assert.IsInstanceOf<ViewResult>(result);

var viewResult = (ViewResult)result;
Assert.IsInstanceOf<List<Book>>(viewResult.Model);
Assert.AreEqual(0, ((List<Book>)viewResult.Model).Count);
}

[Test]
public void Search_ValidId_ReturnsCorrectBook()
{
// Arrange
int validId = 3; // Replace with a valid book ID

// Act
var result = _bookController.Search(validId);

// Assert
Assert.IsNotNull(result);
Assert.IsInstanceOf<ViewResult>(result);

var viewResult = (ViewResult)result;
Assert.IsInstanceOf<List<Book>>(viewResult.Model);
Assert.AreEqual(1, ((List<Book>)viewResult.Model).Count);
}
}
}
2 changes: 1 addition & 1 deletion dotnetproject/dotnetapiapp/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "https://localhost:7096;http://localhost:5002",
"applicationUrl": "http://0.0.0.0:8080",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
Expand Down
2 changes: 1 addition & 1 deletion dotnetproject/dotnetmvcapp/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "https://localhost:7120;http://localhost:5063",
"applicationUrl": "http://0.0.0.0:8081",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
2.0
2.0

0 comments on commit b386bab

Please sign in to comment.