diff --git a/pkg/findincsv/findincsv.go b/pkg/findincsv/findincsv.go index ade18d18..2b6fd40a 100644 --- a/pkg/findincsv/findincsv.go +++ b/pkg/findincsv/findincsv.go @@ -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 @@ -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 @@ -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 } diff --git a/pkg/findincsv/findincsv_test.go b/pkg/findincsv/findincsv_test.go index 02928330..9ea3a4d9 100644 --- a/pkg/findincsv/findincsv_test.go +++ b/pkg/findincsv/findincsv_test.go @@ -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", "jean44@outlook.com").Pack() - resultCsv, err := tempMaskCsv.Mask("jean44@outlook.com", data) - resultEntry, err := tempMaskEntry.Mask("jean44@outlook.com", 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", "luce.vidal@yopmail.fr"). + 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}} @@ -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", "luce.vidal@yopmail.fr"). - With("first_name", "Luce").Unordered(), + With("0", "Luce"). + With("1", "Vidal"). + With("2", "luce.vidal@yopmail.fr").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) }