在本快速入門中,您將瞭解如何使用 GitHub Copilot 來建立實際且主題的數據集,以支援應用程式開發、測試和示範。 藉由分析資料庫的架構和內容,GitHub Copilot 可以產生與真實世界格式一致的模擬數據、模擬邊緣案例,並減少植入資料庫的手動工作,讓測試更快速且更具實際案例代表性。
開始
請確定您已連線到資料庫,並使用 MSSQL 擴充功能開啟使用中的編輯器視窗。 此連線可讓 @mssql 聊天參與者瞭解您的資料庫環境內容,提供精確且切合內容的建議。 如果沒有資料庫連線,聊天參與者就不會有架構或數據內容來提供有意義的回應。
下列範例使用 AdventureWorksLT2022 範例資料庫,您可以從 Microsoft SQL Server 範例和社群專案 首頁下載。
為了獲得最佳結果,請調整數據表和架構名稱以符合您自己的環境。
請確保聊天中包含@mssql前綴。 例如,輸入 @mssql ,後面接著您的問題或提示。 這可確保聊天參與者瞭解您要求與 SQL 相關的協助。
使用 GitHub Copilot 產生實際且可測試的數據
GitHub Copilot 可協助您直接從 SQL 架構或 JSON 範例產生測試和模擬數據。 GitHub Copilot 提供情境建議,幫助縮短時間並提升覆蓋範圍,無論你是準備展示資料集、測試邊緣案例,或是用主題或隨機資料植入開發環境。 這些建議在手動輸入資料較慢或不穩定的情況中特別有用。
以下是您可以透過聊天參與者詢問的常見使用案例和範例。
模擬數據生成
使用 GitHub Copilot 為您的現有數據表產生主題、隨機或代表性的模擬數據。 您可以要求特定的數據列計數、套用名稱/值模式,或根據 JSON 範例等外部結構來建置數據集。
模擬客戶資料範例
Generate mock data for the `SalesLT.Customer` table with 100 sample records.
模擬產品資料範例
Populate the `SalesLT.Product` table with 50 items, each with unique names and prices.
模擬銷售數據範例
Generate mock data for the `SalesLT.SalesOrderHeader` table with 200 records, including order dates and customer IDs.
從 JSON 範例產生模擬資料
Based on this sample JSON with four records, generate a SQL table schema and populate it with 50 mock records. Use character names from well-known sci-fi books (for example, Dune, Foundation, Ready Player One) for the `firstName` and `lastName` fields to make the data more realistic and themed:
[
{ "firstName": "Alice", "lastName": "Smith", "email": "alice@example.com" },
{ "firstName": "Bob", "lastName": "Jones", "email": "bob@example.com" },
{ "firstName": "Charlie", "lastName": "Brown", "email": "charlie@example.com" },
{ "firstName": "Dana", "lastName": "White", "email": "dana@example.com" }
]
邊緣案例測試
超越基本數據生成,使用 GitHub Copilot 來模擬邊緣案例並驗證系統行為。 GitHub Copilot 能協助產生正確的資料,無論你是在壓力測試業務邏輯、檢查資料驗證失敗,或是確保關聯一致性。 它也能撰寫斷言或測試邏輯來驗證結果。
測試量限制
Generate insert statements for `SalesLT.SalesOrderDetail` with `OrderQty` values at the upper boundary (for example, 1,000 units) and verify that the system enforces quantity constraints.
測試電子郵件地址格式
Create test data for `SalesLT.Customer` with invalid email formats and write a query that flags these records for review.
測試極端情況的定價異常
Generate test data for `SalesLT.Product` with edge-case pricing, such as `StandardCost = 0` or negative values, and write a query that highlights anomalies.
測試資料完整性使用模擬技術
Simulate data integrity by generating 500 `SalesOrderDetail` rows that correctly reference valid `ProductID` and `SalesOrderID` values from related tables, and ensure GitHub Copilot includes validation logic.
測試商業邏輯
Write a test script that confirms the `SalesOrderHeader.TotalDue` value is always greater than the `SubTotal` for each order, helpful for spotting miscalculations in business logic.
測試 Null 驗證
Using SQLAlchemy, create a test that attempts to insert a `SalesOrderDetail` record with a null `ProductID` and verify that the ORM raises an integrity error due to the foreign key constraint.
測試負值
With Prisma, generate test logic that tries to insert a `Product` with a `StandardCost` of `-10`. Validate that Prisma rejects the entry and logs an appropriate error message.
分享您的體驗
若要協助我們精簡及改善 MSSQL 延伸模組的 GitHub Copilot,請使用下列 GitHub 問題範本來提交您的意見反應: GitHub Copilot 意見反應
提交意見反應時,請考慮包括:
測試的案例 – 讓我們知道您專注於哪些領域,例如架構建立、查詢產生、安全性、當地語系化。
表現良好之處 – 描述任何感覺順暢、有幫助或超乎您期望的體驗。
問題或錯誤 – 包含任何問題、不一致或混淆的行為。 螢幕快照或螢幕錄製特別有用。
改進建議 – 分享改善可用性、擴大涵蓋範圍或增強 GitHub Copilot 回應的想法。