Skip to content

Commit

Permalink
add for update test for 19c and 23c
Browse files Browse the repository at this point in the history
  • Loading branch information
asmeikal committed Jun 13, 2024
1 parent bd32b2d commit 7773551
Show file tree
Hide file tree
Showing 5 changed files with 201 additions and 6 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: test

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:

test:
name: test
runs-on: ubuntu-latest
container:
image: ghcr.io/moveaxlab/oracle-devcontainer:latest
steps:
- name: checkout
uses: actions/checkout@v4
- name: get dependencies
run: go mod download
- name: Test
continue-on-error: true
run: go test -v ./...
2 changes: 1 addition & 1 deletion .mise.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[tools]
go = "1.22"
go = "1.21"
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
# GORM Oracle Driver

![](https://starchart.cc/CengSin/oracle.svg)

## Description

GORM Oracle driver for connect Oracle DB and Manage Oracle DB, Based on [stevefan1999-personal/gorm-driver-oracle](https://github.com/stevefan1999-personal/gorm-driver-oracle)
,not recommended for use in a production environment
GORM Oracle driver for connect Oracle DB and Manage Oracle DB,
forked from [CengSin/oracle](https://github.com/CengSin/oracle).

## Required dependency Install

Expand All @@ -14,10 +12,13 @@ GORM Oracle driver for connect Oracle DB and Manage Oracle DB, Based on [stevefa
- see [ODPI-C Installation.](https://oracle.github.io/odpi/doc/installation.html)

## Quick Start

### how to install

```bash
go get github.com/cengsin/oracle
```

### usage

```go
Expand Down
6 changes: 5 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
module github.com/cengsin/oracle

go 1.22
go 1.21

require (
github.com/emirpasic/gods v1.18.1
github.com/godror/godror v0.42.1
github.com/stretchr/testify v1.9.0
github.com/thoas/go-funk v0.9.3
gorm.io/gorm v1.25.9
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-logfmt/logfmt v0.6.0 // indirect
github.com/godror/knownpb v0.1.1 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/exp v0.0.0-20240318143956-a85f2c67cd81 // indirect
google.golang.org/protobuf v1.33.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
167 changes: 167 additions & 0 deletions oracle_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
package oracle

import (
"testing"

"github.com/stretchr/testify/suite"
"gorm.io/gorm"
"gorm.io/gorm/clause"
"gorm.io/gorm/logger"
)

type Model struct {
Id int `gorm:"column:ID"`
Name string `gorm:"column:NAME"`
}

func (Model) TableName() string {
return "TEST_TABLE"
}

type OracleSuite struct {
suite.Suite

db *gorm.DB
}

func (s *OracleSuite) TearDownTest() {
err := s.db.Exec("DELETE FROM TEST_TABLE WHERE 1 = 1").Error
s.Require().Nil(err)
}

func (s *OracleSuite) TearDownSuite() {
err := s.db.Exec("DROP TABLE TEST_TABLE").Error
s.Require().Nil(err)
}

type Oracle19Suite struct {
OracleSuite
}

func (s *Oracle19Suite) SetupSuite() {
url := BuildUrl("oracle-19c", 1521, "MYATP", "ADMIN", "TVDGXvpzQat8", map[string]string{
"CONNECTION TIMEOUT": "5",
"LANGUAGE": "ITALIAN",
"TERRITORY": "ITALY",
"SSL": "false",
})

dialector := New(Config{DSN: url})

var err error

s.db, err = gorm.Open(dialector, &gorm.Config{
SkipDefaultTransaction: true,
PrepareStmt: false,
Logger: logger.Default.LogMode(logger.Info),
})
s.Require().Nil(err)

err = s.db.Exec("CREATE TABLE TEST_TABLE (ID INT PRIMARY KEY, NAME VARCHAR2(64 CHAR) NOT NULL)").Error
s.Require().Nil(err)
}

type Oracle23Suite struct {
OracleSuite
}

func (s *Oracle23Suite) SetupSuite() {
url := BuildUrl("oracle-23c", 1521, "FREE", "SYSTEM", "password", map[string]string{
"CONNECTION TIMEOUT": "5",
"LANGUAGE": "ITALIAN",
"TERRITORY": "ITALY",
"SSL": "false",
})

dialector := New(Config{DSN: url})

var err error

logger.Default.LogMode(logger.Info)

s.db, err = gorm.Open(dialector, &gorm.Config{
SkipDefaultTransaction: true,
PrepareStmt: false,
Logger: logger.Default.LogMode(logger.Info),
})
s.Require().Nil(err)

err = s.db.Exec("CREATE TABLE TEST_TABLE (ID INT PRIMARY KEY, NAME VARCHAR2(64 CHAR) NOT NULL)").Error
s.Require().Nil(err)
}

func TestOracle19c(t *testing.T) {
suite.Run(t, new(Oracle19Suite))
}

func TestOracle23c(t *testing.T) {
suite.Run(t, new(Oracle23Suite))
}

func (s *OracleSuite) TestEntityFound() {
var err error

err = s.db.Create(&Model{
Id: 1,
Name: "test",
}).Error
s.Require().Nil(err)

var m Model

err = s.db.
Where("id = ?", 1).
First(&m).
Error

s.Require().Nil(err)
}

func (s *OracleSuite) TestEntityNotFound() {
var err error

var m Model

err = s.db.
Where("id = ?", 2).
First(&m).
Error

s.Require().ErrorIs(err, gorm.ErrRecordNotFound)
}

func (s *OracleSuite) TestEntityFoundForUpdate() {
var err error

err = s.db.Create(&Model{
Id: 1,
Name: "test",
}).Error
s.Require().Nil(err)

var m Model

err = s.db.Transaction(func(tx *gorm.DB) error {
return tx.
Clauses(clause.Locking{Strength: "UPDATE"}).
Where("id = ?", 1).
First(&m).
Error
})
s.Require().Nil(err)
}

func (s *OracleSuite) TestEntityNotFoundForUpdate() {
var err error

var m Model

err = s.db.Transaction(func(tx *gorm.DB) error {
return tx.
Clauses(clause.Locking{Strength: "UPDATE"}).
Where("id = ?", 2).
First(&m).
Error
})
s.Require().ErrorIs(err, gorm.ErrRecordNotFound)
}

0 comments on commit 7773551

Please sign in to comment.