-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migration: refactor output/processing of converted entities
This commit simplifies the `icingaDbOutputStage` type to contain only one entity slice to be insert/upsert. This allows to simplify the handling in `migrateOneType()` by removing nested loops. Additionally, a bit of code inside that function is outsourced into a new `utils.ChanFromSlice()` function. This makes the body of the loop over the insert/upsert operation (the loop using the `op` variable) simple enough so that it can just be unrolled which saves the inline struct and slice definition for that loop.
- Loading branch information
1 parent
03b0e84
commit 71c1d2f
Showing
5 changed files
with
107 additions
and
39 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
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,54 @@ | ||
package utils | ||
|
||
import ( | ||
"github.com/stretchr/testify/require" | ||
"testing" | ||
) | ||
|
||
func TestChanFromSlice(t *testing.T) { | ||
t.Run("Nil", func(t *testing.T) { | ||
ch := ChanFromSlice[int](nil) | ||
require.NotNil(t, ch) | ||
requireClosedEmpty(t, ch) | ||
}) | ||
|
||
t.Run("Empty", func(t *testing.T) { | ||
ch := ChanFromSlice([]int{}) | ||
require.NotNil(t, ch) | ||
requireClosedEmpty(t, ch) | ||
}) | ||
|
||
t.Run("NonEmpty", func(t *testing.T) { | ||
ch := ChanFromSlice([]int{42, 23, 1337}) | ||
require.NotNil(t, ch) | ||
requireReceive(t, ch, 42) | ||
requireReceive(t, ch, 23) | ||
requireReceive(t, ch, 1337) | ||
requireClosedEmpty(t, ch) | ||
}) | ||
} | ||
|
||
// requireReceive is a helper function to check if a value can immediately be received from a channel. | ||
func requireReceive(t *testing.T, ch <-chan int, expected int) { | ||
t.Helper() | ||
|
||
select { | ||
case v, ok := <-ch: | ||
require.True(t, ok, "receiving should return a value") | ||
require.Equal(t, expected, v) | ||
default: | ||
require.Fail(t, "receiving should not block") | ||
} | ||
} | ||
|
||
// requireReceive is a helper function to check if the channel is closed and empty. | ||
func requireClosedEmpty(t *testing.T, ch <-chan int) { | ||
t.Helper() | ||
|
||
select { | ||
case _, ok := <-ch: | ||
require.False(t, ok, "receiving from channel should not return anything") | ||
default: | ||
require.Fail(t, "receiving should not block") | ||
} | ||
} |