Skip to content

Commit

Permalink
feat: add test without header and without match
Browse files Browse the repository at this point in the history
  • Loading branch information
Chao-Ma5566 committed Nov 27, 2023
1 parent 12fabf0 commit 304697d
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 32 deletions.
11 changes: 6 additions & 5 deletions pkg/findincsv/findincsv.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ func (me *MaskEngine) Mask(e model.Entry, context ...model.Dictionary) (model.En
if err != nil {
return nil, err
}

}

// Get template Entry value
Expand Down Expand Up @@ -150,11 +149,9 @@ func (me *MaskEngine) readCSV(filename string) error {
if records, ok := me.csvEntryByKey[key]; ok {
records = append(records, record)
me.csvEntryByKey[key] = records

} else {
me.csvEntryByKey[key] = []model.Entry{record}
}

}

return nil
Expand Down Expand Up @@ -218,8 +215,12 @@ func Factory(conf model.MaskFactoryConfiguration) (model.MaskEngine, bool, error

func Func(seed int64, seedField string) interface{} {
var callnumber int64
return func(uri string, header bool, sep string, comment string, fieldsPerRecord int, trimSpaces bool) (model.Entry, error) {
mask, err := NewMask(model.FindInCSVType{URI: uri, Header: header, Separator: sep, Comment: comment, FieldsPerRecord: fieldsPerRecord, TrimSpace: trimSpaces}, seed+callnumber, model.NewSeeder(seedField, seed+callnumber))
return func(uri string, exactMatchEntry string, exactMatchCsv string, expected string, header bool, sep string, comment string, fieldsPerRecord int, trimSpaces bool) (model.Entry, error) {
exactMatch := model.ExactMatchType{
CSV: exactMatchCsv,
Entry: exactMatchEntry,
}
mask, err := NewMask(model.FindInCSVType{URI: uri, Header: header, ExactMatch: exactMatch, Expected: expected, Separator: sep, Comment: comment, FieldsPerRecord: fieldsPerRecord, TrimSpace: trimSpaces}, seed+callnumber, model.NewSeeder(seedField, seed+callnumber))
if err != nil {
return "", err
}
Expand Down
75 changes: 48 additions & 27 deletions pkg/findincsv/findincsv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,54 +19,51 @@ package findincsv

import (
"testing"
tmpl "text/template"

"github.com/cgi-fr/pimo/pkg/model"
"github.com/cgi-fr/pimo/pkg/templatemask"
"github.com/stretchr/testify/assert"
)

func TestFactoryShouldCreateAMaskWith2Templates(t *testing.T) {
func TestFactoryShouldCreateAMaskAndFindMatchSimple(t *testing.T) {
exactMatch := model.ExactMatchType{
CSV: "{{.last_name}}+123",
Entry: "{{.nom}}+456",
Entry: "{{.nom}}+123",
}
config := model.FindInCSVType{
URI: "file://../../test/persons.csv",
ExactMatch: exactMatch,
Expected: "only-one",
Header: false,
Header: true,
TrimSpace: true,
}
maskingConfig := model.Masking{Mask: model.MaskType{FindInCSV: config}}
factoryConfig := model.MaskFactoryConfiguration{Masking: maskingConfig, Seed: 0}
tempMaskCsv, err := templatemask.NewMask(factoryConfig.Masking.Mask.FindInCSV.ExactMatch.CSV, tmpl.FuncMap{}, 0, "")
if err != nil {
assert.Fail(t, err.Error())
}
tempMaskEntry, err := templatemask.NewMask(factoryConfig.Masking.Mask.FindInCSV.ExactMatch.Entry, tmpl.FuncMap{}, 0, "")
if err != nil {
assert.Fail(t, err.Error())
}
data := model.NewDictionary().With("nom", "Jean").With("last_name", "Bonbeur").With("mail", "[email protected]").Pack()
resultCsv, err := tempMaskCsv.Mask("[email protected]", data)
resultEntry, err := tempMaskEntry.Mask("[email protected]", data)
assert.Equal(t, nil, err, "error should be nil")
waitedCsv := "Bonbeur+123"
assert.Equal(t, waitedCsv, resultCsv, "Should create the right mail")
waitedEntry := "Jean+456"
assert.Equal(t, waitedEntry, resultEntry, "Should create the right mail")
mask, present, err := Factory(factoryConfig)
assert.Nil(t, err, "error should be nil")
assert.True(t, present, "should be true")
data := model.NewDictionary().With("nom", "Vidal").With("info_personne", "").Pack()
masked, err := mask.Mask("info_personne", data)

assert.Equal(t,
model.NewDictionary().
With("last_name", "Vidal").
With("email", "[email protected]").
With("first_name", "Luce").Unordered(),
masked.(model.Dictionary).Unordered(),
)
assert.Nil(t, err, "error should be nil")
}

func TestFactoryShouldCreateAMaskAndFindMatchSimple(t *testing.T) {
func TestFactoryShouldCreateAMaskAndFindMatchWithoutHeader(t *testing.T) {
exactMatch := model.ExactMatchType{
CSV: "{{.last_name}}+123",
CSV: "{{index . \"1\"}}+123",
Entry: "{{.nom}}+123",
}
config := model.FindInCSVType{
URI: "file://../../test/persons.csv",
ExactMatch: exactMatch,
Expected: "only-one",
Header: true,
Header: false,
TrimSpace: true,
}
maskingConfig := model.Masking{Mask: model.MaskType{FindInCSV: config}}
Expand All @@ -76,13 +73,37 @@ func TestFactoryShouldCreateAMaskAndFindMatchSimple(t *testing.T) {
assert.True(t, present, "should be true")
data := model.NewDictionary().With("nom", "Vidal").With("info_personne", "").Pack()
masked, err := mask.Mask("info_personne", data)
assert.Nil(t, err, "error should be nil")

assert.Equal(t,
model.NewDictionary().
With("last_name", "Vidal").
With("email", "[email protected]").
With("first_name", "Luce").Unordered(),
With("0", "Luce").
With("1", "Vidal").
With("2", "[email protected]").Unordered(),
masked.(model.Dictionary).Unordered(),
)
}

func TestFactoryShouldCreateAMaskAndDontFindMatch(t *testing.T) {
exactMatch := model.ExactMatchType{
CSV: "{{.last_name}}+123",
Entry: "{{.nom}}+123",
}
config := model.FindInCSVType{
URI: "file://../../test/persons.csv",
ExactMatch: exactMatch,
Expected: "only-one",
Header: false,
TrimSpace: true,
}
maskingConfig := model.Masking{Mask: model.MaskType{FindInCSV: config}}
factoryConfig := model.MaskFactoryConfiguration{Masking: maskingConfig, Seed: 0}
mask, present, err := Factory(factoryConfig)
assert.Nil(t, err, "error should be nil")
assert.True(t, present, "should be true")
data := model.NewDictionary().With("nom", "Hello").With("info_personne", "").Pack()
masked, err := mask.Mask("info_personne", data)
assert.Nil(t, err, "error should be nil")

assert.Equal(t, []model.Entry{}, masked)
}

0 comments on commit 304697d

Please sign in to comment.