-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(integration): First pass at creating integration tests (#146)
- Loading branch information
Showing
16 changed files
with
284 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package mock | ||
|
||
import ( | ||
"encoding/json" | ||
"io" | ||
"net/http" | ||
"os" | ||
"strings" | ||
) | ||
|
||
type Call struct { | ||
Verb string | ||
Endpoint string | ||
Data any | ||
Error error | ||
Response *http.Response | ||
} | ||
|
||
type RawCall struct { | ||
Verb string `json:"verb"` | ||
Endpoint string `json:"endpoint"` | ||
Data any `json:"data"` | ||
Error error `json:"error"` | ||
RawResponse RawResponse `json:"response"` | ||
} | ||
|
||
type RawResponse struct { | ||
StatusCode int `json:"status_code"` | ||
Body any `json:"body"` | ||
} | ||
|
||
func LoadCallsFromFile(path string) ([]Call, error) { | ||
rawCalls := []RawCall{} | ||
|
||
content, err := os.ReadFile(path) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if err = json.Unmarshal(content, &rawCalls); err != nil { | ||
return nil, err | ||
} | ||
|
||
calls := make([]Call, len(rawCalls)) | ||
for i, call := range rawCalls { | ||
body, err := json.Marshal(call.RawResponse.Body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
calls[i] = Call{ | ||
Verb: call.Verb, | ||
Endpoint: call.Endpoint, | ||
Data: call.Data, | ||
Error: call.Error, | ||
Response: &http.Response{ | ||
StatusCode: call.RawResponse.StatusCode, | ||
Body: io.NopCloser(strings.NewReader(string(body))), | ||
}, | ||
} | ||
} | ||
|
||
return calls, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[ | ||
{ | ||
"verb": "GET", | ||
"endpoint": "https://api.github.com/notifications?all=true", | ||
"response": { | ||
"status_code": 200, | ||
"body": [] | ||
} | ||
} | ||
] |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
[ | ||
{ | ||
"verb": "GET", | ||
"endpoint": "https://api.github.com/notifications?all=true", | ||
"response": { | ||
"status_code": 200, | ||
"body": [ | ||
{ | ||
"id": "1", | ||
"subject": { | ||
"url": "enrichment#1" | ||
} | ||
} | ||
] | ||
} | ||
}, | ||
{ | ||
"verb": "GET", | ||
"endpoint": "enrichment#1", | ||
"response": { | ||
"status_code": 200, | ||
"body": { | ||
"state": "open" | ||
} | ||
} | ||
} | ||
] |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
[ | ||
{ | ||
"id": "1", | ||
"subject": { | ||
"url": "enrichment#1", | ||
"state": "open" | ||
}, | ||
"meta": { | ||
"remote_exists": true | ||
} | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package tests | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
"testing" | ||
|
||
apiMock "github.com/nobe4/gh-not/internal/api/mock" | ||
configPkg "github.com/nobe4/gh-not/internal/config" | ||
"github.com/nobe4/gh-not/internal/logger" | ||
"github.com/nobe4/gh-not/internal/manager" | ||
"github.com/nobe4/gh-not/internal/notifications" | ||
) | ||
|
||
type config struct { | ||
Id string | ||
// TODO: move those into config so it can be set by default as well as via | ||
// CLI | ||
ForceStrategy manager.ForceStrategy | ||
RefreshStrategy manager.RefreshStrategy | ||
} | ||
|
||
func setup(t *testing.T, conf config) (*manager.Manager, *apiMock.Mock, notifications.Notifications) { | ||
logger.Init(5) | ||
|
||
configPath := fmt.Sprintf("./%s/config.yaml", conf.Id) | ||
callsPath := fmt.Sprintf("./%s/calls.json", conf.Id) | ||
wantPath := fmt.Sprintf("./%s/want.json", conf.Id) | ||
cachePath := fmt.Sprintf("./%s/cache.json", conf.Id) | ||
|
||
c, err := configPkg.New(configPath) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
c.Data.Cache.Path = cachePath | ||
|
||
m := manager.New(c.Data) | ||
|
||
// TODO: move those into config so it can be set by default as well as via | ||
// CLI | ||
m.ForceStrategy = conf.ForceStrategy | ||
m.RefreshStrategy = conf.RefreshStrategy | ||
|
||
calls, err := apiMock.LoadCallsFromFile(callsPath) | ||
caller := &apiMock.Mock{Calls: calls} | ||
m.SetCaller(caller) | ||
|
||
if err := m.Load(); err != nil { | ||
t.Fatal(err) | ||
} | ||
if err := m.Refresh(); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
want := notifications.Notifications{} | ||
|
||
raw, err := os.ReadFile(wantPath) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if err := json.Unmarshal(raw, &want); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
return m, caller, want | ||
} | ||
|
||
func TestIntegration(t *testing.T) { | ||
dirs, err := os.ReadDir(".") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
for _, dir := range dirs { | ||
if !dir.IsDir() { | ||
continue | ||
} | ||
|
||
t.Run(dir.Name(), func(t *testing.T) { | ||
m, c, want := setup(t, config{ | ||
Id: dir.Name(), | ||
RefreshStrategy: manager.ForceRefresh, | ||
}) | ||
|
||
got := m.Notifications | ||
|
||
if !want.Equal(got) { | ||
t.Fatalf("mismatch notifications\nwant %s\ngot %s", want.Debug(), got.Debug()) | ||
} | ||
|
||
if err := c.Done(); err != nil { | ||
t.Fatal(err) | ||
} | ||
}) | ||
} | ||
} |