Skip to content

Commit 84706fb

Browse files
Adds testable examples to be automatically pulled in redis.io docs (#2601)
* Adding examples * Update readme * Update Readme * Remove unneeded lines. Added an examples.json file * Update readme for examples * More fixes * Add example tags * Update examples.json * Rename * Add another hide block * Temporary test * Add example id for lpush and lrange * Update readme * Update output text * Improve examples * Move examples test dir to doctests * Add redis v7's ExpireAtNX, ExpireAtXX, ExpireAtGT, ExpireAtLT, PExpireNX, PExpireXX, PExpireGT, PExpireLT, PExpireAtNX, PExpireAtXX, PExpireAtGT, PExpireAtLT feat: Add redis v7's NX, XX, GT, LT expireat, pexpire, pexpireat variants * add tests for new commands add tests to coverage for the new commands * Adds github workflow to add docexamples tests. Flushes db before every test. * Fixes broken workflow file * Adds Igor’s suggestion of keeping the instructions for docexamples in one place * Removes unneeded “Missing” section, because it was solved as a workflow * Revert "add tests for new commands" This reverts commit af12cb6. * Fixes review comments * Specifies versions as strings instead of floats --------- Co-authored-by: carner <[email protected]>
1 parent dc8c93b commit 84706fb

File tree

5 files changed

+158
-0
lines changed

5 files changed

+158
-0
lines changed

.github/workflows/doctests.yaml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
name: Documentation Tests
2+
3+
on:
4+
push:
5+
branches: [master, examples]
6+
pull_request:
7+
branches: [master, examples]
8+
9+
permissions:
10+
contents: read
11+
12+
jobs:
13+
doctests:
14+
name: doctests
15+
runs-on: ubuntu-latest
16+
17+
services:
18+
redis-stack:
19+
image: redis/redis-stack-server:latest
20+
options: >-
21+
--health-cmd "redis-cli ping" --health-interval 10s --health-timeout 5s --health-retries 5
22+
ports:
23+
- 6379:6379
24+
25+
strategy:
26+
fail-fast: false
27+
matrix:
28+
go-version: [ "1.18", "1.19", "1.20" ]
29+
30+
steps:
31+
- name: Set up ${{ matrix.go-version }}
32+
uses: actions/setup-go@v4
33+
with:
34+
go-version: ${{ matrix.go-version }}
35+
36+
- name: Checkout code
37+
uses: actions/checkout@v3
38+
39+
- name: Test doc examples
40+
working-directory: ./doctests
41+
run: go test

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
*.rdb
22
testdata/*
33
.idea/
4+
.DS_Store

doctests/README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Command examples for redis.io
2+
3+
These examples appear on the [Redis documentation](https://redis.io) site as part of the tabbed examples interface.
4+
5+
## How to add examples
6+
7+
- Create a Go test file with a meaningful name in the current folder.
8+
- Create a single method prefixed with `Example` and write your test in it.
9+
- Determine the id for the example you're creating and add it as the first line of the file: `// EXAMPLE: set_and_get`.
10+
- We're using the [Testable Examples](https://go.dev/blog/examples) feature of Go to test the desired output has been written to stdout.
11+
12+
### Special markup
13+
14+
See https://github.com/redis-stack/redis-stack-website#readme for more details.
15+
16+
## How to test the examples
17+
18+
- Start a Redis server locally on port 6379
19+
- CD into the `doctests` directory
20+
- Run `go test` to test all examples in the directory.
21+
- Run `go test filename.go` to test a single file
22+

doctests/lpush_lrange_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// EXAMPLE: lpush_and_lrange
2+
// HIDE_START
3+
package example_commands_test
4+
5+
import (
6+
"context"
7+
"fmt"
8+
"github.com/redis/go-redis/v9"
9+
)
10+
11+
func ExampleLPushLRange() {
12+
ctx := context.Background()
13+
14+
rdb := redis.NewClient(&redis.Options{
15+
Addr: "localhost:6379",
16+
Password: "", // no password docs
17+
DB: 0, // use default DB
18+
})
19+
20+
// HIDE_END
21+
22+
// REMOVE_START
23+
errFlush := rdb.FlushDB(ctx).Err() // Clear the database before each test
24+
if errFlush != nil {
25+
panic(errFlush)
26+
}
27+
// REMOVE_END
28+
29+
listSize, err := rdb.LPush(ctx, "my_bikes", "bike:1", "bike:2").Result()
30+
if err != nil {
31+
panic(err)
32+
}
33+
34+
fmt.Println(listSize)
35+
36+
value, err := rdb.LRange(ctx, "my_bikes", 0, -1).Result()
37+
if err != nil {
38+
panic(err)
39+
}
40+
fmt.Println(value)
41+
// HIDE_START
42+
43+
// Output: 2
44+
// [bike:2 bike:1]
45+
}
46+
47+
// HIDE_END

doctests/set_get_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// EXAMPLE: set_and_get
2+
// HIDE_START
3+
package example_commands_test
4+
5+
import (
6+
"context"
7+
"fmt"
8+
"github.com/redis/go-redis/v9"
9+
)
10+
11+
func ExampleSetGet() {
12+
ctx := context.Background()
13+
14+
rdb := redis.NewClient(&redis.Options{
15+
Addr: "localhost:6379",
16+
Password: "", // no password docs
17+
DB: 0, // use default DB
18+
})
19+
20+
// HIDE_END
21+
22+
// REMOVE_START
23+
errFlush := rdb.FlushDB(ctx).Err() // Clear the database before each test
24+
if errFlush != nil {
25+
panic(errFlush)
26+
}
27+
// REMOVE_END
28+
29+
err := rdb.Set(ctx, "bike:1", "Process 134", 0).Err()
30+
if err != nil {
31+
panic(err)
32+
}
33+
34+
fmt.Println("OK")
35+
36+
value, err := rdb.Get(ctx, "bike:1").Result()
37+
if err != nil {
38+
panic(err)
39+
}
40+
fmt.Printf("The name of the bike is %s", value)
41+
// HIDE_START
42+
43+
// Output: OK
44+
// The name of the bike is Process 134
45+
}
46+
47+
// HIDE_END

0 commit comments

Comments
 (0)