Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Created a testcase for auto primary content #460

Draft
wants to merge 7 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gql/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export interface ContentFields {
type?: Nullable<ContentType>;
link?: Nullable<string>;
parent?: Nullable<string>;
primary: boolean;
primary?: boolean;
}

export interface CreateCollectionArgs {
Expand Down
42 changes: 41 additions & 1 deletion src/program/program.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import { ProgramService } from "./program.service";
import { Prisma, UserRole } from "@prisma/client";
import { UseGuards } from "@nestjs/common";
import { AuthGuard } from "@/auth.guard";
import { isRequiredInputField } from "graphql";
import { async } from "rxjs/internal/scheduler/async";

@Resolver()
// @UseGuards(AuthGuard)
Expand Down Expand Up @@ -266,9 +268,47 @@ export class ProgramResolver {

@Mutation("updateContent")
async updateContent(@Args("input") input: ContentFields) {
let id = input.id;
const original = await this.programService.content({
parent: input.parent
});
if (input.primary == true) {
await original.map(async (org) => {
if (org.primary == true) {
org.primary = false;
await this.programService.updateContent({
id: org.id,
primary: org.primary
});
await this.programService.updateContent({
primary: input.primary,
id: input.id
});
}
});
} else if (input.primary == false) {
// if (original.filter(org => org.primary == true).length == 0) {

await original.map(async (org) => {
if (org.primary == true) {
org.primary = false;
await this.programService.updateContent({
id: org.id,
primary: org.primary
});
}
});
original[0].primary = true;
await this.programService.updateContent({
id: original[0].id,
primary: original[0].primary
});
}

//}

return await this.programService.updateContent(input);
}

@Mutation("deleteContent")
async deleteContent(@Args("contentID") contentID: string) {
return await this.programService.deleteContent(contentID);
Expand Down
5 changes: 3 additions & 2 deletions src/program/program.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1070,7 +1070,7 @@ export class ProgramService {
}

async updateContent(input: ContentFields) {
const { id, type, link, parent } = input;
const { id, type, link, parent, primary } = input;

if (!id) {
throw new Error("Id not provided to updateContent");
Expand All @@ -1083,7 +1083,8 @@ export class ProgramService {
data: {
...(type && { type }),
...(link && { link }),
parent: parent ? { connect: { id: parent } } : undefined
parent: parent ? { connect: { id: parent } } : undefined,
primary: primary
}
});

Expand Down
162 changes: 160 additions & 2 deletions src/program/program.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,22 @@ import {
AssignmentResult,
Course,
PlanOfStudy,
User
User,
ContentType
} from "@/types/graphql";
import { createCollection, createModule } from "../../utils/tests";
import {
createCollection,
createModule,
createContent
} from "../../utils/tests";
import { test, describe, beforeAll, afterAll, expect } from "vitest";
import {
createRandomLesson,
createRandomQuiz,
createRandomContent,
createRandomInputContent
} from "utils";
import { TcpContext } from "@nestjs/microservices";

interface IAssignment extends Assignment {
id: string;
Expand Down Expand Up @@ -302,6 +314,8 @@ describe("Collection", () => {

let testingCollectionID: string;
let testingModuleID: string;
let testingContentID: string;
let fakeContent;

beforeAll(async () => {
service = new ProgramService(prisma);
Expand Down Expand Up @@ -371,3 +385,147 @@ describe("Collection", () => {
expect(true).toBe(true);
});
});
describe("Content", () => {
let prisma: PrismaService;
prisma = new PrismaService();
let service = new ProgramService(prisma);
const resolver = new ProgramResolver(service);;

let testingCollectionID: string;
let testingModuleID: string;
let testingContentID: string;
let fakeContent;

beforeAll(async () => {
<<<<<<< HEAD
service = new ProgramService(prisma);
resolver = new ProgramResolver(service);
=======

>>>>>>> cace1d7 (Requested Changes)
});
afterAll(async () => {
await prisma.$disconnect();
});

test("should set first content entry of a lesson as primary content entry", async function () {
const coll = await resolver.collection({ id: testingCollectionID });
//content
expect(coll).toBeDefined();
if (coll.length > 0) {
coll.map((c) => {
c.lessons.map((lesson) => {
if (lesson.content.length == 1)
expect(lesson.content[0].primary).toBe(true);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are expecting the lesson's first content to be primary if the array length is 1.

  • What is this test cases checking if the content length is more then 1?
  • Is this test case covering creating content with a primary conflict?
  • Is this test case covering updating content to be primary where there already is a primary in the Array?

});
});
}
});
describe("Mutation.updateContent()", async () => {
<<<<<<< HEAD
const inputContent = createRandomContent();
const DBContent = await resolver.content({
parent: inputContent.parentID
});

test("When input-content passed as PRIMARY, content array should update older-primary-content as SECONDARY and passed-input-content as PRIMARY", async () => {
const filteredContentFalse = DBContent.filter(
(org) => org.primary == false
);
const filteredContentTrue = DBContent.filter(
(org) => org.primary == true
);
filteredContentTrue.map((contTrue) => {
if (contTrue.id == inputContent.id) {
expect(contTrue.primary).toBe(true);
} else {
expect(contTrue.primary).toBe(false);
}
});
filteredContentFalse.map((contFalse) => {
if (contFalse.id == inputContent.id) {
expect(contFalse.primary).toBe(true);
} else {
expect(contFalse.primary).toBe(false);
}
});
});
test("When input content is passed as secondary, CONTENT ARRAY'S FIRST ELEMENT should get updated to PRIMARY and the rest elements to SECONDARY", async () => {
DBContent.map((cont) => {
expect(cont.primary).toBe(false);
});

expect(DBContent[0].primary).toBe(true);
});
test("If the input content is passed as secondary and if it's 1st element in the database where it seems to be primary, then the input-content should set as TRUE(PRIMARY) ", async () => {
if (inputContent.id == DBContent[0].id) {
expect(inputContent.primary).toBe(true);
}
=======
const inputContent= createRandomContent()
const DBContent= await resolver.content({parent:inputContent.parentID})

test("When input-content passed as PRIMARY, content array should update older-primary-content as SECONDARY and passed-input-content as PRIMARY",

async () => {

const filteredContentFalse= DBContent.filter(org => org.primary == false)
const filteredContentTrue= DBContent.filter(org => org.primary == true)
filteredContentTrue.map((contTrue)=>{
if(contTrue.id==inputContent.id)
{
expect(contTrue.primary).toBe(
true
);
}
else{
expect(contTrue.primary).toBe(
false
);
}
})
filteredContentFalse.map((contFalse)=>{
if(contFalse.id==inputContent.id)
{
expect(contFalse.primary).toBe(
true
);
}
else{
expect(contFalse.primary).toBe(
false
);
}
})
});
test("When input content is passed as secondary, CONTENT ARRAY'S FIRST ELEMENT should get updated to PRIMARY and the rest elements to SECONDARY",
async () => {
DBContent.map((dbcont)=>{
expect(dbcont[0].primary).toBe(
true
);
})

const filteredContent= DBContent.slice(1)
filteredContent.map((cont)=>{
expect(cont.primary).toBe(
false
);
})

});
test("If the input content is passed as secondary and if it's 1st element in the database where it seems to be primary, then the input-content should set as TRUE(PRIMARY) ",
async () => {
DBContent.map((dbcont)=>{
if(inputContent.id==dbcont[0].id)
{
expect(inputContent.primary).toBe(
true
);
}
})

>>>>>>> cace1d7 (Requested Changes)
});
});
});
11 changes: 10 additions & 1 deletion utils/fakes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,14 @@ export function createRandomContent(parentID?: string): Content {
link: faker.internet.url()
};
}

export function createRandomInputContent(parentID?: string): Content {
return {
id: faker.database.mongodbObjectId(),
parentID: parentID ? parentID : faker.database.mongodbObjectId(),
type: faker.helpers.arrayElement(["TEXT", "IMAGE", "VIDEO", "AUDIO"]),
link: faker.internet.url()
};
}
export function createRandomQuiz(parentID?: string): Quiz {
const questions = faker.datatype.number({min: 5, max:15})
return {
Expand Down Expand Up @@ -265,6 +272,8 @@ export const assignment = createRandomAssignment();
export const assignmentResult = createRandomAssignmentResult();
export const thread = createRandomThread();
export const content = createRandomContent();
export const contentInput = createRandomInputContent();

export const progress = createRandomProgress();
export const quiz = createRandomQuiz();
export const question = createRandomQuestion();
Expand Down
22 changes: 19 additions & 3 deletions utils/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import { PlanOfStudyResolver } from "@/pos";
import { ProgramResolver } from "@/program";
import {
CreateAnswer,
CreateCollectionArgs, CreateQuestion, CreateQuiz,
CreateCollectionArgs, CreateQuestion, CreateQuiz, CreateContentArgs,
EnrollmentStatus, LessonInput,
UserRole
UserRole, ContentType
} from "@/types/graphql";
import {QuizResolver} from "@/quiz/quiz.resolver";
import {Answer, Collection, Lesson, Question, Quiz} from "@prisma/client";
import {Answer, Collection, Lesson, Question, Quiz, Content} from "@prisma/client";

export const shuffle = (str: string) =>
[...str].sort(() => Math.random() - 0.5).join("");
Expand Down Expand Up @@ -135,3 +135,19 @@ export const createAnswer = async (
else return new Error("Failed to create answer");
}

export const createContent = async (
resolver: ProgramResolver,
input: Content
) => {
const data: CreateContentArgs = {

type: ContentType.PDF,
link: input.link,
parent: input.parentID,
primary: input.primary,
}
const content = await resolver.createContent(data);
if (content) return content;
else return new Error("Failed to create content");
}