Skip to content

Commit

Permalink
Membership mappings fixes w.r.t. Joystream/hydra#435 + additional tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Lezek123 committed Jun 25, 2021
1 parent 8a0e655 commit 776a136
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 36 deletions.
3 changes: 1 addition & 2 deletions query-node/mappings/content/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -412,8 +412,7 @@ async function updateVideoLicense(

// Update license (and potentially remove foreign key reference)
// FIXME: Note that we MUST to provide "null" here in order to unset a relation,
// even though the model typings itself are not aware that "null" is a valid value.
// See: https://github.com/typeorm/typeorm/issues/2934
// See: https://github.com/Joystream/hydra/issues/435
video.license = license as License | undefined
video.updatedAt = new Date(ctx.event.blockTimestamp)
await store.save<Video>(video)
Expand Down
5 changes: 3 additions & 2 deletions query-node/mappings/membership.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,13 @@ export async function members_MemberProfileUpdated({ store, event }: EventContex
const member = await getMemberById(store, memberId)
const eventTime = new Date(event.blockTimestamp)

// FIXME: https://github.com/Joystream/hydra/issues/435
if (typeof metadata?.name === 'string') {
member.metadata.name = metadata.name || undefined
member.metadata.name = (metadata.name || null) as string | undefined
member.metadata.updatedAt = eventTime
}
if (typeof metadata?.about === 'string') {
member.metadata.about = metadata.about || undefined
member.metadata.about = (metadata.about || null) as string | undefined
member.metadata.updatedAt = eventTime
}
// TODO: avatar
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,55 @@ import { MemberContext, EventDetails } from '../../types'
import { MembershipFieldsFragment, MemberProfileUpdatedEventFieldsFragment } from '../../graphql/generated/queries'
import { MembershipMetadata } from '@joystream/metadata-protobuf'
import { Utils } from '../../utils'
import { isSet } from '@joystream/metadata-protobuf/utils'

export type MemberProfileData = {
name?: string | null
handle?: string | null
about?: string | null
}

// TODO: Add partial update to make sure it works too
export class UpdateProfileHappyCaseFixture extends BaseQueryNodeFixture {
private memberContext: MemberContext
// Update data
private newName = 'New name'
private newHandle = 'New handle'
private newAbout = 'New about'
private newValues: MemberProfileData
private oldValues: MemberProfileData

private event?: EventDetails
private tx?: SubmittableExtrinsic<'promise'>

public constructor(api: Api, query: QueryNodeApi, memberContext: MemberContext) {
public constructor(
api: Api,
query: QueryNodeApi,
memberContext: MemberContext,
oldValues: MemberProfileData,
newValues: MemberProfileData
) {
super(api, query)
this.memberContext = memberContext
this.oldValues = oldValues
this.newValues = newValues
console.log({ oldValues, newValues })
}

private assertProfileUpdateSuccesful(qMember: MembershipFieldsFragment | null) {
if (!qMember) {
throw new Error('Query node: Membership not found!')
}
const {
handle,
metadata: { name, about },
} = qMember
assert.equal(name, this.newName)
assert.equal(handle, this.newHandle)
const { handle, metadata } = qMember
const expected = this.getExpectedValues()
assert.equal(metadata.name, expected.name)
assert.equal(handle, expected.handle)
// TODO: avatar
assert.equal(about, this.newAbout)
assert.equal(metadata.about, expected.about)
}

public getExpectedValues(): MemberProfileData {
return {
handle: isSet(this.newValues.handle) ? this.newValues.handle : this.oldValues.handle,
name: isSet(this.newValues.name) ? this.newValues.name || null : this.oldValues.name,
about: isSet(this.newValues.about) ? this.newValues.about || null : this.oldValues.about,
}
}

private assertQueryNodeEventIsValid(
Expand All @@ -50,23 +70,24 @@ export class UpdateProfileHappyCaseFixture extends BaseQueryNodeFixture {
newHandle,
newMetadata,
} = qEvent
const expected = this.getExpectedValues()
assert.equal(inExtrinsic, txHash)
assert.equal(memberId, this.memberContext.memberId.toString())
assert.equal(newHandle, this.newHandle)
assert.equal(newMetadata.name, this.newName)
assert.equal(newMetadata.about, this.newAbout)
assert.equal(newHandle, expected.handle)
assert.equal(newMetadata.name, expected.name)
assert.equal(newMetadata.about, expected.about)
// TODO: avatar
}

async execute(): Promise<void> {
const metadata = new MembershipMetadata({
name: this.newName,
about: this.newAbout,
name: this.newValues.name,
about: this.newValues.about,
})
// TODO: avatar
this.tx = this.api.tx.members.updateProfile(
this.memberContext.memberId,
this.newHandle,
this.newValues.handle || null,
Utils.metadataToBytes(MembershipMetadata, metadata)
)
const txFee = await this.api.estimateTxFee(this.tx, this.memberContext.account)
Expand Down
2 changes: 1 addition & 1 deletion tests/integration-tests/src/fixtures/membership/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ export { RemoveStakingAccountsHappyCaseFixture } from './RemoveStakingAccountsHa
export { SudoUpdateMembershipSystem } from './SudoUpdateMembershipSystem'
export { TransferInvitesHappyCaseFixture } from './TransferInvitesHappyCaseFixture'
export { UpdateAccountsHappyCaseFixture } from './UpdateAccountsHappyCaseFixture'
export { UpdateProfileHappyCaseFixture } from './UpdateProfileHappyCaseFixture'
export { UpdateProfileHappyCaseFixture, MemberProfileData } from './UpdateProfileHappyCaseFixture'
23 changes: 16 additions & 7 deletions tests/integration-tests/src/fixtures/membership/utils.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
import { MembershipMetadata } from '@joystream/metadata-protobuf'
import { CreateInterface } from '@joystream/types'
import { BuyMembershipParameters } from '@joystream/types/members'
import { Utils } from '../../utils'
import { Bytes } from '@polkadot/types'

type MemberCreationParams = {
root_account: string
controller_account: string
handle: string
name?: string
about?: string
metadata: Bytes
}

// Common code for Membership fixtures
export function generateParamsFromAccountId(accountId: string): CreateInterface<BuyMembershipParameters> {
const metadataBytes = Utils.metadataToBytes(MembershipMetadata, {
name: `name${accountId.substring(0, 14)}`,
about: `about${accountId.substring(0, 14)}`,
})
export function generateParamsFromAccountId(accountId: string): MemberCreationParams {
const name = `name${accountId.substring(0, 14)}`
const about = `about${accountId.substring(0, 14)}`
const metadataBytes = Utils.metadataToBytes(MembershipMetadata, { name, about })

return {
root_account: accountId,
controller_account: accountId,
handle: `handle${accountId.substring(0, 14)}`,
name,
about,
metadata: metadataBytes,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ export default async function membershipSystem({ api, query, env }: FlowProps):
referralCut: 5,
invitedInitialBalance: new BN(500),
},
// BigInt above Int32 case:
{
membershipPrice: new BN(100_000_000_000),
},
{
defaultInviteCount: 5,
membershipPrice: new BN(500),
Expand Down
31 changes: 25 additions & 6 deletions tests/integration-tests/src/flows/membership/updatingProfile.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,42 @@
import { FlowProps } from '../../Flow'
import { BuyMembershipHappyCaseFixture, UpdateProfileHappyCaseFixture } from '../../fixtures/membership'
import {
BuyMembershipHappyCaseFixture,
MemberProfileData,
UpdateProfileHappyCaseFixture,
} from '../../fixtures/membership'

import { extendDebug } from '../../Debugger'
import { FixtureRunner } from '../../Fixture'
import { generateParamsFromAccountId } from '../../fixtures/membership/utils'

export default async function updatingProfile({ api, query }: FlowProps): Promise<void> {
const debug = extendDebug('flow:member-profile-update')
debug('Started')
api.enableDebugTxLogs()

const updates: MemberProfileData[] = [
// Partial updates
// FIXME: Currently handle always need to be provided, see: https://github.com/Joystream/joystream/issues/2503
{ handle: 'New handle 1', name: 'New name' },
{ handle: 'New handle 2' },
// Setting metadata to null
{ handle: 'New handle 3', name: '', about: '' },
// Full update
{ handle: 'Updated handle', name: 'Updated name', about: 'Updated about' },
]

const [account] = (await api.createKeyPairs(1)).map((key) => key.address)
const buyMembershipHappyCaseFixture = new BuyMembershipHappyCaseFixture(api, query, [account])
await new FixtureRunner(buyMembershipHappyCaseFixture).run()
const [memberId] = buyMembershipHappyCaseFixture.getCreatedMembers()
const updateProfileHappyCaseFixture = new UpdateProfileHappyCaseFixture(api, query, {
account,
memberId,
})
await new FixtureRunner(updateProfileHappyCaseFixture).runWithQueryNodeChecks()

let oldValues: MemberProfileData = generateParamsFromAccountId(account)
for (const newValues of updates) {
const context = { account, memberId }
const updateProfileHappyCaseFixture = new UpdateProfileHappyCaseFixture(api, query, context, oldValues, newValues)
await new FixtureRunner(updateProfileHappyCaseFixture).runWithQueryNodeChecks()
oldValues = updateProfileHappyCaseFixture.getExpectedValues()
}

debug('Done')
}

0 comments on commit 776a136

Please sign in to comment.