Skip to content

Commit 1dba352

Browse files
committed
wip: integration tests
1 parent c16f329 commit 1dba352

File tree

9 files changed

+95
-70
lines changed

9 files changed

+95
-70
lines changed

Diff for: .gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,5 @@ doc/dist
140140
.vscode/*
141141
!.vscode/settings.json
142142
integration_tests/serviceAccount.json
143+
integration_tests/functions/firebase_functions_*.tar.gz
144+
integration_tests/functions/requirements.txt

Diff for: integration_tests/functions/main.py

-5
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,6 @@
22
# To get started, simply uncomment the below code or create your own.
33
# Deploy with `firebase deploy`
44

5-
from firebase_functions import https_fn
65
from firebase_admin import initialize_app
76

87
initialize_app()
9-
10-
@https_fn.on_call()
11-
def hello(_: https_fn.CallableRequest) -> str:
12-
return 'Hello from Firebase!'

Diff for: integration_tests/functions/region.py

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# FIXME: Change to us-central1
2+
REGION = "asia-south1"

Diff for: integration_tests/functions/requirements.txt

-1
This file was deleted.

Diff for: integration_tests/functions/v2/firestore_tests.py

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
from firebase_admin import firestore
2+
from firebase_functions import firestore_fn, logger
3+
from integration_tests.functions.region import REGION
4+
5+
6+
@firestore_fn.on_document_created(document='tests/{documentId}', region=REGION, timeout_sec=540)
7+
def firestoreOnDocumentCreatedTests(event: firestore_fn.Event[firestore_fn.DocumentSnapshot]):
8+
logger.debug(event=event)
9+
documentId = event.params['documentId']
10+
11+
firestore.client().collection('firestoreOnDocumentCreatedTests').document(documentId).set({
12+
'time': event.time,
13+
'id': event.id,
14+
'type': event.type,
15+
'source': event.source,
16+
})
17+
18+
19+
@firestore_fn.on_document_deleted(document='tests/{documentId}', region=REGION, timeout_sec=540)
20+
def firestoreOnDocumentDeletedTests(event: firestore_fn.Event[firestore_fn.DocumentSnapshot]):
21+
logger.debug(event=event)
22+
documentId = event.params['documentId']
23+
24+
firestore.client().collection('firestoreOnDocumentDeletedTests').document(documentId).set({
25+
'time': event.time,
26+
'id': event.id,
27+
'type': event.type,
28+
'source': event.source,
29+
})
30+
31+
32+
@firestore_fn.on_document_updated(document='tests/{documentId}', region=REGION, timeout_sec=540)
33+
def firestoreOnDocumentUpdatedTests(event: firestore_fn.Event[firestore_fn.DocumentSnapshot]):
34+
logger.debug(event=event)
35+
documentId = event.params['documentId']
36+
37+
firestore.client().collection('firestoreOnDocumentUpdatedTests').document(documentId).set({
38+
'time': event.time,
39+
'id': event.id,
40+
'type': event.type,
41+
'source': event.source,
42+
})
43+
44+
45+
@firestore_fn.on_document_written(document='tests/{documentId}', region=REGION, timeout_sec=540)
46+
def firestoreOnDocumentWrittenTests(event: firestore_fn.Event[firestore_fn.DocumentSnapshot]):
47+
logger.debug(event=event)
48+
documentId = event.params['documentId']
49+
50+
firestore.client().collection('firestoreOnDocumentWrittenTests').document(documentId).set({
51+
'time': event.time,
52+
'id': event.id,
53+
'type': event.type,
54+
'source': event.source,
55+
})

Diff for: integration_tests/package.json.template

-20
This file was deleted.

Diff for: integration_tests/requirements.txt.template

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
firebase_admin==__FIREBASE_ADMIN__
2+
__LOCAL_FIREBASE_FUNCTIONS__

Diff for: integration_tests/run.ts

+3-5
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ function loadEnv(): void {
2828
loadEnv();
2929

3030
const {
31-
NODE_VERSION = "18",
3231
FIREBASE_ADMIN = "^10.0.0",
3332
PROJECT_ID,
3433
DATABASE_URL,
@@ -55,7 +54,7 @@ if (
5554
process.exit(1);
5655
}
5756

58-
setup(TEST_RUN_ID, NODE_VERSION, FIREBASE_ADMIN);
57+
setup(TEST_RUN_ID, FIREBASE_ADMIN);
5958

6059
const config = {
6160
projectId: PROJECT_ID,
@@ -185,7 +184,7 @@ function cleanFiles(): void {
185184
try {
186185
const files = fs.readdirSync(".");
187186
files.forEach((file) => {
188-
if (file.match(`firebase-functions-${TEST_RUN_ID}.tgz`)) {
187+
if (file.match(`firebase_functions_${TEST_RUN_ID}.tar.gz`)) {
189188
fs.rmSync(file);
190189
}
191190
if (file.match("package.json")) {
@@ -199,8 +198,7 @@ function cleanFiles(): void {
199198
}
200199
});
201200

202-
fs.rmSync("lib", { recursive: true });
203-
// fs.existsSync("node_modules") && fs.rmSync("node_modules", { recursive: true });
201+
fs.rmSync("venv", { recursive: true });
204202
} catch (error) {
205203
console.error("Error occurred while cleaning files:", error);
206204
}

Diff for: integration_tests/setup.ts

+31-39
Original file line numberDiff line numberDiff line change
@@ -7,81 +7,73 @@ const DIR = process.cwd();
77
/**
88
* Build SDK, and Functions
99
*/
10-
export default function setup(testRunId: string, nodeVersion: string, firebaseAdmin: string) {
10+
export default function setup(testRunId: string, firebaseAdmin: string) {
1111
buildSdk(testRunId);
12-
createPackageJson(testRunId, nodeVersion, firebaseAdmin);
12+
createRequirementsTxt(testRunId, firebaseAdmin);
1313
installDependencies();
14-
buildFunctions();
1514
}
1615

1716
function buildSdk(testRunId: string) {
1817
console.log("Building SDK...");
1918
process.chdir(path.join(DIR, "..")); // go up to root
2019

21-
// remove existing firebase-functions-*.tgz files
22-
const files = fs.readdirSync(".");
23-
files.forEach((file) => {
24-
if (file.match(/^firebase-functions-.*\.tgz$/)) {
25-
fs.rmSync(file);
26-
}
27-
});
20+
// remove existing build
21+
fs.rmdirSync("dist", { recursive: true });
22+
2823
// build the package
29-
execSync("npm run build:pack", { stdio: "inherit" });
24+
execSync("python -m pip install --upgrade build", { stdio: "inherit" });
25+
execSync("python -m build -s", { stdio: "inherit" });
3026

3127
// move the generated tarball package to functions
3228
const generatedFile = fs
33-
.readdirSync(".")
34-
.find((file) => file.match(/^firebase-functions-.*\.tgz$/));
29+
.readdirSync("dist")
30+
.find((file) => file.match(/^firebase_functions-.*\.tar\.gz$/));
3531

3632
if (generatedFile) {
3733
const targetPath = path.join(
38-
"integration_test",
34+
"integration_tests",
3935
"functions",
40-
`firebase-functions-${testRunId}.tgz`
36+
`firebase_functions_${testRunId}.tar.gz`
4137
);
42-
fs.renameSync(generatedFile, targetPath);
38+
fs.renameSync(path.join("dist", generatedFile), targetPath);
4339
console.log("SDK moved to", targetPath);
4440
}
4541

4642
process.chdir(DIR); // go back to integration_test
4743
}
4844

49-
function createPackageJson(testRunId: string, nodeVersion: string, firebaseAdmin: string) {
45+
function createRequirementsTxt(testRunId: string, firebaseAdmin: string) {
5046
console.log("Creating package.json...");
51-
const packageJsonTemplatePath = `${DIR}/package.json.template`;
52-
const packageJsonPath = `${DIR}/functions/package.json`;
47+
const requirementsTemplatePath = `${DIR}/requirements.txt.template`;
48+
const requirementsPath = `${DIR}/functions/requirements.txt`;
5349

54-
fs.copyFileSync(packageJsonTemplatePath, packageJsonPath);
50+
fs.copyFileSync(requirementsTemplatePath, requirementsPath);
5551

56-
let packageJsonContent = fs.readFileSync(packageJsonPath, "utf8");
57-
packageJsonContent = packageJsonContent.replace(
58-
/__SDK_TARBALL__/g,
59-
`firebase-functions-${testRunId}.tgz`
52+
let requirementsContent = fs.readFileSync(requirementsPath, "utf8");
53+
requirementsContent = requirementsContent.replace(
54+
/__LOCAL_FIREBASE_FUNCTIONS__/g,
55+
`firebase_functions_${testRunId}.tar.gz`
56+
);
57+
requirementsContent = requirementsContent.replace(
58+
/__FIREBASE_ADMIN__/g,
59+
firebaseAdmin
6060
);
61-
packageJsonContent = packageJsonContent.replace(/__NODE_VERSION__/g, nodeVersion);
62-
packageJsonContent = packageJsonContent.replace(/__FIREBASE_ADMIN__/g, firebaseAdmin);
6361

64-
fs.writeFileSync(packageJsonPath, packageJsonContent);
62+
fs.writeFileSync(requirementsPath, requirementsContent);
6563
}
6664

6765
function installDependencies() {
6866
console.log("Installing dependencies...");
6967
const functionsDir = "functions";
7068
process.chdir(functionsDir); // go to functions
7169

72-
const modulePath = path.join("node_modules", "firebase-functions");
73-
if (fs.existsSync(modulePath)) {
74-
execSync(`rm -rf ${modulePath}`, { stdio: "inherit" });
70+
const venvPath = path.join("venv");
71+
if (fs.existsSync(venvPath)) {
72+
execSync(`rm -rf ${venvPath}`, { stdio: "inherit" });
7573
}
7674

77-
execSync("npm install", { stdio: "inherit" });
75+
execSync("python3 -m venv venv", { stdio: "inherit" });
76+
execSync("source venv/bin/activate", { stdio: "inherit" });
77+
execSync("python3 -m pip install -r requirements.txt", { stdio: "inherit" });
7878
process.chdir("../"); // go back to integration_test
7979
}
80-
81-
function buildFunctions() {
82-
console.log("Building functions...");
83-
process.chdir(path.join(DIR, "functions")); // go to functions
84-
85-
execSync("npm run build", { stdio: "inherit" });
86-
process.chdir(DIR); // go back to integration_test
87-
}

0 commit comments

Comments
 (0)