Skip to content

Commit

Permalink
fix(tags): Tags are now trimmed down of white spaces before updating …
Browse files Browse the repository at this point in the history
…them
  • Loading branch information
KevSanchez committed Jan 22, 2024
1 parent 37b4e20 commit 7cf5a95
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsNotEmpty, MaxLength, Validate } from 'class-validator';
import { IsNotEmpty, IsString, MaxLength, Validate } from 'class-validator';
import { IsValidTagNameValidator } from '@marxan-api/modules/geo-feature-tags/validators/is-valid-tag-name.custom.validator';
import { Transform } from 'class-transformer';

export const tagMaxlength = 30;

Expand All @@ -9,7 +10,9 @@ export const tagMaxLengthErrorMessage = `A tag should not be longer than ${tagMa
export class UpdateGeoFeatureTagDTO {
@ApiProperty()
@IsNotEmpty({ message: 'The Tag cannot not be empty' })
@IsString()
@Validate(IsValidTagNameValidator)
@Transform((value: string): string => value.trim())
@MaxLength(tagMaxlength, {
message: tagMaxLengthErrorMessage,
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsNotEmpty, MaxLength, Validate } from 'class-validator';
import { IsNotEmpty, IsString, MaxLength, Validate } from 'class-validator';
import { IsValidTagNameValidator } from '@marxan-api/modules/geo-feature-tags/validators/is-valid-tag-name.custom.validator';
import {
tagMaxlength,
tagMaxLengthErrorMessage,
} from '@marxan-api/modules/geo-feature-tags/dto/update-geo-feature-tag.dto';
import { Transform } from 'class-transformer';

export class UpdateProjectTagDTO {
@ApiProperty()
Expand All @@ -13,7 +14,9 @@ export class UpdateProjectTagDTO {

@ApiProperty()
@IsNotEmpty({ message: 'The Tag cannot not be empty' })
@IsString()
@Validate(IsValidTagNameValidator)
@Transform((value: string): string => value.trim())
@MaxLength(tagMaxlength, {
message: tagMaxLengthErrorMessage,
})
Expand Down
3 changes: 3 additions & 0 deletions api/apps/api/src/modules/projects/dto/upload-shapefile.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
tagMaxLengthErrorMessage,
} from '@marxan-api/modules/geo-feature-tags/dto/update-geo-feature-tag.dto';
import { IsValidTagNameValidator } from '@marxan-api/modules/geo-feature-tags/validators/is-valid-tag-name.custom.validator';
import { Transform } from 'class-transformer';

export class UploadShapefileDTO {
@ApiProperty()
Expand All @@ -18,7 +19,9 @@ export class UploadShapefileDTO {

@ApiPropertyOptional()
@IsOptional()
@IsString()
@Validate(IsValidTagNameValidator)
@Transform((value: string): string => value.trim())
@MaxLength(tagMaxlength, {
message: tagMaxLengthErrorMessage,
})
Expand Down
24 changes: 24 additions & 0 deletions api/apps/api/test/geo-features/geo-feature-tags.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,30 @@ describe('GeoFeatureTag PATCH (e2e)', () => {
await fixtures.ThenFeatureHasTag(projectId, featureId, newTag);
});

test('should update the tag of the geo feature with trimmed down leading and trailing white spaces', async () => {
// ARRANGE
const projectId = await fixtures.GivenProject('someProject');
const featureId = await fixtures.GivenFeatureOnProject(
projectId,
'someFeature',
);
await fixtures.GivenTagOnFeature(projectId, featureId, 'oldTag');
const newPaddedTag = ' padded TAG ';

// ACT
const response = await fixtures.WhenPatchingAGeoFeatureTag(
projectId,
featureId,
newPaddedTag,
);

// ASSERT
expect(response.status).toBe(HttpStatus.OK);
expect(response.body.data.type).toBe('geo_features');
expect(response.body.data.attributes.featureClassName).toBe('someFeature');
await fixtures.ThenFeatureHasTag(projectId, featureId, newPaddedTag.trim());
});

test('should tag the feature properly, even if the feature does not previously have a tag', async () => {
// ARRANGE
const projectId = await fixtures.GivenProject('someProject');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,29 @@ describe('Projects Tag PATCH (e2e)', () => {
await fixtures.ThenFeatureHasTag(projectId1, featureId11, 'updatedTAG');
await fixtures.ThenFeatureHasTag(projectId1, featureId14, 'updatedTAG');
});

test('should update all feature tag rows that match exactly with the tag to be updated trimmed down of leading and trailing white spaces', async () => {
// ARRANGE
const projectId = await fixtures.GivenProject('someProject');
const featureId1 = await fixtures.GivenFeatureOnProject(projectId, 'f1');
const featureId2 = await fixtures.GivenFeatureOnProject(projectId, 'f2');

await fixtures.GivenTagOnFeature(projectId, featureId1, 'toBeUpdated');
await fixtures.GivenTagOnFeature(projectId, featureId2, 'toBeUpdated');
const paddedTag = ' paddedTAG ';

//ACT
const response = await fixtures.WhenPatchingAProjectTag(
projectId,
'toBeUpdated',
paddedTag,
);

//ASSERT
expect(response.status).toBe(HttpStatus.OK);
await fixtures.ThenFeatureHasTag(projectId, featureId1, paddedTag.trim());
await fixtures.ThenFeatureHasTag(projectId, featureId2, paddedTag.trim());
});
});

describe('Projects Tag DELETE (e2e)', () => {
Expand Down
24 changes: 24 additions & 0 deletions api/apps/api/test/upload-feature/upload-feature.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,30 @@ test(`if tagging info is included in DTO and valid, created feature should be ta
await fixtures.ThenGeoFeatureTagIsCreated(name, tag);
});

test(`if tagging info is included in, the feature's tag should be trimmed down of white spaces `, async () => {
// ARRANGE
const name = 'someFeature';
const description = 'someDescrip';
const paddedTag = ' paddedTag ';
await fixtures.GivenProjectPusWithGeometryForProject();

// ACT
const result = await fixtures.WhenUploadingCustomFeature(
name,
description,
paddedTag,
);

// ASSERT
await fixtures.ThenGeoFeaturesAreCreated(
result,
name,
description,
paddedTag.trim(),
);
await fixtures.ThenGeoFeatureTagIsCreated(name, paddedTag.trim());
});

test(`if there is already an existing feature with a tag that has equivalent capitalization to the one included in the custom feature DTO, the existing one will be used for the new feature`, async () => {
// ARRANGE
const equivalentTag = 'some-Tag';
Expand Down

1 comment on commit 7cf5a95

@vercel
Copy link

@vercel vercel bot commented on 7cf5a95 Jan 22, 2024

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

marxan – ./

marxan23.vercel.app
marxan-git-develop-vizzuality1.vercel.app
marxan-vizzuality1.vercel.app

Please sign in to comment.