Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
corps-directory
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
fhurtado14
corps-directory
Commits
3d56d2b7
Commit
3d56d2b7
authored
5 months ago
by
Federico Hurtado
Browse files
Options
Downloads
Patches
Plain Diff
Unit tests for peopleRepository.
parent
e4e55ca9
No related branches found
Branches containing commit
No related tags found
1 merge request
!2
able to bypass cas in localhost
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
backend/package.json
+5
-1
5 additions, 1 deletion
backend/package.json
backend/repository/peopleRepository.js
+5
-0
5 additions, 0 deletions
backend/repository/peopleRepository.js
backend/test/repository/peopleRepository.test.js
+73
-0
73 additions, 0 deletions
backend/test/repository/peopleRepository.test.js
with
83 additions
and
1 deletion
backend/package.json
+
5
−
1
View file @
3d56d2b7
...
...
@@ -5,7 +5,7 @@
"main"
:
"index.js"
,
"scripts"
:
{
"start"
:
"nodemon index.js"
,
"test"
:
"
echo
\"
Error: no test specified
\"
&& exit 1
"
"test"
:
"
jest
"
},
"author"
:
""
,
"license"
:
"ISC"
,
...
...
@@ -21,5 +21,9 @@
"passport"
:
"^0.7.0"
,
"passport-cas"
:
"^0.1.1"
,
"sequelize"
:
"^6.37.4"
},
"devDependencies"
:
{
"jest"
:
"^29.7.0"
,
"jest-mock"
:
"^29.7.0"
}
}
This diff is collapsed.
Click to expand it.
backend/repository/peopleRepository.js
+
5
−
0
View file @
3d56d2b7
...
...
@@ -47,7 +47,12 @@ async function addPerson(person) {
return
results
.
insertId
;
}
catch
(
error
)
{
console
.
error
(
error
);
return
null
;
}
}
async
function
getPersonById
()
{}
async
function
deletePersonById
()
{}
module
.
exports
=
{
addPerson
};
This diff is collapsed.
Click to expand it.
backend/test/repository/peopleRepository.test.js
0 → 100644
+
73
−
0
View file @
3d56d2b7
// Mock the database's pool and query function
jest
.
mock
(
"
../../config/database/database.config
"
,
()
=>
{
const
queryMock
=
jest
.
fn
();
// Mock query function
return
{
pool
:
{
query
:
queryMock
}
};
// Return the mocked pool directly
});
const
{
pool
}
=
require
(
"
../../config/database/database.config
"
);
const
{
addPerson
}
=
require
(
"
../../repository/peopleRepository
"
);
/*
Test methods for peopleRepository
*/
describe
(
"
Testing peopleRepository.js
"
,
()
=>
{
// mock person for testing
const
mockPerson
=
{
firstName
:
"
John
"
,
lastName
:
"
Doe
"
,
middleName
:
"
A
"
,
maidenName
:
null
,
suffix
:
null
,
nickName
:
"
Johnny
"
,
techAlumniChapter
:
"
Tidewater AC
"
,
classYear
:
"
2024
"
,
gradYear
:
"
2024
"
,
gradSemester
:
"
spring
"
,
gender
:
"
male
"
,
};
/*
Tests for the addPerson method
*/
describe
(
"
addPerson Tests
"
,
()
=>
{
it
(
"
Insert works with no errors
"
,
async
()
=>
{
// Mock the database to return the new insert ID as 1
const
mockInsertId
=
1
;
pool
.
query
.
mockResolvedValueOnce
([{
insertId
:
mockInsertId
}]);
// call add person
const
result
=
await
addPerson
(
mockPerson
);
// Verify the SQL query and parameters
expect
(
pool
.
query
).
toHaveBeenCalledWith
(
expect
.
any
(
String
),
// SQL query string, can be further detailed if needed
[
"
John
"
,
"
Doe
"
,
"
A
"
,
null
,
null
,
"
Johnny
"
,
2
,
// hardcoded for now
"
2024
"
,
"
2024
"
,
"
spring
"
,
"
male
"
,
]
);
// expect function to return the new insert id
expect
(
result
).
toBe
(
mockInsertId
);
});
it
(
"
insert throws an error if query fails
"
,
async
()
=>
{
// Mock the database to throw an error
const
mockError
=
new
Error
(
"
Database query failed
"
);
pool
.
query
.
mockRejectedValueOnce
(
mockError
);
// call addPerson and expect it to handle the error
const
result
=
await
addPerson
(
mockPerson
);
expect
(
result
).
toBe
(
null
);
});
});
});
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment