| 1 | import { Model } from 'mongoose'; |
| 2 | import { getModelToken } from '@nestjs/mongoose'; |
| 3 | import { platform, platform2, ranking, user, hotTopics, viralTitles } from './mockData'; |
| 4 | import { Platform } from '../src/db/schema/platform.schema'; |
| 5 | import { Ranking } from '../src/db/schema/ranking.schema'; |
| 6 | import { INestApplication } from '@nestjs/common'; |
| 7 | import { User } from '../src/db/schema/user.schema'; |
| 8 | import { HotTopic } from '../src/db/schema/hot-topic.schema'; |
| 9 | import { ViralTitle } from '../src/db/schema/viral-title.schema'; |
| 10 | |
| 11 | export async function setupTestData(app: INestApplication) { |
| 12 | // 获取模型 |
| 13 | const platformModel = app.get<Model<Platform>>(getModelToken(Platform.name)); |
| 14 | const rankingModel = app.get<Model<Ranking>>(getModelToken(Ranking.name)); |
| 15 | const userModel = app.get<Model<User>>(getModelToken(User.name)); |
| 16 | const hotTopicModel = app.get<Model<HotTopic>>(getModelToken(HotTopic.name)); |
| 17 | const viralTitleModel = app.get<Model<ViralTitle>>(getModelToken(ViralTitle.name)); |
| 18 | |
| 19 | // 清理已有数据 |
| 20 | await platformModel.deleteMany({}); |
| 21 | await rankingModel.deleteMany({}); |
| 22 | await userModel.deleteMany({}); |
| 23 | await hotTopicModel.deleteMany({}); |
| 24 | await viralTitleModel.deleteMany({}); |
| 25 | |
| 26 | // 创建用户数据 |
| 27 | await userModel.create(user); |
| 28 | |
| 29 | // 创建平台数据 |
| 30 | const [createdPlatform, createdPlatform2] = await Promise.all([ |
| 31 | platformModel.create({ |
| 32 | ...platform, |
| 33 | _id: platform._id, |
| 34 | }), |
| 35 | platformModel.create({ |
| 36 | ...platform2, |
| 37 | _id: platform2._id, |
| 38 | }), |
| 39 | ]); |
| 40 | |
| 41 | // 创建排名数据 |
| 42 | await rankingModel.create(ranking); |
| 43 | |
| 44 | // 创建热门话题数据 |
| 45 | await hotTopicModel.insertMany(hotTopics); |
| 46 | |
| 47 | // 创建爆款标题数据 |
| 48 | await viralTitleModel.insertMany(viralTitles); |
| 49 | |
| 50 | return { |
| 51 | platform: createdPlatform, |
| 52 | platform2: createdPlatform2, |
| 53 | }; |
| 54 | } |
| 55 |