Skip to content

Commit

Permalink
task2 implementation and test
Browse files Browse the repository at this point in the history
  • Loading branch information
Amr Ebada committed Jan 1, 2020
1 parent 277a59a commit 325cc24
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 40 deletions.
35 changes: 0 additions & 35 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions src/controllers/task1.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import films from "../models/films";

export const getLongestOpeningCredit = async () => {
const allFilms = await films.find({});
return allFilms.reduce((prev, next) => {
return allFilms.reduce((prev, current) => {
if (!prev.max) {
prev = { max: prev.opening_crawl.length, name: prev.title };
}
if (next.opening_crawl.length > prev.max) {
if (current.opening_crawl.length > prev.max) {
return {
max: next.opening_crawl.length,
name: next.title
max: current.opening_crawl.length,
name: current.title
};
}
return prev;
Expand Down
28 changes: 28 additions & 0 deletions src/controllers/task2.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* eslint-disable radix */
/* eslint-disable no-restricted-syntax */
/* eslint-disable import/prefer-default-export */
import { connection } from "mongoose";
import films from "../models/films";

export const MostAppearedCharacter = async () => {
const allFilms = await films.find({});
let characters = [];
for (const film of allFilms) {
characters = [...characters, ...film.characters];
}
const count = {};
for (const character of characters) {
count[character] = (count[character] || 0) + 1;
}
let mostCharacter = { max: 0, id: -1 };
for (const key in count) {
if (count[key] > mostCharacter.max) {
mostCharacter = { max: count[key], id: parseInt(key) };
}
}
const people = await connection.db
.collection("people")
.findOne({ id: mostCharacter.id });

return people.name;
};
4 changes: 3 additions & 1 deletion src/schemas/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { GraphQLObjectType, GraphQLSchema } from "graphql";
import task1 from "./task1";
import task2 from "./task2";

const rootQuery = new GraphQLObjectType({
name: "rootQuery",
fields: {
...task1
...task1,
...task2
}
});

Expand Down
9 changes: 9 additions & 0 deletions src/schemas/task2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { GraphQLString } from "graphql";
import { MostAppearedCharacter } from "../controllers/task2.controller";

export default {
MostAppearedCharacter: {
type: GraphQLString,
resolve: MostAppearedCharacter
}
};
49 changes: 49 additions & 0 deletions test/task2.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/* eslint-disable import/first */
import Films from "../src/models/films";
import Config from "../src/config";
import { MostAppearedCharacter } from "../src/controllers/task2.controller";
// jest.mock("mongoose");
// eslint-disable-next-line import/order
import { connection, connect, disconnect } from "mongoose";

beforeAll(async () => {
jest
.spyOn(Films, "find")
.mockImplementationOnce(() =>
Promise.resolve([
{ characters: [1, 2, 3, 4, 5] },
{ characters: [10, 2, 6, 7] },
{ characters: [2, 6, 3] },
{ characters: [9, 5, 8, 4] }
])
);
await connect(Config.DB_URL);

jest.spyOn(connection.db, "collection").mockImplementationOnce(() => ({
findOne: ({ id }) =>
[
{ name: "Luke Skywalker" },
{ name: "Yoda" },
{ name: "C-3PO" },
{ name: "R2-D2" },
{ name: "Darth Vader" },
{ name: "Leia Organa" },
{ name: "Owen Lars" },
{ name: "Beru Whitesun lars" },
{ name: "R5-D4" },
{ name: "Biggs Darklighter" },
{ name: "Obi-Wan Kenobi" },
{ name: "Anakin Skywalker" }
][id - 1]
}));
});

afterAll(async () => {
await disconnect();
});

describe("Most Character Appeared in all movies", () => {
it("should return Yoda", () => {
return MostAppearedCharacter().then(value => expect(value).toEqual("Yoda"));
});
});

0 comments on commit 325cc24

Please sign in to comment.