Skip to content

Commit

Permalink
Adding tableid tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tang8330 committed Sep 30, 2024
1 parent 4fa9739 commit 99255e3
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
48 changes: 48 additions & 0 deletions clients/databricks/tableid.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package databricks

import (
"fmt"

"github.com/artie-labs/transfer/clients/databricks/dialect"
"github.com/artie-labs/transfer/lib/sql"
)

var _dialect = dialect.DatabricksDialect{}

type TableIdentifier struct {
database string
schema string
table string
}

func NewTableIdentifier(database, schema, table string) TableIdentifier {
return TableIdentifier{
database: database,
schema: schema,
table: table,
}
}

func (ti TableIdentifier) Database() string {
return ti.database
}

func (ti TableIdentifier) Schema() string {
return ti.schema
}

func (ti TableIdentifier) EscapedTable() string {
return _dialect.QuoteIdentifier(ti.table)
}

func (ti TableIdentifier) Table() string {
return ti.table
}

func (ti TableIdentifier) WithTable(table string) sql.TableIdentifier {
return NewTableIdentifier(ti.database, ti.schema, table)
}

func (ti TableIdentifier) FullyQualifiedName() string {
return fmt.Sprintf("%s.%s.%s", _dialect.QuoteIdentifier(ti.database), _dialect.QuoteIdentifier(ti.schema), ti.EscapedTable())
}
33 changes: 33 additions & 0 deletions clients/databricks/tableid_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package databricks

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestTableIdentifier_WithTable(t *testing.T) {
tableID := NewTableIdentifier("database", "schema", "foo")
tableID2 := tableID.WithTable("bar")
typedTableID2, ok := tableID2.(TableIdentifier)
assert.True(t, ok)
assert.Equal(t, "database", typedTableID2.Database())
assert.Equal(t, "schema", typedTableID2.Schema())
assert.Equal(t, "bar", tableID2.Table())
}

func TestTableIdentifier_FullyQualifiedName(t *testing.T) {
// Table name that is not a reserved word:
assert.Equal(t, "`database`.`schema`.`foo`", NewTableIdentifier("database", "schema", "foo").FullyQualifiedName())

// Table name that is a reserved word:
assert.Equal(t, "`database`.`schema`.`table`", NewTableIdentifier("database", "schema", "table").FullyQualifiedName())
}

func TestTableIdentifier_EscapedTable(t *testing.T) {
// Table name that is not a reserved word:
assert.Equal(t, "`foo`", NewTableIdentifier("database", "schema", "foo").EscapedTable())

// Table name that is a reserved word:
assert.Equal(t, "`table`", NewTableIdentifier("database", "schema", "table").EscapedTable())
}

0 comments on commit 99255e3

Please sign in to comment.