Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sample Data and Docker shells #3277

Conversation

JaiPannu-IITI
Copy link

@JaiPannu-IITI JaiPannu-IITI commented Feb 22, 2025

Closes #3214

Description

  1. Corrects docker shells configuration
  2. Corrects vitest integration tests conflicts
  3. Ensures scripts are in script directory
  4. Removed unnecessary packages as mentioned by xoldd
  5. Provided fail safe scripts
  6. Updates documentation

Following can be used to at ease now as one line commands directly from cli:

docker exec talawa-api-1 /bin/bash -l -c '. pnpm run add:sample_data && exit'
docker exec talawa-api-1 /bin/sh -l -c 'pnpm run add:sample_data && exit'
docker exec talawa-api-1 /bin/zsh -l -c 'pnpm run add:sample_data && exit'

Flow of script

add:sample_data

  1. Ping connection to database.
  2. Ensure administrator privileges exits.
  3. Imports sample data with proper unique/primary key checking and skips duplicate data.
  4. Gracefully disconnects database

Proper error catching is implemented for best UX.

reset:db

  1. Confirm developer before clearing all data
  2. Formats database completely
  3. Restore administrator privileges

Key Changes

  • Fixed sample data script to make sure administrator is present and add data without fail.
  • Fixed sample data script to correctly console log before and after stats of database.
  • Ensure tests run in a clean environment using postgres-test.
  • Refactored functions and reused.
  • Moved scripts to /scripts
  • Wrote integration tests to ensure database access and update
  • Fixed Docker environment to make scripts executable in non interactive environment

Next Steps

Improve code coverage for scripts.

Summary by CodeRabbit

  • Documentation

    • Updated installation instructions to streamline sample data import with a new CLI approach and added guidance for VS Code Dev Container usage.
    • Enhanced Docker reset documentation with clearer post-reset directions.
  • New Features

    • Introduced new commands for database reset and sample data seeding to simplify development workflows.
  • Chores

    • Removed outdated workflow steps and unneeded dependencies for improved maintainability.
  • Tests

    • Added comprehensive test coverage for database management functionalities to ensure higher stability.

Copy link

codecov bot commented Feb 22, 2025

Codecov Report

Attention: Patch coverage is 78.50679% with 95 lines in your changes missing coverage. Please review.

Project coverage is 48.91%. Comparing base (792e6ee) to head (772dbe5).

Files with missing lines Patch % Lines
scripts/dbManagement/helpers.ts 83.43% 53 Missing ⚠️
scripts/dbManagement/addSampleData.ts 63.15% 21 Missing ⚠️
scripts/dbManagement/resetDB.ts 67.69% 21 Missing ⚠️
Additional details and impacted files
@@                 Coverage Diff                  @@
##           develop-postgres    #3277      +/-   ##
====================================================
+ Coverage             48.04%   48.91%   +0.87%     
====================================================
  Files                   455      457       +2     
  Lines                 33942    34152     +210     
  Branches                892      959      +67     
====================================================
+ Hits                  16308    16707     +399     
+ Misses                17634    17445     -189     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@JaiPannu-IITI
Copy link
Author

@palisadoes This is ready for review

@JaiPannu-IITI
Copy link
Author

JaiPannu-IITI commented Feb 22, 2025

REGARDING VITEST

Sir since each contributor has there own way of writing tests, there are chances in future that some tests conflicts unexpectedly since integration tests are performed on actual database, like happened after last PR

To reduce this I have enabled single threaded vitest flow so that integration tests don't conflict.

Also if this problem comes in future, please suggest contributors to use database transactions so that after their tests are completed, database is rolled back to previous state ensuring other tests can run easily without conflicts, I have applied same to these tests, I hope they don't cause problem but if they please tag me I'll review right away, not only this, if there is any error regarding vitests conflicts you can tag me I have got some good understanding of our test environment and integration conflicts, I'll be happy to help.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 9

