This repository has been archived by the owner on Nov 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #74 from mithi/tests/abilities-tests
It is better to have two separate test environments (two test databases), one for testing resolvers (which is closer to an integration test), and the other for testing everything else. When testing graphql resolvers, seeding the database should only be done once as this is a slow operation. Currently, using 'globalSetup` and `globalTeardown` with jest doesn't work for setting up the `typeorm` database. The tests are not able to find the connection. See: #73 typeorm/typeorm#5308 jestjs/jest#10178 It is also not efficient to remove all of the entries for this database in order to start with a clean slate for testing the models and testing the seeding operations. So what we do here is we have two test databases. 1. a prepopulated frozen test database (called `test_db`) to be used by the graphql resolvers 2. an empty database (called `empty_test_db`) which we can safely mutate (like inserting and deleting operation) to test seeding functions, among others So this pull request updates all the scripts and other files for the current tests to work I have also refactored some of the test scripts to be more readable and also added a simple test for testing the `abilityById` graphql query..
- Loading branch information
Showing
22 changed files
with
292 additions
and
117 deletions.
There are no files selected for viewing
This file contains 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,3 +1,4 @@ | ||
DB_PASSWORD=password | ||
DB_DEFAULT_URL="postgres://kingdom_rush_user:password@localhost:5432/kingdom_rush_db" | ||
DB_TEST_URL="postgres://kingdom_rush_user:password@localhost:5432/test_db" | ||
DB_TEST_URL="postgres://kingdom_rush_user:password@localhost:5432/test_db" | ||
DB_EMPTY_TEST_URL="postgres://kingdom_rush_user:password@localhost:5432/empty_test_db" |
This file contains 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 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 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 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 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 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 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 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,6 +1,18 @@ | ||
npm run db:create; | ||
npm install; | ||
npm run db:migrate-all; | ||
npm run test; | ||
psql kingdom_rush_user -h localhost -d kingdom_rush_db --pset=pager -f ./scripts/db_gen_info.sql; | ||
cp .env.sample .env; | ||
|
||
# migrate and populate kingdom_rush_db | ||
npm run db:migrate; | ||
psql kingdom_rush_user -h localhost -d kingdom_rush_db -f ./scripts/db_load_csv.sql; | ||
|
||
# migrate and populate test_db | ||
psql kingdom_rush_user -h localhost -d test_db -f ./scripts/db_load_csv.sql; | ||
psql kingdom_rush_user -h localhost -d test_db --pset=pager -f ./scripts/db_gen_info.sql; | ||
psql kingdom_rush_user -h localhost -d test_db --pset=pager -f ./scripts/db_table_info.sql; | ||
|
||
# sleep for a while to help prevent errors in testing the database | ||
sleep 5 | ||
|
||
# run tests | ||
npm run test |
This file contains 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 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 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 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,35 @@ | ||
const result = () => { | ||
return ` | ||
Object { | ||
"data": Object { | ||
"abilityById": Object { | ||
"abilityDescription": "Fills a zone with powerful healing roots. Allies over it heal over 4 seconds. Each upgrade level increases HP healed", | ||
"abilityId": 85, | ||
"abilityName": "Healing Roots", | ||
"kingdom": "KRV", | ||
"levelCosts": Array [ | ||
130, | ||
130, | ||
130, | ||
], | ||
"numberOfLevels": 3, | ||
"totalAbilityCost": 390, | ||
"totalCostWithTowers": 1290, | ||
"towerId": 104, | ||
"towerImageUrl": "https://storage.googleapis.com/kingdom-rush-towers.appspot.com/krv-shaman4.png", | ||
"towerName": "orc shaman, 4", | ||
"towerType": "MAGE", | ||
}, | ||
}, | ||
"errors": undefined, | ||
"extensions": undefined, | ||
"http": Object { | ||
"headers": Headers { | ||
Symbol(map): Object {}, | ||
}, | ||
}, | ||
} | ||
` | ||
} | ||
|
||
export default result |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains 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,45 @@ | ||
import { createConnection, getConnection } from "typeorm" | ||
import { buildSchema } from "type-graphql" | ||
import { AbilityResolver } from "../../src/resolvers/AbilityResolver" | ||
import { ApolloServer, gql } from "apollo-server" | ||
import { createTestClient } from "apollo-server-testing" | ||
import ABILITY_BY_ID_RESULT from "./__snapshots__/ABILITY_BY_ID" | ||
import { DocumentNode } from "graphql" | ||
|
||
beforeAll(async () => { | ||
await createConnection("test") | ||
}) | ||
|
||
afterAll(async () => { | ||
await getConnection("test").close() | ||
}) | ||
|
||
const executeTest = async (testQuery: DocumentNode, correctAnswer: string) => { | ||
const schema = await buildSchema({ resolvers: [AbilityResolver] }) | ||
const { query } = createTestClient(new ApolloServer({ schema })) | ||
|
||
const result = await query({ query: testQuery }) | ||
expect(result).toMatchInlineSnapshot(correctAnswer) | ||
} | ||
|
||
test("1. Be able to get ability data by its id", async () => { | ||
const testQuery = gql` | ||
{ | ||
abilityById(id: 85) { | ||
abilityDescription | ||
abilityId | ||
abilityName | ||
kingdom | ||
levelCosts | ||
numberOfLevels | ||
totalAbilityCost | ||
totalCostWithTowers | ||
towerId | ||
towerImageUrl | ||
towerName | ||
towerType | ||
} | ||
} | ||
` | ||
await executeTest(testQuery, ABILITY_BY_ID_RESULT()) | ||
}) |
Oops, something went wrong.