From 6b4786b128a6c37c9217b39bc787eb75e156c066 Mon Sep 17 00:00:00 2001 From: feywind <57276408+feywind@users.noreply.github.com> Date: Wed, 10 Jan 2024 15:15:24 -0500 Subject: [PATCH] tests: add more system tests for new schema samples --- samples/system-test/schema.test.ts | 86 ++++++++++++++++++++---------- 1 file changed, 59 insertions(+), 27 deletions(-) diff --git a/samples/system-test/schema.test.ts b/samples/system-test/schema.test.ts index 8cf868050..47616f1b2 100644 --- a/samples/system-test/schema.test.ts +++ b/samples/system-test/schema.test.ts @@ -40,6 +40,7 @@ const execSync = (cmd: string) => cp.execSync(cmd, {encoding: 'utf-8'}); describe('schema', () => { const projectId = process.env.GCLOUD_PROJECT; const pubsub = new PubSub({projectId}); + const clientPromise = pubsub.getSchemaClient(); const resources = new TestResources('schema'); @@ -127,45 +128,34 @@ describe('schema', () => { } async function commitSchema( - testName: string, + schemaId: string, type: 'avro' | 'proto' - ): Promise { + ): Promise { const suffix = type === 'avro' ? 'avsc' : 'proto'; - const encoding = + const typeId = type === 'avro' ? SchemaTypes.Avro : SchemaTypes.ProtocolBuffer; - const def = ( + const definition = ( await fs.readFile(fixturePath(`provinces.${suffix}`)) ).toString(); - const schemaId = getSchemaId(testName); const schema = pubsub.schema(schemaId); - //await schema.commitSchema(encoding, def); - } - - async function createTopicWithSchema( - testName: string, - schemaName: string, - encodingType: SchemaEncoding - ): Promise { - const topicId = getTopicId(testName); - const [topic] = await pubsub.createTopic({ - name: topicId, - schemaSettings: { - schema: fullSchemaName(schemaName), - encoding: encodingType, + const schemaName = await schema.getName(); + const schemaClient = await clientPromise; + const [result] = await schemaClient.commitSchema({ + name: schemaName, + schema: { + type: typeId, + definition, }, }); - assert.ok(topic); - return topic; + return result; } - async function createTopicWithSchemaRevisions( + async function createTopicWithSchema( testName: string, schemaName: string, - encodingType: SchemaEncoding, - firstRevisionId: string, - lastRevisionId: string + encodingType: SchemaEncoding ): Promise { const topicId = getTopicId(testName); const [topic] = await pubsub.createTopic({ @@ -173,8 +163,6 @@ describe('schema', () => { schemaSettings: { schema: fullSchemaName(schemaName), encoding: encodingType, - firstRevisionId, - lastRevisionId, }, }); assert.ok(topic); @@ -193,6 +181,18 @@ describe('schema', () => { return sub; } + it('should commit an avro schema', async () => { + const schema = await createSchema('commit_schema', 'avro'); + const name = await schema.getName(); + const output = execSync( + `${commandFor('commitAvroSchema')} ${name} ${fixturePath( + 'provinces.avsc' + )}` + ); + assert.include(output, name); + assert.include(output, 'committed'); + }); + it('should create an avro schema', async () => { const schemaId = getSchemaId('create_avro'); const output = execSync( @@ -253,6 +253,9 @@ describe('schema', () => { it('should create a topic with schema revisions', async () => { const id = 'create_topic_rev'; const schema = await createSchema(id, 'proto'); + const committed = await commitSchema(await schema.getName(), 'proto'); + const revId = committed.revisionId!; + const topicId = getTopicId(id); const output = execSync( `${commandFor('createTopicWithSchemaRevisions')} ${topicId} ${ @@ -265,6 +268,8 @@ describe('schema', () => { const [topic] = await pubsub.topic(topicId).get(); assert.include(topic.metadata?.schemaSettings?.schema, schema.id); + assert.strictEqual(topic.metadata?.schemaSettings?.firstRevisionId, revId); + assert.strictEqual(topic.metadata?.schemaSettings?.lastRevisionId, revId); }); it('should delete a schema', async () => { @@ -283,6 +288,33 @@ describe('schema', () => { }*/ }); + it('should delete a schema revision', async () => { + const id = 'delete_rev'; + const schema = await createSchema(id, 'proto'); + const committed = await commitSchema(await schema.getName(), 'proto'); + const revId = committed.revisionId!; + + const output = execSync( + `${commandFor('deleteSchemaRevision')} ${schema.id} ${revId}` + ); + assert.include(output, schema.id); + assert.include(output, 'deleted.'); + }); + + it('should rollback a schema revision', async () => { + const id = 'rollback_rev'; + const schema = await createSchema(id, 'proto'); + const committed = await commitSchema(await schema.getName(), 'proto'); + const revId = committed.revisionId!; + await commitSchema(await schema.getName(), 'proto'); + + const output = execSync( + `${commandFor('rollbackSchema')} ${schema.id} ${revId}` + ); + assert.include(output, schema.id); + assert.include(output, 'rolled back.'); + }); + it('should get a schema', async () => { const schema = await createSchema('get', 'proto');