dramaling-vocab-learning/backend/DramaLing.Api.Tests/Integration/Fixtures/TestDataSeeder.cs

176 lines
6.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using DramaLing.Api.Data;
using DramaLing.Api.Models.Entities;
namespace DramaLing.Api.Tests.Integration.Fixtures;
/// <summary>
/// 測試資料種子類別
/// 提供一致的測試資料給所有整合測試使用
/// </summary>
public static class TestDataSeeder
{
// 測試使用者 IDs
public static readonly Guid TestUser1Id = new("11111111-1111-1111-1111-111111111111");
public static readonly Guid TestUser2Id = new("22222222-2222-2222-2222-222222222222");
// 測試詞卡 IDs
public static readonly Guid TestFlashcard1Id = new("AAAAAAAA-AAAA-AAAA-AAAA-AAAAAAAAAAAA");
public static readonly Guid TestFlashcard2Id = new("BBBBBBBB-BBBB-BBBB-BBBB-BBBBBBBBBBBB");
public static readonly Guid TestFlashcard3Id = new("CCCCCCCC-CCCC-CCCC-CCCC-CCCCCCCCCCCC");
/// <summary>
/// 種子測試資料
/// </summary>
public static void SeedTestData(DramaLingDbContext context)
{
// 如果已有資料則跳過
if (context.Users.Any()) return;
SeedUsers(context);
SeedFlashcards(context);
SeedFlashcardReviews(context);
context.SaveChanges();
}
private static void SeedUsers(DramaLingDbContext context)
{
var users = new[]
{
new User
{
Id = TestUser1Id,
Username = "testuser1",
Email = "test1@example.com",
PasswordHash = "$2a$11$TestHashForUser1Password123", // bcrypt hash for "password123"
DisplayName = "Test User 1",
CreatedAt = DateTime.UtcNow.AddDays(-30),
UpdatedAt = DateTime.UtcNow
},
new User
{
Id = TestUser2Id,
Username = "testuser2",
Email = "test2@example.com",
PasswordHash = "$2a$11$TestHashForUser2Password456", // bcrypt hash for "password456"
DisplayName = "Test User 2",
CreatedAt = DateTime.UtcNow.AddDays(-15),
UpdatedAt = DateTime.UtcNow
}
};
context.Users.AddRange(users);
}
private static void SeedFlashcards(DramaLingDbContext context)
{
var flashcards = new[]
{
new Flashcard
{
Id = TestFlashcard1Id,
UserId = TestUser1Id,
Word = "hello",
Translation = "你好",
Definition = "A greeting used when meeting someone",
PartOfSpeech = "interjection",
Pronunciation = "/həˈloʊ/",
Example = "Hello, how are you today?",
ExampleTranslation = "你好,你今天好嗎?",
DifficultyLevelNumeric = 1, // A1
IsFavorite = false,
Synonyms = "[\"hi\", \"greetings\", \"salutations\"]",
CreatedAt = DateTime.UtcNow.AddDays(-10),
UpdatedAt = DateTime.UtcNow.AddDays(-5)
},
new Flashcard
{
Id = TestFlashcard2Id,
UserId = TestUser1Id,
Word = "beautiful",
Translation = "美麗的",
Definition = "Having qualities that give great pleasure to see or hear",
PartOfSpeech = "adjective",
Pronunciation = "/ˈbjuː.tɪ.fəl/",
Example = "The sunset was absolutely beautiful.",
ExampleTranslation = "夕陽非常美麗。",
DifficultyLevelNumeric = 2, // A2
IsFavorite = true,
Synonyms = "[\"gorgeous\", \"stunning\", \"lovely\"]",
CreatedAt = DateTime.UtcNow.AddDays(-8),
UpdatedAt = DateTime.UtcNow.AddDays(-3)
},
new Flashcard
{
Id = TestFlashcard3Id,
UserId = TestUser2Id,
Word = "sophisticated",
Translation = "精緻的,複雜的",
Definition = "Having a refined knowledge of the ways of the world",
PartOfSpeech = "adjective",
Pronunciation = "/səˈfɪs.tɪ.keɪ.tɪd/",
Example = "She has very sophisticated taste in art.",
ExampleTranslation = "她對藝術有非常精緻的品味。",
DifficultyLevelNumeric = 5, // C1
IsFavorite = false,
Synonyms = "[\"refined\", \"elegant\", \"cultured\"]",
CreatedAt = DateTime.UtcNow.AddDays(-5),
UpdatedAt = DateTime.UtcNow.AddDays(-1)
}
};
context.Flashcards.AddRange(flashcards);
}
private static void SeedFlashcardReviews(DramaLingDbContext context)
{
var reviews = new[]
{
new FlashcardReview
{
Id = Guid.NewGuid(),
UserId = TestUser1Id,
FlashcardId = TestFlashcard1Id,
SuccessCount = 3,
TotalCorrectCount = 5,
TotalWrongCount = 2,
TotalSkipCount = 1,
LastReviewDate = DateTime.UtcNow.AddDays(-2),
LastSuccessDate = DateTime.UtcNow.AddDays(-2),
NextReviewDate = DateTime.UtcNow.AddDays(4), // 2^3 = 8 天後 (但已過 4 天)
CreatedAt = DateTime.UtcNow.AddDays(-10),
UpdatedAt = DateTime.UtcNow.AddDays(-2)
},
new FlashcardReview
{
Id = Guid.NewGuid(),
UserId = TestUser1Id,
FlashcardId = TestFlashcard2Id,
SuccessCount = 1,
TotalCorrectCount = 2,
TotalWrongCount = 3,
TotalSkipCount = 0,
LastReviewDate = DateTime.UtcNow.AddDays(-3),
LastSuccessDate = DateTime.UtcNow.AddDays(-3),
NextReviewDate = DateTime.UtcNow.AddDays(-1), // 應該要複習了
CreatedAt = DateTime.UtcNow.AddDays(-8),
UpdatedAt = DateTime.UtcNow.AddDays(-3)
}
// TestFlashcard3 沒有複習記錄 (新詞卡)
};
context.FlashcardReviews.AddRange(reviews);
}
/// <summary>
/// 清除所有測試資料
/// </summary>
public static void ClearTestData(DramaLingDbContext context)
{
context.FlashcardReviews.RemoveRange(context.FlashcardReviews);
context.Flashcards.RemoveRange(context.Flashcards);
context.Users.RemoveRange(context.Users);
context.OptionsVocabularies.RemoveRange(context.OptionsVocabularies);
context.SaveChanges();
}
}