Skip to content

Commit f954e3b

Browse files
authored
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
1 parent 24ee2a2 commit f954e3b

9 files changed

Lines changed: 293 additions & 58 deletions

File tree

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
"@capacitor/android": "7.4.4",
4949
"@capacitor/assets": "3.0.5",
5050
"@capacitor/cli": "7.4.4",
51+
"@faker-js/faker": "10.1.0",
5152
"@testing-library/dom": "^10.0.0",
5253
"@testing-library/jest-dom": "^6.6.4",
5354
"@testing-library/react": "^16.3.0",

scripts/userCreation.ts

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
//Run with:
2+
// export ENVIRONMENT=DEV && ./scripts/build_api.sh && npx tsx ./scripts/userCreation.ts
3+
4+
import {createSupabaseDirectClient} from "../backend/shared/lib/supabase/init";
5+
import {insert} from "../backend/shared/lib/supabase/utils";
6+
import {PrivateUser} from "../common/lib/user";
7+
import {getDefaultNotificationPreferences} from "../common/lib/user-notification-preferences";
8+
import {randomString} from "../common/lib/util/random";
9+
import UserAccountInformation from "../tests/e2e/backend/utils/userInformation";
10+
11+
type ProfileType = 'basic' | 'medium' | 'full'
12+
13+
/**
14+
* Function used to populate the database with profiles.
15+
*
16+
* @param pg - Supabase client used to access the database.
17+
* @param userInfo - Class object containing information to create a user account generated by `fakerjs`.
18+
* @param profileType - Optional param used to signify how much information is used in the account generation.
19+
*/
20+
async function seedDatabase (pg: any, userInfo: UserAccountInformation, profileType?: string) {
21+
22+
const userId = userInfo.user_id
23+
const deviceToken = randomString()
24+
const bio = {
25+
"type": "doc",
26+
"content": [
27+
{
28+
"type": "paragraph",
29+
"content": [
30+
{
31+
"text": userInfo.bio,
32+
"type": "text"
33+
}
34+
]
35+
}
36+
]
37+
}
38+
const basicProfile = {
39+
user_id: userId,
40+
bio_length: userInfo.bio.length,
41+
bio: bio,
42+
age: userInfo.age,
43+
born_in_location: userInfo.born_in_location,
44+
company: userInfo.company,
45+
}
46+
47+
const mediumProfile = {
48+
...basicProfile,
49+
drinks_per_month: userInfo.drinks_per_month,
50+
diet: [userInfo.randomElement(userInfo.diet)],
51+
education_level: userInfo.randomElement(userInfo.education_level),
52+
ethnicity: [userInfo.randomElement(userInfo.ethnicity)],
53+
gender: userInfo.randomElement(userInfo.gender),
54+
height_in_inches: userInfo.height_in_inches,
55+
pref_gender: [userInfo.randomElement(userInfo.pref_gender)],
56+
pref_age_min: userInfo.pref_age.min,
57+
pref_age_max: userInfo.pref_age.max,
58+
}
59+
60+
const fullProfile = {
61+
...mediumProfile,
62+
occupation_title: userInfo.occupation_title,
63+
political_beliefs: [userInfo.randomElement(userInfo.political_beliefs)],
64+
pref_relation_styles: [userInfo.randomElement(userInfo.pref_relation_styles)],
65+
religion: [userInfo.randomElement(userInfo.religion)],
66+
}
67+
68+
const profileData = profileType === 'basic' ? basicProfile
69+
: profileType === 'medium' ? mediumProfile
70+
: fullProfile
71+
72+
const user = {
73+
// avatarUrl,
74+
isBannedFromPosting: false,
75+
link: {},
76+
}
77+
78+
const privateUser: PrivateUser = {
79+
id: userId,
80+
email: userInfo.email,
81+
initialIpAddress: userInfo.ip,
82+
initialDeviceToken: deviceToken,
83+
notificationPreferences: getDefaultNotificationPreferences(),
84+
blockedUserIds: [],
85+
blockedByUserIds: [],
86+
}
87+
88+
await pg.tx(async (tx:any) => {
89+
90+
await insert(tx, 'users', {
91+
id: userId,
92+
name: userInfo.name,
93+
username: userInfo.name,
94+
data: user,
95+
})
96+
97+
await insert(tx, 'private_users', {
98+
id: userId,
99+
data: privateUser,
100+
})
101+
102+
await insert(tx, 'profiles', profileData )
103+
104+
})
105+
}
106+
107+
(async () => {
108+
const pg = createSupabaseDirectClient()
109+
110+
//Edit the count seedConfig to specify the amount of each profiles to create
111+
const seedConfig = [
112+
{ count: 1, profileType: 'basic' as ProfileType },
113+
{ count: 1, profileType: 'medium' as ProfileType },
114+
{ count: 1, profileType: 'full' as ProfileType },
115+
]
116+
117+
for (const {count, profileType } of seedConfig) {
118+
for (let i = 0; i < count; i++) {
119+
const userInfo = new UserAccountInformation()
120+
await seedDatabase(pg, userInfo, profileType)
121+
}
122+
}
123+
process.exit(0)
124+
})()

