Skip to content
This repository has been archived by the owner on Apr 3, 2019. It is now read-only.

Commit

Permalink
Merge pull request #19 from Financial-Times/isDeprecated_bug
Browse files Browse the repository at this point in the history
Fix isDeprecated field
  • Loading branch information
tosan88 authored Sep 10, 2018
2 parents 74b8318 + 1205fed commit b92b0a2
Show file tree
Hide file tree
Showing 6 changed files with 165 additions and 22 deletions.
30 changes: 30 additions & 0 deletions test-data/term.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<term xmlns:c="http://dev.nstein.com/common/1.3" xmlns="http://dev.nstein.com/tme-authority-file/4.10">
<id>Nstein_GL_AFTM_GL_111</id>
<name>'Ar'ara</name>
<variations>
<variation>
<name>'Ar'ara</name>
<weight>100.0</weight>
<case>ucs</case>
<accent>ai</accent>
<languages>
<language>und</language>
</languages>
</variation>
</variations>
<relations>
<relation>
<id>Nstein_GL_AFTM_GL_112</id>
<type>BT</type>
</relation>
</relations>
<attributes>
<attribute>
<name>UN</name>
<values>
<value>'Ar'ara</value>
</values>
</attribute>
</attributes>
</term>
67 changes: 66 additions & 1 deletion test-data/terms.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,69 @@
</attribute>
</attributes>
</term>
</terms>
<term xmlns:c="http://dev.nstein.com/common/1.3" xmlns="http://dev.nstein.com/tme-authority-file/4.10">
<id>Nstein_GL_AFTM_GL_111</id>
<name>'Ar'ara</name>
<variations>
<variation>
<name>'Ar'ara</name>
<weight>100.0</weight>
<case>ucs</case>
<accent>ai</accent>
<languages>
<language>und</language>
</languages>
</variation>
</variations>
<relations>
<relation>
<id>Nstein_GL_AFTM_GL_112</id>
<type>BT</type>
</relation>
</relations>
<attributes>
<attribute>
<name>UN</name>
<values>
<value>'Ar'ara</value>
</values>
</attribute>
</attributes>
</term>
<term xmlns:c="http://dev.nstein.com/common/1.3" xmlns="http://dev.nstein.com/tme-authority-file/4.10">
<id>Nstein_GL_AFTM_GL_9493</id>
<name>Barolong</name>
<enabled>false</enabled>
<variations>
<variation>
<name>Barolong</name>
<weight>100.0</weight>
<case>ucs</case>
<accent>ai</accent>
<languages>
<language>und</language>
</languages>
</variation>
</variations>
<relations>
<relation>
<id>Nstein_GL_BW</id>
<type>BT</type>
</relation>
</relations>
<attributes>
<attribute>
<name>Type</name>
<values>
<value>DIVISION</value>
</values>
</attribute>
<attribute>
<name>UN</name>
<values>
<value>Barolong</value>
</values>
</attribute>
</attributes>
</term>
</terms>
2 changes: 2 additions & 0 deletions tme/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ type BasicConcept struct {
AuthorityValue string `json:"authorityValue,omitempty"`
Aliases []string `json:"aliases,omitempty"`
IsAuthor bool `json:"isAuthor,omitempty"`
IsDeprecated bool `json:"isDeprecated,omitempty"`
}

type ConceptUUID struct {
Expand All @@ -24,6 +25,7 @@ type Term struct {
CanonicalName string `xml:"name"`
RawID string `xml:"id"`
Aliases aliases `xml:"variations"`
Enabled *bool `xml:"enabled,omitempty"`
}

type aliases struct {
Expand Down
6 changes: 5 additions & 1 deletion tme/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,11 @@ func createTestTmeService(repos map[string]tmereader.Repository, httpClient http
}

func TestServiceImpl_GetCount(t *testing.T) {
repo := &mockTmeRepo{terms: []Term{{CanonicalName: "Bob", RawID: "bob"}, {CanonicalName: "Fred", RawID: "fred"}}}
bob := Term{CanonicalName: "Bob", RawID: "bob", Enabled: new(bool)}
*bob.Enabled = true
fred := Term{CanonicalName: "Fred", RawID: "fred", Enabled: new(bool)}
*fred.Enabled = true
repo := &mockTmeRepo{terms: []Term{bob, fred}}
repos := map[string]tmereader.Repository{
"topics": repo,
}
Expand Down
17 changes: 16 additions & 1 deletion tme/transformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,19 @@ func (*Transformer) UnMarshallTaxonomy(contents []byte) ([]interface{}, error) {
if len(taxonomy.TermsC) > 0 {
interfaces = make([]interface{}, len(taxonomy.TermsC))
for i, d := range taxonomy.TermsC {
if d.Enabled == nil {
d.Enabled = new(bool)
*d.Enabled = true
}
interfaces[i] = d
}
} else {
interfaces = make([]interface{}, len(taxonomy.TermsT))
for i, d := range taxonomy.TermsT {
if d.Enabled == nil {
d.Enabled = new(bool)
*d.Enabled = true
}
interfaces[i] = d
}
}
Expand All @@ -41,21 +49,28 @@ func (*Transformer) UnMarshallTerm(content []byte) (interface{}, error) {
if err != nil {
return nil, err
}
if term.Enabled == nil {
term.Enabled = new(bool)
*term.Enabled = true
}
return term, nil
}

func transformConcept(tmeTerm Term, endpoint string) *BasicConcept {
identifier := buildTmeIdentifier(tmeTerm.RawID, EndpointTypeMappings[endpoint]["taxonomy"].(string))
generatedUUID := uuid.NewMD5(uuid.UUID{}, []byte(identifier)).String()
aliasList := buildAliasList(tmeTerm.Aliases)

basicConcept := &BasicConcept{
UUID: generatedUUID,
PrefLabel: tmeTerm.CanonicalName,
Type: EndpointTypeMappings[endpoint]["type"].(string),
Authority: "TME",
AuthorityValue: identifier,
Aliases: aliasList,
IsDeprecated: false,
}
if tmeTerm.Enabled != nil && *tmeTerm.Enabled == false {
basicConcept.IsDeprecated = true
}
if (EndpointTypeMappings[endpoint]["taxonomy"].(string)) == "Brands" {
basicConcept.ParentUUIDs = []string{financialTimesBrandUuid}
Expand Down
65 changes: 46 additions & 19 deletions tme/transformer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,39 @@ import (

func TestTransformer_transformConcept(t *testing.T) {
type testStruct struct {
testName string
testTerm Term
endpoint string
expectedUuid string
expectedPrefLabel string
expectedType string
expectedParentUuid string
expectedAuthority string
expectedAuthValue string
expectedAliases []string
expectedIsAuthor bool
testName string
testTerm Term
endpoint string
expectedUuid string
expectedPrefLabel string
expectedType string
expectedParentUuid string
expectedAuthority string
expectedAuthValue string
expectedAliases []string
expectedIsAuthor bool
expectedIsDeprecated bool
}

//Tme terms
exampleGenre := Term{CanonicalName: "NewGenre", RawID: "newGenre"}
examplePersonWithAliases := Term{CanonicalName: "John Smith", RawID: "johnSmith", Aliases: aliases{Alias: []alias{{Name: "Johnny Boy"}, {Name: "Smithy"}}}}
examplePersonWithoutAliases := Term{CanonicalName: "Jane Doe", RawID: "janeDoe"}
exampleBrand := Term{CanonicalName: "NewBrand", RawID: "newBrand"}
exampleAuthor := Term{CanonicalName: "Author McAuthorface", RawID: "mcAuthorFace", Aliases: aliases{Alias: []alias{{Name: "John"}, {Name: "Bob"}}}}
exampleAuthor := Term{CanonicalName: "Author McAuthorface", RawID: "mcAuthorFace", Aliases: aliases{Alias: []alias{{Name: "John"}, {Name: "Bob"}}}, Enabled: new(bool)}
*exampleAuthor.Enabled = true
exampleDeprecatedGenre := Term{CanonicalName: "OldGenre", RawID: "oldGenre", Enabled: new(bool)}
*exampleDeprecatedGenre.Enabled = false

//Scenarios
genreTest := testStruct{testName: "genreTest", testTerm: exampleGenre, endpoint: "genres", expectedUuid: "7c80229b-3ad4-3bee-bb7a-45eaafe3f83a", expectedType: "Genre", expectedPrefLabel: "NewGenre", expectedAliases: []string{}, expectedParentUuid: "", expectedAuthority: "TME", expectedAuthValue: "bmV3R2VucmU=-R2VucmVz", expectedIsAuthor: false}
personWithAliases := testStruct{testName: "personWithAliases", testTerm: examplePersonWithAliases, endpoint: "people", expectedUuid: "05d18aac-9d8e-35d6-9a50-a950fc10aa0e", expectedType: "Person", expectedPrefLabel: "John Smith", expectedAliases: []string{"Johnny Boy", "Smithy"}, expectedParentUuid: "", expectedAuthority: "TME", expectedAuthValue: "am9oblNtaXRo-UE4=", expectedIsAuthor: false}
personNoAliases := testStruct{testName: "personNoAliases", testTerm: examplePersonWithoutAliases, endpoint: "people", expectedUuid: "ee34e2fd-f363-339b-aa25-191483cb909e", expectedType: "Person", expectedPrefLabel: "Jane Doe", expectedAliases: []string{}, expectedParentUuid: "", expectedAuthority: "TME", expectedAuthValue: "amFuZURvZQ==-UE4=", expectedIsAuthor: false}
brandTest := testStruct{testName: "brandTest", testTerm: exampleBrand, endpoint: "brands", expectedUuid: "dcb6cc7c-0e5b-3537-8c98-5405a52484f3", expectedType: "Brand", expectedPrefLabel: "NewBrand", expectedAliases: []string{}, expectedParentUuid: financialTimesBrandUuid, expectedAuthority: "TME", expectedAuthValue: "bmV3QnJhbmQ=-QnJhbmRz", expectedIsAuthor: false}
authorTest := testStruct{testName: "authorTest", testTerm: exampleAuthor, endpoint: "authors", expectedUuid: "829f073f-6666-338f-abd6-d69c37f2e5d0", expectedType: "Person", expectedPrefLabel: "Author McAuthorface", expectedAliases: []string{"John", "Bob"}, expectedParentUuid: "", expectedAuthority: "TME", expectedAuthValue: "bWNBdXRob3JGYWNl-QXV0aG9ycw==", expectedIsAuthor: true}
genreTest := testStruct{testName: "genreTest", testTerm: exampleGenre, endpoint: "genres", expectedUuid: "7c80229b-3ad4-3bee-bb7a-45eaafe3f83a", expectedType: "Genre", expectedPrefLabel: "NewGenre", expectedAliases: []string{}, expectedParentUuid: "", expectedAuthority: "TME", expectedAuthValue: "bmV3R2VucmU=-R2VucmVz", expectedIsAuthor: false, expectedIsDeprecated: false}
personWithAliases := testStruct{testName: "personWithAliases", testTerm: examplePersonWithAliases, endpoint: "people", expectedUuid: "05d18aac-9d8e-35d6-9a50-a950fc10aa0e", expectedType: "Person", expectedPrefLabel: "John Smith", expectedAliases: []string{"Johnny Boy", "Smithy"}, expectedParentUuid: "", expectedAuthority: "TME", expectedAuthValue: "am9oblNtaXRo-UE4=", expectedIsAuthor: false, expectedIsDeprecated: false}
personNoAliases := testStruct{testName: "personNoAliases", testTerm: examplePersonWithoutAliases, endpoint: "people", expectedUuid: "ee34e2fd-f363-339b-aa25-191483cb909e", expectedType: "Person", expectedPrefLabel: "Jane Doe", expectedAliases: []string{}, expectedParentUuid: "", expectedAuthority: "TME", expectedAuthValue: "amFuZURvZQ==-UE4=", expectedIsAuthor: false, expectedIsDeprecated: false}
brandTest := testStruct{testName: "brandTest", testTerm: exampleBrand, endpoint: "brands", expectedUuid: "dcb6cc7c-0e5b-3537-8c98-5405a52484f3", expectedType: "Brand", expectedPrefLabel: "NewBrand", expectedAliases: []string{}, expectedParentUuid: financialTimesBrandUuid, expectedAuthority: "TME", expectedAuthValue: "bmV3QnJhbmQ=-QnJhbmRz", expectedIsAuthor: false, expectedIsDeprecated: false}
authorTest := testStruct{testName: "authorTest", testTerm: exampleAuthor, endpoint: "authors", expectedUuid: "829f073f-6666-338f-abd6-d69c37f2e5d0", expectedType: "Person", expectedPrefLabel: "Author McAuthorface", expectedAliases: []string{"John", "Bob"}, expectedParentUuid: "", expectedAuthority: "TME", expectedAuthValue: "bWNBdXRob3JGYWNl-QXV0aG9ycw==", expectedIsAuthor: true, expectedIsDeprecated: false}
deprecatedGenreTest := testStruct{testName: "deprecatedGenreTest", testTerm: exampleDeprecatedGenre, endpoint: "genres", expectedUuid: "0f2b2e49-74a2-3357-ba22-80a353922dab", expectedType: "Genre", expectedPrefLabel: "OldGenre", expectedAliases: []string{}, expectedParentUuid: "", expectedAuthority: "TME", expectedAuthValue: "b2xkR2VucmU=-R2VucmVz", expectedIsAuthor: false, expectedIsDeprecated: true}

testScenarios := []testStruct{genreTest, personWithAliases, personNoAliases, brandTest, authorTest}
testScenarios := []testStruct{genreTest, personWithAliases, personNoAliases, brandTest, authorTest, deprecatedGenreTest}

for _, scenario := range testScenarios {
result := transformConcept(scenario.testTerm, scenario.endpoint)
Expand All @@ -53,11 +58,11 @@ func TestTransformer_transformConcept(t *testing.T) {
assert.Equal(t, scenario.expectedAuthority, result.Authority, "Scenario "+scenario.testName+" failed")
assert.Equal(t, scenario.expectedAuthValue, result.AuthorityValue, "Scenario "+scenario.testName+" failed")
assert.Equal(t, scenario.expectedIsAuthor, result.IsAuthor, "Scenario "+scenario.testName+" failed")
assert.Equal(t, scenario.expectedIsDeprecated, result.IsDeprecated, "Scenario "+scenario.testName+" failed")
}
}

func TestTransformer_UnMarshallTaxonomy(t *testing.T) {

t.Run("Test terms XML", func(t *testing.T) {
content, err := ioutil.ReadFile("../test-data/terms.xml")
if err != nil {
Expand All @@ -67,6 +72,14 @@ func TestTransformer_UnMarshallTaxonomy(t *testing.T) {
iFace, err := tr.UnMarshallTaxonomy(content)
assert.Equal(t, "A term", iFace[0].(Term).CanonicalName)
assert.Equal(t, "Nstein_GL_AFTM_GL_123456", iFace[0].(Term).RawID)
assert.NotNil(t, iFace[0].(Term).Enabled)
assert.True(t, *iFace[0].(Term).Enabled)
assert.Equal(t, "Nstein_GL_AFTM_GL_111", iFace[1].(Term).RawID)
assert.NotNil(t, iFace[1].(Term).Enabled)
assert.True(t, *iFace[1].(Term).Enabled)
assert.Equal(t, "Nstein_GL_AFTM_GL_9493", iFace[2].(Term).RawID)
assert.NotNil(t, iFace[2].(Term).Enabled)
assert.False(t, *iFace[2].(Term).Enabled)
})

t.Run("Test categories XML", func(t *testing.T) {
Expand Down Expand Up @@ -98,3 +111,17 @@ func TestTransformer_UnMarshallTerm(t *testing.T) {
assert.Nil(t, iFace)
assert.Error(t, err, errors.New("Not Implemented"))
}

func TestTransformer_UnMarshallTermEnabledDefault(t *testing.T) {
t.Run("Test term XML", func(t *testing.T) {
content, err := ioutil.ReadFile("../test-data/term.xml")
if err != nil {
log.Errorf("Failed to read test file: %s", err)
}
tr := Transformer{}
term, err := tr.UnMarshallTerm(content)
assert.Equal(t, "'Ar'ara", term.(Term).CanonicalName)
assert.NotNil(t, term.(Term).Enabled)
assert.True(t, *term.(Term).Enabled)
})
}

0 comments on commit b92b0a2

Please sign in to comment.