Skip to content

Commit

Permalink
feat: finish return formatted facets and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
g-saracca committed Dec 6, 2024
1 parent 5c22df8 commit a67b55b
Show file tree
Hide file tree
Showing 7 changed files with 162 additions and 2 deletions.
15 changes: 15 additions & 0 deletions src/collections/domain/models/CollectionItemSubset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,20 @@ import { CollectionPreview } from './CollectionPreview'

export interface CollectionItemSubset {
items: (CollectionPreview | DatasetPreview | FilePreview)[]
facets: CollectionItemsFacet[]
totalItemCount: number
}

export interface CollectionItemsFacet {
[key: string]: CollectionItemsFacetValue
}

interface CollectionItemsFacetValue {
friendly: string
labels: CollectionItemsFacetLabel[]
}

interface CollectionItemsFacetLabel {
name: string
count: number
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export type CollectionItemsFacetPayload = [Record<string, CollectionItemsFacetPayloadValue>]

export interface CollectionItemsFacetPayloadValue {
friendly: string
labels: Record<string, number>[]
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ import { transformPayloadToOwnerNode } from '../../../../core/infra/repositories
import { transformHtmlToMarkdown } from '../../../../datasets/infra/repositories/transformers/datasetTransformers'
import { CollectionFacet } from '../../../domain/models/CollectionFacet'
import { CollectionFacetPayload } from './CollectionFacetPayload'
import { CollectionItemSubset } from '../../../domain/models/CollectionItemSubset'
import {
CollectionItemsFacet,
CollectionItemSubset
} from '../../../domain/models/CollectionItemSubset'
import { DatasetPreview } from '../../../../datasets'
import { FilePreview } from '../../../../files'
import { DatasetPreviewPayload } from '../../../../datasets/infra/repositories/transformers/DatasetPreviewPayload'
Expand All @@ -21,6 +24,7 @@ import { CollectionPreviewPayload } from './CollectionPreviewPayload'
import { CollectionPreview } from '../../../domain/models/CollectionPreview'
import { CollectionContact } from '../../../domain/models/CollectionContact'
import { CollectionType } from '../../../domain/models/CollectionType'
import { CollectionItemsFacetPayload } from './CollectionItemsFacetsPayload'

export const transformCollectionResponseToCollection = (response: AxiosResponse): Collection => {
const collectionPayload = response.data.data
Expand Down Expand Up @@ -79,7 +83,10 @@ export const transformCollectionItemsResponseToCollectionItemSubset = (
): CollectionItemSubset => {
const responseDataPayload = response.data.data
const itemsPayload = responseDataPayload.items
const facets = responseDataPayload.facets as CollectionItemsFacetPayload

const items: (DatasetPreview | FilePreview | CollectionPreview)[] = []

itemsPayload.forEach(function (
itemPayload: CollectionPreviewPayload | DatasetPreviewPayload | FilePreviewPayload
) {
Expand All @@ -97,8 +104,22 @@ export const transformCollectionItemsResponseToCollectionItemSubset = (
)
}
})

const transformedFacets: CollectionItemsFacet[] = Object.entries(facets[0]).map(
([key, facetData]) => ({
[key]: {
friendly: facetData.friendly,
labels: facetData.labels.map((label: Record<string, number>) => {
const [name, count] = Object.entries(label)[0]
return { name, count }
})
}
})
)

return {
items: items,
facets: transformedFacets,
totalItemCount: responseDataPayload.total_count
}
}
Expand Down
68 changes: 68 additions & 0 deletions test/integration/collections/CollectionsRepository.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,68 @@ describe('CollectionsRepository', () => {
const expectedFileName = 'test-file-1.txt'
const expectedCollectionsName = 'Scientific Research'

//prettier-ignore
const expectedFacetsAll = [
{
dvCategory: { friendly: 'Dataverse Category', labels: [{ name: 'Laboratory', count: 1 }] }
},
{
publicationStatus: { friendly: 'Publication Status', labels: [{ name: 'Unpublished', count: 3 },{ name: 'Draft', count: 2 }] }
},
{
authorName_ss: { friendly: 'Author Name', labels: [{ name: 'Admin, Dataverse', count: 1 },{ name: 'Owner, Dataverse', count: 1 }]}
},
{
subject_ss: { friendly: 'Subject', labels: [{ name: 'Medicine, Health and Life Sciences', count: 1 }]}
},
{
fileTypeGroupFacet: { friendly: 'File Type', labels: [{ name: 'Text', count: 1 }] }
},
{
fileAccess: { friendly: 'Access', labels: [{ name: 'Public', count: 1 }] }
}
]
//prettier-ignore
const expectedFacetsFromCollectionOnly = [
{
dvCategory: { friendly: 'Dataverse Category', labels: [{ name: 'Laboratory', count: 1 }] }
},
{
publicationStatus: { friendly: 'Publication Status', labels: [{ name: 'Unpublished', count: 1 }]}
}
]
//prettier-ignore
const expectedFacetsFromDatasetOnly = [
{
publicationStatus: { friendly: 'Publication Status', labels: [{ name: 'Draft', count: 1 },{ name: 'Unpublished', count: 1 }] }
},
{
authorName_ss: { friendly: 'Author Name', labels: [{ name: 'Admin, Dataverse', count: 1 },{ name: 'Owner, Dataverse', count: 1 }] }
},
{
subject_ss: { friendly: 'Subject', labels: [{ name: 'Medicine, Health and Life Sciences', count: 1 }] }
}
]
//prettier-ignore
const expectedFacetsFromFileOnly = [
{
publicationStatus: { friendly: 'Publication Status', labels: [{ name: 'Draft', count: 1 },{ name: 'Unpublished', count: 1 }] }
},
{ fileTypeGroupFacet: { friendly: 'File Type', labels: [{ name: 'Text', count: 1 }] } },
{ fileAccess: { friendly: 'Access', labels: [{ name: 'Public', count: 1 }] } }
]
//prettier-ignore
const expectedFacetsFromCollectionAndFile = [
{
dvCategory: { friendly: 'Dataverse Category', labels: [{ name: 'Laboratory', count: 1 }] }
},
{
publicationStatus: { friendly: 'Publication Status', labels: [{ name: 'Unpublished', count: 2 },{ name: 'Draft', count: 1 }] }
},
{ fileTypeGroupFacet: { friendly: 'File Type', labels: [{ name: 'Text', count: 1 }] } },
{ fileAccess: { friendly: 'Access', labels: [{ name: 'Public', count: 1 }] } }
]

expect(actualFilePreview.checksum?.type).toBe('MD5')
expect(actualFilePreview.checksum?.value).toBe(expectedFileMd5)
expect(actualFilePreview.datasetCitation).toContain(expectedDatasetCitationFragment)
Expand Down Expand Up @@ -347,6 +409,8 @@ describe('CollectionsRepository', () => {

expect(actual.totalItemCount).toBe(3)

expect(actual.facets).toEqual(expectedFacetsAll)

// Test limit and offset
actual = await sut.getCollectionItems(testCollectionAlias, 1, 1)
expect((actual.items[0] as DatasetPreview).persistentId).toBe(testDatasetIds.persistentId)
Expand Down Expand Up @@ -413,6 +477,7 @@ describe('CollectionsRepository', () => {
expect(actual.items.length).toBe(1)
expect(actual.totalItemCount).toBe(1)
expect((actual.items[0] as CollectionPreview).name).toBe(expectedCollectionsName)
expect(actual.facets).toEqual(expectedFacetsFromCollectionOnly)

// Test type dataset
const collectionSearchCriteriaForDatasetType = new CollectionSearchCriteria().withItemTypes([
Expand All @@ -427,6 +492,7 @@ describe('CollectionsRepository', () => {
expect(actual.items.length).toBe(1)
expect(actual.totalItemCount).toBe(1)
expect((actual.items[0] as DatasetPreview).title).toBe(expectedDatasetDescription)
expect(actual.facets).toEqual(expectedFacetsFromDatasetOnly)

// Test type file
const collectionSearchCriteriaForFileType = new CollectionSearchCriteria().withItemTypes([
Expand All @@ -441,6 +507,7 @@ describe('CollectionsRepository', () => {
expect(actual.items.length).toBe(1)
expect(actual.totalItemCount).toBe(1)
expect((actual.items[0] as FilePreview).name).toBe(expectedFileName)
expect(actual.facets).toEqual(expectedFacetsFromFileOnly)

// Test multiple types
const collectionSearchCriteriaForMultiTypes = new CollectionSearchCriteria().withItemTypes([
Expand All @@ -457,6 +524,7 @@ describe('CollectionsRepository', () => {
expect(actual.totalItemCount).toBe(2)
expect((actual.items[0] as FilePreview).name).toBe(expectedFileName)
expect((actual.items[1] as CollectionPreview).name).toBe(expectedCollectionsName)
expect(actual.facets).toEqual(expectedFacetsFromCollectionAndFile)
})

test('should return error when collection does not exist', async () => {
Expand Down
40 changes: 40 additions & 0 deletions test/testHelpers/collections/collectionItemsFacetsHelper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { CollectionItemsFacet } from '../../../src/collections/domain/models/CollectionItemSubset'
import { CollectionItemsFacetPayload } from '../../../src/collections/infra/repositories/transformers/CollectionItemsFacetsPayload'

export const createCollectionItemsFacetsModel = (): CollectionItemsFacet[] => {
return [
{
facet1: {
friendly: 'Facet 1',
labels: [
{ name: 'Label 1', count: 5 },
{ name: 'Label 2', count: 4 }
]
}
},
{
facet2: {
friendly: 'Facet 2',
labels: [
{ name: 'Label 3', count: 8 },
{ name: 'Label 4', count: 9 }
]
}
}
]
}

export const createCollectionItemsFacetsPayload = (): CollectionItemsFacetPayload => {
return [
{
facet1: {
friendly: 'Facet 1',
labels: [{ 'Label 1': 5 }, { 'Label 2': 4 }]
},
facet2: {
friendly: 'Facet 2',
labels: [{ 'Label 3': 8 }, { 'Label 4': 9 }]
}
}
]
}
9 changes: 8 additions & 1 deletion test/unit/collections/CollectionsRepository.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ import {
createCollectionPreviewPayload
} from '../../testHelpers/collections/collectionPreviewHelper'
import { GetCollectionItemsQueryParams } from '../../../src/collections/domain/models/GetCollectionItemsQueryParams'
import {
createCollectionItemsFacetsModel,
createCollectionItemsFacetsPayload
} from '../../testHelpers/collections/collectionItemsFacetsHelper'

describe('CollectionsRepository', () => {
const sut: CollectionsRepository = new CollectionsRepository()
Expand Down Expand Up @@ -362,9 +366,11 @@ describe('CollectionsRepository', () => {
createCollectionPreviewModel()
]
const testTotalCount = 2
const testFacets = createCollectionItemsFacetsModel()

const testItemSubset: CollectionItemSubset = {
items: testItems,
facets: testFacets,
totalItemCount: testTotalCount
}

Expand All @@ -377,7 +383,8 @@ describe('CollectionsRepository', () => {
createDatasetPreviewPayload(),
createFilePreviewPayload(),
createCollectionPreviewPayload()
]
],
facets: createCollectionItemsFacetsPayload()
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions test/unit/collections/GetCollectionItems.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { createDatasetPreviewModel } from '../../testHelpers/datasets/datasetPre
import { createFilePreviewModel } from '../../testHelpers/files/filePreviewHelper'
import { CollectionItemSubset } from '../../../src/collections/domain/models/CollectionItemSubset'
import { createCollectionPreviewModel } from '../../testHelpers/collections/collectionPreviewHelper'
import { createCollectionItemsFacetsModel } from '../../testHelpers/collections/collectionItemsFacetsHelper'

describe('execute', () => {
let collectionRepositoryStub: ICollectionsRepository
Expand All @@ -16,9 +17,11 @@ describe('execute', () => {
createCollectionPreviewModel()
]
const testTotalCount = 3
const testFacets = createCollectionItemsFacetsModel()

const testItemSubset: CollectionItemSubset = {
items: testItems,
facets: testFacets,
totalItemCount: testTotalCount
}

Expand Down

0 comments on commit a67b55b

Please sign in to comment.