Skip to content

Commit

Permalink
Merge pull request #82 from AtakanColak/fix-tests
Browse files Browse the repository at this point in the history
Fix Unit Tests
  • Loading branch information
riferrei authored Jun 3, 2022
2 parents c5e82d3 + 268cc15 commit 6beda27
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 44 deletions.
73 changes: 31 additions & 42 deletions mockSchemaRegistryClient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,43 +6,32 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

/*
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.
Expand Down Expand Up @@ -71,31 +60,31 @@ 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
_, 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) {
Expand All @@ -105,15 +94,15 @@ 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")
if err != nil {
fmt.Println(err.Error())
t.Fail()
} else {
assert.Equal(t, schema4, latest.schema)
require.JSONEq(t, schema4, latest.schema)
}
}

Expand All @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions schemaRegistryClient.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down
4 changes: 2 additions & 2 deletions schemaRegistryClient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 6beda27

Please sign in to comment.