Skip to content

Commit

Permalink
feat: Add Print to client
Browse files Browse the repository at this point in the history
  • Loading branch information
rlch committed Aug 25, 2023
1 parent 399bb27 commit af31477
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 30 deletions.
2 changes: 2 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ type Updater[To any] interface {

// Runner allows the query to be executed.
type Runner interface {
Print() Runner

// Run executes the query, populating all the values bound within the query if
// their identifiers exist in the returning scope.
Run(ctx context.Context) error
Expand Down
10 changes: 10 additions & 0 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,16 @@ func ExampleClient_where() {
// ORDER BY name
}

func ExampleClient_print() {
c().
Match(db.Node("n")).
Return("n").
Print()
// Output:
// MATCH (n)
// RETURN n
}

func ExampleScope() {
var n any
c().
Expand Down
5 changes: 5 additions & 0 deletions client_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,11 @@ func (c *yielderImpl) Yield(identifiers ...any) client.Querier {
return c.newQuerier(c.cy.Yield(identifiers...))
}

func (c *runnerImpl) Print() client.Runner {
c.cy.Print()
return c
}

func (c *runnerImpl) Run(ctx context.Context) (err error) {
cy, err := c.cy.Compile()
if err != nil {
Expand Down
58 changes: 28 additions & 30 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ func TestUnmarshalResult(t *testing.T) {
})
}

func TestRunnerImpl(t *testing.T) {
func TestStreamImpl(t *testing.T) {
if testing.Short() {
return
}
Expand All @@ -282,37 +282,35 @@ func TestRunnerImpl(t *testing.T) {
}
})

t.Run("Stream", func(t *testing.T) {
t.Run("should fail when invalid parameters passed", func(t *testing.T) {
var nums []chan int
err := d.Exec().Unwind(db.NamedParam(nums, "nums"), "i").
Return(db.Qual(&nums, "i")).
Stream(ctx, func(r client.Result) error {
return nil
})
assert.Error(t, err)
})
t.Run("should fail when invalid parameters passed", func(t *testing.T) {
var nums []chan int
err := d.Exec().Unwind(db.NamedParam(nums, "nums"), "i").
Return(db.Qual(&nums, "i")).
Stream(ctx, func(r client.Result) error {
return nil
})
assert.Error(t, err)
})

t.Run("should stream when valid query", func(t *testing.T) {
expectedOut := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
var num int
err := d.Exec().
Unwind("range(0, 10)", "i").
Return(db.Qual(&num, "i")).
Stream(ctx, func(r client.Result) error {
n := 0
for r.Next(ctx) {
if err := r.Read(); err != nil {
return err
}
assert.Equal(t, expectedOut[n], num)
n++
t.Run("should stream when valid query", func(t *testing.T) {
expectedOut := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
var num int
err := d.Exec().
Unwind("range(0, 10)", "i").
Return(db.Qual(&num, "i")).
Stream(ctx, func(r client.Result) error {
n := 0
for r.Next(ctx) {
if err := r.Read(); err != nil {
return err
}
assert.Equal(t, len(expectedOut), n)
return nil
})
assert.NoError(t, err)
})
assert.Equal(t, expectedOut[n], num)
n++
}
assert.Equal(t, len(expectedOut), n)
return nil
})
assert.NoError(t, err)
})
}

Expand Down

0 comments on commit af31477

Please sign in to comment.