Skip to content

test: Added Unit tests for stream.go file's methods#980

Open
supersanchayrx wants to merge 14 commits into
datazip-inc:stagingfrom
supersanchayrx:unit-test-stream.go
Open

test: Added Unit tests for stream.go file's methods#980
supersanchayrx wants to merge 14 commits into
datazip-inc:stagingfrom
supersanchayrx:unit-test-stream.go

Conversation

@supersanchayrx

@supersanchayrx supersanchayrx commented Jun 9, 2026

Copy link
Copy Markdown

Description

Added comprehensive unit tests for stream.go file.

Fixes #661

Type of change

  • New feature (non-breaking change which adds functionality)

How Has This Been Tested?

All tests verified via go test ./types -v -run "TestStream_"

  • TestStream_NewStream — 8 table-driven cases: empty name, empty namespace, nil/empty sourceDatabase, all empty, all filled, special characters (Reformat verification), viper prefix.
  • TestStream_ID — 5 table-driven cases: empty name, empty namespace, all filled, all empty, special characters (no reformat applied).
  • TestStream_WithSyncMode — 5 table-driven cases: single, multiple, all, duplicate, empty modes.
  • TestStream_WithPrimaryKey — 6 table-driven cases: single, multiple, composite, duplicate, empty, underscore keys.
  • TestStream_WithCursorField — 6 table-driven cases: single, multiple, timestamp fields, duplicate, empty, underscore fields.
  • TestStream_WithSchema — 2 subtests: set new schema, replace existing schema (pointer verification).
  • TestStream_Wrap — 4 table-driven cases: index 0, 1, negative, large index.
  • TestStream_WithUpsertField — 11 table-driven cases (nullable/non-nullable string, int, float, bool, timestamp + olake column + special char column) + merge subtest verifying type accumulation on same column.
  • TestStream_UnmarshalJSON — 5 subtests: safe initialization on missing fields, correct data loading, invalid JSON (error path), empty JSON (set initialization), all fields populated.
  • TestStream_StreamsToMap — 4 subtests: empty input, single stream, multiple streams, duplicate IDs (last wins).
  • TestStream_LogCatalog — Verifies catalog file creation, valid JSON output, and correct stream count.

Screenshots or Recordings

stream.file.tests.mp4

Video Content :

All unit test cases pass.

sync.tests.mp4

Video Content:

The streams.go interaction with other files still work properly as the sync shows expected behaviour.

Documentation

  • N/A (bug fix, refactor, or test changes only)

Related PR's (If Any):

#680

added test for NewStream(), WithSyncMode(), WithPrimaryKey(), WithCursorField(), WithSchema(), Wrap().
left for WithUpsertField(), UnmarshalJson(), StreamsToMap(), LogCatalog(), ID().
@supersanchayrx supersanchayrx changed the title Added Unit tests for stream.go file's methods test : Added Unit tests for stream.go file's methods Jun 9, 2026
@supersanchayrx

Copy link
Copy Markdown
Author

Also very grateful to @Beeram12 for his contributions. These tests were added on top of his work done from pr #680

@CLAassistant

CLAassistant commented Jun 9, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@nayanj98

Copy link
Copy Markdown
Collaborator

@supersanchayrx Thanks a lot! Will assign a reviewer for this PR soon.

@nayanj98

Copy link
Copy Markdown
Collaborator

@supersanchayrx Assigning @saksham-datazip to review your PR

@nayanj98
nayanj98 requested a review from saksham-datazip June 11, 2026 13:18
@supersanchayrx supersanchayrx changed the title test : Added Unit tests for stream.go file's methods test: Added Unit tests for stream.go file's methods Jun 22, 2026
Signed-off-by: Sanchay <grindinforvalore@gmail.com>

@saksham-datazip saksham-datazip left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the late review and please make the respective changes.

Comment thread types/stream_test.go Outdated
Comment thread types/stream_test.go Outdated
Comment thread types/stream_test.go Outdated
Comment thread types/stream_test.go Outdated
Comment thread types/stream_test.go Outdated
Comment thread types/stream_test.go
Comment thread types/stream_test.go Outdated
Comment thread types/stream_test.go Outdated
Comment thread types/stream_test.go Outdated
Comment thread types/stream_test.go Outdated
})
}

func TestStream_UnmarshalJSON(t *testing.T) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please try to follow patterns as the way we followed in other files by creating a struct and also make the same changes in function below as well.

