Skip to content

Commit 0b14716

Browse files
committed
Fix bug of child entity seen as attachments
1 parent 2ade026 commit 0b14716

File tree

3 files changed

+29
-30
lines changed

3 files changed

+29
-30
lines changed

lib/csn-runtime-extension.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ Object.defineProperty(cds.builtin.classes.entity.prototype, '_attachments', {
66
const entity = this;
77
return {
88
get hasAttachmentsComposition() {
9-
return entity.compositions && Object.keys(entity.compositions).some(ele => entity.compositions[ele]._target?.["@_is_media_data"] || entity.compositions[ele]._target?._attachments.hasAttachmentsComposition)
9+
return entity.compositions && Object.keys(entity.compositions).some(ele => entity.compositions[ele]._target?.["@_is_media_data"])
1010
},
1111
get attachmentCompositions() {
1212
const resultSet = new LinkedDefinitions()
1313
if (!entity.compositions) return resultSet
14-
for (const ele of Object.keys(entity.compositions).filter(ele => entity.compositions[ele]._target?.["@_is_media_data"] || entity.compositions[ele]._target?._attachments.hasAttachmentsComposition)) {
14+
for (const ele of Object.keys(entity.compositions).filter(ele => entity.compositions[ele]._target?.["@_is_media_data"])) {
1515
resultSet[ele] = entity.compositions[ele]
1616
};
1717
return resultSet;

tests/incidents-app/db/schema.cds

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ type PhoneNumber : String;
7373

7474

7575
entity SampleRootWithComposedEntity {
76-
key sampleID : String;
77-
key gjahr : Integer;
76+
key sampleID : String;
77+
key gjahr : Integer;
7878
}
7979

8080
entity Test : cuid, managed {

tests/integration/attachments.test.js

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,6 @@ describe("Tests for uploading/deleting attachments through API calls", () => {
413413
})
414414

415415
// Upload attachment
416-
await utils.draftModeEdit("processor", "Test", testID, "ProcessorService")
417416
const res = await POST(
418417
`odata/v4/processor/Test(ID=${testID},IsActiveEntity=false)/attachments`,
419418
{
@@ -424,17 +423,17 @@ describe("Tests for uploading/deleting attachments through API calls", () => {
424423
createdBy: "alice",
425424
}
426425
)
427-
expect(res.data.ID).to.not.be.null
426+
expect(res.data.ID).not.toBeNull()
428427

429-
await utils.draftModeSave("processor", "Test", testID, () => {}, "ProcessorService")
428+
await utils.draftModeSave("processor", "Test", testID, "ProcessorService")
430429

431430
// Test that attachment exists and scan status
432431
const getRes = await GET(
433432
`odata/v4/processor/Test(ID=${testID},IsActiveEntity=true)/attachments`
434433
)
435-
expect(getRes.status).to.equal(200)
436-
expect(getRes.data.value.length).to.equal(1)
437-
expect(getRes.data.value[0].status).to.be.oneOf(["Scanning", "Clean", "Unscanned"])
434+
expect(getRes.status).toEqual(200)
435+
expect(getRes.data.value.length).toEqual(1)
436+
expect(["Scanning", "Clean", "Unscanned"]).toContain(getRes.data.value[0].status)
438437
})
439438

440439
it("Uploading attachment to TestDetails works and scan status is set (expected to fail)", async () => {
@@ -468,17 +467,17 @@ describe("Tests for uploading/deleting attachments through API calls", () => {
468467
createdBy: "alice",
469468
}
470469
)
471-
expect(res.data.ID).to.not.be.null
470+
expect(res.data.ID).not.toBeNull()
472471

473-
await utils.draftModeSave("processor", "Test", testID, () => {}, "ProcessorService")
472+
await utils.draftModeSave("processor", "Test", testID, "ProcessorService")
474473

475474
// Test that attachment exists and scan status
476475
const getRes = await GET(
477476
`odata/v4/processor/TestDetails(ID=${detailsID},IsActiveEntity=true)/attachments`
478477
)
479-
expect(getRes.status).to.equal(200)
480-
expect(getRes.data.value.length).to.equal(1)
481-
expect(getRes.data.value[0].status).to.be.oneOf(["Scanning", "Clean", "Unscanned"])
478+
expect(getRes.status).toEqual(200)
479+
expect(getRes.data.value.length).toEqual(1)
480+
expect(["Scanning", "Clean", "Unscanned"]).toContain(getRes.data.value[0].status)
482481
})
483482

484483
it("Deleting Test deletes Test attachment", async () => {
@@ -496,14 +495,14 @@ describe("Tests for uploading/deleting attachments through API calls", () => {
496495
createdBy: "alice",
497496
}
498497
)
499-
expect(attachRes.data.ID).to.not.be.null
500-
await utils.draftModeSave("processor", "Test", testID, () => {}, "ProcessorService")
501-
498+
expect(attachRes.data.ID).not.toBeNull()
499+
const abc = await utils.draftModeSave("processor", "Test", testID, "ProcessorService")
500+
console.log(abc)
502501
// Delete the parent Test entity
503502
const delRes = await DELETE(
504503
`odata/v4/processor/Test(ID=${testID},IsActiveEntity=true)`
505504
)
506-
expect(delRes.status).to.equal(204)
505+
expect(delRes.status).toEqual(204)
507506

508507
// Check that the attachment is deleted
509508
let error
@@ -514,7 +513,7 @@ describe("Tests for uploading/deleting attachments through API calls", () => {
514513
} catch (e) {
515514
error = e
516515
}
517-
expect(error?.response?.status || error?.status).to.equal(404)
516+
expect(error?.response?.status || error?.status).toEqual(404)
518517
})
519518

520519
it("Deleting TestDetails deletes TestDetails attachment", async () => {
@@ -538,14 +537,14 @@ describe("Tests for uploading/deleting attachments through API calls", () => {
538537
createdBy: "alice",
539538
}
540539
)
541-
expect(attachRes.data.ID).to.not.be.null
542-
await utils.draftModeSave("processor", "Test", testID, () => {}, "ProcessorService")
540+
expect(attachRes.data.ID).not.toBeNull()
541+
await utils.draftModeSave("processor", "Test", testID, "ProcessorService")
543542

544543
// Delete the child TestDetails entity
545544
const delRes = await DELETE(
546545
`odata/v4/processor/TestDetails(ID=${detailsID},IsActiveEntity=true)`
547546
)
548-
expect(delRes.status).to.equal(204)
547+
expect(delRes.status).toEqual(204)
549548

550549
// Check that the attachment is deleted
551550
let error
@@ -556,7 +555,7 @@ describe("Tests for uploading/deleting attachments through API calls", () => {
556555
} catch (e) {
557556
error = e
558557
}
559-
expect(error?.response?.status || error?.status).to.equal(404)
558+
expect(error?.response?.status || error?.status).toEqual(404)
560559
})
561560

562561
it("Deleting Test deletes both Test and TestDetails attachments", async () => {
@@ -574,7 +573,7 @@ describe("Tests for uploading/deleting attachments through API calls", () => {
574573
createdBy: "alice",
575574
}
576575
)
577-
expect(attachResTest.data.ID).to.not.be.null
576+
expect(attachResTest.data.ID).not.toBeNull()
578577

579578
const detailsID = cds.utils.uuid()
580579
await POST(
@@ -592,14 +591,14 @@ describe("Tests for uploading/deleting attachments through API calls", () => {
592591
createdBy: "alice",
593592
}
594593
)
595-
expect(attachResDetails.data.ID).to.not.be.null
596-
await utils.draftModeSave("processor", "Test", testID, () => {}, "ProcessorService")
594+
expect(attachResDetails.data.ID).not.toBeNull()
595+
await utils.draftModeSave("processor", "Test", testID, "ProcessorService")
597596

598597
// Delete the child TestDetails entity
599598
const delRes = await DELETE(
600599
`odata/v4/processor/Test(ID=${testID},IsActiveEntity=true)`
601600
)
602-
expect(delRes.status).to.equal(204)
601+
expect(delRes.status).toEqual(204)
603602

604603
// Check that the attachment is deleted
605604
let error
@@ -610,7 +609,7 @@ describe("Tests for uploading/deleting attachments through API calls", () => {
610609
} catch (e) {
611610
error = e
612611
}
613-
expect(error?.response?.status || error?.status).to.equal(404)
612+
expect(error?.response?.status || error?.status).toEqual(404)
614613
error = null
615614

616615
try {
@@ -620,7 +619,7 @@ describe("Tests for uploading/deleting attachments through API calls", () => {
620619
} catch (e) {
621620
error = e
622621
}
623-
expect(error?.response?.status || error?.status).to.equal(404)
622+
expect(error?.response?.status || error?.status).toEqual(404)
624623
})
625624
})
626625

0 commit comments

Comments
 (0)