MCPcopy
hub / github.com/PostHog/posthog / resetTestDatabase

Function resetTestDatabase

plugin-server/tests/helpers/sql.ts:40–93  ·  view source on GitHub ↗
(
    code?: string,
    extraServerConfig: Partial<PluginsServerConfig> = {},
    extraRows: ExtraDatabaseRows = {},
    { withExtendedTestData = true }: { withExtendedTestData?: boolean } = {}
)

Source from the content-addressed store, hash-verified

38`
39
40export async function resetTestDatabase(
41 code?: string,
42 extraServerConfig: Partial<PluginsServerConfig> = {},
43 extraRows: ExtraDatabaseRows = {},
44 { withExtendedTestData = true }: { withExtendedTestData?: boolean } = {}
45): Promise<void> {
46 const config = { ...defaultConfig, ...extraServerConfig }
47 const db = new Pool({ connectionString: config.DATABASE_URL!, max: 1 })
48
49 await db.query(POSTGRES_DELETE_TABLES_QUERY)
50 const mocks = makePluginObjects(code)
51 const teamIds = mocks.pluginConfigRows.map((c) => c.team_id)
52 const teamIdToCreate = teamIds[0]
53 await createUserTeamAndOrganization(db, teamIdToCreate)
54 if (withExtendedTestData) {
55 await insertRow(db, 'posthog_action', {
56 id: teamIdToCreate + 67,
57 team_id: teamIdToCreate,
58 name: 'Test Action',
59 description: '',
60 created_at: new Date().toISOString(),
61 created_by_id: commonUserId,
62 deleted: false,
63 post_to_slack: true,
64 slack_message_format: '',
65 is_calculating: false,
66 updated_at: new Date().toISOString(),
67 last_calculated_at: new Date().toISOString(),
68 } as RawAction)
69 await insertRow(db, 'posthog_actionstep', {
70 id: teamIdToCreate + 911,
71 action_id: teamIdToCreate + 67,
72 tag_name: null,
73 text: null,
74 href: null,
75 selector: null,
76 url: null,
77 url_matching: null,
78 name: null,
79 event: null,
80 properties: [{ type: 'event', operator: PropertyOperator.Exact, key: 'foo', value: ['bar'] }],
81 })
82 for (const plugin of mocks.pluginRows.concat(extraRows.plugins ?? [])) {
83 await insertRow(db, 'posthog_plugin', plugin)
84 }
85 for (const pluginConfig of mocks.pluginConfigRows.concat(extraRows.pluginConfigs ?? [])) {
86 await insertRow(db, 'posthog_pluginconfig', pluginConfig)
87 }
88 for (const pluginAttachment of mocks.pluginAttachmentRows.concat(extraRows.pluginAttachments ?? [])) {
89 await insertRow(db, 'posthog_pluginattachment', pluginAttachment)
90 }
91 }
92 await db.end()
93}
94
95export async function insertRow(db: Pool, table: string, objectProvided: Record<string, any>) {
96 // Handling of related fields

Callers 15

e2e.buffer.test.tsFile · 0.90
errors.test.tsFile · 0.90
sql.test.tsFile · 0.90
server.test.tsFile · 0.90
vm.test.tsFile · 0.90
vm.timeout.test.tsFile · 0.90

Calls 4

makePluginObjectsFunction · 0.90
insertRowFunction · 0.85
queryMethod · 0.45

Tested by 1

Used in the wild real call sites across dependent graphs

searching dependent graphs…