Skip to content

Commit

Permalink
Update tests in array methods to use test helper (neo4j#4858)
Browse files Browse the repository at this point in the history
* Add test helpers

* WIP fix issue tests

* Update issue integration tests to use TestHelpers

* Fix eslint errors

* Update test 349

* Improve contextValue in testHelper

* Address PR comments

* Fix bug with cleanNodes

* Remove getSession from testHelper

* Fix int test 464

* Remove deprecation notice from cleanNodesUsingSession

* Update integration test for 505

* Update test 915

* Update remaining integration/issues tests with testHelper

* Update tests in array methods to use test helper
  • Loading branch information
angrykoala authored Mar 13, 2024
1 parent bbeef3d commit f7bdadb
Show file tree
Hide file tree
Showing 7 changed files with 188 additions and 431 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,42 +17,26 @@
* limitations under the License.
*/

import { gql } from "graphql-tag";
import type { GraphQLError } from "graphql";
import { graphql } from "graphql";
import type { Driver, Session } from "neo4j-driver";
import { generate } from "randomstring";
import { gql } from "graphql-tag";
import { IncomingMessage } from "http";
import { Socket } from "net";

import { Neo4jGraphQL } from "../../../src/classes";
import Neo4jHelper from "../neo4j";
import { UniqueType } from "../../utils/graphql-types";
import { generate } from "randomstring";
import { TestHelper } from "../utils/tests-helper";

describe("array-pop-and-push", () => {
let driver: Driver;
let session: Session;
let neo4j: Neo4jHelper;

beforeAll(async () => {
neo4j = new Neo4jHelper();
driver = await neo4j.getDriver();
});
let testHelper: TestHelper;

afterAll(async () => {
await driver.close();
});

beforeEach(async () => {
session = await neo4j.getSession();
beforeEach(() => {
testHelper = new TestHelper();
});

afterEach(async () => {
await session.close();
await testHelper.close();
});

test("should throw an error when trying to pop an element from a non-existing array", async () => {
const typeMovie = new UniqueType("Movie");
const typeMovie = testHelper.createUniqueType("Movie");

const typeDefs = gql`
type ${typeMovie} {
Expand All @@ -62,7 +46,7 @@ describe("array-pop-and-push", () => {
}
`;

const neoSchema = new Neo4jGraphQL({ typeDefs });
await testHelper.initNeo4jGraphQL({ typeDefs });

const movieTitle = generate({
charset: "alphabetic",
Expand All @@ -84,17 +68,9 @@ describe("array-pop-and-push", () => {
CREATE (m:${typeMovie} {title:$movieTitle, tags: ["abc"] })
`;

await session.run(cypher, { movieTitle });

const gqlResult = await graphql({
schema: await neoSchema.getSchema(),
source: update,
contextValue: neo4j.getContextValues(),
});
await testHelper.runCypher(cypher, { movieTitle });

if (gqlResult.errors) {
console.log(JSON.stringify(gqlResult.errors, null, 2));
}
const gqlResult = await testHelper.runGraphQL(update);

expect(gqlResult.errors).toBeDefined();
expect(
Expand All @@ -105,7 +81,7 @@ describe("array-pop-and-push", () => {
});

test("should throw an error if not authenticated on field definition", async () => {
const typeMovie = new UniqueType("Movie");
const typeMovie = testHelper.createUniqueType("Movie");
const typeDefs = `
type ${typeMovie} {
title: String
Expand All @@ -114,7 +90,7 @@ describe("array-pop-and-push", () => {
}
`;

const neoSchema = new Neo4jGraphQL({ typeDefs, features: { authorization: { key: "secret" } } });
await testHelper.initNeo4jGraphQL({ typeDefs, features: { authorization: { key: "secret" } } });

const movieTitle = generate({
charset: "alphabetic",
Expand All @@ -136,27 +112,23 @@ describe("array-pop-and-push", () => {
CREATE (m:${typeMovie} {title:$movieTitle, tags: ['a', 'b'], moreTags: []})
`;

await session.run(cypher, { movieTitle });
await testHelper.runCypher(cypher, { movieTitle });

const token = "not valid token";

const socket = new Socket({ readable: true });
const req = new IncomingMessage(socket);
req.headers.authorization = `Bearer ${token}`;

const gqlResult = await graphql({
schema: await neoSchema.getSchema(),
source: update,
contextValue: neo4j.getContextValues(),
});
const gqlResult = await testHelper.runGraphQL(update);

expect(gqlResult.errors).toBeDefined();
expect((gqlResult.errors as GraphQLError[]).some((el) => el.message.includes("Unauthenticated"))).toBeTruthy();
expect(gqlResult.data).toBeNull();
});

test("should throw an error when input is invalid", async () => {
const typeMovie = new UniqueType("Movie");
const typeMovie = testHelper.createUniqueType("Movie");

const typeDefs = gql`
type ${typeMovie} {
Expand All @@ -166,7 +138,7 @@ describe("array-pop-and-push", () => {
}
`;

const neoSchema = new Neo4jGraphQL({ typeDefs });
await testHelper.initNeo4jGraphQL({ typeDefs });

const movieTitle = generate({
charset: "alphabetic",
Expand All @@ -188,13 +160,9 @@ describe("array-pop-and-push", () => {
CREATE (m:${typeMovie} {title:$movieTitle, tags: ["abc"], moreTags: ["this", "that", "them"] })
`;

await session.run(cypher, { movieTitle });
await testHelper.runCypher(cypher, { movieTitle });

const gqlResult = await graphql({
schema: await neoSchema.getSchema(),
source: update,
contextValue: neo4j.getContextValues(),
});
const gqlResult = await testHelper.runGraphQL(update);

expect(gqlResult.errors).toBeDefined();
expect(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,39 +17,23 @@
* limitations under the License.
*/

import { graphql } from "graphql";
import { gql } from "graphql-tag";
import type { Driver, Session } from "neo4j-driver";
import { generate } from "randomstring";

import Neo4jHelper from "../neo4j";
import { Neo4jGraphQL } from "../../../src/classes";
import { UniqueType } from "../../utils/graphql-types";
import { TestHelper } from "../utils/tests-helper";

describe("array-pop-and-push", () => {
let driver: Driver;
let session: Session;
let neo4j: Neo4jHelper;

beforeAll(async () => {
neo4j = new Neo4jHelper();
driver = await neo4j.getDriver();
});
let testHelper: TestHelper;

afterAll(async () => {
await driver.close();
});

beforeEach(async () => {
session = await neo4j.getSession();
beforeEach(() => {
testHelper = new TestHelper();
});

afterEach(async () => {
await session.close();
await testHelper.close();
});

test("should push to and pop from two different arrays in the same update", async () => {
const typeMovie = new UniqueType("Movie");
const typeMovie = testHelper.createUniqueType("Movie");

const typeDefs = gql`
type ${typeMovie} {
Expand All @@ -59,7 +43,7 @@ describe("array-pop-and-push", () => {
}
`;

const neoSchema = new Neo4jGraphQL({ typeDefs });
await testHelper.initNeo4jGraphQL({ typeDefs });

const movieTitle = generate({
charset: "alphabetic",
Expand All @@ -81,13 +65,9 @@ describe("array-pop-and-push", () => {
CREATE (m:${typeMovie} {title:$movieTitle, tags: ["abc"], moreTags: ["this", "that", "them"] })
`;

await session.run(cypher, { movieTitle });
await testHelper.runCypher(cypher, { movieTitle });

const gqlResult = await graphql({
schema: await neoSchema.getSchema(),
source: update,
contextValue: neo4j.getContextValues(),
});
const gqlResult = await testHelper.runGraphQL(update);

if (gqlResult.errors) {
console.log(JSON.stringify(gqlResult.errors, null, 2));
Expand Down
Loading

0 comments on commit f7bdadb

Please sign in to comment.