From 82507a6e9ffe638c9aabee9b80f7fdb7232234ee Mon Sep 17 00:00:00 2001 From: Atakan Colak Date: Thu, 2 Jun 2022 13:50:41 +0100 Subject: [PATCH 1/2] Fix Unit Tests --- mockSchemaRegistryClient_test.go | 4 ++-- schemaRegistryClient.go | 1 + schemaRegistryClient_test.go | 4 ++-- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/mockSchemaRegistryClient_test.go b/mockSchemaRegistryClient_test.go index d40bafd..1c9133b 100644 --- a/mockSchemaRegistryClient_test.go +++ b/mockSchemaRegistryClient_test.go @@ -91,11 +91,11 @@ func TestMockSchemaRegistryClient_CreateSchema(t *testing.T) { // Test registering already registered schema _, err := srClient.CreateSchema("test1-key", schema, Avro) - assert.EqualError(t, err, "POST \"mock://testingUrl/subjects/test1-key/versions\": Schema already registered with id 2") + assert.EqualError(t, err, "POST mock://testingUrl/subjects/test1-key/versions: Schema already registered with id 2") // Test registering already registered schema _, err = srClient.CreateSchema("test1_arb", schema3, Avro) - assert.EqualError(t, err, "POST \"mock://testingUrl/subjects/test1_arb/versions\": Schema already registered with id 5") + assert.EqualError(t, err, "POST mock://testingUrl/subjects/test1_arb/versions: Schema already registered with id 5") } func TestMockSchemaRegistryClient_GetLatestSchema(t *testing.T) { diff --git a/schemaRegistryClient.go b/schemaRegistryClient.go index ccaf044..92a6c0d 100644 --- a/schemaRegistryClient.go +++ b/schemaRegistryClient.go @@ -226,6 +226,7 @@ func (client *SchemaRegistryClient) GetSchema(schemaID int) (*Schema, error) { var schema = &Schema{ id: schemaID, schema: schemaResp.Schema, + version: schemaResp.Version, schemaType: schemaResp.SchemaType, codec: codec, } diff --git a/schemaRegistryClient_test.go b/schemaRegistryClient_test.go index 736526b..dcbb66e 100644 --- a/schemaRegistryClient_test.go +++ b/schemaRegistryClient_test.go @@ -46,7 +46,7 @@ func TestSchemaRegistryClient_CreateSchemaWithoutReferences(t *testing.T) { assert.Equal(t, bodyToString(req.Body), string(expected)) // Send response to be tested rw.Write(response) - case "/subjects/test1-value/versions/latest": + case fmt.Sprintf("/schemas/ids/%d", responsePayload.Version): // Send response to be tested rw.Write(response) default: @@ -89,7 +89,7 @@ func TestSchemaRegistryClient_CreateSchemaWithoutReferences(t *testing.T) { assert.Equal(t, bodyToString(req.Body), string(expected)) // Send response to be tested rw.Write(response) - case "/subjects/test1/versions/latest": + case fmt.Sprintf("/schemas/ids/%d", responsePayload.Version): // Send response to be tested rw.Write(response) default: From 268cc1541ba152d5f99f884f7d7cafead1f4b764 Mon Sep 17 00:00:00 2001 From: Atakan Colak Date: Thu, 2 Jun 2022 14:35:50 +0100 Subject: [PATCH 2/2] Fix Unit Tests --- mockSchemaRegistryClient_test.go | 69 ++++++++++++++------------------ 1 file changed, 29 insertions(+), 40 deletions(-) diff --git a/mockSchemaRegistryClient_test.go b/mockSchemaRegistryClient_test.go index 1c9133b..5d86b57 100644 --- a/mockSchemaRegistryClient_test.go +++ b/mockSchemaRegistryClient_test.go @@ -6,6 +6,7 @@ import ( "testing" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) /* @@ -13,36 +14,24 @@ We are going to test the client meant to be used for testing */ var srClient MockSchemaRegistryClient -var schema = "{ " + - "\"type\": \"record\", " + - "\"namespace\": \"com.mycorp.mynamespace\", " + - "\"name\": \"value_cdc_fake_2\", " + - "\"doc\": \"Sample schema to help you get started.\", " + - "\"fields\": [ " + - "{ " + - "\"name\": \"aField\"," + - "\"type\": \"int\", " + - "\"doc\": \"The int type is a 32-bit signed integer.\" " + - "}" + - "]" + - "}" - -var schema2 = "{ " + - "\"type\": \"record\", " + - "\"namespace\": \"com.mycorp.mynamespace\", " + - "\"name\": \"value_cdc_fake_2\", " + - "\"doc\": \"Sample schema to help you get started.\", " + - "\"fields\": [ " + - "{ " + - "\"name\": \"bField\"," + - "\"type\": \"int\", " + - "\"doc\": \"The int type is a 32-bit signed integer.\" " + - "}" + - "]" + - "}" - -var schema3 = "\"string\"" -var schema4 = "\"int\"" +var schema = `{ + "type": "record", + "namespace": "com.mycorp.mynamespace", + "name": "value_cdc_fake_2", + "doc": "Sample schema to help you get started.", + "fields": [ { "name": "aField", "type": "int", "doc": "The int type is a 32-bit signed integer."}] +}` + +var schema2 = `{ + "type": "record", + "namespace": "com.mycorp.mynamespace", + "name": "value_cdc_fake_2", + "doc": "Sample schema to help you get started.", + "fields": [ { "name": "bField", "type": "int", "doc": "The int type is a 32-bit signed integer."}] +}` + +var schema3 = `"string"` +var schema4 = `"int"` /* We will use init to register some schemas to run our test with. @@ -71,22 +60,22 @@ func TestMockSchemaRegistryClient_CreateSchema(t *testing.T) { By virtue of this test, we also test MockSchemaRegistryClient.GetSchema */ schemaReg1, _ := srClient.GetSchema(1) - assert.Equal(t, schema, schemaReg1.schema) + require.JSONEq(t, schema, schemaReg1.schema) assert.Equal(t, 1, schemaReg1.version) schemaReg2, _ := srClient.GetSchema(2) - assert.Equal(t, schema, schemaReg2.schema) + require.JSONEq(t, schema, schemaReg2.schema) assert.Equal(t, 1, schemaReg2.version) schemaReg3, _ := srClient.GetSchema(3) - assert.Equal(t, schema2, schemaReg3.schema) + require.JSONEq(t, schema2, schemaReg3.schema) assert.Equal(t, 2, schemaReg3.version) schemaReg4, _ := srClient.GetSchema(4) - assert.Equal(t, schema2, schemaReg4.schema) + require.JSONEq(t, schema2, schemaReg4.schema) assert.Equal(t, 2, schemaReg4.version) schemaReg5, _ := srClient.GetSchema(5) - assert.Equal(t, schema3, schemaReg5.schema) + require.JSONEq(t, schema3, schemaReg5.schema) assert.Equal(t, 1, schemaReg5.version) schemaReg6, _ := srClient.GetSchema(6) - assert.Equal(t, schema4, schemaReg6.schema) + require.JSONEq(t, schema4, schemaReg6.schema) assert.Equal(t, 2, schemaReg6.version) // Test registering already registered schema @@ -105,7 +94,7 @@ func TestMockSchemaRegistryClient_GetLatestSchema(t *testing.T) { fmt.Println(err.Error()) t.Fail() } else { - assert.Equal(t, schema2, latest.schema) + require.JSONEq(t, schema2, latest.schema) } latest, err = srClient.GetLatestSchema("test1_arb") @@ -113,7 +102,7 @@ func TestMockSchemaRegistryClient_GetLatestSchema(t *testing.T) { fmt.Println(err.Error()) t.Fail() } else { - assert.Equal(t, schema4, latest.schema) + require.JSONEq(t, schema4, latest.schema) } } @@ -136,10 +125,10 @@ func TestMockSchemaRegistryClient_GetSchemaVersions(t *testing.T) { func TestMockSchemaRegistryClient_GetSchemaByVersion(t *testing.T) { oldVersion, _ := srClient.GetSchemaByVersion("test1-value", 1) - assert.Equal(t, schema, oldVersion.schema) + require.JSONEq(t, schema, oldVersion.schema) oldVersion, _ = srClient.GetSchemaByVersion("test1_arb", 1) - assert.Equal(t, schema3, oldVersion.schema) + require.JSONEq(t, schema3, oldVersion.schema) } func TestMockSchemaRegistryClient_GetSubjects(t *testing.T) {