♻️ Duplicate comments (1)
test/scripts/dbManagement/resetDB.test.ts (1)

25-40: 🛠️ Refactor suggestion

Add error handling for beforeEach and afterEach hooks.

The setup and teardown hooks should handle potential failures in database transactions and administrator creation.

 beforeEach(async () => {
   vi.resetModules();
-  await helpers.db.transaction(async (trx) => {
-    console.log("created transaction");
-    await trx.execute(sql`BEGIN;`);
-  });
-  await helpers.ensureAdministratorExists();
+  try {
+    await helpers.db.transaction(async (trx) => {
+      await trx.execute(sql`BEGIN;`);
+    });
+    await helpers.ensureAdministratorExists();
+  } catch (error) {
+    console.error('Failed to setup test environment:', error);
+    throw error;
+  }
 });

 afterEach(async () => {
   vi.restoreAllMocks();
-  await helpers.ensureAdministratorExists();
-  await helpers.db.transaction(async (trx) => {
-    await trx.execute(sql`ROLLBACK;`);
-    console.log("rolledback");
-  });
+  try {
+    await helpers.ensureAdministratorExists();
+    await helpers.db.transaction(async (trx) => {
+      await trx.execute(sql`ROLLBACK;`);
+    });
+  } catch (error) {
+    console.error('Failed to cleanup test environment:', error);
+    throw error;
+  }
 });
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: ASSERTIVE
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 8c7db69 and ff2a210.

📒 Files selected for processing (4)
  • test/scripts/dbManagement/addSampleData.test.ts (1 hunks)
  • test/scripts/dbManagement/helpers.test.ts (1 hunks)
  • test/scripts/dbManagement/resetDB.test.ts (1 hunks)
  • vitest.config.ts (2 hunks)
🔇 Additional comments (7)
vitest.config.ts (1)

13-13: Consider making the timeout configurable.

A 15-second timeout might be insufficient for large integration tests. Exposing this timeout as an environment variable or configuration property can provide flexibility across different test scenarios.

test/scripts/dbManagement/resetDB.test.ts (1)

42-139: LGTM! Comprehensive test coverage.

The test suite provides excellent coverage of various scenarios including:

  • Happy path with successful execution
  • User cancellation
  • Database connection failures
  • Formatting failures
  • Administrator creation failures
  • Module import behavior

The tests are well-structured with proper mocking and assertions.

test/scripts/dbManagement/addSampleData.test.ts (3)

14-14: Fix typo in administrator email address.

There's a typo in the mocked administrator email address.


56-68: Add timeout for concurrent operation test.

The concurrent operation test might need a timeout to prevent hanging in case of deadlocks.


70-122: Well-structured error handling tests!

The error handling tests are comprehensive, with good use of spies and clear error messages.

test/scripts/dbManagement/helpers.test.ts (2)

19-19: Fix typo in administrator email address.

There's a typo in the mocked administrator email address.


89-110: Excellent date parsing test coverage!

The tests cover various date formats, edge cases, and invalid inputs comprehensively.

coderabbitai[bot]
coderabbitai bot previously approved these changes Feb 22, 2025
Copy link
Contributor

@palisadoes palisadoes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It appears that you have to do this so that any user running the docker container can get access to the fnm path.

Please move this modification to the very end of the file with an explanation in the comments. We need to ensure that this doesn't impact the starting of the containers in any way.

Validate that both the production and development containers start and can import the data after this change.

image

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: ASSERTIVE
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 772dbe5 and 38536a2.

📒 Files selected for processing (1)
  • docs/docs/docs/getting-started/installation.md (1 hunks)
🧰 Additional context used
🪛 markdownlint-cli2 (0.17.2)
docs/docs/docs/getting-started/installation.md

401-401: Trailing spaces
Expected: 0 or 2; Actual: 3

(MD009, no-trailing-spaces)

⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: Run tests for talawa api
  • GitHub Check: Analyse Code With CodeQL (typescript)
🔇 Additional comments (2)
docs/docs/docs/getting-started/installation.md (2)

401-401: Trailing Spaces Detected
Static analysis flagged trailing spaces on this line. Please remove the extra whitespace to comply with markdown lint rules (MD009).

🧰 Tools
🪛 markdownlint-cli2 (0.17.2)

401-401: Trailing spaces
Expected: 0 or 2; Actual: 3

(MD009, no-trailing-spaces)


404-415: Updated VS Code Dev Container Instructions: Environment Clarity
The "Using VS Code Dev Container" section now directs users to import sample data using the command pnpm run add:sample_data. This is clear and aligns with the new script naming. Consider adding a short note to remind users to ensure that their terminal session within the container has the correct environment variables loaded.

coderabbitai[bot]
coderabbitai bot previously approved these changes Feb 23, 2025
@palisadoes palisadoes added the ignore-sensitive-files-pr The contributor has a legitimate reason for editiing protected files label Feb 23, 2025
Copy link
Contributor

@palisadoes palisadoes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comment

@JaiPannu-IITI
Copy link
Author

@palisadoes #3278 This will conflict with this PR since folders have been moved according to xoldd, what we can do is,

  1. First resolve the other PR and merge it.
  2. Then I will pull that changes here and resolve conflicts.
  3. Then this PR can be merged and function as expected with even more sample data.

Meanwhile I will come up with a future proof solution for docker and vitest.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: ASSERTIVE
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 38536a2 and d97cd80.

📒 Files selected for processing (2)
  • docs/docs/docs/developer-resources/testing.md (1 hunks)
  • vitest.config.ts (2 hunks)
🧰 Additional context used
🪛 markdownlint-cli2 (0.17.2)
docs/docs/docs/developer-resources/testing.md

795-795: Multiple consecutive blank lines
Expected: 1; Actual: 2

(MD012, no-multiple-blanks)

⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: Run tests for talawa api
  • GitHub Check: Analyse Code With CodeQL (typescript)
🔇 Additional comments (1)
vitest.config.ts (1)

13-13: Consider making the timeout configurable.

A 15-second timeout might be insufficient for database operations, especially for sample data scripts. Consider exposing this timeout as an environment variable or configuration property.

Comment on lines +795 to 796

Now you can resume your development work.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick (assertive)

Enhancement & Markdown Formatting Adjustment:
The concluding statement "Now you can resume your development work." serves as a clear directive for users after restarting the Docker environment, which is a helpful addition. However, static analysis indicates that there are multiple consecutive blank lines before this message (MD012). It is recommended to remove the extra blank line so that only a single blank line separates the previous code block from this concluding text.

🧰 Tools
🪛 markdownlint-cli2 (0.17.2)

795-795: Multiple consecutive blank lines
Expected: 1; Actual: 2

(MD012, no-multiple-blanks)

@@ -21,6 +21,6 @@ export default defineConfig({
// teardownTimeout: 10000

hookTimeout: 30000, // 30 seconds for hooks
pool: "threads", // for faster test execution and to avoid postgres max-limit error
pool: "threads",
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Verify the thread pool configuration.

The PR objectives mention enabling single-threaded flow to prevent test conflicts, but the current configuration pool: "threads" enables multi-threading. This could lead to integration test conflicts when multiple tests modify the same database state.

Apply this diff to enable single-threaded execution:

-		pool: "threads",
+		poolOptions: {
+			// Force single-threaded execution to prevent conflicts in integration tests
+			// that modify the same database state
+			threads: {
+				singleThread: true
+			}
+		},
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
pool: "threads",
poolOptions: {
// Force single-threaded execution to prevent conflicts in integration tests
// that modify the same database state
threads: {
singleThread: true
}
},

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ignore-sensitive-files-pr The contributor has a legitimate reason for editiing protected files
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants