Skip to content

Commit

Permalink
Update tests snapshots
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulLeCam committed Nov 1, 2023
1 parent 1f905bc commit 0a4204d
Show file tree
Hide file tree
Showing 12 changed files with 320 additions and 388 deletions.
19 changes: 9 additions & 10 deletions packages/devtools/src/composite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -543,16 +543,15 @@ export class Composite {
*/
async startIndexingOn(ceramic: CeramicApi): Promise<void> {
assertAuthenticatedDID(ceramic)
const definedIndices: Array<ModelData> = Array.from(Object.keys(this.#definition.models)).map(
(id) => {
const streamID = StreamID.fromString(id)
const indices = this.#definition.indices?.[id] ?? []
return {
streamID,
indices,
}
},
)
const definedIndices: Array<ModelData> = []
for (const [id, model] of Object.entries(this.#definition.models)) {
if (model.version === '1.0' || !model.interface) {
definedIndices.push({
streamID: StreamID.fromString(id),
indices: this.#definition.indices?.[id] ?? [],
})
}
}
await ceramic.admin.startIndexingModelData(definedIndices)
}

Expand Down
2 changes: 1 addition & 1 deletion packages/devtools/src/schema/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ export class SchemaParser {
interface: isInterfaceType(type),
implements: type.getInterfaces().map((i) => i.name),
description: args.description,
accountRelation: accountRelationValue as ModelAccountRelationV2,
accountRelation: accountRelationValue,
relations: object.relations,
}
}
Expand Down
29 changes: 25 additions & 4 deletions packages/devtools/src/schema/resolution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,12 @@ function executeCreateFactory(
if (existing == null) {
compositeViews[name] = view
} else {
viewsPromises[name] = getDependencyID(view.model).then((model) => ({
...view,
model,
}))
viewsPromises[name] = getDependencyID(view.model).then((model) => {
return { ...view, model }
})
}
} else {
viewsPromises[name] = Promise.resolve(view)
}
}

Expand Down Expand Up @@ -282,6 +283,8 @@ export async function createIntermediaryCompositeDefinition(
indices: {},
views: {},
}
const modelIDs: Record<string, string> = {}

await Promise.all(
Object.values(executing).map(async (executedPromise) => {
const res = await executedPromise
Expand All @@ -290,7 +293,25 @@ export async function createIntermediaryCompositeDefinition(
definition.commits[res.id] = await res.commitsPromise
definition.indices[res.id] = res.indices
definition.views[res.id] = res.views
modelIDs[res.name] = res.id
}),
)
// Replace referenced models in composite views by their ID
for (const modelViews of Object.values(definition.views)) {
for (const view of Object.values(modelViews)) {
if (
(view.type === 'relationCountFrom' ||
view.type === 'relationFrom' ||
view.type === 'relationDocument') &&
view.model !== null
) {
const id = modelIDs[view.model]
if (id == null) {
throw new Error(`ID not found for referenced model ${view.model}`)
}
view.model = id
}
}
}
return definition
}
10 changes: 10 additions & 0 deletions packages/devtools/src/test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ export function mockDefinition(
for (const abstractModel of Object.values(definition.models)) {
if (abstractModel.action === 'create') {
const definition = abstractModel.model
for (const view of Object.values(definition.views ?? {})) {
if (
(view.type === 'relationCountFrom' ||
view.type === 'relationDocument' ||
view.type === 'relationFrom') &&
view.model !== null
) {
view.model = `${view.model}ID`
}
}
models[`${definition.name}ID`] = definition
if (abstractModel.indices) {
modelsIndices[`${definition.name}ID`] = abstractModel.indices
Expand Down
139 changes: 4 additions & 135 deletions packages/devtools/test/__snapshots__/format-runtime.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -56,53 +56,6 @@ exports[`Runtime format Note model definition with views 1`] = `
`;

exports[`Runtime format Post with comments relations 1`] = `
{
"accountData": {
"postList": {
"name": "Post",
"type": "connection",
},
},
"enums": {},
"models": {
"Post": {
"accountRelation": {
"type": "list",
},
"id": "PostID",
"implements": [],
"interface": false,
},
},
"objects": {
"Post": {
"author": {
"type": "view",
"viewType": "documentAccount",
},
"date": {
"required": false,
"type": "datetime",
},
"text": {
"required": true,
"type": "string",
},
"title": {
"indexed": true,
"required": true,
"type": "string",
},
"version": {
"type": "view",
"viewType": "documentVersion",
},
},
},
}
`;

exports[`Runtime format Post with comments relations 2`] = `
{
"accountData": {
"commentList": {
Expand Down Expand Up @@ -141,7 +94,7 @@ exports[`Runtime format Post with comments relations 2`] = `
},
"post": {
"relation": {
"model": "Post",
"model": "PostID",
"property": "postID",
"source": "document",
},
Expand All @@ -162,99 +115,14 @@ exports[`Runtime format Post with comments relations 2`] = `
"viewType": "documentVersion",
},
},
"Post": {
"author": {
"type": "view",
"viewType": "documentAccount",
},
"date": {
"required": false,
"type": "datetime",
},
"text": {
"required": true,
"type": "string",
},
"title": {
"required": true,
"type": "string",
},
"version": {
"type": "view",
"viewType": "documentVersion",
},
},
},
}
`;

exports[`Runtime format Post with comments relations 3`] = `
{
"accountData": {
"commentList": {
"name": "Comment",
"type": "connection",
},
"postList": {
"name": "Post",
"type": "connection",
},
},
"enums": {},
"models": {
"Comment": {
"accountRelation": {
"type": "list",
},
"id": "CommentID",
"implements": [],
"interface": false,
},
"Post": {
"accountRelation": {
"type": "list",
},
"id": "PostID",
"implements": [],
"interface": false,
},
},
"objects": {
"Comment": {
"author": {
"type": "view",
"viewType": "documentAccount",
},
"post": {
"relation": {
"model": "Post",
"property": "postID",
"source": "document",
},
"type": "view",
"viewType": "relation",
},
"postID": {
"required": true,
"type": "streamid",
},
"text": {
"required": true,
"type": "string",
},
"version": {
"type": "view",
"viewType": "documentVersion",
},
},
"Post": {
"author": {
"type": "view",
"viewType": "documentAccount",
},
"comments": {
"relation": {
"model": "Comment",
"model": "CommentID",
"property": "postID",
"source": "queryConnection",
},
Expand All @@ -263,7 +131,7 @@ exports[`Runtime format Post with comments relations 3`] = `
},
"commentsCount": {
"relation": {
"model": "Comment",
"model": "CommentID",
"property": "postID",
"source": "queryCount",
},
Expand All @@ -279,6 +147,7 @@ exports[`Runtime format Post with comments relations 3`] = `
"type": "string",
},
"title": {
"indexed": true,
"required": true,
"type": "string",
},
Expand Down
30 changes: 10 additions & 20 deletions packages/devtools/test/composite.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ import type { CeramicApi } from '@ceramicnetwork/common'
import { StreamID } from '@ceramicnetwork/streamid'
import {
ImageMetadataType,
createCommentSchemaWithPost,
loadPostSchemaWithComments,
mediaSchema,
noteSchema,
postSchema,
profilesSchema,
noteSchema,
ratingSchema,
} from '@composedb/test-schemas'
import type { ModelDefinition } from '@composedb/types'
Expand Down Expand Up @@ -561,7 +560,7 @@ describe('composite', () => {
Composite.create({ ceramic, schema: noteSchema }),
])
const mergedComposite = postComposite.merge([ratingComposite, noteComposite]).toJSON()
expect(Object.keys(mergedComposite.indices ?? {})).toHaveLength(3)
expect(Object.keys(mergedComposite.indices ?? {})).toHaveLength(4)
})
})
})
Expand Down Expand Up @@ -739,21 +738,12 @@ describe('composite', () => {
})

test('Relations support', async () => {
const postComposite = await Composite.create({ ceramic, schema: postSchema })
const postID = postComposite.modelIDs[0]
expect(postID).toBeDefined()

const postAndCommentComposite = await Composite.create({
ceramic,
schema: createCommentSchemaWithPost(postID),
})
const commentID = postAndCommentComposite.modelIDs.find((id) => id !== postID)
expect(commentID).toBeDefined()
const composite = await Composite.create({ ceramic, schema: postSchema })
expect(composite.modelIDs).toHaveLength(2)
})

const postWithCommentComposite = await Composite.create({
ceramic,
schema: loadPostSchemaWithComments(postID, commentID as string),
})
expect(postWithCommentComposite.modelIDs).toHaveLength(2)
}, 120000)
test('Interfaces support', async () => {
const composite = await Composite.create({ ceramic, schema: mediaSchema })
expect(composite.modelIDs).toHaveLength(6)
})
})
24 changes: 3 additions & 21 deletions packages/devtools/test/format-runtime.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
/* eslint-disable @typescript-eslint/no-unsafe-assignment */

import {
createCommentSchemaWithPost,
loadPostSchemaWithComments,
noteSchema,
postSchema,
profilesSchema,
socialSchema,
} from '@composedb/test-schemas'
import { noteSchema, postSchema, profilesSchema, socialSchema } from '@composedb/test-schemas'

import { createRuntimeDefinition, getName, mockDefinitionFromSchema } from '../src'

Expand Down Expand Up @@ -52,19 +45,8 @@ describe('Runtime format', () => {

test('Post with comments relations', () => {
const postDefinition = mockDefinitionFromSchema(postSchema)
expect(createRuntimeDefinition(postDefinition)).toMatchSnapshot()

const commentDefinition = mockDefinitionFromSchema(
createCommentSchemaWithPost('PostID'),
postDefinition.models,
)
expect(createRuntimeDefinition(commentDefinition)).toMatchSnapshot()

const postWithCommentsDefinition = mockDefinitionFromSchema(
loadPostSchemaWithComments('PostID', 'CommentID'),
commentDefinition.models,
)
expect(createRuntimeDefinition(postWithCommentsDefinition)).toMatchSnapshot()
const runtime = createRuntimeDefinition(postDefinition)
expect(runtime).toMatchSnapshot()
})

test('Social with account reference', () => {
Expand Down
Loading

0 comments on commit 0a4204d

Please sign in to comment.