Skip to content

Commit

Permalink
fix: 🐛 check if schema exists now works as intended
Browse files Browse the repository at this point in the history
Check if schema exists now checks if id is part of the name. Removed zod
to avro implementation that was only used in testing.
  • Loading branch information
emilcardell committed Oct 25, 2024
1 parent 6b7b76f commit a637b1e
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 159 deletions.
5 changes: 5 additions & 0 deletions .changeset/wicked-ligers-hope.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@sebspark/pubsub": minor
---

Fixes bug that did not check if a schema exists in the correct way.
4 changes: 1 addition & 3 deletions packages/pubsub/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@
"@types/html-escaper": "3.0.2",
"avsc": "^5.7.7",
"express": "4.21.0",
"html-escaper": "3.0.3",
"ts-pattern": "^5.5.0",
"zod": "^3.23.8"
"html-escaper": "3.0.3"
},
"keywords": [
"google apis",
Expand Down
50 changes: 27 additions & 23 deletions packages/pubsub/src/lib/publisher.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,35 @@ import {
} from '@google-cloud/pubsub'
import { Type } from 'avsc'
import { type Mock, type MockedObject, describe, expect, it, vi } from 'vitest'
import { z } from 'zod'
import { createPublisher } from './publisher'
import { zodToAvro } from './zod-to-avro'

const exampleSchema = z.object({
messageType: z.string(),
created: z.string(),
data: z.string().optional(),
})
type ExampleMessage = {
messageType: string
message: string
}

type ExampleMessage = z.infer<typeof exampleSchema>
const exampleAvroSchema = zodToAvro('Example', exampleSchema, {
namespace: 'com.acme.example',
})
const exampleAvroSchema = `
{
"type": "record",
"name": "ExampleMessage",
"namespace": "com.example",
"fields": [
{
"name": "messageType",
"type": "string"
},
{
"name": "message",
"type": "string"
}
]
}
`

const message = {
messageType: 'type of message',
message: 'message data',
} satisfies ExampleMessage

type ExamplePubsubChannels = {
example: ExampleMessage
Expand Down Expand Up @@ -74,14 +89,6 @@ vi.mock('@google-cloud/pubsub', () => {
let subscriberFn: (args: { ack: Mock; nack: Mock; data: string }) => void

const description = 'This is an example message'
const zodValue = z.object(
{
messageType: z.string(),
message: z.number(),
},
{ description }
)
const avroSchema = zodToAvro('ExampleMessage', zodValue)

describe('when creating a new publisher client with no schema and publish a message', () => {
it('should call the underlaying api with the correct values and message format', async () => {
Expand All @@ -96,7 +103,6 @@ describe('when creating a new publisher client with no schema and publish a mess
projectId: 'test',
})

const message = { messageType: 'TYPE', created: new Date().toISOString() }
await client.topic('example').publish(message)

expect(pubSubMock.topic).toBeCalledWith('example')
Expand All @@ -119,11 +125,10 @@ describe('when creating a new publisher client with schema that does not exist a
projectId: 'test',
})

const message = { messageType: 'TYPE', created: new Date().toISOString() }
await client
.topic('example', {
schemaId: 'schemaId',
avroDefinition: JSON.stringify(exampleAvroSchema),
avroDefinition: exampleAvroSchema,
})
.publish(message)
const schemaType = Type.forSchema(exampleAvroSchema)
Expand Down Expand Up @@ -152,7 +157,6 @@ describe('when creating a new publisher client with schema that does exist and p
projectId: 'test',
})

const message = { messageType: 'TYPE', created: new Date().toISOString() }
await client
.topic('example', {
schemaId: 'schemaId-does-not-exist',
Expand Down
4 changes: 2 additions & 2 deletions packages/pubsub/src/lib/publisher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const syncTopicSchema = async (client: PubSub, cloudSchema: CloudSchema) => {
)
}

const schema = await client.schema(cloudSchema.schemaId)
const schema = client.schema(cloudSchema.schemaId)

const exits = await schemaExists(client, cloudSchema.schemaId)
if (exits) {
Expand Down Expand Up @@ -123,7 +123,7 @@ export const createPublisher = <T extends Record<string, unknown>>(

const schemaExists = async (client: PubSub, schemaId: string) => {
for await (const s of client.listSchemas()) {
if (s.name === schemaId) {
if (s.name?.endsWith(`/${schemaId}`)) {
return true
}
}
Expand Down
131 changes: 0 additions & 131 deletions packages/pubsub/src/lib/zod-to-avro.ts

This file was deleted.

0 comments on commit a637b1e

Please sign in to comment.