Skip to content
This repository has been archived by the owner on Nov 15, 2022. It is now read-only.

pass only required parameter to functions #214

Merged

Conversation

surajssd
Copy link
Member

@surajssd surajssd commented Aug 9, 2017

Lot of functions are being passed the entire app struct so refactoring, it to accept only what is needed.

Fixes #113

@kedge-bot
Copy link
Collaborator

@surajssd, thank you for the pull request! We'll ping some people to review your PR. @containscafeine, please review this.

1 similar comment
@kedge-bot
Copy link
Collaborator

@surajssd, thank you for the pull request! We'll ping some people to review your PR. @containscafeine, please review this.

@surajssd surajssd force-pushed the fix-functions-necessary-objects branch from 0d0c532 to fe8ed8e Compare August 9, 2017 11:50
Copy link
Member

@kadel kadel left a comment

Choose a reason for hiding this comment

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

Why?
I don't like this to be honest. I think that in a lot of cases it makes sense to pass whole app struct.
You are always passing at least two arguments that are always in the same base struct why not passing the whole struct?

@surajssd
Copy link
Member Author

surajssd commented Aug 9, 2017

@kadel because that struct has lot of other things that are not needed, so passing the function only things that it needs. why pass extra unnecessary things?

What is the rationale behind passing things to function that it does not need or might manipulate?

@surajssd
Copy link
Member Author

surajssd commented Aug 9, 2017

@kadel
Copy link
Member

kadel commented Aug 9, 2017

@kadel because that struct has lot of other things that are not needed, so passing the function only things that it needs. why pass extra unnecessary things?

but you are passing arguments from the same struct. How does it make sense to break it to multiple arguments?

@kadel
Copy link
Member

kadel commented Aug 9, 2017

What is @containscafeine's opinion on this?

If he agrees with this lets merge it. I'm OK with that.

@surajssd
Copy link
Member Author

ping @containscafeine

)

func TestFixServices(t *testing.T) {
failingTest := []spec.ServiceSpecMod{
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm confused, why are the tests like this? Why not put all the tests in one tests struct which has a field succeed: bool

Copy link
Member Author

Choose a reason for hiding this comment

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

that way the testing code becomes really hard to understand with all the conditions around whether the test is expected to pass or fail, this is mere separation of successful tests and failing tests.

Copy link
Collaborator

Choose a reason for hiding this comment

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

How does it become difficult to understand, there is a field right there next to the test which says if the test is expected to pass or fail.
Also, if we separate passing and failing tests, we are sorting by test behavior not by the functionality that is being tested. One behavior will have passing and failing tests defined separately and will be very difficult to track the tests for a given behavior.

If we absolutely have to make things concise, we can have different []struct for different behavior.

Copy link
Member Author

@surajssd surajssd Aug 17, 2017

Choose a reason for hiding this comment

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

How does it become difficult to understand, there is a field right there next to the test which says if the test is expected to pass or fail.

The part that becomes difficult to understand is the code that runs test, for example:

	for _, test := range tests {
		t.Run(test.Name, func(t *testing.T) {
			kd, err := transformer.CreateDeployments(test.Service)
			if err != nil {
				if test.Succeed {
					t.Errorf("Failed to create deployment from %#v\nErr: %s", test.Service, err)
				}
				return
			}


			if !test.Succeed {
				t.Errorf("Expected failure, but succeeded, service: %#v\nConverted k8s Deployment: %#v", test.Service, spew.Sprint(kd))
				return
			}


			if !reflect.DeepEqual(kd, test.K8sDeployments) {
				t.Errorf("Expected: %#v\nGot: %#v\n", spew.Sprint(test.K8sDeployments), spew.Sprint(kd))
				return
			}
		})
	}

Taken from opencompose in here

If you are checking condition on test.Succeed which is used twice, it goes out of the window when we have separated those two things.

And see the code we have here:

	for _, test := range passingTests {
		t.Logf("Running test: %s", test.Name)
		got, err := fixServices(test.Input, appName)
		if err != nil {
			t.Errorf("expected to pass but failed with: %v", err)
		}
		if !reflect.DeepEqual(got, test.Output) {
			t.Errorf("expected: %s, got: %s", prettyPrintObjects(test.Output),
				prettyPrintObjects(got))
		}
	}

source here

No convoluted logic of test.Succeed here. Because we know test should pass and all output should match as well.

And for failing tests we just need to see if it failed:

	got, err := fixVolumeClaims(passingTest, appName)
	if err != nil {
		t.Errorf("expected to pass but failed with: %v", err)
	}
	if !reflect.DeepEqual(got, expected) {
		t.Errorf("expected: %s, got: %s", prettyPrintObjects(expected),
			prettyPrintObjects(got))
	}

source here

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we can sort that out by adding comments, I don't think we need to have different passing and failing structs. It will be very hard to track passing and failing tests for a given behavior.
ping @kadel @cdrage WDYT?

Copy link
Member Author

Choose a reason for hiding this comment

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

@surajssd okay, suppose I want to test the behavior of fixing names, then I'd logically want to put the failing and passing tests together instead of splitting in different functions.

What does test the behavior of fixing names mean ?

Copy link
Member Author

Choose a reason for hiding this comment

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

Let's not bike shed on this and get something done, I think!

Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm sorry, I'm nack on this.
Separating tests based on passing or failing makes it impossible to track the behavior being tested. We already have a structure in place, I don't understand why are we deviating from that.
I'm sorry if you feel this is bikeshedding, but this PR is deviating from the way things have been getting done for no good reason.
ping @kadel @cdrage

Copy link
Member Author

Choose a reason for hiding this comment

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

Please see the code from Kubernetes again, the tests are separated on passing and failing behavior.

Passing tests, where output is matched: here

Failing tests, where you will certainly get an error and you are not matching anything just checking if the function being tested errored out: here

Copy link
Member

Choose a reason for hiding this comment

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

Isn't this the same discussion as in #208 (comment) ?
Can we solve this in one place?

@pradeepto
Copy link
Member

Let's move ahead on this bit and merge it unless there is huge problem. We can come back to it if need be.

Lot of functions are being passed the entire app struct
so refactoring, it to accept only what is needed.
@surajssd surajssd force-pushed the fix-functions-necessary-objects branch from fb12863 to 6bc8012 Compare August 21, 2017 06:44
@surajssd surajssd merged commit 273fd4c into kedgeproject:master Aug 21, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants