Skip to content

Commit

Permalink
Merge pull request #451 from SciCatProject/swap-3226-2
Browse files Browse the repository at this point in the history
Swap 3226 2: add unique name to instrument
  • Loading branch information
nitrosx authored Apr 20, 2023
2 parents 3049657 + 404dd4c commit a65ee7c
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 13 deletions.
8 changes: 8 additions & 0 deletions src/instruments/dto/create-instrument.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@ import { IsObject, IsOptional, IsString } from "class-validator";

@ApiTags("instruments")
export class CreateInstrumentDto {
@ApiProperty({
type: String,
uniqueItems: true,
required: true,
})
@IsString()
readonly uniqueName: string;

@ApiProperty({
type: String,
required: true,
Expand Down
15 changes: 13 additions & 2 deletions src/instruments/instruments.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export class InstrumentsController {
return instrument;
} catch (e) {
throw new HttpException(
"Instrument with the same name already exists",
"Instrument with the same unique name already exists",
HttpStatus.BAD_REQUEST,
);
}
Expand Down Expand Up @@ -134,7 +134,18 @@ export class InstrumentsController {
@Param("id") id: string,
@Body() updateInstrumentDto: UpdateInstrumentDto,
): Promise<Instrument | null> {
return this.instrumentsService.update({ _id: id }, updateInstrumentDto);
try {
const instrument = await this.instrumentsService.update(
{ _id: id },
updateInstrumentDto,
);
return instrument;
} catch (e) {
throw new HttpException(
"Instrument with the same unique name already exists",
HttpStatus.BAD_REQUEST,
);
}
}

@UseGuards(PoliciesGuard)
Expand Down
1 change: 1 addition & 0 deletions src/instruments/instruments.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Instrument } from "./schemas/instrument.schema";
const mockInstrument: Instrument = {
_id: "testPid",
pid: "testPid",
uniqueName: "Test",
name: "Test",
customMetadata: {},
};
Expand Down
15 changes: 14 additions & 1 deletion src/instruments/schemas/instrument.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,26 @@ export class Instrument {
@ApiProperty({
type: String,
required: true,
description: "The name of the instrument. The name has to be unique.",
description:
"The unique name of the instrument. This name has to be unique within the scicat instance.",
})
@Prop({
type: String,
unique: true,
required: true,
})
uniqueName: string;

@ApiProperty({
type: String,
required: true,
description:
"The common name of the instrument. This name can be non unique as it is the name that users use to commnoly refer to this instrument.",
})
@Prop({
type: String,
required: true,
})
name: string;

@ApiProperty({
Expand Down
15 changes: 12 additions & 3 deletions test/Instrument.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ let accessTokenIngestor = null,
instrumentId3 = null,
encodedInstrumentId3 = null;

const newName = "ESS2.5";
const newName = "ESS3-1";

describe("0900: Instrument: instrument management, creation, update, deletion and search", () => {
beforeEach((done) => {
Expand Down Expand Up @@ -105,7 +105,7 @@ describe("0900: Instrument: instrument management, creation, update, deletion an
});
});

it("0040: adds instrument #2 again as ingestor, which should fail", async () => {
it("0040: adds instrument #2 again as ingestor, which should fail because uniqueName is not unique", async () => {
return request(appUrl)
.post("/api/v3/Instruments")
.send(TestData.InstrumentCorrect2)
Expand All @@ -114,7 +114,7 @@ describe("0900: Instrument: instrument management, creation, update, deletion an
.expect(400);
});

it("0050: adds invalid instrument as ingestor, which should fail", async () => {
it("0050: adds invalid instrument as ingestor, which should fail because it is missing the uniqeName", async () => {
return request(appUrl)
.post("/api/v3/Instruments")
.send(TestData.InstrumentWrong1)
Expand Down Expand Up @@ -275,6 +275,15 @@ describe("0900: Instrument: instrument management, creation, update, deletion an
});
});

it("0155: update unique name for instrument #2 as ingestor to ESS3-1, which should fail becuase is not unique ", async () => {
return request(appUrl)
.patch("/api/v3/Instruments/" + instrumentId2)
.send({ uniqueName: newName })
.set("Accept", "application/json")
.set({ Authorization: `Bearer ${accessTokenIngestor}` })
.expect(400);
});

it("0160: should fetch same instrument by id as ingestor", async () => {
return request(appUrl)
.get("/api/v3/Instruments/" + instrumentId2)
Expand Down
11 changes: 6 additions & 5 deletions test/InstrumentsFilter.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,24 @@ let instrumentPid1 = null,


const InstrumentCorrect1 = {
...TestData.InstrumentCorrect,
...TestData.InstrumentCorrect1,
name: "ESS instrument one",
};

const InstrumentCorrect2 = {
...TestData.InstrumentCorrect,
...TestData.InstrumentCorrect2,
name: "ESS instrument two",
};

const InstrumentCorrect3 = {
...TestData.InstrumentCorrect,
...TestData.InstrumentCorrect3,
name: "Another instrument at ESS, number three",
};

const InstrumentCorrect4 = {
...TestData.InstrumentCorrect,
name: "Yet another instrument at ESS, number four",
...TestData.InstrumentCorrect3,
uniqueName: "ESS3-2",
name: "Yet another instrument at ESS, a new number three different from the other",
};

describe("InstrumentFilter: Test retrieving instruments using filtering capabilities", () => {
Expand Down
7 changes: 5 additions & 2 deletions test/TestData.js
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,7 @@ const TestData = {
},

InstrumentCorrect1: {
uniqueName: "ESS1-1",
name: "ESS1",
customMetadata: {
institute: "An immaginary intitution #1",
Expand All @@ -721,6 +722,7 @@ const TestData = {
},
},
InstrumentCorrect2: {
uniqueName: "ESS2-1",
name: "ESS2",
customMetadata: {
institute: "An immaginary intitution #2",
Expand All @@ -729,15 +731,16 @@ const TestData = {
},
},
InstrumentCorrect3: {
name: "ESS3",
uniqueName: "ESS3-1",
name: "ESS1",
customMetadata: {
institute: "An immaginary intitution #3",
department: "An immaginary department #3",
main_user: "somebody else",
},
},
InstrumentWrong1: {
instrumentName: "ESS-wrong",
name: "ESS-wrong",
},

};
Expand Down

0 comments on commit a65ee7c

Please sign in to comment.