-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Amr Ebada
committed
Jan 1, 2020
1 parent
277a59a
commit 325cc24
Showing
6 changed files
with
93 additions
and
40 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,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; | ||
}; |
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,9 @@ | ||
import { GraphQLString } from "graphql"; | ||
import { MostAppearedCharacter } from "../controllers/task2.controller"; | ||
|
||
export default { | ||
MostAppearedCharacter: { | ||
type: GraphQLString, | ||
resolve: MostAppearedCharacter | ||
} | ||
}; |
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,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")); | ||
}); | ||
}); |