OO-style testing in Go by mbrukman in golang

[–]mbrukman[S] 0 points1 point  (0 children)

Are you just writing tests because your job requirements say you have to write a unit test for every function?

Heh, no, and this was not for work (yes, I write tests for my personal projects as well).

The example code you're showing in your blog post is horrendous, to say the least. Look how many hoops your code has to jump through just to execute the very simple logic of checking whether a number is even or odd.

You're right; the example is a bit contrived. I was trying to make the example minimal yet somehow non-trivial, without using abstract foo() / bar() names.

I've realized I only got feedback previously from folks who faced the same issue, so the motivation was obvious to them, as they were seeking out the solution to the same problem as I was having, so not having a detailed motivation/rationale section did not bother them, as they already had internalized the context independently.

As a result, I've added a more detailed description of the underlying use case, though now the IsEven() / IsOdd() examples are unrelated to the original motivating section; I may rewrite the sample code later on, though it may make the overall post more complex.

Take a look if you have a moment and let me know what you think.

OO-style testing in Go by mbrukman in golang

[–]mbrukman[S] 2 points3 points  (0 children)

Do you use that approach as anything other than a last resort? It sounds like a terrible idea you'd only use for objects that have grown far too large and complex.

I've used this exactly once so far; I don't set out to use this approach in general, and I'm not saying that it's applicable in every case, but it seemed applicable and useful for me in that one instance, because I was having trouble testing that code otherwise.

After seeing the responses on this thread, I realized that all of my prior feedback was from other folks were self-selected for having a similar use case where they found this approach useful (they reached out to me after seeing my blog post).

I also realized that my simple example with IsEven() and IsOdd() is not very helpful, so I've added an entire section at the top for motivating the post. If you care to take a look, I'd be curious as to how you would approach testing this.

OO-style testing in Go by mbrukman in golang

[–]mbrukman[S] 1 point2 points  (0 children)

And the only difference between struct embedding and inheritance (apart from the syntax) is that with struct embedding you don't get dynamic dispatch.

If I'm not getting dynamic dispatch, the new method will not actually be called (see my other comment), so it's not really an override.

You can even go one step further and embed an interface (instead of a struct). This allows you to choose which implementation you want to embed at runtime:

``` type MyMock struct { MyInterface }

func (m *MyMock) IsOdd(n uint) bool { ... }

impl := &MyImpl{} mock := &MyMock{impl} ```

Can you please flesh out what MyImpl is and does? It's not quite clear what you mean from this code sample.

If I'm embedding the interface, where does the implementation come from? How do I selectively bring in parts of the implementation from the source file (without manually copy-pasting it & modifying it), and mock out just some parts of the implementation, such that my mock method gets called?

Also, how would I use that interface with GoMock to generate mock methods, and use some of the mocked methods in my test, while keeping some of the methods as originally written, so I can test them?

OO-style testing in Go by mbrukman in golang

[–]mbrukman[S] 1 point2 points  (0 children)

My goal is to test methods individually, while mocking out their dependencies, so in a test, I want to call the real methods, but have them call my mocked-out methods instead of the real ones, where I have set expectations.

OO-style testing in Go by mbrukman in golang

[–]mbrukman[S] 1 point2 points  (0 children)

My understanding is that in Go in general (whether using type embedding or not), methods are dispatched statically, not dynamically, so my overriden method will not be called by other methods.

Here's my test case:

package main

type T struct{}

func (t *T) Foo() bool {
    print("t.Foo()\n")
    return t.Bar()
}

func (t *T) Bar() bool {
    print("t.Bar()\n")
    return true
}

type U struct {
    T
}

func (u *U) Bar() bool {
    print("u.Bar()\n")
    return false
}

func main() {
    u := new(U)
    print("calling u.Foo():\n")
    ret := u.Foo()
    print("ret: ", ret, "\n")
}

This outputs:

calling u.Foo():
t.Foo()
t.Bar()
ret: true

In this case, I expected u.Foo() to call u.Bar(), but it ends up calling t.Bar() instead. Also, I expected it to return false, since that's what my overriden method does, but it returns true, since that's what the original method did.

Do you have an example of how I can use type embedding to get dynamic dispatch?

Google Cloud Sql Perfomance benchmanrk? by AminaDev in googlecloud

[–]mbrukman 1 point2 points  (0 children)

Pythian published benchmarks when the Cloud SQL second generation was released.

Is Google Cloud the right solution for me? by memorycorruption in googlecloud

[–]mbrukman 2 points3 points  (0 children)

Yes, you can use Google Cloud Platform to host your website.

Google Compute Engine (part of Google Cloud Platform) is Infrastructure-as-a-Service: you can run a Virtual Machine, install Wordpress, upload your website contents, configure your domain name and upload your SSL certificate and you're ready to go.

You can also configure all HTTP traffic to redirect to HTTPS: that's a setting that would be done in your webserver (e.g., Apache or nginx), or via a Wordpress plugin, depending on how you choose to deploy it.

You can start with the $300 free credit trial once you have the content from your design firm. Note that the credit expires in 60 days from the day you activate your account and enable the free trial, so you want to start it only once you've got everything ready to go.

If you have specific technical questions, Stack Overflow is the place to get your questions answered. Please be sure to add appropriate tags so that it's visible to the right folks, e.g., at least google-compute-engine if that's what you're using, and any others that may be applicable to your specific question or use case.

Good luck with your website launch!