Skip to content

Conversation

@Angel43v3r
Copy link

Description

This pull request adds a 'resetDatabase.js' script that resets and repopulate the test database before running the Cypress UI tests. This also includes a resetDatabase.cy.ts which is a Cypress test file that runs the test using the resetDatabase.js (UITesting.md - Standardized testing data repeatability).

Contributors:
@Angel43v3r

Partly Addresses #1419

Type of change

  • Note merging this changes the database configuration.
  • This change requires a documentation update

Checklist

  • I have followed the OED pull request ideas
  • I have removed text in ( ) from the issue request
  • You acknowledge that every person contributing to this work has signed the [OED Contributing License Agreement] and each author is listed in the Description section.

Limitations

  • The Cypress test resetDatabase.cy.ts currently covers only the database reset functionality, so additional UI tests with more edge cases and error handling may be needed to fully verify this feature.
  • Comments I added in the code during development are not professionally written and may need to be reviewed, updated, or removed for clarity and professionality. The comments contain multiple dashes and my username at times. I marked this pull request as draft because of it.

…base (resetDatabase.cy.ts). Updated cypress.config.ts, docker-compose.yml, tsconfig.json
Copy link
Member

@huss huss left a comment

Choose a reason for hiding this comment

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

Thanks to @Angel43v3r for their first contribution. I've made a few comments where the main ones are to try to better understand the code. Note I have not run the code yet nor carefully reviewed it in detail. I did this since this is a draft and I want to understand more before doing that. Also note there is a merge conflict.

If anything is not clear, I can help or you would like to talk then please let me know.