scripts/users.ts

Lines changed: 0 additions & 58 deletions
This file was deleted.

tests/e2e/backend/fixtures/base.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { test as base, APIRequestContext, request } from '@playwright/test';
2+
3+
export type TestOptions = {
4+
apiContextPage: APIRequestContext,
5+
}
6+
7+
export const test = base.extend<TestOptions>({
8+
apiContextPage: async ({}, use) => {
9+
const apiContext = await request.newContext({
10+
baseURL: 'https://api.compassmeet.com'
11+
});
12+
await use(apiContext)
13+
},
14+
})
15+
16+
export { expect } from "@playwright/test"
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { test, expect } from "../fixtures/base";
2+
3+
test('Check API health', async ({apiContextPage}) => {
4+
const responseHealth = await apiContextPage.get('/health');
5+
expect(responseHealth.status()).toBe(200)
6+
7+
const responseBody = await responseHealth.json()
8+
console.log(JSON.stringify(responseBody, null, 2));
9+
10+
});
11+
12+
test.afterAll(async ({apiContextPage}) => {
13+
await apiContextPage?.dispose();
14+
})

tests/e2e/backend/specs/db.test.ts

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import {expect, test } from '@playwright/test';
2+
import { createSupabaseDirectClient } from "../../../../backend/shared/src/supabase/init";
3+
4+
test('View database', async () => {
5+
// const dbClient = createSupabaseDirectClient()
6+
// const queryUserID = `
7+
// SELECT p.*
8+
// FROM public.profiles AS p
9+
// WHERE id = $1
10+
// `;
11+
12+
// const queryTableColumns = `
13+
// SELECT
14+
// column_name,
15+
// data_type,
16+
// character_maximum_length,
17+
// is_nullable,
18+
// column_default
19+
// FROM information_schema.columns
20+
// WHERE table_schema = 'public'
21+
// AND table_name ='profiles'
22+
// ORDER BY ordinal_position;
23+
// `;
24+
25+
// const queryTableColumnsNullable = `
26+
// SELECT
27+
// column_name,
28+
// data_type,
29+
// character_maximum_length,
30+
// column_default
31+
// FROM information_schema.columns
32+
// WHERE table_schema = 'public'
33+
// AND table_name =$1
34+
// AND is_nullable = $2
35+
// ORDER BY ordinal_position;
36+
// `;
37+
38+
// const queryInsertUserProfile = `
39+
// INSERT INTO profiles (name, username)
40+
// VALUES ($1, $2)
41+
// RETURNING *;
42+
// `;
43+
44+
// const queryInsertUsers = `
45+
// INSERT INTO profiles (id, bio)
46+
// VALUES ($1, $2)
47+
// RETURNING *;
48+
// `;
49+
50+
51+
// const rows = await dbClient.query(
52+
// queryInsertUsers,
53+
// [
54+
// 'JFTZOhrBagPk',
55+
// {
56+
// "type": "doc",
57+
// "content": [
58+
// {
59+
// "type": "paragraph",
60+
// "content": [
61+
// {
62+
// "text": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.",
63+
// "type": "text"
64+
// }
65+
// ]
66+
// }
67+
// ]
68+
// }
69+
// ]
70+
// )
71+
72+
// console.log("Type of: ",typeof(rows));
73+
// console.log("Number of rows: ",rows.length);
74+
75+
// console.log(JSON.stringify(await rows, null, 2));
76+
77+
78+
})
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { faker } from "@faker-js/faker";
2+
import {
3+
RELATIONSHIP_CHOICES,
4+
POLITICAL_CHOICES,
5+
RELIGION_CHOICES,
6+
DIET_CHOICES,
7+
EDUCATION_CHOICES,
8+
} from "../../../../web/components/filters/choices";
9+
import { Races } from "../../../../web/components/race";
10+
11+
class UserAccountInformation {
12+
13+
name = faker.person.fullName();
14+
email = faker.internet.email();
15+
user_id = faker.string.alpha(28)
16+
password = faker.internet.password();
17+
ip = faker.internet.ip()
18+
age = faker.number.int({min: 18, max:100});
19+
bio = faker.lorem.words({min: 200, max:350});
20+
born_in_location = faker.location.country();
21+
gender = [
22+
'Female',
23+
'Male',
24+
'Other'
25+
];
26+
27+
pref_gender = [
28+
'Female',
29+
'Male',
30+
'Other'
31+
];
32+
33+
pref_age = {
34+
min: faker.number.int({min: 18, max:27}),
35+
max: faker.number.int({min: 36, max:68})
36+
};
37+
38+
pref_relation_styles = Object.values(RELATIONSHIP_CHOICES);
39+
political_beliefs = Object.values(POLITICAL_CHOICES);
40+
religion = Object.values(RELIGION_CHOICES);
41+
diet = Object.values(DIET_CHOICES);
42+
drinks_per_month = faker.number.int({min: 4, max:40});
43+
height_in_inches = faker.number.float({min: 56, max: 78, fractionDigits:2});
44+
ethnicity = Object.values(Races);
45+
education_level = Object.values(EDUCATION_CHOICES);
46+
company = faker.company.name();
47+
occupation_title = faker.person.jobTitle();
48+
university = faker.company.name();
49+
50+
randomElement (array: Array<string>) {
51+
return array[Math.floor(Math.random() * array.length)].toLowerCase()
52+
}
53+
}
54+
55+
export default UserAccountInformation;

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1386,6 +1386,11 @@
13861386
resolved "https://registry.yarnpkg.com/@eslint/js/-/js-8.57.0.tgz#a5417ae8427873f1dd08b70b3574b453e67b5f7f"
13871387
integrity sha512-Ys+3g2TaW7gADOJzPt83SJtCDhMjndcDMFVQ/Tj9iA1BfJzFKD9mAUXT3OenpuPHbI6P/myECxRJrofUsDx/5g==
13881388

1389+
"@faker-js/faker@10.1.0":
1390+
version "10.1.0"
1391+
resolved "https://registry.yarnpkg.com/@faker-js/faker/-/faker-10.1.0.tgz#eb72869d01ccbff41a77aa7ac851ce1ac9371129"
1392+
integrity sha512-C3mrr3b5dRVlKPJdfrAXS8+dq+rq8Qm5SNRazca0JKgw1HQERFmrVb0towvMmw5uu8hHKNiQasMaR/tydf3Zsg==
1393+
13891394
"@fastify/busboy@^3.0.0":
13901395
version "3.2.0"
13911396
resolved "https://registry.yarnpkg.com/@fastify/busboy/-/busboy-3.2.0.tgz#13ed8212f3b9ba697611529d15347f8528058cea"

0 commit comments

Comments
 (0)