mirror of
https://github.com/CompassConnections/Compass.git
synced 2026-05-07 22:54:02 -04:00
Add database seeding script and backend testing folder structure (#18)
* setting up test structure * added playwright config file, deleted original playwright folder and moved "some.test" file * continued test structure setup * Updating test folder structure * Added database seeding script and backend testing folder structure * removed the database test * Replaced db seeding script * Updated userInformation.ts to use values from choices.tsx
This commit is contained in:
committed by
GitHub
parent
24ee2a206e
commit
f954e3b2d7
124
scripts/userCreation.ts
Normal file
124
scripts/userCreation.ts
Normal file
@@ -0,0 +1,124 @@
|
||||
//Run with:
|
||||
// export ENVIRONMENT=DEV && ./scripts/build_api.sh && npx tsx ./scripts/userCreation.ts
|
||||
|
||||
import {createSupabaseDirectClient} from "../backend/shared/lib/supabase/init";
|
||||
import {insert} from "../backend/shared/lib/supabase/utils";
|
||||
import {PrivateUser} from "../common/lib/user";
|
||||
import {getDefaultNotificationPreferences} from "../common/lib/user-notification-preferences";
|
||||
import {randomString} from "../common/lib/util/random";
|
||||
import UserAccountInformation from "../tests/e2e/backend/utils/userInformation";
|
||||
|
||||
type ProfileType = 'basic' | 'medium' | 'full'
|
||||
|
||||
/**
|
||||
* Function used to populate the database with profiles.
|
||||
*
|
||||
* @param pg - Supabase client used to access the database.
|
||||
* @param userInfo - Class object containing information to create a user account generated by `fakerjs`.
|
||||
* @param profileType - Optional param used to signify how much information is used in the account generation.
|
||||
*/
|
||||
async function seedDatabase (pg: any, userInfo: UserAccountInformation, profileType?: string) {
|
||||
|
||||
const userId = userInfo.user_id
|
||||
const deviceToken = randomString()
|
||||
const bio = {
|
||||
"type": "doc",
|
||||
"content": [
|
||||
{
|
||||
"type": "paragraph",
|
||||
"content": [
|
||||
{
|
||||
"text": userInfo.bio,
|
||||
"type": "text"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
const basicProfile = {
|
||||
user_id: userId,
|
||||
bio_length: userInfo.bio.length,
|
||||
bio: bio,
|
||||
age: userInfo.age,
|
||||
born_in_location: userInfo.born_in_location,
|
||||
company: userInfo.company,
|
||||
}
|
||||
|
||||
const mediumProfile = {
|
||||
...basicProfile,
|
||||
drinks_per_month: userInfo.drinks_per_month,
|
||||
diet: [userInfo.randomElement(userInfo.diet)],
|
||||
education_level: userInfo.randomElement(userInfo.education_level),
|
||||
ethnicity: [userInfo.randomElement(userInfo.ethnicity)],
|
||||
gender: userInfo.randomElement(userInfo.gender),
|
||||
height_in_inches: userInfo.height_in_inches,
|
||||
pref_gender: [userInfo.randomElement(userInfo.pref_gender)],
|
||||
pref_age_min: userInfo.pref_age.min,
|
||||
pref_age_max: userInfo.pref_age.max,
|
||||
}
|
||||
|
||||
const fullProfile = {
|
||||
...mediumProfile,
|
||||
occupation_title: userInfo.occupation_title,
|
||||
political_beliefs: [userInfo.randomElement(userInfo.political_beliefs)],
|
||||
pref_relation_styles: [userInfo.randomElement(userInfo.pref_relation_styles)],
|
||||
religion: [userInfo.randomElement(userInfo.religion)],
|
||||
}
|
||||
|
||||
const profileData = profileType === 'basic' ? basicProfile
|
||||
: profileType === 'medium' ? mediumProfile
|
||||
: fullProfile
|
||||
|
||||
const user = {
|
||||
// avatarUrl,
|
||||
isBannedFromPosting: false,
|
||||
link: {},
|
||||
}
|
||||
|
||||
const privateUser: PrivateUser = {
|
||||
id: userId,
|
||||
email: userInfo.email,
|
||||
initialIpAddress: userInfo.ip,
|
||||
initialDeviceToken: deviceToken,
|
||||
notificationPreferences: getDefaultNotificationPreferences(),
|
||||
blockedUserIds: [],
|
||||
blockedByUserIds: [],
|
||||
}
|
||||
|
||||
await pg.tx(async (tx:any) => {
|
||||
|
||||
await insert(tx, 'users', {
|
||||
id: userId,
|
||||
name: userInfo.name,
|
||||
username: userInfo.name,
|
||||
data: user,
|
||||
})
|
||||
|
||||
await insert(tx, 'private_users', {
|
||||
id: userId,
|
||||
data: privateUser,
|
||||
})
|
||||
|
||||
await insert(tx, 'profiles', profileData )
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
(async () => {
|
||||
const pg = createSupabaseDirectClient()
|
||||
|
||||
//Edit the count seedConfig to specify the amount of each profiles to create
|
||||
const seedConfig = [
|
||||
{ count: 1, profileType: 'basic' as ProfileType },
|
||||
{ count: 1, profileType: 'medium' as ProfileType },
|
||||
{ count: 1, profileType: 'full' as ProfileType },
|
||||
]
|
||||
|
||||
for (const {count, profileType } of seedConfig) {
|
||||
for (let i = 0; i < count; i++) {
|
||||
const userInfo = new UserAccountInformation()
|
||||
await seedDatabase(pg, userInfo, profileType)
|
||||
}
|
||||
}
|
||||
process.exit(0)
|
||||
})()
|
||||
@@ -1,58 +0,0 @@
|
||||
// This is a script to add a user to the DB: entries in the users and private_users table
|
||||
// Run with:
|
||||
// export ENVIRONMENT=DEV && ./../scripts/build_api.sh && npx tsx users.ts
|
||||
|
||||
import {createSupabaseDirectClient} from "shared/lib/supabase/init";
|
||||
import {insert} from "shared/lib/supabase/utils";
|
||||
import {PrivateUser} from "common/lib/user";
|
||||
import {getDefaultNotificationPreferences} from "common/lib/user-notification-preferences";
|
||||
import {randomString} from "common/lib/util/random";
|
||||
|
||||
(async () => {
|
||||
|
||||
const userId = '...'
|
||||
const email = '...'
|
||||
const name = '...'
|
||||
const username = '...'
|
||||
const ip = '...'
|
||||
const deviceToken = randomString()
|
||||
const pg = createSupabaseDirectClient()
|
||||
|
||||
const user = {
|
||||
// avatarUrl,
|
||||
isBannedFromPosting: false,
|
||||
link: {},
|
||||
}
|
||||
|
||||
const privateUser: PrivateUser = {
|
||||
id: userId,
|
||||
email,
|
||||
initialIpAddress: ip,
|
||||
initialDeviceToken: deviceToken,
|
||||
notificationPreferences: getDefaultNotificationPreferences(),
|
||||
blockedUserIds: [],
|
||||
blockedByUserIds: [],
|
||||
}
|
||||
|
||||
await pg.tx(async (tx) => {
|
||||
|
||||
const newUserRow = await insert(tx, 'users', {
|
||||
id: userId,
|
||||
name,
|
||||
username,
|
||||
data: user,
|
||||
})
|
||||
|
||||
console.log(newUserRow)
|
||||
|
||||
const newPrivateUserRow = await insert(tx, 'private_users', {
|
||||
id: userId,
|
||||
data: privateUser,
|
||||
})
|
||||
|
||||
console.log(newPrivateUserRow)
|
||||
|
||||
})
|
||||
|
||||
process.exit(0)
|
||||
})()
|
||||
Reference in New Issue
Block a user