Code Monkey home page Code Monkey logo

go-twowaysql's People

Contributors

future-taga avatar gmidorii avatar hayaor avatar ma91n avatar modockey avatar shibukawa avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

go-twowaysql's Issues

Add sql comment support

If SQL comments are included, the execution is returned with an error or empty.
I would like to be able to include comments.

When "no parameter that matches the bind value" then show hint log for developper

Scenario

Log error.

{s: "no parameter that matches the bind value: role"}}

Bind params.

	param := struct {
		UserID   any    `twowaysql:"user_id"`
		role       []any  `twowaysql:"role"`
	}{
		UserID:                   loginId,
		role:                      role,
	}

Because role fields is private then twowaysql shows error message.
At this time, can you issue a help message saying "role may be private"?

db_sql_error: (anonymous) when line breaks and spaces exist

An error occurs when line breaks and spaces exist when writing JS source in SQL

error contents

"error":"system_error: db_sql_error: (anonymous)

Error example
error for line breaks and spaces
xxx.sql

/*
    IF 1 == 1
*/

Not Error exapmle
no error for line breaks and tab spaces
xxx.sql

/*
	IF 1 == 1
*/

update panic recover in Twowaysql.Transaction.

Improved recover and automatic rollback in the panic

func (t *Twowaysql) Transaction(ctx context.Context, fn func(tx TwowaysqlTx) error) error {
tx, err := t.Begin(ctx)
if err != nil {
return err
}
if err := fn(*tx); err != nil {
if rerr := tx.Rollback(); rerr != nil {
return fmt.Errorf("failed rollback %v: %w", rerr, err)
}
return err
}
if err := tx.Commit(); err != nil {
return err
}
return nil
}

テスト実行のDesign Document

SQLテストランナー

コンテキストとスコープ

アプリケーションコードを書かずに、SQLだけで高速にテスト駆動開発を行い、品質の高いSQLを書く環境を用意する。

Markdownに書かれたテストケースを読み込んで、テストを実行する。

また、サブの機能としてtwowaysql.RunTestメソッドを提供し、ユニットテストに組み込めるようにする。

ゴール

  • プロジェクトで使用するSQL類を一括でテストする
  • テストケースはSQLを書いたMarkdownで記述する
    • テストケースには、テストデータ、パラメータ、結果を含む

ゴール外

  • カバレッジ計測は将来的にはやりたいがコアへの変更が必要なのでこのDesign Docの対象外とする
  • 実行時にシーケンシャルスキャンがあったらエラーにするなど

実際のデザイン

Markdownに書かれたテストケースの抽出を行い、実行をする。

Markdownはセクションタイトルのルールを定めて、テストケースの情報を記述する。

実行時はまずはトランザクションでくくって、テスト終了後にDBを戻せるようにする。事前に投入するデータがあればそれを入れる。その後対象のクエリーを実行し、テストケースに書かれた検証手法(レスポンス、あるいはDBの内容、外部のファイルなどのチェック)で検証する。最後にロールバックする

テストデータは以下の形式をサポート

  • YAML
  • SQL

パラメータと期待する結果はYAML形式をサポート

また、アプリケーションのテストコードに追加可能な関数も提供する。

sqltest.RunTest(t, db, "testdata/sqlr")

システムコンテキスト図

graph TD;

Markdown -->|読み込み| テストランナー
テストランナー --> テスト結果
Loading

コードと疑似コード

以下はMarkdownのサンプル。

# タイトル

```sql
SELECT email, name FROM users WHERE name=/*name*/'bob';
```

## Test

テストの事前データはYAMLでコンパクトに書いて各テーブルにinsertする想定。名前に(reset)がついていたら、一度テーブルの中をすべて空にしてからinsert。Testセクション以下は全テストの前で毎回実行されるもの(結果は毎回rollback)

```yaml
fixtures:
  - users
    - [employee_no, dept_no, first_name, last_name, email]
	- [1, 10, Evan, MacMans, [email protected]]
    - [2, 11, Malvin, FitzSimons, [email protected]]
    - [3, 12, Jimmie, Bruce, [email protected]]
```

あるいはSQLを書いても良い。これもtx.Execで実行される。

```sql
INSERT INTO persons(employee_no, dept_no, first_name, last_name, email, created_at) VALUES
			(1, 10, 'Evan', 'MacMans', '[email protected]', CURRENT_TIMESTAMP),
			(2, 11, 'Malvina', 'FitzSimons', '[email protected]', CURRENT_TIMESTAMP),
			(3, 12, 'Jimmie', 'Bruce', '[email protected]', CURRENT_TIMESTAMP)
			;
```

### Case: query test with alice

各テストケースではYAMLを書く

fixture/fixturesは全体のfixturesに追加して入れられる。
param/paramsは実行パラメータ
結果は以下のキーで記述する

- result: tx.Selectで実行。結果は順番通りの比較

```yaml
fixtures:
  - users
	- ["", "", ""]
params: { name: "Alice" }
result:
  - { email: [email protected], name: Alice }
```

### Case: exec test

deleteやupdate、insertの場合はその後の検証用クエリーをtestQueryに書く。

```yaml
fixtures:
  - users
	- ["", "", ""]
params: { name: "Alice" }
testQuery: "select count(id) as count  from users"
result:
  - { count: 1 }
```

制約の度合い

  • GitHubなどのMarkdownで記述でき、複雑な記法とならない程度のMarkdownの書き方にする必要がある

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.