Skip to content

Commit

Permalink
Merge pull request #21 from btnguyen2k/dev
Browse files Browse the repository at this point in the history
Prepare to release v0.2.1
  • Loading branch information
btnguyen2k authored Jun 8, 2023
2 parents 44e4be5 + 33de924 commit 038911f
Show file tree
Hide file tree
Showing 21 changed files with 3,997 additions and 2,912 deletions.
105 changes: 85 additions & 20 deletions .github/workflows/gocosmos.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,27 @@ on:
branches: [ main ]

jobs:
testDriver:
name: Test driver
testDriverQueryParsing:
name: Test driver query parsing
runs-on: ubuntu-latest
steps:
- name: Set up Go env
uses: actions/setup-go@v2
with:
go-version: ^1.13
- name: Check out code into the Go module directory
uses: actions/checkout@v2
- name: Test
run: |
go test -cover -coverprofile="coverage_driver_parse.txt" -v -count 1 -p 1 -run "_parse" .
- name: Codecov
uses: codecov/codecov-action@v3
with:
flags: driver_parse
name: driver_parse

testDriverStmtDatabase:
name: Test driver database statements
runs-on: windows-latest
steps:
- name: Set up Go env
Expand All @@ -26,15 +45,15 @@ jobs:
netstat -nt
$env:COSMOSDB_DRIVER='gocosmos'
$env:COSMOSDB_URL='AccountEndpoint=https://127.0.0.1:8081/;AccountKey=C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw=='
go test -cover -coverprofile="coverage_driver.txt" -v -count 1 -p 1 -run "Test_" .
go test -cover -coverprofile="coverage_driver_database.txt" -v -count 1 -p 1 -run "TestStmt.*Databases?_(Exec|Query)" .
- name: Codecov
uses: codecov/codecov-action@v3
with:
flags: driver
name: driver
flags: driver_database
name: driver_database

testDriverSelect:
name: Test driver SELECT query
testDriverStmtCollection:
name: Test driver collection statements
runs-on: windows-latest
steps:
- name: Set up Go env
Expand All @@ -52,12 +71,64 @@ jobs:
netstat -nt
$env:COSMOSDB_DRIVER='gocosmos'
$env:COSMOSDB_URL='AccountEndpoint=https://127.0.0.1:8081/;AccountKey=C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw=='
go test -cover -coverprofile="coverage_driver_select.txt" -v -count 1 -p 1 -run "TestSelect_" .
go test -cover -coverprofile="coverage_driver_collection.txt" -v -count 1 -p 1 -run "TestStmt.*Collections?_(Exec|Query)" .
- name: Codecov
uses: codecov/codecov-action@v3
with:
flags: driver_select
name: driver_select
flags: driver_collection
name: driver_collection

