Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix coding style for mock #806

Merged
merged 30 commits into from
Nov 10, 2020
Merged
Show file tree
Hide file tree
Changes from 26 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
e6347f9
feat: add mock document
hlts2 Oct 29, 2020
6bb578b
Update docs/contributing/coding-style.md
hlts2 Nov 4, 2020
6de917c
Update docs/contributing/coding-style.md
hlts2 Nov 4, 2020
3b09a7d
Update docs/contributing/coding-style.md
hlts2 Nov 4, 2020
4a1d831
Update docs/contributing/coding-style.md
hlts2 Nov 4, 2020
9f86b64
Update docs/contributing/coding-style.md
hlts2 Nov 4, 2020
895a93b
Update docs/contributing/coding-style.md
hlts2 Nov 4, 2020
a8794fa
Update docs/contributing/coding-style.md
hlts2 Nov 4, 2020
fca2121
Update docs/contributing/coding-style.md
hlts2 Nov 4, 2020
7a2f654
fix: apply suggestion
hlts2 Nov 4, 2020
f7ff539
fix: apply suggestion
hlts2 Nov 5, 2020
c5f9b23
fix: apply suggestion
hlts2 Nov 5, 2020
8b96fab
Update docs/contributing/coding-style.md
hlts2 Nov 5, 2020
136b482
Update docs/contributing/coding-style.md
hlts2 Nov 5, 2020
0a730d7
fix: apply suggestion
hlts2 Nov 5, 2020
1ff68ce
Update docs/contributing/coding-style.md
hlts2 Nov 10, 2020
bc5cd3f
Update docs/contributing/coding-style.md
hlts2 Nov 10, 2020
9bd038e
Update docs/contributing/coding-style.md
hlts2 Nov 10, 2020
f162a47
Update docs/contributing/coding-style.md
hlts2 Nov 10, 2020
08adf78
Update docs/contributing/coding-style.md
hlts2 Nov 10, 2020
abd53e6
Update docs/contributing/coding-style.md
hlts2 Nov 10, 2020
593e8a3
Update docs/contributing/coding-style.md
hlts2 Nov 10, 2020
a6e1b3f
Update docs/contributing/coding-style.md
hlts2 Nov 10, 2020
39631fd
Update docs/contributing/coding-style.md
hlts2 Nov 10, 2020
a3a51d9
Update docs/contributing/coding-style.md
hlts2 Nov 10, 2020
966674b
fix: apply suggestion
hlts2 Nov 10, 2020
7a25996
fix: delete unncessary sentence
hlts2 Nov 10, 2020
2e63b68
fix: apply suggestion
hlts2 Nov 10, 2020
e6eea30
fix: apply suggestion
hlts2 Nov 10, 2020
43f3d6d
Update docs/contributing/coding-style.md
hlts2 Nov 10, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/reviewdog-markdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ jobs:
level: warning
language: en-US
disabled_rules: "DOUBLE_PUNCTUATION,WORD_CONTAINS_UNDERSCORE,ARROWS,CURRENCY,DASH_RULE,EN_QUOTES"
disabled_categories: "TYPOS"
disabled_categories: "TYPOS,TYPOGRAPHY"
87 changes: 87 additions & 0 deletions docs/contributing/coding-style.md
Original file line number Diff line number Diff line change
Expand Up @@ -965,3 +965,90 @@ We do not suggest to modify the generated code other than the `tests` variable,
type want struct {
// generated test code
```
### Using Mock
hlts2 marked this conversation as resolved.
Show resolved Hide resolved

In Vald, we use a lot of external library, there are a lot of dependencies between libraries.
hlts2 marked this conversation as resolved.
Show resolved Hide resolved

As a result, due to the more complexity of the test, it has become more difficult to determine whether or not to mock dependencies.
hlts2 marked this conversation as resolved.
Show resolved Hide resolved
hlts2 marked this conversation as resolved.
Show resolved Hide resolved

#### Condition

When dependencies have the following factor, you can decide to mock the dependencies.

- Incomplete implementation
- I/O
- e. g. Network access, disk operation, or etc.
hlts2 marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Contributor

@kevindiu kevindiu Nov 10, 2020

Choose a reason for hiding this comment

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

Suggested change
- e. g. Network access, disk operation, or etc.
- e.g. Network access, disk operation, etc.

- Hardware dependent
- e. g. CPU, Memory usage, disk I/O, or etc.
hlts2 marked this conversation as resolved.
Show resolved Hide resolved
hlts2 marked this conversation as resolved.
Show resolved Hide resolved
- Difficult to create error of dependent object (when we will write error test case)
hlts2 marked this conversation as resolved.
Show resolved Hide resolved
- Difficult to initialize
- e. g. Random number and time, file IO initialization, environment dependent, or etc.
hlts2 marked this conversation as resolved.
Show resolved Hide resolved
hlts2 marked this conversation as resolved.
Show resolved Hide resolved
- Test result may change in each runtime
- e. g. Only test result may change in each runtime, System call inside implementation, or etc.
hlts2 marked this conversation as resolved.
Show resolved Hide resolved
hlts2 marked this conversation as resolved.
Show resolved Hide resolved

#### Risk

Before applying mock to the object, you should be aware of the following risks.

- We **do not** know whether the dependencies is correctly implemented or not.
hlts2 marked this conversation as resolved.
Show resolved Hide resolved
hlts2 marked this conversation as resolved.
Show resolved Hide resolved
- We cannot notice the change in dependencies.
hlts2 marked this conversation as resolved.
Show resolved Hide resolved

#### Implementation

The implementation of mock object should be:
hlts2 marked this conversation as resolved.
Show resolved Hide resolved

- Same package as the mock target.
- File name is `xxx_mock.go`
- Struct name is `Mock{Interface name}`

For example, we decided to mock the following implementation `Encoder`.

```go
package json

type Encoder interface {
Encode(interface{}) ([]byte, error)
}
```

hlts2 marked this conversation as resolved.
Show resolved Hide resolved
hlts2 marked this conversation as resolved.
Show resolved Hide resolved
```go
type encoder struct {
encoder json.Encoder
}

hlts2 marked this conversation as resolved.
Show resolved Hide resolved
func (e *encoder) Encode(obj interface{}) ([]byte, error) {
return e.encoder.Encode(obj)
}
```

The following is an example of mock implementation:

hlts2 marked this conversation as resolved.
Show resolved Hide resolved
```go
package json

hlts2 marked this conversation as resolved.
Show resolved Hide resolved
type MockEncoder struct {
EncoderFunc func(interface{}) ([]byte, error)
}

Copy link
Contributor

Choose a reason for hiding this comment

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

[LanguageTool] reported by reviewdog 🐶
This sentence does not start with an uppercase letter (UPPERCASE_SENTENCE_START)
Suggestions: Func
Rule: https://community.languagetool.org/rule/show/UPPERCASE_SENTENCE_START?lang=en-US
Category: CASING

func (m *MockEncoder) Encode(obj interface{}) ([]byte, error) {
return m.EncodeFunc(obj)
}
```

The following is an example implementation of test code to create the mock object and mock the implementation.

kevindiu marked this conversation as resolved.
Show resolved Hide resolved
hlts2 marked this conversation as resolved.
Show resolved Hide resolved
hlts2 marked this conversation as resolved.
Show resolved Hide resolved
hlts2 marked this conversation as resolved.
Show resolved Hide resolved
hlts2 marked this conversation as resolved.
Show resolved Hide resolved
hlts2 marked this conversation as resolved.
Show resolved Hide resolved
hlts2 marked this conversation as resolved.
Show resolved Hide resolved
```go
tests := []test {
{
name: "returns (byte{}, nil) when encode success"
fields: fields {
encoding: &json.MockEncoder {
EncoderFunc: func(interface{}) ([]byte, error) {
return []byte{}, nil
},
},
}
hlts2 marked this conversation as resolved.
Show resolved Hide resolved
hlts2 marked this conversation as resolved.
Show resolved Hide resolved
hlts2 marked this conversation as resolved.
Show resolved Hide resolved
hlts2 marked this conversation as resolved.
Show resolved Hide resolved
hlts2 marked this conversation as resolved.
Show resolved Hide resolved
hlts2 marked this conversation as resolved.
Show resolved Hide resolved
hlts2 marked this conversation as resolved.
Show resolved Hide resolved
......
}
kevindiu marked this conversation as resolved.
Show resolved Hide resolved
}
```