Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: resultx ut #738

Merged
merged 1 commit into from
Aug 24, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 104 additions & 0 deletions pkg/resultx/resultx_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package resultx
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

加点异常case吧,比如有 ds 的时候,也有 InsertID 应该是什么返回。

Copy link
Contributor Author

@baerwang baerwang Aug 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个代码没法加异常呀,这个确定不了...,对这个不太懂,我理解的是这个覆盖率能达到就好


import (
"testing"
)

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

import (
"github.com/arana-db/arana/pkg/proto"
)

func TestEmptyResult(t *testing.T) {
res := New()
defer Drain(res)
assert.Empty(t, res)
dataset, err := res.Dataset()
assert.NoError(t, err)
assert.Empty(t, dataset)
affected, err := res.RowsAffected()
assert.NoError(t, err)
assert.Equal(t, affected, uint64(0))
id, err := res.LastInsertId()
assert.NoError(t, err)
assert.Equal(t, id, uint64(0))
}

func TestSlimResult(t *testing.T) {
res := New(WithLastInsertID(1))
defer Drain(res)
assert.NotEmpty(t, res)
dataset, err := res.Dataset()
assert.NoError(t, err)
assert.Empty(t, dataset)
affected, err := res.RowsAffected()
assert.NoError(t, err)
assert.Equal(t, affected, uint64(0))
id, err := res.LastInsertId()
assert.NoError(t, err)
assert.Equal(t, id, uint64(1))
}

Comment on lines +32 to +61
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

方便的话也加点注释吧,其实 Empty 和 Slim 是 Exec 的时候执行的,Empty 更像创表,等行为,Slim 像 insert,update等行为。

func TestFullResult(t *testing.T) {
res := New(WithDataset(&utDataset{}), WithRowsAffected(1))
defer Drain(res)
assert.NotEmpty(t, res)
dataset, err := res.Dataset()
assert.NoError(t, err)
assert.Empty(t, dataset)
affected, err := res.RowsAffected()
assert.NoError(t, err)
assert.Equal(t, affected, uint64(1))
id, err := res.LastInsertId()
assert.NoError(t, err)
assert.Equal(t, id, uint64(0))
}

func TestDsResult(t *testing.T) {
res := New(WithDataset(&utDataset{}))
defer Drain(res)
assert.NotEmpty(t, res)
dataset, err := res.Dataset()
assert.NoError(t, err)
assert.Empty(t, dataset)
affected, err := res.RowsAffected()
assert.NoError(t, err)
assert.Equal(t, affected, uint64(0))
id, err := res.LastInsertId()
assert.NoError(t, err)
assert.Equal(t, id, uint64(0))
}

type utDataset struct{}

func (cu *utDataset) Close() error {
return nil
}

func (cu *utDataset) Fields() ([]proto.Field, error) {
return nil, nil
}

func (cu *utDataset) Next() (proto.Row, error) {
return nil, nil
}
Loading