Sfoglia il codice sorgente

Content directory integration tests: add update_channel_fixture & flow, fix issues

iorveth 4 anni fa
parent
commit
cb59fdb7d8

+ 2 - 0
content-directory-schemas/types/extrinsics/index.d.ts

@@ -0,0 +1,2 @@
+export { AddClassSchema } from './AddClassSchema'
+export { CreateClass } from './CreateClass'

+ 32 - 11
tests/network-tests/src/Api.ts

@@ -33,7 +33,8 @@ import { v4 as uuid } from 'uuid'
 import { InputParser } from 'cd-schemas'
 import { ChannelEntity } from 'cd-schemas/types/entities/ChannelEntity'
 import { VideoEntity } from 'cd-schemas/types/entities/VideoEntity'
-import { initializeContentDir } from 'cd-schemas'
+import { initializeContentDir, ExtrinsicsHelper } from 'cd-schemas'
+import { OperationType } from '@joystream/types/content-directory';
 
 
 import Debugger from 'debug'
@@ -1925,18 +1926,19 @@ export class Api {
     return this.api.createType('u32', this.api.consts[module].maxWorkerNumberLimit)
   }
 
-  async sendContentDirectoryTransaction(memberId: number, parser: InputParser): Promise<Hash> {
-    // We parse the input into CreateEntity and AddSchemaSupportToEntity operations
-    const operations = await parser.getEntityBatchOperations()
-    return await this.api.tx.contentDirectory
+  async sendContentDirectoryTransaction(operations: OperationType[], pair: KeyringPair): Promise<void> {
+
+    const transaction = this.api.tx.contentDirectory
       .transaction(
-        { Member: memberId }, // We use member with id 0 as actor (in this case we assume this is Alice)
+        { Lead: null }, // We use member with id 0 as actor (in this case we assume this is Alice)
         operations // We provide parsed operations as second argument
       )
-      .signAndSend(this.createKeyPairs(1)[0])
+
+      const txHelper = new ExtrinsicsHelper(this.api)
+      await txHelper.sendAndCheck(pair, [transaction], 'Transaction failed!')
   }
 
-  public async createChannelEntity(memberId: number, channel: ChannelEntity): Promise<Hash> {
+  public async createChannelEntity(channel: ChannelEntity, pair: KeyringPair): Promise<void> {
     // Create the parser with known entity schemas (the ones in content-directory-schemas/inputs)
   const parser = InputParser.createWithKnownSchemas(
     this.api,
@@ -1948,10 +1950,12 @@ export class Api {
       },
     ]
   )
-  return await this.sendContentDirectoryTransaction(memberId, parser)
+  // We parse the input into CreateEntity and AddSchemaSupportToEntity operations
+  const operations = await parser.getEntityBatchOperations()
+  return await this.sendContentDirectoryTransaction(operations, pair)
   }
 
-  public async createVideoEntity(memberId: number, video: VideoEntity): Promise<Hash> {
+  public async createVideoEntity(video: VideoEntity, pair: KeyringPair): Promise<void> {
     // Create the parser with known entity schemas (the ones in content-directory-schemas/inputs)
   const parser = InputParser.createWithKnownSchemas(
     this.api,
@@ -1963,7 +1967,24 @@ export class Api {
       },
     ]
   )
-  return await this.sendContentDirectoryTransaction(memberId, parser)
+  // We parse the input into CreateEntity and AddSchemaSupportToEntity operations
+  const operations = await parser.getEntityBatchOperations()
+  return await this.sendContentDirectoryTransaction(operations, pair)
+  }
+
+  public async updateChannelEntity(channelUpdateInput: Record<string, any>, uniquePropValue: Record<string, any>, pair: KeyringPair): Promise<void> {
+    // Create the parser with known entity schemas (the ones in content-directory-schemas/inputs)
+  const parser = InputParser.createWithKnownSchemas(this.api)
+
+  // We can reuse InputParser's `findEntityIdByUniqueQuery` method to find entityId of the channel we
+  // created in ./createChannel.ts example (normally we would probably use some other way to do it, ie.: query node)
+  const CHANNEL_ID = await parser.findEntityIdByUniqueQuery(uniquePropValue, 'Channel')// Use getEntityUpdateOperations to parse the update input
+  const updateOperations = await parser.getEntityUpdateOperations(
+    channelUpdateInput,
+    'Channel', // Class name
+    CHANNEL_ID // Id of the entity we want to update
+  )
+  return await this.sendContentDirectoryTransaction(updateOperations, pair)
   }
 
   public async initializeContentDirectory(leadKeyPair: KeyringPair) {

+ 32 - 8
tests/network-tests/src/fixtures/contentDirectoryModule.ts

@@ -7,20 +7,21 @@ import { Utils } from '../utils'
 import { Fixture } from '../Fixture'
 import { ChannelEntity } from 'cd-schemas/types/entities/ChannelEntity'
 import { VideoEntity } from 'cd-schemas/types/entities/VideoEntity'
+import { KeyringPair } from '@polkadot/keyring/types'
 
 export class CreateChannelFixture implements Fixture {
   private api: Api
-  private memberId: number
   private channelEntity: ChannelEntity
+  private pair: KeyringPair
 
-  public constructor(api: Api, memberId: number, channelEntity: ChannelEntity) {
+  public constructor(api: Api,  channelEntity: ChannelEntity, pair: KeyringPair) {
     this.api = api
-    this.memberId = memberId
+    this.pair = pair
     this.channelEntity = channelEntity
   }
 
   public async runner(expectFailure: boolean): Promise<void> {
-    await this.api.createChannelEntity(this.memberId, this.channelEntity)
+    await this.api.createChannelEntity(this.channelEntity, this.pair)
 
 
     if (expectFailure) {
@@ -31,17 +32,40 @@ export class CreateChannelFixture implements Fixture {
 
 export class CreateVideoFixture implements Fixture {
   private api: Api
-  private memberId: number
+  private pair: KeyringPair
   private videoEntity: VideoEntity
 
-  public constructor(api: Api, memberId: number, videoEntity: VideoEntity) {
+  public constructor(api: Api, videoEntity: VideoEntity, pair: KeyringPair) {
     this.api = api
-    this.memberId = memberId
     this.videoEntity = videoEntity
+    this.pair = pair
   }
 
   public async runner(expectFailure: boolean): Promise<void> {
-    await this.api.createVideoEntity(this.memberId, this.videoEntity)
+    await this.api.createVideoEntity(this.videoEntity, this.pair)
+
+
+    if (expectFailure) {
+      throw new Error('Successful fixture run while expecting failure')
+    }
+  }
+}
+
+export class UpdateChannelFixture implements Fixture {
+  private api: Api
+  private pair: KeyringPair
+  private channelUpdateInput: Record<string, any>
+  private uniquePropValue: Record<string, any>
+
+  public constructor(api: Api, channelUpdateInput: Record<string, any>, uniquePropValue: Record<string, any>, pair: KeyringPair) {
+    this.api = api
+    this.channelUpdateInput = channelUpdateInput
+    this.uniquePropValue = uniquePropValue
+    this.pair = pair
+  }
+
+  public async runner(expectFailure: boolean): Promise<void> {
+    await this.api.updateChannelEntity(this.channelUpdateInput, this.uniquePropValue, this.pair)
 
 
     if (expectFailure) {

+ 5 - 4
tests/network-tests/src/flows/contentDirectory/creatingChannel.ts

@@ -2,8 +2,9 @@ import { Api, WorkingGroups } from '../../Api'
 import { CreateChannelFixture } from '../../fixtures/contentDirectoryModule'
 import { ChannelEntity } from 'cd-schemas/types/entities/ChannelEntity'
 import { assert } from 'chai'
+import { KeyringPair } from '@polkadot/keyring/types'
 
-export function createSimpleChannelFixture(api: Api): CreateChannelFixture {
+export function createSimpleChannelFixture(api: Api, pair: KeyringPair): CreateChannelFixture {
   const channelEntity: ChannelEntity = {
       title: 'Example channel',
       description: 'This is an example channel',
@@ -15,11 +16,11 @@ export function createSimpleChannelFixture(api: Api): CreateChannelFixture {
       avatarPhotoURL: '',
       isPublic: true,
     }
-    return new CreateChannelFixture (api, 0, channelEntity)
+    return new CreateChannelFixture (api, channelEntity, pair)
 }
 
-export default async function channelCreation(api: Api) {
-  const createChannelHappyCaseFixture = createSimpleChannelFixture(api)
+export default async function channelCreation(api: Api, pair: KeyringPair) {
+  const createChannelHappyCaseFixture = createSimpleChannelFixture(api, pair)
 
   await createChannelHappyCaseFixture.runner(false)
 }

+ 5 - 4
tests/network-tests/src/flows/contentDirectory/creatingVideo.ts

@@ -2,8 +2,9 @@ import { Api, WorkingGroups } from '../../Api'
 import { CreateVideoFixture } from '../../fixtures/contentDirectoryModule'
 import { VideoEntity } from 'cd-schemas/types/entities/VideoEntity'
 import { assert } from 'chai'
+import { KeyringPair } from '@polkadot/keyring/types'
 
-export function createVideoReferencingChannelFixture(api: Api): CreateVideoFixture {
+export function createVideoReferencingChannelFixture(api: Api, pair: KeyringPair): CreateVideoFixture {
     const videoEntity: VideoEntity = {
       title: 'Example video',
       description: 'This is an example video',
@@ -39,11 +40,11 @@ export function createVideoReferencingChannelFixture(api: Api): CreateVideoFixtu
       isExplicit: false,
       isPublic: true,
     }
-    return new CreateVideoFixture (api, 0, videoEntity)
+    return new CreateVideoFixture (api, videoEntity, pair)
 }
 
-export default async function createVideo(api: Api) {
-  const createVideoHappyCaseFixture = createVideoReferencingChannelFixture(api)
+export default async function createVideo(api: Api, pair: KeyringPair) {
+  const createVideoHappyCaseFixture = createVideoReferencingChannelFixture(api, pair)
  
   await createVideoHappyCaseFixture.runner(false)
 }

+ 22 - 0
tests/network-tests/src/flows/contentDirectory/updatingChannel.ts

@@ -0,0 +1,22 @@
+import { Api, WorkingGroups } from '../../Api'
+import { UpdateChannelFixture } from '../../fixtures/contentDirectoryModule'
+import { ChannelEntity } from 'cd-schemas/types/entities/ChannelEntity'
+import { assert } from 'chai'
+import { KeyringPair } from '@polkadot/keyring/types'
+
+export function createUpdateChannelTitleFixture(api: Api, pair: KeyringPair): UpdateChannelFixture {
+     // Create partial channel entity, only containing the fields we wish to update
+  const channelUpdateInput: Partial<ChannelEntity> = {
+    title: 'Updated channel title',
+  }
+
+  const uniquePropVal: Record<string, any> = { title: 'Example channel' }
+  
+    return new UpdateChannelFixture (api, channelUpdateInput, uniquePropVal, pair)
+}
+
+export default async function updateChannel(api: Api, pair: KeyringPair) {
+  const createVideoHappyCaseFixture = createUpdateChannelTitleFixture(api, pair)
+ 
+  await createVideoHappyCaseFixture.runner(false)
+}

+ 5 - 2
tests/network-tests/src/scenarios/content-directory.ts

@@ -5,6 +5,7 @@ import leaderSetup from '../flows/workingGroup/leaderSetup'
 import initializeContentDirectory from '../flows/contentDirectory/contentDirectoryInitialization'
 import createChannel from '../flows/contentDirectory/creatingChannel'
 import createVideo from '../flows/contentDirectory/creatingVideo'
+import updateChannel from '../flows/contentDirectory/updatingChannel'
 
 const scenario = async () => {
   // Load env variables
@@ -23,9 +24,11 @@ const scenario = async () => {
 
   await initializeContentDirectory(api, leadKeyPair)
 
-  await createChannel(api)
+  await createChannel(api, leadKeyPair)
 
-  await createVideo(api)
+  await createVideo(api, leadKeyPair)
+
+  await updateChannel(api, leadKeyPair)
   
   // Note: disconnecting and then reconnecting to the chain in the same process
   // doesn't seem to work!