在本快速入门中,你将了解如何使用 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.
测试空值验证
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 响应的想法。