|
1 | 1 | import { AssetService } from "./assetService"; |
2 | | -import { AssetType, IAssetMetadata, AssetState } from "../models/applicationState"; |
| 2 | +import { AssetType, IAssetMetadata, AssetState, IAsset, IProject } from "../models/applicationState"; |
3 | 3 | import MockFactory from "../common/mockFactory"; |
4 | 4 | import { AssetProviderFactory, IAssetProvider } from "../providers/storage/assetProviderFactory"; |
5 | 5 | import { StorageProviderFactory } from "../providers/storage/storageProviderFactory"; |
6 | 6 | import { constants } from "../common/constants"; |
7 | 7 | import { TFRecordsBuilder, FeatureType } from "../providers/export/tensorFlowRecords/tensorFlowBuilder"; |
8 | 8 | import HtmlFileReader from "../common/htmlFileReader"; |
9 | 9 | import { encodeFileURI } from "../common/utils"; |
| 10 | +import _ from "lodash"; |
10 | 11 |
|
11 | 12 | describe("Asset Service", () => { |
12 | 13 | describe("Static Methods", () => { |
@@ -323,4 +324,105 @@ describe("Asset Service", () => { |
323 | 324 | expect(Math.floor(result.regions[1].points[1].y)).toEqual(800); |
324 | 325 | }); |
325 | 326 | }); |
| 327 | + |
| 328 | + describe("Tag Update functions", () => { |
| 329 | + |
| 330 | + function populateProjectAssets(project?: IProject, assetCount = 10) { |
| 331 | + if (!project) { |
| 332 | + project = MockFactory.createTestProject(); |
| 333 | + } |
| 334 | + const assets = MockFactory.createTestAssets(assetCount); |
| 335 | + assets.forEach((asset) => { |
| 336 | + asset.state = AssetState.Tagged; |
| 337 | + }); |
| 338 | + |
| 339 | + project.assets = _.keyBy(assets, (asset) => asset.id); |
| 340 | + return project; |
| 341 | + } |
| 342 | + |
| 343 | + it("Deletes tag from assets", async () => { |
| 344 | + const tag1 = "tag1"; |
| 345 | + const tag2 = "tag2"; |
| 346 | + const region = MockFactory.createTestRegion(undefined, [tag1, tag2]); |
| 347 | + const asset: IAsset = { |
| 348 | + ...MockFactory.createTestAsset("1"), |
| 349 | + state: AssetState.Tagged, |
| 350 | + }; |
| 351 | + const assetMetadata = MockFactory.createTestAssetMetadata(asset, [region]); |
| 352 | + AssetService.prototype.getAssetMetadata = jest.fn((asset: IAsset) => Promise.resolve(assetMetadata)); |
| 353 | + |
| 354 | + const saveMetadata = jest.fn(); |
| 355 | + AssetService.prototype.save = saveMetadata; |
| 356 | + |
| 357 | + const expectedAssetMetadata: IAssetMetadata = { |
| 358 | + ...MockFactory.createTestAssetMetadata( |
| 359 | + asset, |
| 360 | + [ |
| 361 | + { |
| 362 | + ...region, |
| 363 | + tags: [tag2], |
| 364 | + }, |
| 365 | + ], |
| 366 | + ), |
| 367 | + |
| 368 | + }; |
| 369 | + |
| 370 | + const project = populateProjectAssets(); |
| 371 | + const assetService = new AssetService(project); |
| 372 | + await assetService.deleteTag(project.assets, tag1, assetMetadata); |
| 373 | + expect(saveMetadata).toBeCalledWith(expectedAssetMetadata); |
| 374 | + }); |
| 375 | + |
| 376 | + it("Deletes empty regions after deleting only tag from region", async () => { |
| 377 | + const tag1 = "tag1"; |
| 378 | + const region = MockFactory.createTestRegion(undefined, [tag1]); |
| 379 | + const asset: IAsset = { |
| 380 | + ...MockFactory.createTestAsset("1"), |
| 381 | + state: AssetState.Tagged, |
| 382 | + }; |
| 383 | + const assetMetadata = MockFactory.createTestAssetMetadata(asset, [region]); |
| 384 | + AssetService.prototype.getAssetMetadata = jest.fn((asset: IAsset) => Promise.resolve(assetMetadata)); |
| 385 | + |
| 386 | + const saveMetadata = jest.fn(); |
| 387 | + AssetService.prototype.save = saveMetadata; |
| 388 | + |
| 389 | + const expectedAssetMetadata: IAssetMetadata = MockFactory.createTestAssetMetadata(asset, []); |
| 390 | + const project = populateProjectAssets(); |
| 391 | + const assetService = new AssetService(project); |
| 392 | + await assetService.deleteTag(project.assets, tag1, assetMetadata); |
| 393 | + expect(saveMetadata).toBeCalledWith(expectedAssetMetadata); |
| 394 | + }); |
| 395 | + |
| 396 | + it("Updates renamed tag within all assets", async () => { |
| 397 | + const tag1 = "tag1"; |
| 398 | + const newTag = "tag2"; |
| 399 | + const region = MockFactory.createTestRegion(undefined, [tag1]); |
| 400 | + const asset: IAsset = { |
| 401 | + ...MockFactory.createTestAsset("1"), |
| 402 | + state: AssetState.Tagged, |
| 403 | + }; |
| 404 | + const assetMetadata = MockFactory.createTestAssetMetadata(asset, [region]); |
| 405 | + AssetService.prototype.getAssetMetadata = jest.fn((asset: IAsset) => Promise.resolve(assetMetadata)); |
| 406 | + |
| 407 | + const saveMetadata = jest.fn(); |
| 408 | + AssetService.prototype.save = saveMetadata; |
| 409 | + |
| 410 | + const expectedAssetMetadata: IAssetMetadata = { |
| 411 | + ...MockFactory.createTestAssetMetadata( |
| 412 | + asset, |
| 413 | + [ |
| 414 | + { |
| 415 | + ...region, |
| 416 | + tags: [newTag], |
| 417 | + }, |
| 418 | + ], |
| 419 | + ), |
| 420 | + |
| 421 | + }; |
| 422 | + const project = populateProjectAssets(); |
| 423 | + const assetService = new AssetService(project); |
| 424 | + await assetService.renameTag(project.assets, tag1, newTag, assetMetadata); |
| 425 | + expect(saveMetadata).toBeCalledWith(expectedAssetMetadata); |
| 426 | + }); |
| 427 | + }); |
326 | 428 | }); |
0 commit comments