Files
Excel-Pajak-Generator-Ak/excel_pajak_test/ExcelServiceTests.cs
2026-03-09 08:54:42 +07:00

373 lines
11 KiB
C#

using Microsoft.VisualStudio.TestTools.UnitTesting;
using excel_pajak.Services;
using Shouldly;
using System;
using System.IO;
namespace excel_pajak_test;
[TestClass]
public sealed class ExcelServiceTests
{
#region WriteExcel Parameter Validation Tests
[TestMethod]
public void WriteExcel_EmptyFileName_ReturnsFailure()
{
// Act
var result = ExcelService.WriteExcel("", "value", 1, 1);
// Assert
result.ShouldBeFalse();
}
[TestMethod]
public void WriteExcel_NullFileName_ReturnsFailure()
{
// Act
var result = ExcelService.WriteExcel(null!, "value", 1, 1);
// Assert
result.ShouldBeFalse();
}
[TestMethod]
public void WriteExcel_NonExistentFile_ReturnsFailure()
{
// Act
var result = ExcelService.WriteExcel("nonexistent.xlsx", "value", 1, 1);
// Assert
result.ShouldBeFalse();
}
[TestMethod]
public void WriteExcel_InvalidRowZero_ReturnsFailure()
{
// Act
var result = ExcelService.WriteExcel("test.xlsx", "value", 0, 1);
// Assert
result.ShouldBeFalse();
}
[TestMethod]
public void WriteExcel_InvalidRowNegative_ReturnsFailure()
{
// Act
var result = ExcelService.WriteExcel("test.xlsx", "value", -1, 1);
// Assert
result.ShouldBeFalse();
}
[TestMethod]
public void WriteExcel_InvalidColumnZero_ReturnsFailure()
{
// Act
var result = ExcelService.WriteExcel("test.xlsx", "value", 1, 0);
// Assert
result.ShouldBeFalse();
}
[TestMethod]
public void WriteExcel_InvalidColumnNegative_ReturnsFailure()
{
// Act
var result = ExcelService.WriteExcel("test.xlsx", "value", 1, -1);
// Assert
result.ShouldBeFalse();
}
#endregion
#region InitExcel Parameter Validation Tests
[TestMethod]
public void InitExcel_EmptyOutputName_ReturnsFailure()
{
// Act - This test validates the empty string check happens before initialization check
// by using a file that doesn't exist (which would fail if initialization check was first)
var result = ExcelService.InitExcel("", "template.xlsx", "output");
// Assert - Should fail due to empty output name
result.success.ShouldBeFalse();
result.error.ShouldNotBeNull();
}
[TestMethod]
public void InitExcel_NullOutputName_ReturnsFailure()
{
// Act
var result = ExcelService.InitExcel(null!, null!, null!);
// Assert
result.success.ShouldBeFalse();
result.error.ShouldNotBeNull();
}
#endregion
#region InitExcel Overwrite Control Tests
[TestMethod]
public void InitExcel_OverwriteFalse_ExistingFile_ThrowsIOException()
{
// Arrange
var tempDir = Path.Combine(Path.GetTempPath(), "excel_pajak_test_" + Guid.NewGuid());
Directory.CreateDirectory(tempDir);
var templatePath = Path.Combine(tempDir, "template.xlsx");
var outputPath = Path.Combine(tempDir, "output.xlsx");
// Create a minimal template file
var workbook = new NPOI.XSSF.UserModel.XSSFWorkbook();
workbook.CreateSheet("Test");
using (var fs = new FileStream(templatePath, FileMode.Create)) { workbook.Write(fs, false); }
workbook.Close();
// Create existing output file
File.WriteAllText(outputPath, "existing content");
try
{
// Act
var result = ExcelService.InitExcel("output", templatePath, tempDir, overwrite: false);
// Assert
result.success.ShouldBeFalse();
result.error.ShouldBeOfType<IOException>();
result.error!.Message.ShouldContain("already exists");
}
finally
{
// Cleanup
Directory.Delete(tempDir, true);
}
}
[TestMethod]
public void InitExcel_OverwriteTrue_ExistingFile_Succeeds()
{
// Arrange
var tempDir = Path.Combine(Path.GetTempPath(), "excel_pajak_test_" + Guid.NewGuid());
Directory.CreateDirectory(tempDir);
var templatePath = Path.Combine(tempDir, "template.xlsx");
var outputPath = Path.Combine(tempDir, "output.xlsx");
// Create a minimal template file
var workbook = new NPOI.XSSF.UserModel.XSSFWorkbook();
workbook.CreateSheet("Test");
using (var fs = new FileStream(templatePath, FileMode.Create)) { workbook.Write(fs, false); }
workbook.Close();
// Create existing output file
File.WriteAllText(outputPath, "existing content");
try
{
// Act
var result = ExcelService.InitExcel("output.xlsx", templatePath, tempDir, overwrite: true);
// Assert
result.success.ShouldBeTrue();
result.result.ShouldNotBeNull();
File.Exists(result.result!).ShouldBeTrue();
}
finally
{
// Cleanup
Directory.Delete(tempDir, true);
}
}
[TestMethod]
public void InitExcel_OverwriteFalse_NewFile_Succeeds()
{
// Arrange
var tempDir = Path.Combine(Path.GetTempPath(), "excel_pajak_test_" + Guid.NewGuid());
Directory.CreateDirectory(tempDir);
var templatePath = Path.Combine(tempDir, "template.xlsx");
// Create a minimal template file
var workbook = new NPOI.XSSF.UserModel.XSSFWorkbook();
workbook.CreateSheet("Test");
using (var fs = new FileStream(templatePath, FileMode.Create)) { workbook.Write(fs, false); }
workbook.Close();
try
{
// Act
var result = ExcelService.InitExcel("output.xlsx", templatePath, tempDir, overwrite: false);
// Assert
result.success.ShouldBeTrue();
result.result.ShouldNotBeNull();
File.Exists(result.result!).ShouldBeTrue();
}
finally
{
// Cleanup
Directory.Delete(tempDir, true);
}
}
[TestMethod]
public void InitExcel_DefaultOverwrite_ExistingFile_Overwrites()
{
// Arrange
var tempDir = Path.Combine(Path.GetTempPath(), "excel_pajak_test_" + Guid.NewGuid());
Directory.CreateDirectory(tempDir);
var templatePath = Path.Combine(tempDir, "template.xlsx");
var outputPath = Path.Combine(tempDir, "output.xlsx");
// Create a minimal template file
var workbook = new NPOI.XSSF.UserModel.XSSFWorkbook();
workbook.CreateSheet("Test");
using (var fs = new FileStream(templatePath, FileMode.Create)) { workbook.Write(fs, false); }
workbook.Close();
// Create existing output file
File.WriteAllText(outputPath, "existing content");
try
{
// Act - Default behavior should overwrite
var result = ExcelService.InitExcel("output.xlsx", templatePath, tempDir);
// Assert
result.success.ShouldBeTrue();
result.result.ShouldNotBeNull();
File.Exists(result.result!).ShouldBeTrue();
}
finally
{
// Cleanup
Directory.Delete(tempDir, true);
}
}
#endregion
#region Integration Tests (manual execution)
[TestMethod]
[Ignore] // Requires proper initialization sequence - run manually
public void IntegrationTest_InitializeAndInitExcel()
{
// This test requires manual setup - skip in CI
Assert.Inconclusive("Requires manual configuration setup");
}
[TestMethod]
[Ignore] // File locking issues in parallel tests
public void WriteExcel_WithValidFile_WritesValue_Integration()
{
// Arrange - Create a simple test Excel file using absolute path
var testFilePath = Path.Combine(Path.GetTempPath(), "test_excel_integration.xlsx");
// Create a minimal workbook using NPOI
var workbook = new NPOI.XSSF.UserModel.XSSFWorkbook();
var sheet = workbook.CreateSheet("Test");
var row = sheet.CreateRow(0);
var cell = row.CreateCell(0);
cell.SetCellValue((string?)"Initial");
using (var fs = new FileStream(testFilePath, FileMode.Create))
{
workbook.Write(fs, false);
}
workbook.Close();
try
{
// Act - Write to cell
var result = ExcelService.WriteExcel(testFilePath, "NewValue", 1, 1);
// Assert
result.ShouldBeTrue();
}
finally
{
// Clean up
if (File.Exists(testFilePath))
{
try { File.Delete(testFilePath); } catch { }
}
}
}
[TestMethod]
[Ignore] // File locking issues in parallel tests
public void WriteExcel_LongOverload_WritesNumericValue()
{
// Arrange
var testFilePath = Path.Combine(Path.GetTempPath(), "test_excel_long.xlsx");
var workbook = new NPOI.XSSF.UserModel.XSSFWorkbook();
workbook.CreateSheet("Test").CreateRow(0).CreateCell(0).SetCellValue(0);
using (var fs = new FileStream(testFilePath, FileMode.Create)) { workbook.Write(fs, false); }
workbook.Close();
try
{
// Act
long testValue = 1234567890L;
var result = ExcelService.WriteExcel(testFilePath, testValue, 1, 1);
// Assert
result.ShouldBeTrue();
// Verify
using (var fs = new FileStream(testFilePath, FileMode.Open, FileAccess.Read))
{
var wb = new NPOI.XSSF.UserModel.XSSFWorkbook(fs);
var cellValue = wb.GetSheetAt(0).GetRow(0).GetCell(0).NumericCellValue;
cellValue.ShouldBe(1234567890.0);
wb.Close();
}
}
finally
{
if (File.Exists(testFilePath)) { try { File.Delete(testFilePath); } catch { } }
}
}
[TestMethod]
[Ignore] // File locking issues in parallel tests
public void WriteExcel_LargeLongValue_MaintainsPrecision()
{
// Arrange
var testFilePath = Path.Combine(Path.GetTempPath(), "test_excel_large_long.xlsx");
var workbook = new NPOI.XSSF.UserModel.XSSFWorkbook();
workbook.CreateSheet("Test").CreateRow(0).CreateCell(0).SetCellValue(0);
using (var fs = new FileStream(testFilePath, FileMode.Create)) { workbook.Write(fs, false); }
workbook.Close();
try
{
// Act - A large number that still fits within double's exact integer representation (53 bits)
long testValue = 9007199254740991L; // 2^53 - 1
var result = ExcelService.WriteExcel(testFilePath, testValue, 1, 1);
// Assert
result.ShouldBeTrue();
// Verify
using (var fs = new FileStream(testFilePath, FileMode.Open, FileAccess.Read))
{
var wb = new NPOI.XSSF.UserModel.XSSFWorkbook(fs);
var cellValue = wb.GetSheetAt(0).GetRow(0).GetCell(0).NumericCellValue;
cellValue.ShouldBe((double)testValue);
wb.Close();
}
}
finally
{
if (File.Exists(testFilePath)) { try { File.Delete(testFilePath); } catch { } }
}
}
#endregion
}