-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from aswinkarthik93/improve-performance
Improve performance
- Loading branch information
Showing
21 changed files
with
550 additions
and
422 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 |
---|---|---|
|
@@ -19,4 +19,6 @@ out/ | |
|
||
# End of https://www.gitignore.io/api/go | ||
|
||
vendor/ | ||
vendor/ | ||
|
||
majestic_million*.csv |
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 |
---|---|---|
@@ -1,30 +1,72 @@ | ||
package cmd | ||
package cmd_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/aswinkarthik93/csvdiff/cmd" | ||
"github.com/aswinkarthik93/csvdiff/pkg/digest" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestPrimaryKeyPositions(t *testing.T) { | ||
config := Config{PrimaryKeyPositions: []int{0, 1}} | ||
config := cmd.Config{PrimaryKeyPositions: []int{0, 1}} | ||
assert.Equal(t, digest.Positions([]int{0, 1}), config.GetPrimaryKeys()) | ||
|
||
config = Config{PrimaryKeyPositions: []int{}} | ||
config = cmd.Config{PrimaryKeyPositions: []int{}} | ||
assert.Equal(t, digest.Positions([]int{0}), config.GetPrimaryKeys()) | ||
|
||
config = Config{} | ||
config = cmd.Config{} | ||
assert.Equal(t, digest.Positions([]int{0}), config.GetPrimaryKeys()) | ||
} | ||
|
||
func TestValueColumnPositions(t *testing.T) { | ||
config := Config{ValueColumnPositions: []int{0, 1}} | ||
config := cmd.Config{ValueColumnPositions: []int{0, 1}} | ||
assert.Equal(t, digest.Positions([]int{0, 1}), config.GetValueColumns()) | ||
|
||
config = Config{ValueColumnPositions: []int{}} | ||
config = cmd.Config{ValueColumnPositions: []int{}} | ||
assert.Equal(t, digest.Positions([]int{}), config.GetValueColumns()) | ||
|
||
config = Config{} | ||
config = cmd.Config{} | ||
assert.Equal(t, digest.Positions([]int{}), config.GetValueColumns()) | ||
} | ||
|
||
func TestConfigValidate(t *testing.T) { | ||
var config *cmd.Config | ||
|
||
config = &cmd.Config{} | ||
assert.Error(t, config.Validate()) | ||
|
||
config = &cmd.Config{Format: "rowmark"} | ||
assert.NoError(t, config.Validate()) | ||
|
||
config = &cmd.Config{Format: "rowMARK"} | ||
assert.NoError(t, config.Validate()) | ||
|
||
config = &cmd.Config{Format: "json"} | ||
assert.NoError(t, config.Validate()) | ||
} | ||
|
||
func TestDefaultConfigFormatter(t *testing.T) { | ||
config := &cmd.Config{} | ||
|
||
formatter, ok := config.Formatter().(*cmd.RowMarkFormatter) | ||
|
||
assert.True(t, ok) | ||
assert.NotNil(t, formatter) | ||
} | ||
|
||
func TestConfigFormatter(t *testing.T) { | ||
var config *cmd.Config | ||
var formatter cmd.Formatter | ||
var ok bool | ||
|
||
config = &cmd.Config{Format: "rowmark"} | ||
formatter, ok = config.Formatter().(*cmd.RowMarkFormatter) | ||
assert.True(t, ok) | ||
assert.NotNil(t, formatter) | ||
|
||
config = &cmd.Config{Format: "json"} | ||
formatter, ok = config.Formatter().(*cmd.JSONFormatter) | ||
assert.True(t, ok) | ||
assert.NotNil(t, formatter) | ||
} |
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,48 @@ | ||
package cmd | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
|
||
"github.com/aswinkarthik93/csvdiff/pkg/digest" | ||
) | ||
|
||
// Formatter defines the interface through which differences | ||
// can be formatted and displayed | ||
type Formatter interface { | ||
Format(digest.Difference, io.Writer) | ||
} | ||
|
||
// RowMarkFormatter formats diff by marking each row as | ||
// ADDED/MODIFIED. It mutates the row and adds as a new column. | ||
type RowMarkFormatter struct{} | ||
|
||
// Format prints the diff to os.Stdout | ||
func (f *RowMarkFormatter) Format(diff digest.Difference, w io.Writer) { | ||
fmt.Fprintf(w, "Additions %d\n", len(diff.Additions)) | ||
fmt.Fprintf(w, "Modifications %d\n", len(diff.Modifications)) | ||
fmt.Fprintf(w, "Rows:\n") | ||
|
||
for _, added := range diff.Additions { | ||
fmt.Fprintf(w, "%s,%s\n", added, "ADDED") | ||
} | ||
|
||
for _, modified := range diff.Modifications { | ||
fmt.Fprintf(w, "%s,%s\n", modified, "MODIFIED") | ||
} | ||
} | ||
|
||
// JSONFormatter formats diff to as a JSON Object | ||
type JSONFormatter struct{} | ||
|
||
// Format prints the diff as a JSON | ||
func (f *JSONFormatter) Format(diff digest.Difference, w io.Writer) { | ||
data, err := json.MarshalIndent(diff, "", " ") | ||
|
||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
w.Write(data) | ||
} |
Oops, something went wrong.