-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement export functionality, add test for it
- Loading branch information
1 parent
af8e9c9
commit c1a59ea
Showing
6 changed files
with
115 additions
and
31 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package hibpsync | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
) | ||
|
||
var lineSeparator = []byte("\n") | ||
|
||
func export(from, to int64, store storage, w io.Writer) error { | ||
for i := from; i < to; i++ { | ||
err := func() error { | ||
rangePrefix := toRangeString(i) | ||
|
||
data, err := store.LoadData(rangePrefix) | ||
if err != nil { | ||
return fmt.Errorf("loading data for range %q: %w", rangePrefix, err) | ||
} | ||
defer data.Close() | ||
|
||
if _, err := io.Copy(w, data); err != nil { | ||
return fmt.Errorf("writing data to export writer: %w", err) | ||
} | ||
|
||
if i+1 < to { | ||
if _, err := w.Write(lineSeparator); err != nil { | ||
return fmt.Errorf("writing line separator to export writer: %w", err) | ||
} | ||
} | ||
|
||
return nil | ||
}() | ||
|
||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
if closer, ok := w.(io.Closer); ok { | ||
return closer.Close() | ||
} | ||
|
||
return 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package hibpsync | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"testing" | ||
|
||
"go.uber.org/mock/gomock" | ||
) | ||
|
||
func TestExport(t *testing.T) { | ||
ctrl := gomock.NewController(t) | ||
storageMock := NewMockstorage(ctrl) | ||
|
||
storageMock.EXPECT().LoadData("00000").Return(io.NopCloser(bytes.NewReader([]byte("prefix:counter1"))), nil) | ||
storageMock.EXPECT().LoadData("00001").Return(io.NopCloser(bytes.NewReader([]byte("prefix:counter2"))), nil) | ||
storageMock.EXPECT().LoadData("00002").Return(io.NopCloser(bytes.NewReader([]byte("prefix:counter3"))), nil) | ||
|
||
buf := bytes.NewBuffer([]byte{}) | ||
|
||
if err := export(0, 3, storageMock, buf); err != nil { | ||
t.Fatalf("unexpected error: %v", err) | ||
} | ||
|
||
if buf.String() != "prefix:counter1\nprefix:counter2\nprefix:counter3" { | ||
t.Fatalf("unexpected output: %q", buf.String()) | ||
} | ||
} |
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