-
-
Notifications
You must be signed in to change notification settings - Fork 1k
test: add unit tests for main and db and improve coverage #596
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
Open
Laertes87
wants to merge
34
commits into
main
Choose a base branch
from
AddMoreUnitTests
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
d00bfdc
test: add unit tests for main and db and improve coverage
Laertes87 761cc8a
test: fix db test actually exercising db.ts code
Laertes87 82af881
test: test isolated test db
Laertes87 8c204a7
test: translate comments to English
Laertes87 2108975
test: adapt main.test.ts to test against isolated db
Laertes87 1d72254
test: improve test cleanup in libreoffice.test.ts
Laertes87 396dd9f
test: correctly use isolated test db instead of production db in tests
Laertes87 c2bebb1
test: correctly create isolated test db in main.test.ts
Laertes87 7b80678
test: set DB_PATH in process.env for main.test.ts
Laertes87 7d7415b
test: make sure data directory exists when executing main.test.ts
Laertes87 7d411b1
test: restore parent-directory creation for db
Laertes87 b32c28d
test: fall back to isolated test db instead of production db
Laertes87 210fa5b
test: remove duplicate initializeDatabase call
Laertes87 1d328f4
test: create isolated test db in temp directory and clean up afterward
Laertes87 a2f0005
test: remove dead fallback logic
Laertes87 dd85304
test: fix copy and paste comment
Laertes87 ab92a06
test: rename commonTests.ts to not be included in coverage report
Laertes87 165c487
test: revert renaming of commonTests.ts and add bunfig.toml to ignore…
Laertes87 b316452
test: add unit test for printVersions
Laertes87 87924f3
test: ensure that test does not touch production database
Laertes87 aa81ed1
test: ensure that test database connection is properly closed
Laertes87 823c83e
test: ensure that migrateDb database always gets cleaned up
Laertes87 81334c7
test: do not silently swallow every unexpected error
Laertes87 6fac7cb
test: fix test name and comment
Laertes87 152be90
test: test the actual default export
Laertes87 ecfa591
test: test that handleConvert does not throw
Laertes87 2fa4dcf
test: remove dead mock code line
Laertes87 310a065
test: consolidate db test
Laertes87 46705b6
test: ensure that module is loaded after environment variable is set
Laertes87 86cf842
test: wait for resolves to finish before assertion
Laertes87 bbe5748
test: fix test name and comment language
Laertes87 0e91478
test: make sure to only update database version if needed
Laertes87 ce540ca
test: improve readability of sql in db test
Laertes87 ee8137b
test: make check for status column case-insensitive
Laertes87 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| [test] | ||
| coverage = true | ||
| coveragePathIgnorePatterns = [ | ||
| "tests/converters/helpers/commonTests.ts" | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,43 +1,58 @@ | ||
| import { mkdirSync } from "node:fs"; | ||
| import { Database } from "bun:sqlite"; | ||
| import { mkdirSync } from "node:fs"; | ||
| import { dirname } from "node:path"; | ||
|
|
||
| mkdirSync("./data", { recursive: true }); | ||
| const db = new Database("./data/mydb.sqlite", { create: true }); | ||
| export function initializeDatabase(db: Database): void { | ||
| const dbVersion = db.query("PRAGMA user_version").get() as { user_version?: number }; | ||
| const hasTables = db.query("SELECT * FROM sqlite_master WHERE type='table'").get(); | ||
|
|
||
| if (!db.query("SELECT * FROM sqlite_master WHERE type='table'").get()) { | ||
| db.exec(` | ||
| CREATE TABLE IF NOT EXISTS users ( | ||
| id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
| email TEXT NOT NULL, | ||
| password TEXT NOT NULL | ||
| ); | ||
| CREATE TABLE IF NOT EXISTS file_names ( | ||
| id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
| job_id INTEGER NOT NULL, | ||
| file_name TEXT NOT NULL, | ||
| output_file_name TEXT NOT NULL, | ||
| status TEXT DEFAULT 'not started', | ||
| FOREIGN KEY (job_id) REFERENCES jobs(id) | ||
| ); | ||
| CREATE TABLE IF NOT EXISTS jobs ( | ||
| id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
| user_id INTEGER NOT NULL, | ||
| date_created TEXT NOT NULL, | ||
| status TEXT DEFAULT 'not started', | ||
| num_files INTEGER DEFAULT 0, | ||
| FOREIGN KEY (user_id) REFERENCES users(id) | ||
| ); | ||
| PRAGMA user_version = 1;`); | ||
| } | ||
| if (!hasTables) { | ||
| db.exec(` | ||
| CREATE TABLE IF NOT EXISTS users ( | ||
| id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
| email TEXT NOT NULL, | ||
| password TEXT NOT NULL | ||
| ); | ||
| CREATE TABLE IF NOT EXISTS file_names ( | ||
| id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
| job_id INTEGER NOT NULL, | ||
| file_name TEXT NOT NULL, | ||
| output_file_name TEXT NOT NULL, | ||
| status TEXT DEFAULT 'not started', | ||
| FOREIGN KEY (job_id) REFERENCES jobs(id) | ||
| ); | ||
| CREATE TABLE IF NOT EXISTS jobs ( | ||
| id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
| user_id INTEGER NOT NULL, | ||
| date_created TEXT NOT NULL, | ||
| status TEXT DEFAULT 'not started', | ||
| num_files INTEGER DEFAULT 0, | ||
| FOREIGN KEY (user_id) REFERENCES users(id) | ||
| ); | ||
| `); | ||
| db.exec("PRAGMA user_version = 1;"); | ||
| } else if ((dbVersion?.user_version ?? 0) < 1) { | ||
| // Don't trust user_version alone — verify the column is actually | ||
| // missing before altering. This makes the migration safe to re-run | ||
| // even against a file left in an inconsistent state. | ||
| const columns = db.query("PRAGMA table_info(file_names)").all() as { name: string }[]; | ||
| const hasStatusColumn = columns.some((c) => c.name.toLowerCase() === "status"); | ||
|
|
||
| if (!hasStatusColumn) { | ||
| db.exec("ALTER TABLE file_names ADD COLUMN status TEXT DEFAULT 'not started';"); | ||
| } | ||
|
|
||
| db.exec("PRAGMA user_version = 1;"); | ||
| console.log("Updated database to version 1."); | ||
| } | ||
|
|
||
| const dbVersion = (db.query("PRAGMA user_version").get() as { user_version?: number }).user_version; | ||
| if (dbVersion === 0) { | ||
| db.exec("ALTER TABLE file_names ADD COLUMN status TEXT DEFAULT 'not started';"); | ||
| db.exec("PRAGMA user_version = 1;"); | ||
| console.log("Updated database to version 1."); | ||
| // enable WAL mode | ||
| db.exec("PRAGMA journal_mode = WAL;"); | ||
| } | ||
|
|
||
| // enable WAL mode | ||
| db.exec("PRAGMA journal_mode = WAL;"); | ||
| const dbPath = process.env.DB_PATH ?? "./data/mydb.sqlite"; | ||
| mkdirSync(dirname(dbPath), { recursive: true }); | ||
| const db = new Database(dbPath, { create: true }); | ||
| initializeDatabase(db); | ||
|
|
||
| export default db; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.