-
Couldn't load subscription status.
- Fork 99
test: Add new unit test framework with JSON fixtures #589
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
Merged
ykim-akamai
merged 14 commits into
linode:main
from
ykim-akamai:test/new_unit_testing_framework
Oct 23, 2024
Merged
Changes from 1 commit
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
977e07a
new unit testing framework
ykim-akamai e0b319b
load fixtures with embed
ykim-akamai 83155cc
load fixtures with embed
ykim-akamai 0b63cd9
migrating account, image, instance_stats tests; fix var type in Accou…
ykim-akamai 8b4007c
migrate profile grants, tfa tcs and create fixtures
ykim-akamai 07f9489
Merge branch 'main' into test/new_unit_testing_framework
ykim-akamai 94c6df4
go mod tidy
ykim-akamai a04f4b9
Merge remote-tracking branch 'origin/test/new_unit_testing_framework'…
ykim-akamai a279098
go mod tidy
ykim-akamai 431d164
active_since field update
ykim-akamai 68f3e38
update variable type and assertion
ykim-akamai b820b90
Update test/unit/fixtures.go
ykim-akamai 8327f62
change to trim suffix on fixtureName
ykim-akamai d1014bc
Merge branch 'main' into test/new_unit_testing_framework
ykim-akamai File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,39 @@ | ||
| package unit | ||
|
|
||
| import ( | ||
| "github.com/jarcoal/httpmock" | ||
| "github.com/linode/linodego" | ||
| "github.com/linode/linodego/internal/testutil" | ||
| "github.com/stretchr/testify/mock" | ||
| "testing" | ||
| ) | ||
|
|
||
| type MockResponse struct { | ||
| StatusCode int | ||
| Body interface{} | ||
| } | ||
|
|
||
| // ClientBaseCase provides a base for unit tests | ||
| type ClientBaseCase struct { | ||
| Client *linodego.Client | ||
| Mock *mock.Mock | ||
| BaseURL string // Base URL up to /v4 | ||
| } | ||
|
|
||
| // SetUp initializes the Linode client using the mock HTTP client | ||
| func (c *ClientBaseCase) SetUp(t *testing.T) { | ||
| c.Mock = &mock.Mock{} | ||
| c.Client = testutil.CreateMockClient(t, linodego.NewClient) | ||
| c.BaseURL = "https://api.linode.com/v4/" | ||
| } | ||
|
|
||
| func (c *ClientBaseCase) TearDown(t *testing.T) { | ||
| httpmock.DeactivateAndReset() // Reset HTTP mock after tests | ||
| c.Mock.AssertExpectations(t) | ||
| } | ||
|
|
||
| // MockGet mocks a GET request to the client. | ||
| func (c *ClientBaseCase) MockGet(path string, response interface{}) { | ||
| fullURL := c.BaseURL + path | ||
| httpmock.RegisterResponder("GET", fullURL, httpmock.NewJsonResponderOrPanic(200, response)) | ||
| } |
This file contains hidden or 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,59 @@ | ||
| package unit | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "fmt" | ||
| "os" | ||
| "path/filepath" | ||
| ) | ||
|
|
||
| // TestFixtures manages loading and retrieving test fixtures | ||
| type TestFixtures struct { | ||
| fixtures map[string]interface{} | ||
| } | ||
|
|
||
| // NewTestFixtures creates a new TestFixtures instance | ||
| func NewTestFixtures() *TestFixtures { | ||
| tf := &TestFixtures{} | ||
| tf.loadFixtures() | ||
| return tf | ||
| } | ||
|
|
||
| // loadFixtures loads all JSON files in fixtures directory | ||
| func (tf *TestFixtures) loadFixtures() { | ||
| tf.fixtures = make(map[string]interface{}) | ||
| fixturesDir := "fixtures" | ||
|
|
||
| err := filepath.Walk(fixturesDir, func(path string, info os.FileInfo, err error) error { | ||
| if err != nil { | ||
| return err | ||
| } | ||
| if !info.IsDir() && filepath.Ext(path) == ".json" { | ||
| data, err := os.ReadFile(path) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| var jsonData interface{} | ||
| if err := json.Unmarshal(data, &jsonData); err != nil { | ||
| return err | ||
| } | ||
| fixtureName := filepath.Base(path) | ||
| tf.fixtures[fixtureName[:len(fixtureName)-5]] = jsonData | ||
| } | ||
| return nil | ||
| }) | ||
| if err != nil { | ||
| panic(err) | ||
| } | ||
| } | ||
|
|
||
| // GetFixture retrieves the fixture data for the given URL | ||
| func (tf *TestFixtures) GetFixture(jsonFile string) (interface{}, error) { | ||
| data, ok := tf.fixtures[jsonFile] | ||
| if !ok { | ||
| return nil, os.ErrNotExist | ||
| } | ||
| fmt.Printf("Loading fixture: %s\n", jsonFile) // TODO:: debugging remove later | ||
|
|
||
| return data, nil | ||
| } | ||
This file contains hidden or 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,60 @@ | ||
| { | ||
| "data": [ | ||
| { | ||
| "alerts": { | ||
| "cpu": 180, | ||
| "io": 10000, | ||
| "network_in": 10, | ||
| "network_out": 10, | ||
| "transfer_quota": 80 | ||
| }, | ||
| "backups": { | ||
| "available": true, | ||
| "enabled": true, | ||
| "last_successful": "2018-01-01T00:01:01", | ||
| "schedule": { | ||
| "day": "Saturday", | ||
| "window": "W22" | ||
| } | ||
| }, | ||
| "created": "2018-01-01T00:01:01", | ||
| "has_user_data": true, | ||
| "host_uuid": "3a3ddd59d9a78bb8de041391075df44de62bfec8", | ||
| "hypervisor": "kvm", | ||
| "id": 123, | ||
| "image": "linode/debian10", | ||
| "ipv4": [ | ||
| "203.0.113.1", | ||
| "192.0.2.1" | ||
| ], | ||
| "ipv6": "c001:d00d::1337/128", | ||
| "label": "linode123", | ||
| "placement_group": { | ||
| "id": 528, | ||
| "label": "PG_Miami_failover", | ||
| "migrating_to": 2468, | ||
| "placement_group_policy": "strict", | ||
| "placement_group_type": "anti-affinity:local" | ||
| }, | ||
| "region": "us-east", | ||
| "specs": { | ||
| "disk": 81920, | ||
| "gpus": 0, | ||
| "memory": 4096, | ||
| "transfer": 4000, | ||
| "vcpus": 2 | ||
| }, | ||
| "status": "running", | ||
| "tags": [ | ||
| "example tag", | ||
| "another example" | ||
| ], | ||
| "type": "g6-standard-1", | ||
| "updated": "2018-01-01T00:01:01", | ||
| "watchdog_enabled": true | ||
| } | ||
| ], | ||
| "page": 1, | ||
| "pages": 1, | ||
| "results": 1 | ||
| } |
This file contains hidden or 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,45 @@ | ||
| package unit | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func TestListInstances(t *testing.T) { | ||
| // Initialize the TestFixtures to load the fixture | ||
| fixtures := NewTestFixtures() | ||
|
|
||
| // Mock the expected response from the ListInstances endpoint | ||
| fixtureData, err := fixtures.GetFixture("linodes_list") // Use the correct fixture name without ".json" | ||
| if err != nil { | ||
| t.Fatalf("Failed to load fixture: %v", err) | ||
| } | ||
|
|
||
| var base ClientBaseCase | ||
| base.SetUp(t) | ||
| defer base.TearDown(t) | ||
|
|
||
| base.MockGet("linode/instances", fixtureData) | ||
|
|
||
| instances, err := base.Client.ListInstances(context.Background(), nil) | ||
| if err != nil { | ||
| t.Fatalf("Error listing instances: %v", err) | ||
| } | ||
|
|
||
| assert.Equal(t, 1, len(instances)) | ||
| linode := instances[0] | ||
| assert.Equal(t, 123, linode.ID) | ||
| assert.Equal(t, "linode123", linode.Label) | ||
|
|
||
| // Fix for the status comparison | ||
| assert.Equal(t, "running", string(linode.Status)) | ||
|
|
||
| // Fix for the IP address comparison | ||
| assert.Equal(t, "203.0.113.1", linode.IPv4[0].String()) | ||
|
|
||
| assert.Equal(t, "g6-standard-1", linode.Type) | ||
| assert.Equal(t, "us-east", linode.Region) | ||
| assert.Equal(t, 4096, linode.Specs.Memory) | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
optional: One other potential way to handle this would be using the embed directive with the embed.FS type. I think the current implementation is perfectly acceptable too, just figured I'd share another potential approach 🙂
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Appreciate the feedback. I wasn't aware of this option at all, but it seems like a great improvement to implement. This change should enhance performance as the test framework scales with additional tests and components 👍