74 lines
3.3 KiB
C#
74 lines
3.3 KiB
C#
using System.Net;
|
|
using System.Net.Http.Json;
|
|
|
|
namespace WordVersioning.Api.IntegrationTests;
|
|
|
|
/// <summary>setup ที่ใช้ร่วมกันหลาย test class: สร้าง template + ตั้ง field definitions ครบชุด
|
|
/// (employee_name = required text, position = text, salary = number, start_date = date)</summary>
|
|
public static class TestFlow
|
|
{
|
|
public static readonly Dictionary<string, string> DefaultValues = new()
|
|
{
|
|
["employee_name"] = "สมหญิง รักงาน",
|
|
["position"] = "วิศวกรซอฟต์แวร์",
|
|
["salary"] = "75000",
|
|
["start_date"] = "2026-08-01",
|
|
};
|
|
|
|
public static async Task<(Guid TemplateId, List<TemplateFieldItem> Fields)> CreateConfiguredTemplateAsync(
|
|
HttpClient client, string baseName)
|
|
{
|
|
// create (detect defaults)
|
|
using var create = await client.PostAsync(
|
|
"api/templates", ApiExtensions.TemplateForm(TestDocx.BuildTemplateDocx(), $"{baseName} {Guid.NewGuid():N}"));
|
|
create.EnsureSuccessStatusCode();
|
|
var created = await create.Content.ReadFromJsonAsync<CreateTemplateResult>(IntegrationTestFixture.Json);
|
|
|
|
// ตั้ง label/type/required ให้ครบทุกรูปแบบ (มี required, number, date)
|
|
using var put = await client.PutAsJsonAsync($"api/templates/{created!.Id}", new
|
|
{
|
|
fields = created.Fields.Select(f => new
|
|
{
|
|
key = f.Key,
|
|
label = LabelOf(f.Key),
|
|
type = TypeOf(f.Key),
|
|
isRequired = f.Key == "employee_name",
|
|
sortOrder = f.SortOrder,
|
|
}),
|
|
}, IntegrationTestFixture.Json);
|
|
put.EnsureSuccessStatusCode();
|
|
var detail = await put.Content.ReadFromJsonAsync<TemplateDetailResult>(IntegrationTestFixture.Json);
|
|
|
|
return (created.Id, detail!.Fields);
|
|
}
|
|
|
|
/// <summary>generate เอกสาร v1 จาก template ที่ตั้งค่าแล้ว — คืน response ของ generate</summary>
|
|
public static Task<GenerateResult> GenerateDocumentAsync(HttpClient client, Guid templateId) =>
|
|
GenerateDocumentAsync(client, templateId, DefaultValues);
|
|
|
|
/// <summary>generate พร้อมค่าที่กำหนดเอง (ทดสอบ validation หรือค่าเกินนิยาม)</summary>
|
|
public static async Task<GenerateResult> GenerateDocumentAsync(
|
|
HttpClient client, Guid templateId, Dictionary<string, string> values)
|
|
{
|
|
using var response = await client.PostAsJsonAsync(
|
|
"api/documents/generate", new { templateId, values }, IntegrationTestFixture.Json);
|
|
Assert.Equal(HttpStatusCode.Created, response.StatusCode);
|
|
return (await response.Content.ReadFromJsonAsync<GenerateResult>(IntegrationTestFixture.Json))!;
|
|
}
|
|
|
|
public static string LabelOf(string key) => key switch
|
|
{
|
|
"employee_name" => "ชื่อพนักงาน",
|
|
"position" => "ตำแหน่ง",
|
|
"salary" => "เงินเดือน",
|
|
"start_date" => "วันเริ่มงาน",
|
|
_ => key,
|
|
};
|
|
|
|
public static string TypeOf(string key) => key switch
|
|
{
|
|
"salary" => "number",
|
|
"start_date" => "date",
|
|
_ => "text",
|
|
};
|
|
}
|