testDriverStmtDocumentNonQuery:
name: Test driver document non-query statements
runs-on: windows-latest
steps:
- name: Set up Go env
uses: actions/setup-go@v2
with:
go-version: ^1.13
- name: Check out code into the Go module directory
uses: actions/checkout@v2
- name: Test
run: |
choco install azure-cosmosdb-emulator
& "C:\Program Files\Azure Cosmos DB Emulator\Microsoft.Azure.Cosmos.Emulator.exe" /DisableRateLimiting /NoUI /NoExplorer
Start-Sleep -s 60
try { Invoke-RestMethod -Method GET https://127.0.0.1:8081/ } catch {}
netstat -nt
$env:COSMOSDB_DRIVER='gocosmos'
$env:COSMOSDB_URL='AccountEndpoint=https://127.0.0.1:8081/;AccountKey=C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw=='
go test -cover -coverprofile="coverage_driver_document_non_query.txt" -v -count 1 -p 1 -run "TestStmt(Insert|Upsert|Update|Delete)_(Exec|Query)" .
- name: Codecov
uses: codecov/codecov-action@v3
with:
flags: driver_document_non_query
name: driver_document_non_query

testDriverStmtDocumentQuery:
name: Test driver document query statements
runs-on: windows-latest
steps:
- name: Set up Go env
uses: actions/setup-go@v2
with:
go-version: ^1.13
- name: Check out code into the Go module directory
uses: actions/checkout@v2
- name: Test
run: |
choco install azure-cosmosdb-emulator
& "C:\Program Files\Azure Cosmos DB Emulator\Microsoft.Azure.Cosmos.Emulator.exe" /DisableRateLimiting /NoUI /NoExplorer
Start-Sleep -s 60
try { Invoke-RestMethod -Method GET https://127.0.0.1:8081/ } catch {}
netstat -nt
$env:COSMOSDB_DRIVER='gocosmos'
$env:COSMOSDB_URL='AccountEndpoint=https://127.0.0.1:8081/;AccountKey=C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw=='
go test -timeout 30m -cover -coverprofile="coverage_driver_document_query.txt" -v -count 1 -p 1 -run "TestStmtSelect_(Exec|Query)" .
- name: Codecov
uses: codecov/codecov-action@v3
with:
flags: driver_document_query
name: driver_document_query

testRestClientNonQuery:
name: Test RestClient non-query
Expand Down Expand Up @@ -104,7 +175,7 @@ jobs:
netstat -nt
$env:COSMOSDB_DRIVER='gocosmos'
$env:COSMOSDB_URL='AccountEndpoint=https://127.0.0.1:8081/;AccountKey=C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw=='
go test -cover -coverprofile="coverage_restclient_query.txt" -v -count 1 -p 1 -run "TestRestClient_QueryDocuments" .
go test -timeout 30m -cover -coverprofile="coverage_restclient_query.txt" -v -count 1 -p 1 -run "TestRestClient_QueryDocuments" .
- name: Codecov
uses: codecov/codecov-action@v3
with:
Expand All @@ -113,7 +184,7 @@ jobs:

testOther:
name: Test other
runs-on: windows-latest
runs-on: ubuntu-latest
steps:
- name: Set up Go env
uses: actions/setup-go@v2
Expand All @@ -123,14 +194,8 @@ jobs:
uses: actions/checkout@v2
- name: Test
run: |
choco install azure-cosmosdb-emulator
& "C:\Program Files\Azure Cosmos DB Emulator\Microsoft.Azure.Cosmos.Emulator.exe" /DisableRateLimiting /NoUI /NoExplorer
Start-Sleep -s 60
try { Invoke-RestMethod -Method GET https://127.0.0.1:8081/ } catch {}
netstat -nt
$env:COSMOSDB_DRIVER='gocosmos'
$env:COSMOSDB_URL='AccountEndpoint=https://127.0.0.1:8081/;AccountKey=C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw=='
go test -cover -coverprofile="coverage_other.txt" -v -count 1 -p 1 -run "TestNew" .
export COSMOSDB_URL="AccountEndpoint=https://127.0.0.1:8081/;AccountKey=C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw=="
go test -cover -coverprofile="coverage_other.txt" -v -count 1 -p 1 -run "TestNew|TestStmt_NumInput|TestDriver_" .
- name: Codecov
uses: codecov/codecov-action@v3
with:
Expand Down
4 changes: 4 additions & 0 deletions RELEASE-NOTES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# gocosmos release notes

## 2023-06-xx - v0.2.1

- Bug fixes, Refactoring & Enhancements.

## 2023-03-14 - v0.2.0

- `RestClient`:
Expand Down
31 changes: 23 additions & 8 deletions conn.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gocosmos

import (
"context"
"database/sql/driver"
"errors"
"time"
Expand All @@ -10,29 +11,43 @@ var (
locGmt, _ = time.LoadLocation("GMT")
)

// Conn is Azure CosmosDB connection handle.
// Conn is Azure Cosmos DB implementation of driver.Conn.
type Conn struct {
restClient *RestClient // Azure CosmosDB REST API client.
restClient *RestClient // Azure Cosmos DB REST API client.
defaultDb string // default database used in Cosmos DB operations.
}

// Prepare implements driver.Conn.Prepare.
// Prepare implements driver.Conn/Prepare.
func (c *Conn) Prepare(query string) (driver.Stmt, error) {
return c.PrepareContext(context.Background(), query)
}

// PrepareContext implements driver.ConnPrepareContext/PrepareContext.
//
// @Available since v0.2.1
func (c *Conn) PrepareContext(_ context.Context, query string) (driver.Stmt, error) {
return parseQueryWithDefaultDb(c, c.defaultDb, query)
}

// Close implements driver.Conn.Close.
// Close implements driver.Conn/Close.
func (c *Conn) Close() error {
return nil
}

// Begin implements driver.Conn.Begin.
// Begin implements driver.Conn/Begin.
func (c *Conn) Begin() (driver.Tx, error) {
return c.BeginTx(context.Background(), driver.TxOptions{})
}

// BeginTx implements driver.ConnBeginTx/BeginTx.
//
// @Available since v0.2.1
func (c *Conn) BeginTx(_ context.Context, _ driver.TxOptions) (driver.Tx, error) {
return nil, errors.New("transaction is not supported")
}

// CheckNamedValue implements driver.NamedValueChecker.CheckNamedValue.
func (c *Conn) CheckNamedValue(value *driver.NamedValue) error {
// since CosmosDB is document db, it accepts any value types
// CheckNamedValue implements driver.NamedValueChecker/CheckNamedValue.
func (c *Conn) CheckNamedValue(_ *driver.NamedValue) error {
// since Cosmos DB is document db, it accepts any value types
return nil
}
17 changes: 11 additions & 6 deletions data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28475,7 +28475,7 @@ const _testDataVolcano = `

func _initDataNutrition(t *testing.T, testName string, client *RestClient, db, container string) {
dataListNutrition := make([]DocInfo, 0)
dataMapNutrition := make(map[string]DocInfo)
dataMapNutrition := sync.Map{}
err := json.Unmarshal([]byte(_testDataNutrition), &dataListNutrition)
if err != nil {
t.Fatalf("%s failed: %s", testName, err)
Expand All @@ -28493,16 +28493,16 @@ func _initDataNutrition(t *testing.T, testName string, client *RestClient, db, c
defer wg.Done()
for doc := range buff {
docId := doc["id"].(string)
dataMapNutrition[docId] = doc
dataMapNutrition.Store(docId, doc)
if result := client.CreateDocument(DocumentSpec{DbName: db, CollName: container, PartitionKeyValues: []interface{}{docId}, DocumentData: doc}); result.Error() != nil {
t.Fatalf("%s failed: (%#v) %s", testName, id, result.Error())
}
atomic.AddInt64(&numDocWritten, 1)
for {
now := time.Now()
d := now.Sub(start)
r := float64(numDocWritten) / (d.Seconds() + 0.01)
if r <= 123.0 {
r := float64(numDocWritten) / (d.Seconds() + 0.001)
if r <= 81.19 {
break
}
fmt.Printf("\t[DEBUG] too fast, slowing down...(Id: %d / NumDocs: %d / Dur: %.3f / Rate: %.3f)\n", id, numDocWritten, d.Seconds(), r)
Expand All @@ -28520,11 +28520,16 @@ func _initDataNutrition(t *testing.T, testName string, client *RestClient, db, c
{
now := time.Now()
d := now.Sub(start)
r := float64(numDocWritten) / (d.Seconds() + 0.01)
r := float64(numDocWritten) / (d.Seconds() + 0.001)
fmt.Printf("\t[DEBUG] Dur: %.3f / Rate: %.3f\n", d.Seconds(), r)
time.Sleep(1*time.Second + time.Duration(rand.Intn(1234))*time.Millisecond)
}
fmt.Printf("\tDataset: %#v / (checksum) Number of records: %#v\n", "Nutrition", len(dataMapNutrition))
count := 0
dataMapNutrition.Range(func(_, _ interface{}) bool {
count++
return true
})
fmt.Printf("\tDataset: %#v / (checksum) Number of records: %#v\n", "Nutrition", count)
}

func _initDataNutritionSmallRU(t *testing.T, testName string, client *RestClient, db, container string) {
Expand Down
25 changes: 23 additions & 2 deletions driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,34 @@ var (

// ErrConflict is returned when the executing operation cause conflict (e.g. duplicated id).
ErrConflict = errors.New("StatusCode=409 Conflict")

// ErrPreconditionFailure is returned when operation specified an eTag that is different from the version available
// at the server, that is, an optimistic concurrency error.
//
// @Available since v0.2.1
ErrPreconditionFailure = errors.New("StatusCode=412 Precondition failure")

// ErrOperationNotSupported is returned to indicate that the operation is not supported.
//
// @Available since v0.2.1
ErrOperationNotSupported = errors.New("this operation is not supported")

// ErrExecNotSupported is returned to indicate that the Exec/ExecContext operation is not supported.
//
// @Available since v0.2.1
ErrExecNotSupported = errors.New("this operation is not supported, please use Query")

// ErrQueryNotSupported is returned to indicate that the Query/QueryContext operation is not supported.
//
// @Available since v0.2.1
ErrQueryNotSupported = errors.New("this operation is not supported, please use Exec")
)

// Driver is Azure CosmosDB driver for database/sql.
// Driver is Azure Cosmos DB implementation of driver.Driver.
type Driver struct {
}

// Open implements driver.Driver.Open.
// Open implements driver.Driver/Open.
//
// connStr is expected in the following format:
//
Expand Down
89 changes: 89 additions & 0 deletions driver_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package gocosmos

import (
"context"
"database/sql"
"strings"
"testing"
)

func TestDriver_invalidConnectionString(t *testing.T) {
name := "TestDriver_invalidConnectionString"
driver := "gocosmos"
{
db, _ := sql.Open(driver, "AccountEndpoint;AccountKey=demo")
if err := db.Ping(); err == nil {
t.Fatalf("%s failed: should have error", name)
}
}
{
db, _ := sql.Open(driver, "AccountEndpoint=demo;AccountKey")
if err := db.Ping(); err == nil {
t.Fatalf("%s failed: should have error", name)
}
}
{
db, _ := sql.Open(driver, "AccountEndpoint=demo;AccountKey=demo/invalid_key")
if err := db.Ping(); err == nil {
t.Fatalf("%s failed: should have error", name)
}
}
}

func TestDriver_missingEndpoint(t *testing.T) {
name := "TestDriver_missingEndpoint"
driver := "gocosmos"
dsn := "AccountKey=demo"
db, _ := sql.Open(driver, dsn)
if err := db.Ping(); err == nil {
t.Fatalf("%s failed: should have error", name)
}
}

func TestDriver_missingAccountKey(t *testing.T) {
name := "TestDriver_missingAccountKey"
driver := "gocosmos"
dsn := "AccountEndpoint=demo"
db, _ := sql.Open(driver, dsn)
if err := db.Ping(); err == nil {
t.Fatalf("%s failed: should have error", name)
}
}

func TestDriver_Conn(t *testing.T) {
name := "TestDriver_Conn"
db := _openDb(t, name)
_, err := db.Conn(context.Background())
if err != nil {
t.Fatalf("%s failed: %s", name, err)
}
}

func TestDriver_Transaction(t *testing.T) {
name := "TestDriver_Transaction"
db := _openDb(t, name)
if tx, err := db.BeginTx(context.Background(), nil); tx != nil || err == nil {
t.Fatalf("%s failed: transaction is not supported yet", name)
} else if strings.Index(err.Error(), "not supported") < 0 {
t.Fatalf("%s failed: transaction is not supported yet / %s", name, err)
}
}

func TestDriver_Open(t *testing.T) {
name := "TestDriver_Open"
db := _openDb(t, name)
if err := db.Ping(); err != nil {
t.Fatalf("%s failed: %s", name, err)
}
}

func TestDriver_Close(t *testing.T) {
name := "TestDriver_Close"
db := _openDb(t, name)
if err := db.Ping(); err != nil {
t.Fatalf("%s failed: %s", name, err)
}
if err := db.Close(); err != nil {
t.Fatalf("%s failed: %s", name, err)
}
}
Loading

0 comments on commit 038911f

Please sign in to comment.