resetDatabase() {
return new Promise((resolve, reject) => {
let output = '';

Copy link
Member

Choose a reason for hiding this comment

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

extra tabs on otherwise blank line.

Copy link
Author

Choose a reason for hiding this comment

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

Thanks for pointing that out, I will clean up the extra tabs from the blank line before submitting the final.

* It then uses generateTestingData to populate the table using generateSine function.
*/

console.log('PGPASSWORD:', typeof process.env.PGPASSWORD, process.env.PGPASSWORD);
Copy link
Member

Choose a reason for hiding this comment

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

This is a draft but this will need to go when finalized

Copy link
Author

Choose a reason for hiding this comment

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

Thanks for the note, I'll make sure its cleaned up before submitting the final version

//Test configuration to connect to the test database (oed_testing)
//NOTE: if want to run the test locally change host: 'database' to 'localhost'
const testDbConfig = {
user: process.env.OED_DB_USER,
Copy link
Member

Choose a reason for hiding this comment

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

Please change space indenting to tab indenting.

Copy link
Author

Choose a reason for hiding this comment

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

Got it, will fix and update the indentation to use tabs instead of spaces.


//Verifying by running a sample query
const res = await client.query('SELECT COUNT(*) FROM sine_data;');
console.log('Inserted rows of sine data successfully! Total rows of sine data:', res.rows[0].count);
Copy link
Member

Choose a reason for hiding this comment

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

Another console.log.

Copy link
Author

Choose a reason for hiding this comment

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

I tend to use console.log during development to help verify things are running smoothly. I will be happy to remove it if necessary, just let me know what you prefer.

Copy link
Member

Choose a reason for hiding this comment

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

It is fine to use console.log during your work. They need to be removed or use the OED logging system before the work is finalized.

Copy link
Author

Choose a reason for hiding this comment

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

Where can I find the documentation for the OED logging system?

Copy link
Member

Choose a reason for hiding this comment

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

Since you are doing UI testing, I should have said before that it is unusual to use the logging system. It would log to the testing DB that is not normally used. I would expect that most (or all) of the console.log msgs are for debugging and would go once the code is finalized.

If you want to learn about the logging system, look at src/server/log.js. It uses src/server/models/LogMsg.js to actually store the log msg. src/server/routes/logs.js is the route for the server to accept new log msgs. I has not been used much on the client but see src/client/app/utils/api/LogsApi.ts, src/client/app/utils/api/index.ts & src/client/app/redux/actions/logs.ts for a start. Searching for logToServer from the last file will find uses on the client.

I hope this helps and let me know if you want anything else.

*/

console.log('PGPASSWORD:', typeof process.env.PGPASSWORD, process.env.PGPASSWORD);

Copy link
Member

Choose a reason for hiding this comment

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

Can you help me understand how this differs and why from src/server/test/common.js that is used in testing to check/reset DB/etc?

Copy link
Author

Choose a reason for hiding this comment

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

Hi yes, I initially thought the same thing and tried to use the reset logic from common.js, but then I realized Cypress runs test differently. Mocha/Chai runs test in Node.js and can directly connect to or access the database using 'pg', but Cypress cannot and does not have direct access to the database. Because of that, I had to create resetDatabase.js to set up the reset and test data logic for Cypress. It still uses the same test database, oed_testing, so the main database is safe. It also follows the same idea as common.js where it wipes and repopulates the data for Mocha/Chai, I just adjusted it to work with Cypress.

Copy link
Member

Choose a reason for hiding this comment

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

Thank you for the clarification. I think putting a comment in the code to explain this would be valuable so people will understand what is up. It can even link to the other code for the Mocha/Chai version.

await insertTestData();
console.log('Finished generating the test data!');
} catch (err) {
console.error('Error during test setup:', err);
Copy link
Member

Choose a reason for hiding this comment

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

I'd like to think about how errors are handled for this.

} catch (err) {
console.error('Error during test setup:', err);
}
process.exit(0);
Copy link
Member

Choose a reason for hiding this comment

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

It seems to exit with 0 whether it worked or not. Is that what is desired?

Copy link
Author

Choose a reason for hiding this comment

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

Thank you for pointing that out, the current setup does exit with 0 whether it worked or not, so this definitely needs to be corrected. Ill do an update on the script and make it exit with 0 only on success and then with 1 if any errors occurs. Will that work with you?

Copy link
Member

Choose a reason for hiding this comment

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

Seems fine.


//Test configuration to connect to the test database (oed_testing)
//NOTE: if want to run the test locally change host: 'database' to 'localhost'
const testDbConfig = {
Copy link
Member

Choose a reason for hiding this comment

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

How does this relate to an item with a very similar name in src/server/test/common.js?

Copy link
Author

Choose a reason for hiding this comment

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

Hi, just want to make sure I understand the question. Are you asking about how my resetDatabase.js relate to the common.js?

Copy link
Member

Choose a reason for hiding this comment

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

Yes. This object seems to be very similar to the one in the file I mentioned. Do both need to be defined or could one be reused?

Copy link
Author

Choose a reason for hiding this comment

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

Hi yes the logic in resetDatabase.js and common.js looks similar and they both reset and repopulate the test database. The difference is that common.js is designed to access the database directly using Node.js which works for Mocha/Chai but does not work for Cypress. Cypress runs in a browser and cannot access the database directly and so I made resetDatabase.js to work around that. This allows Cypress to call the database indirectly and pretty much mimics the logic in common.js. I am pretty sure that if you really want to use some of the logic in common.js that can be implemented, but I choose to do it separately because its cleaner and there is not much code that I found that I can reuse.


//Admin configuration to connect to the Postgres database
//NOTE: if want to run the test locally change host: 'database' to 'localhost'
const adminConfig = {
Copy link
Member

Choose a reason for hiding this comment

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

Is this used to drop the user's DB information? If so, does this mean that running the Cypress tests would remove their data? I'm not sure but OED should try to avoid that.

Copy link
Author

Choose a reason for hiding this comment

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

No, this should not drop or affect the main database or their data, the resetDatabase.js is suppose to only wipe and repopulate the data inside the oed_testing.

Copy link
Member

Choose a reason for hiding this comment

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

Given this, I recall there being this config and a test one. How do those relate and how are they used? I suspect comments in the code would clear this up.

Copy link
Author

Choose a reason for hiding this comment

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

In resetDatabase.js I am connecting to it using the OED_DB_TEST_DATABASE = oed_testing. I specified it in the environment in the docker-compose.yml

};

//Test configuration to connect to the test database (oed_testing)
//NOTE: if want to run the test locally change host: 'database' to 'localhost'
Copy link
Member

Choose a reason for hiding this comment

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

Can you help me understand what this means? OED usually runs on the local machine (for server) and client web browser. I have not tried this yet since I don't want to accidentally wipe my DB of other data.

Copy link
Author

Choose a reason for hiding this comment

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

Sorry about that, I tend to write notes for myself during development and haven't cleaned up the code. This note I was just trying to mention that if I run the test inside Docker container then the database hostname need to be set to 'database' but if I want to run the tests directly on my local machine (not using docker) then I need to change the host from database to localhost. This comment is mostly a note to myself since I was still trying to learn how the tools work.

Copy link
Member

Choose a reason for hiding this comment

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

Okay. I think the current directions on the design doc have people run the tests in the cypress Docker container. Do you think that should change?

Copy link
Author

Choose a reason for hiding this comment

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

Yes, I followed the documentation and it specifies to run the tests in the Cypress and Docker container so I assumed that's the intended approach. I think it's fine to continue running the tests this way. The only problem on my end was that I have to interact with the database to reset and repopulate it, which required a script, but other UI tests should work properly since they don't require direct access to the database and can operate through the apps UI.

@Angel43v3r
Copy link
Author

Angel43v3r commented Sep 18, 2025

Hi @huss, Thank you for the feedbacks. I have gone through each comment and tried to answer all of the questions. I saw the merge conflict as well, but left it since this was still a draft. I will make sure to fix it properly before submitting the final version. Let me know if there is anything I missed or if you'd like me to make any additional changes.

@huss
Copy link
Member

huss commented Nov 19, 2025

@Angel43v3r I'm looking at all the open PRs and wondering the status of this one. Should I be reviewing it? Are you still working on it now? Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants