Skip to content

Commit

Permalink
Merge pull request #11 from atlanhq/integration-tests
Browse files Browse the repository at this point in the history
DVX:183 - Add Integration Tests and Github Actions
  • Loading branch information
0xquark authored Apr 16, 2024
2 parents d3ce94c + 0e8e090 commit da98f7a
Show file tree
Hide file tree
Showing 16 changed files with 454 additions and 142 deletions.
31 changes: 31 additions & 0 deletions .github/workflows/go-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Go Tests

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

env:
ATLAN_API_KEY: ${{ secrets.ATLAN_API_KEY }}
ATLAN_BASE_URL: ${{ secrets.ATLAN_BASE_URL }}

jobs:
build:
name: Run Go Tests
runs-on: ubuntu-latest

steps:
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.22.2'

- name: Check out code into the Go module directory
uses: actions/checkout@v3

- name: Install gotestsum
run: go install gotest.tools/gotestsum@latest

- name: Run tests
run: gotestsum --format testdox -- -v ./...
4 changes: 3 additions & 1 deletion atlan/client/atlan_tag_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package client

import (
"encoding/json"
"fmt"
"github.com/atlanhq/atlan-go/atlan/model"
"sync"
)
Expand Down Expand Up @@ -78,14 +79,15 @@ func (c *AtlanTagCache) RefreshCache() error {

response, err := DefaultAtlanClient.CallAPI(api, nil, nil)
if err != nil {
fmt.Printf("Error making API call: %v", err)
return err
}

// Parse the response and populate the cacheByID, mapIDToName, mapNameToID accordingly
var atlanTags model.TypeDefResponse
err = json.Unmarshal(response, &atlanTags)
if err != nil {
return AtlanError{ErrorCode: errorCodes[EXPIRED_API_TOKEN]}
return fmt.Errorf("error unmarshalling response: %v", err)
}

c.cacheByID = make(map[string]model.AtlanTagDef)
Expand Down
74 changes: 74 additions & 0 deletions atlan/client/atlan_tag_cache_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package client

import (
"fmt"
"github.com/stretchr/testify/assert"
"testing"
)

func TestIntegrationAtlanTagCache_RefreshCache(t *testing.T) {
if testing.Short() {
t.Skip("skipping integration test")
}
LoggingEnabled = false
client := NewContext()
cache := NewAtlanTagCache(client)

// Execute
err := cache.RefreshCache()

// Verify
assert.NoError(t, err)
// Check that cache is not empty, indicating data was fetched
assert.NotEmpty(t, cache.cacheByID)
}

func TestIntegrationAtlanTagCache_GetIDForName(t *testing.T) {
if testing.Short() {
t.Skip("skipping integration test")
}
LoggingEnabled = false

client := NewContext()
cache := NewAtlanTagCache(client)

// Ensure the cache is populated
resp, _ := GetAll()
tagName := resp.AtlanTagDefs[0].DisplayName

//_ = cache.RefreshCache()

fmt.Println("Tag Name: ", tagName)
id, err := cache.GetIDForName(tagName)

// Verify
assert.NoError(t, err)
assert.NotEmpty(t, id) // ID should be non-empty if the tag exists

// Test not found scenario
_, err = cache.GetIDForName("NonExistentTag")
assert.Nil(t, nil) // Expect error(nil) since tag does not exist
}

func TestIntegrationAtlanTagCache_GetNameForID(t *testing.T) {
if testing.Short() {
t.Skip("skipping integration test")
}
LoggingEnabled = false
client := NewContext()
cache := NewAtlanTagCache(client)

// Ensure the cache is populated
_ = cache.RefreshCache()

// Assuming "BBDjIBZUNHtKPExR1Z3a5I" is a valid GUID
name, err := cache.GetNameForID("BBDjIBZUNHtKPExR1Z3a5I")

// Verify
assert.NoError(t, err)
assert.NotEmpty(t, name) // Name should be non-empty if the ID is valid

// Test not found scenario
_, err = cache.GetNameForID("123456")
assert.Nil(t, nil) // Expect error(nil) since ID does not exist
}
5 changes: 3 additions & 2 deletions atlan/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,9 @@ func (ac *AtlanClient) CallAPI(api *API, queryParams map[string]string, requestO
}

if requestObj != nil {
fmt.Println("Request Object:", requestObj)
//fmt.Println("Request Object:", requestObj)
requestJSON, err := json.Marshal(requestObj)
fmt.Println("Request JSON:", string(requestJSON))
//fmt.Println("Request JSON:", string(requestJSON))
if err != nil {
return nil, fmt.Errorf("error marshaling request object: %v", err)
}
Expand All @@ -120,6 +120,7 @@ func (ac *AtlanClient) CallAPI(api *API, queryParams map[string]string, requestO

response, err := ac.makeRequest(api.Method, path, params)
if err != nil {
fmt.Println(err)
errorMessage, _ := ioutil.ReadAll(response.Body)
return nil, handleApiError(response, string(errorMessage))
}
Expand Down
29 changes: 0 additions & 29 deletions atlan/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,32 +68,3 @@ func TestCallAPI(t *testing.T) {
t.Errorf("Unexpected response. Expected: %s, Got: %s", expectedResponse, string(response))
}
}

func TestMakeRequest(t *testing.T) {
// Create a new AtlanClient instance
atlanClient := &AtlanClient{
Session: http.DefaultClient,
host: "http://example.com",
ApiKey: "mock_api_key",
loggingEnabled: true,
requestParams: make(map[string]interface{}),
}

// Test GET request
req, err := atlanClient.makeRequest(http.MethodGet, "https://example.com", nil)
if err != nil {
t.Errorf("Error creating GET request: %v", err)
}
if req.Request.Method != http.MethodGet {
// Test POST request
req, err = atlanClient.makeRequest(http.MethodPost, "http://example.com", map[string]interface{}{
"data": "test data",
})
if err != nil {
t.Errorf("Error creating POST request: %v", err)
}
if req.Request.Method != http.MethodPost {
t.Errorf("Unexpected request method. Expected: %s, Got: %s", http.MethodPost, req.Request.Method)
}
}
}
59 changes: 59 additions & 0 deletions atlan/client/fluent_search_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package client

import (
"fmt"
"github.com/atlanhq/atlan-go/atlan"
"github.com/stretchr/testify/assert"
"testing"
"time"
)

func TestIntegrationFluentSearch(t *testing.T) {
if testing.Short() {
t.Skip("skipping integration test")
}
LoggingEnabled = false
ctx := NewContext()

// Create a glossary
g := &AtlasGlossary{}
g.Creator(GlossaryName, atlan.AtlanIconAirplaneInFlight)
response, err := Save(g)
if err != nil {
t.Errorf("Error: %v", err)
}
assert.NotNil(t, response, "fetched glossary should not be nil")

time.Sleep(5 * time.Second)
// Search for glossary with Active Status and Name as GlossaryName
searchResult, err := NewFluentSearch().
PageSizes(10).
ActiveAssets().
Where(ctx.Glossary.NAME.Eq(GlossaryName)).
//IncludeOnResults("guid").
Execute()

if err != nil {
fmt.Printf("Error executing search: %v\n", err)
return
}

assert.NotNil(t, searchResult, "search result should not be nil")
assert.Equal(t, 1, len(searchResult), "number of glossaries should be 1")
assert.Equal(t, GlossaryName, *searchResult[0].Entities[0].DisplayName, "glossary name should match")

// Search for glossaries starts with letter G and sort them in ascending order by name
searchResult, err = NewFluentSearch().
PageSizes(10).
ActiveAssets().
Where(ctx.Glossary.NAME.StartsWith("gsdk", nil)).
Sort(NAME, atlan.SortOrderAscending).
Execute()

assert.Equal(t, 1, len(searchResult), "number of glossaries should be 1")
assert.Equal(t, "g", string((*searchResult[0].Entities[0].DisplayName)[0]), "glossary name should start with G")

// Delete already created glossary
deleteresponse, _ := PurgeByGuid([]string{response.MutatedEntities.CREATE[0].Guid})
assert.NotNil(t, deleteresponse, "fetched glossary should not be nil")
}
6 changes: 5 additions & 1 deletion atlan/client/glossary_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func GetGlossaryTermByGuid(glossaryGuid string) (*assets.AtlasGlossaryTerm, erro
func (g *AtlasGlossary) Creator(name string, icon atlan.AtlanIcon) {
g.TypeName = assets.StringPtr("AtlasGlossary")
g.Name = assets.StringPtr(name)
g.QualifiedName = assets.StringPtr("CBtveYe0Avp5iwU8q3M7Y12")
g.QualifiedName = assets.StringPtr(name)
g.AssetIcon = atlan.AtlanIconPtr(icon)
}

Expand Down Expand Up @@ -112,6 +112,10 @@ func (g *AtlasGlossary) MarshalJSON() ([]byte, error) {
customJSON["guid"] = *g.Guid
}

if g.DisplayName != nil && *g.DisplayName != "" {
customJSON["attributes"].(map[string]interface{})["displayName"] = *g.DisplayName
}

// Marshal the custom JSON
return json.MarshalIndent(customJSON, "", " ")
}
Loading

0 comments on commit da98f7a

Please sign in to comment.