Skip to content

Commit

Permalink
Merge pull request #19 from xataio/feat/files-client-continue
Browse files Browse the repository at this point in the history
Feat/files client continue
  • Loading branch information
omerdemirok authored Nov 7, 2023
2 parents 3c0e2f8 + 51075db commit 6ab6936
Show file tree
Hide file tree
Showing 10 changed files with 617 additions and 50 deletions.
10 changes: 7 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,16 @@ smoke-test:

test:
@echo "Running unit tests"
@go test -count=1 -cover -race ./...
@go test -count=1 -cover -race ./xata

integration-test:
@echo "Running integration test"
@go test -v --tags=integration -count=1 -cover -race ./internal/integration-tests
@go test -count=1 -cover -race ./internal/integration-tests

download-openapi-specs:
@echo "Downloading openapi specs"
@cd internal/docs && go run . && cd ../..
@cd internal/docs && go run . && cd ../..

clean-workspaces:
@echo "Cleaning integration test workspaces"
CLEAN_UP_INTEGRATION_WORKSPACES=true go test -v -run Test_cleanupIntegrationWorkspaces ./...
128 changes: 117 additions & 11 deletions internal/integration-tests/files_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ func Test_filesClient(t *testing.T) {
}
})

filesCli, err := xata.NewFilesClient(xata.WithAPIKey(cfg.apiKey),
recordsCli, err := xata.NewRecordsClient(
xata.WithAPIKey(cfg.apiKey),
xata.WithBaseURL(fmt.Sprintf(
"https://%s.%s.xata.sh",
cfg.wsID,
Expand All @@ -38,11 +39,12 @@ func Test_filesClient(t *testing.T) {
xata.WithHTTPClient(retryablehttp.NewClient().StandardClient()),
)
if err != nil {
t.Fatalf("unable to construct files cli: %v", err)
t.Fatal(err)
}

recordsCli, err := xata.NewRecordsClient(
xata.WithAPIKey(cfg.apiKey),
insertRecordRequest := generateInsertRecordRequest(cfg.databaseName, cfg.tableName)

filesCli, err := xata.NewFilesClient(xata.WithAPIKey(cfg.apiKey),
xata.WithBaseURL(fmt.Sprintf(
"https://%s.%s.xata.sh",
cfg.wsID,
Expand All @@ -51,18 +53,58 @@ func Test_filesClient(t *testing.T) {
xata.WithHTTPClient(retryablehttp.NewClient().StandardClient()),
)
if err != nil {
t.Fatal(err)
t.Fatalf("unable to construct files cli: %v", err)
}

insertRecordRequest := generateInsertRecordRequest(cfg.databaseName, cfg.tableName)
t.Run("get a file", func(t *testing.T) {
record, err := recordsCli.Insert(ctx, insertRecordRequest)
if err != nil {
t.Fatal(err)
}
assert.NotNil(t, record)

record, err := recordsCli.Insert(ctx, insertRecordRequest)
if err != nil {
t.Fatal(err)
}
assert.NotNil(t, record)
getFileRes, err := filesCli.Get(ctx, xata.GetFileRequest{
BranchRequestOptional: xata.BranchRequestOptional{
DatabaseName: xata.String(cfg.databaseName),
},
TableName: cfg.tableName,
RecordId: record.Id,
ColumnName: fileColumn,
})
assert.NoError(t, err)
assert.Equal(t, fileContent, string(getFileRes.Content))
})

t.Run("put a file", func(t *testing.T) {
record, err := recordsCli.Insert(ctx, insertRecordRequest)
if err != nil {
t.Fatal(err)
}
assert.NotNil(t, record)

fileRes, err := filesCli.Put(ctx, xata.PutFileRequest{
BranchRequestOptional: xata.BranchRequestOptional{
DatabaseName: xata.String(cfg.databaseName),
},
TableName: cfg.tableName,
RecordId: record.Id,
ColumnName: fileColumn,
ContentType: xata.String(""application/octet-stream""),
Data: []byte(`new content`),
})
assert.NoError(t, err)
assert.NotNil(t, fileRes.Attributes)
assert.Equal(t, "", fileRes.Name)
assert.Nil(t, fileRes.Id)
})

t.Run("delete a file", func(t *testing.T) {
record, err := recordsCli.Insert(ctx, insertRecordRequest)
if err != nil {
t.Fatal(err)
}
assert.NotNil(t, record)

delRes, err := filesCli.Delete(ctx, xata.DeleteFileRequest{
BranchRequestOptional: xata.BranchRequestOptional{
DatabaseName: xata.String(cfg.databaseName),
Expand All @@ -74,4 +116,68 @@ func Test_filesClient(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, testFileName, delRes.Name)
})

t.Run("get file item", func(t *testing.T) {
record, err := recordsCli.Insert(ctx, insertRecordRequest)
if err != nil {
t.Fatal(err)
}
assert.NotNil(t, record)

getItemRes, err := filesCli.GetItem(ctx, xata.GetFileItemRequest{
BranchRequestOptional: xata.BranchRequestOptional{
DatabaseName: xata.String(cfg.databaseName),
},
TableName: cfg.tableName,
RecordId: record.Id,
ColumnName: fileArrayColumn,
FileID: record.Data[fileArrayColumn].([]interface{})[0].(map[string]any)["id"].(string),
})
assert.NoError(t, err)
assert.Equal(t, fileContent, string(getItemRes.Content))
})

t.Run("put a file item", func(t *testing.T) {
record, err := recordsCli.Insert(ctx, insertRecordRequest)
if err != nil {
t.Fatal(err)
}
assert.NotNil(t, record)

fileRes, err := filesCli.PutItem(ctx, xata.PutFileItemRequest{
BranchRequestOptional: xata.BranchRequestOptional{
DatabaseName: xata.String(cfg.databaseName),
},
TableName: cfg.tableName,
RecordId: record.Id,
ColumnName: fileArrayColumn,
FileID: record.Data[fileArrayColumn].([]interface{})[0].(map[string]any)["id"].(string),
ContentType: xata.String("text/plain"),
Data: []byte(`new content`),
})
assert.NoError(t, err)
assert.NotNil(t, fileRes.Attributes)
assert.Equal(t, "", fileRes.Name)
assert.NotNil(t, fileRes.Id)
})

t.Run("delete a file item", func(t *testing.T) {
record, err := recordsCli.Insert(ctx, insertRecordRequest)
if err != nil {
t.Fatal(err)
}
assert.NotNil(t, record)

delRes, err := filesCli.DeleteItem(ctx, xata.DeleteFileItemRequest{
BranchRequestOptional: xata.BranchRequestOptional{
DatabaseName: xata.String(cfg.databaseName),
},
TableName: cfg.tableName,
RecordId: record.Id,
ColumnName: fileArrayColumn,
FileID: record.Data[fileArrayColumn].([]interface{})[0].(map[string]any)["id"].(string),
})
assert.NoError(t, err)
assert.Equal(t, testFileName, delRes.Name)
})
}
41 changes: 27 additions & 14 deletions internal/integration-tests/records_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package integrationtests

import (
"context"
"encoding/base64"
"fmt"
"testing"
"time"
Expand All @@ -13,18 +14,20 @@ import (
)

const (
stringColumn = "user-name"
boolColumn = "active"
textColumn = "text-column"
emailColumn = "email"
dateTimeColumn = "date-of-birth"
integerColumn = "integer-column"
floatColumn = "float-column"
fileColumn = "file-column"
jsonColumn = "json-column"
vectorColumn = "vector-column" // it is important to set a vector dimension on the UI: 2
multipleColumn = "multiple-column"
testFileName = "file-name.txt"
stringColumn = "user-name"
boolColumn = "active"
textColumn = "text-column"
emailColumn = "email"
dateTimeColumn = "date-of-birth"
integerColumn = "integer-column"
floatColumn = "float-column"
fileColumn = "file-column"
fileArrayColumn = "fileArrayColumn"
fileContent = "file content"
jsonColumn = "json-column"
vectorColumn = "vector-column" // it is important to set a vector dimension on the UI: 2
multipleColumn = "multiple-column"
testFileName = "file-name.txt"
)

func Test_recordsClient_Insert_Get(t *testing.T) {
Expand Down Expand Up @@ -73,7 +76,8 @@ func Test_recordsClient_Insert_Get(t *testing.T) {
assert.Equal(t, insertRecordRequest.Body[textColumn].String, record.Data[textColumn])
assert.Equal(t, insertRecordRequest.Body[integerColumn].Double, record.Data[integerColumn])
assert.Equal(t, insertRecordRequest.Body[floatColumn].Double, record.Data[floatColumn])
assert.Equal(t, insertRecordRequest.Body[fileColumn].InputFile.Name, record.Data[fileColumn].(map[string]interface{})["name"])
assert.Equal(t, insertRecordRequest.Body[fileColumn].InputFile.Name, record.Data[fileColumn].(map[string]any)["name"])
assert.Equal(t, *(insertRecordRequest.Body[fileArrayColumn].InputFileArray[0]).Name, record.Data[fileArrayColumn].([]interface{})[0].(map[string]any)["name"])
assert.ElementsMatch(t, insertRecordRequest.Body[vectorColumn].DoubleList, record.Data[vectorColumn])
assert.ElementsMatch(t, insertRecordRequest.Body[multipleColumn].StringList, record.Data[multipleColumn])
assert.Equal(t, insertRecordRequest.Body[jsonColumn].String, record.Data[jsonColumn])
Expand Down Expand Up @@ -102,6 +106,8 @@ func Test_recordsClient_Insert_Get(t *testing.T) {
// 400: {"errors":[{
//"status":400,"message":"column [file-column]: file upload not permitted in transaction"},
delete(insertRecordRequest.Body, fileColumn)
//"status":400,"message":"column [fileArrayColumn]: file entry [0]: file upload not permitted in transaction"}
delete(insertRecordRequest.Body, fileArrayColumn)

records, err := recordsCli.BulkInsert(ctx, xata.BulkInsertRecordRequest{
RecordRequest: insertRecordRequest.RecordRequest,
Expand Down Expand Up @@ -443,6 +449,7 @@ func generateInsertRecordRequest(databaseName, tableName string) xata.InsertReco
integerColumn,
floatColumn,
fileColumn,
fileArrayColumn,
jsonColumn,
vectorColumn,
multipleColumn,
Expand All @@ -457,7 +464,13 @@ func generateInsertRecordRequest(databaseName, tableName string) xata.InsertReco
floatColumn: xata.ValueFromDouble(10.3),
fileColumn: xata.ValueFromInputFile(xata.InputFile{
Name: testFileName,
Base64Content: xata.String("ZmlsZSBjb250ZW50"), // file content
Base64Content: xata.String(base64.StdEncoding.EncodeToString([]byte(fileContent))),
}),
fileArrayColumn: xata.ValueFromInputFileArray(xata.InputFileArray{
{
Name: xata.String(testFileName),
Base64Content: xata.String(base64.StdEncoding.EncodeToString([]byte(fileContent))),
},
}),
vectorColumn: xata.ValueFromDoubleList([]float64{10.3, 20.2}),
multipleColumn: xata.ValueFromStringList([]string{"hello", "world"}),
Expand Down
19 changes: 19 additions & 0 deletions internal/integration-tests/setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,25 @@ func setupTableWithColumns(ctx context.Context, cfg *config) error {
return err
}

_, err = tableCli.AddColumn(ctx, xata.AddColumnRequest{
TableRequest: xata.TableRequest{
TableName: cfg.tableName,
DatabaseName: xata.String(cfg.databaseName),
},
Column: &xata.Column{
Name: fileArrayColumn,
Type: xata.ColumnTypeFileMap,
File: &xata.ColumnFile{
DefaultPublicAccess: xata.Bool(true),
},
NotNull: xata.Bool(false),
Unique: xata.Bool(false),
},
})
if err != nil {
return err
}

_, err = tableCli.AddColumn(ctx, xata.AddColumnRequest{
TableRequest: xata.TableRequest{
TableName: cfg.tableName,
Expand Down
4 changes: 4 additions & 0 deletions internal/integration-tests/setup_test_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package integrationtests

import (
"context"
"os"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -30,6 +31,9 @@ func Test_setup_cleanup(t *testing.T) {
}

func Test_cleanupIntegrationWorkspaces(t *testing.T) {
if _, found := os.LookupEnv("CLEAN_UP_INTEGRATION_WORKSPACES"); !found {
t.Skip("skipping integration workspaces cleanup")
}
err := cleanAllWorkspaces()
if err != nil {
t.Fatal(err)
Expand Down
Loading

0 comments on commit 6ab6936

Please sign in to comment.