@saksham-datazip saksham-datazip Jul 2, 2026

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please use the same test case structure that's used in the other tests as well? It would help maintain consistency across the test suite for All the functions.Like this:-

	tests := []struct {
		testName  string
		syncIndex int
	}{

Instead of directly using t.Run

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done
it now uses this kind of tests struct pattern only. it has per field expected values to cross check against. all of the test cases are traversed through loop. Made sure this pattern remains consistent with other methods as well.

@supersanchayrx

Copy link
Copy Markdown
Author

Hey sure I'll fix these asap thanks for the review

@saksham-datazip

Copy link
Copy Markdown
Collaborator

@supersanchayrx Are you still working on this unit test?

@supersanchayrx

Copy link
Copy Markdown
Author

Hey @saksham-datazip sorry for the delay. Yup I implemented your suggestions and fixed the code wherever needed. Please review it once more. Thanks!

@saksham-datazip saksham-datazip left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've addressed the previous comments on my end, but it looks like a few comments are still pending from your side. Could you please take a look at those as well? If you have any questions or need any clarification, feel free to ask in the comments.

Comment thread types/stream_test.go Outdated
Comment on lines +120 to +127
asserts.Equal(tt.expectedStream.Name, stream.Name)
asserts.Equal(tt.expectedStream.Namespace, stream.Namespace)
asserts.NotNil(stream.SupportedSyncModes, "SupportedSyncModes should be initialized")
asserts.NotNil(stream.SourceDefinedPrimaryKey, "SourceDefinedPrimaryKey should be initialized")
asserts.NotNil(stream.AvailableCursorFields, "AvailableCursorFields should be initialized")
asserts.NotNil(stream.Schema, "Schema should be initialized")
asserts.Equal(tt.expectedStream.DestinationDatabase, stream.DestinationDatabase)
asserts.Equal(tt.expectedStream.DestinationTable, stream.DestinationTable)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of this much checks you should use asserts.Equal(tt.expectedStream, stream)
and apart from this the streams which we are getting as output are different from expected so consider using complete structure, Like this :-

			expectedStream: &Stream{
				Name:                    "User-Orders.v2",
				Namespace:               "My.Schema",
				Schema:                  NewTypeSchema(),
				SupportedSyncModes:      NewSet[SyncMode](),
				SourceDefinedPrimaryKey: NewSet[string](),
				AvailableCursorFields:   NewSet[string](),
				DestinationDatabase:     ":my_schema",
				DestinationTable:        "user_orders_v2",
			},

Note: This is a dummy code

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done! thanks for the dummy code 👍

Comment thread types/stream_test.go
Comment on lines +269 to +271
stream := NewStream("users", "public", nil)
returnedStream := stream.WithPrimaryKey(tt.keys...)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

returnedStream was previously named outputStream. Could we make the naming consistent? Also, if there are any other variable names that could be made more consistent throughout the test for better readability, it would be nice to update those as well.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have changed this and also tried to keep consistent naming across file

Comment thread types/stream_test.go Outdated
Comment on lines +241 to +249
testName: "multiple keys",
keys: []string{"id", "user_uuid"},
expectedKeys: []string{"id", "user_uuid"},
},
{
testName: "composite key",
keys: []string{"tenant_id", "user_id", "order_id"},
expectedKeys: []string{"tenant_id", "user_id", "order_id"},
},

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

multiple keys test and composite keys tests are kind of same just with different number of keys and due to their naming ig this testName "keys with underscores" is redundant.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes i've removed the redundant test cases.

Comment thread types/stream_test.go Outdated
Comment on lines +281 to +283
testName string
fields []string
expectedFields []string

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you change this fields name with the function input name also check for other variables.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed 'fields' -> 'columns' to match the function input var name

Comment thread types/stream_test.go Outdated
Comment on lines +285 to +311
{
testName: "single field",
fields: []string{"updated_at"},
expectedFields: []string{"updated_at"},
},
{
testName: "multiple fields",
fields: []string{"updated_at", "inserted_at"},
expectedFields: []string{"updated_at", "inserted_at"},
},
{
testName: "timestamp fields",
fields: []string{"created_at", "updated_at", "deleted_at"},
expectedFields: []string{"created_at", "updated_at", "deleted_at"},
},
{
testName: "duplicate fields",
fields: []string{"updated_at", "updated_at", "inserted_at"},
expectedFields: []string{"updated_at", "inserted_at"},
},
{
testName: "empty fields",
fields: []string{},
expectedFields: []string{},
},
{
testName: "fields with underscores",

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also Can you please confirm these all checks seems to be redundant ?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes removed the redundant check

Comment thread types/stream_test.go
Comment on lines +484 to +491
//test for merging two datatypes on subsequent upsert calls with same names
t.Run("Multiple datatypes test", func(t *testing.T) {
asserts := assert.New(t)
stream := NewStream("phones", "seller", nil)

stream.UpsertField("codename", Int64, false, false)
stream.UpsertField("codename", String, true, true)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we write this testcase like others no need for a new t.Run ?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can do it but I purposely kept it separate as it needs to check whether 2 usertfields on 1 col actually gives merged types. Making this into the table tests would reduce readibilty in my opinion and would also look a bit odd but nonetheless I can make it happen. Do let me know I'll make that change quick

Comment thread types/stream_test.go Outdated
{
testName: "wrap with large index",
syncIndex: 100,
},

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Look into those tc as this function is input independent 1-2 basic testcases is more than enough

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah removed trivial ones. Just kept the -ve and 0

Comment thread types/stream_test.go Outdated
for _, tt := range tests {
t.Run(tt.testName, func(t *testing.T) {
stream := NewStream("users", "public", nil)
configuredStream := stream.Wrap(tt.syncIndex)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here also try to make this name configuredStream consistent as told earlier

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes followed consistency for all variables in general throughout the file

Comment thread types/stream_test.go Outdated
Comment on lines +533 to +534
assert.NotNil(t, configuredStream, "Should return a configuredStream")
assert.Same(t, stream, configuredStream.Stream, "Should wrap the exact same stream instance")

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use equal instead of those two lines ?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes used equal directly of 2 individual checks.

Comment thread types/stream_test.go
})
}
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the function after line 538 i am expecting you to make changes if possible else a reason why we cant follow olake pattern.

@supersanchayrx supersanchayrx Jul 12, 2026

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done. Sorry for not resolving this in last review
it now uses this kind of tests struct pattern only as you suggested in other comment. it has per field expected values to cross check against. all of the test cases are traversed through loop. Made sure this pattern remains consistent with other methods as well.

@saksham-datazip

Copy link
Copy Markdown
Collaborator

@supersanchayrx Hey! Are you still working on this issue? If so, please let us know. Otherwise, we'll have to close this PR due to inactivity.

@supersanchayrx

Copy link
Copy Markdown
Author

hey yes sorry for the delay. I'm pushing an update asap

@supersanchayrx

Copy link
Copy Markdown
Author

Please have a review once more

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants