From 9171d258ecd77ee44af196682252367ea5a6828b Mon Sep 17 00:00:00 2001 From: Reid Beels Date: Fri, 23 Sep 2022 04:06:42 -0700 Subject: [PATCH] Fix Latitude and Longitude anonymization (#141) --- pkg/anonymiser/anonymiser.go | 4 ++++ pkg/anonymiser/anonymiser_test.go | 24 ++++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/pkg/anonymiser/anonymiser.go b/pkg/anonymiser/anonymiser.go index b15ee71..aea6270 100644 --- a/pkg/anonymiser/anonymiser.go +++ b/pkg/anonymiser/anonymiser.go @@ -19,6 +19,8 @@ const ( literalPrefix = "literal:" email = "EmailAddress" username = "UserName" + latitude = "Latitude" + longitude = "Longitude" ) var requireArgs = map[string]bool{ @@ -97,6 +99,8 @@ func (a *anonymiser) ReadTable(tableName string, rowChan chan<- database.Row, op faker.Call([]reflect.Value{})[0].String(), hex.EncodeToString(b), ) + case latitude, longitude: + value = fmt.Sprintf("%f", faker.Call(args)[0].Float()) default: value = faker.Call(args)[0].String() } diff --git a/pkg/anonymiser/anonymiser_test.go b/pkg/anonymiser/anonymiser_test.go index 3a514e0..0392712 100644 --- a/pkg/anonymiser/anonymiser_test.go +++ b/pkg/anonymiser/anonymiser_test.go @@ -48,6 +48,12 @@ func TestReadTable(t *testing.T) { opts: reader.ReadTableOpt{}, config: config.Tables{{Name: "test", Anonymise: map[string]string{"column_test": "literal:Hello"}}}, }, + { + scenario: "when column is anonymised with float value", + function: testWhenColumnIsAnonymisedWithFloatValue, + opts: reader.ReadTableOpt{}, + config: config.Tables{{Name: "test", Anonymise: map[string]string{"column_test": "Latitude"}}}, + }, { scenario: "when column anonymiser in invalid", function: testWhenColumnAnonymiserIsInvalid, @@ -143,6 +149,24 @@ func testWhenColumnIsAnonymisedWithLiteral(t *testing.T, opts reader.ReadTableOp } } +func testWhenColumnIsAnonymisedWithFloatValue(t *testing.T, opts reader.ReadTableOpt, tables config.Tables) { + anonymiser := NewAnonymiser(&mockReader{}, tables) + + rowChan := make(chan database.Row) + defer close(rowChan) + + err := anonymiser.ReadTable("test", rowChan, opts) + require.NoError(t, err) + + timeoutChan := time.After(waitTimeout) + select { + case row := <-rowChan: + assert.NotEqual(t, "", row["column_test"]) + case <-timeoutChan: + assert.FailNow(t, "Failing due to timeout") + } +} + func testWhenColumnAnonymiserIsInvalid(t *testing.T, opts reader.ReadTableOpt, tables config.Tables) { anonymiser := NewAnonymiser(&mockReader{}, tables)