Skip to content

Add tests & small refactoring of recipedb#895

Open
burov wants to merge 2 commits intoGoogleCloudPlatform:masterfrom
burov:testing
Open

Add tests & small refactoring of recipedb#895
burov wants to merge 2 commits intoGoogleCloudPlatform:masterfrom
burov:testing

Conversation

@burov
Copy link
Copy Markdown
Member

@burov burov commented Dec 8, 2025

No description provided.

@google-oss-prow
Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: burov

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

recipes map[string]Recipe
}

func newRecipeDB(path string) (*recipeDB, error) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Maybe also take a timeFunc here as an argument? This would result with a clear separation between a factory method that creates from all components and one that uses defaults.


func newRecipeDB(path string) (*recipeDB, error) {
db := &recipeDB{
file: path,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

maybe dbFile?


wantRecipes map[string]Recipe
wantErr error
}{
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

maybe also a case with empty DB (but correct json) ?

return nil
},

wantRecipes: map[string]Recipe{
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit: I wonder if it makes sense to check for content (instead of e.g. a count), if you have a separate cases for checking the content

feel free to ignore

}
}

func Test_recipeDB_saveToFS(t *testing.T) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I wonder if instead of checking the JSON string (and relying on zero changes to serialization code) it might make more sense to re-read the DB from the drive and checking if recipe is there.

utiltest.EnsureResults(t, false, ok2)
}

func Test_recipeDB_addRecipe(t *testing.T) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Maybe re-read DB from drive instead of checking the JSON string?

if _, err := f.Write(dbBytes); err != nil {
if _, err := f.Write(raw); err != nil {
f.Close()
return err
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

should we also delete the temp file when writing error occurs?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants