This file is indexed.

/usr/share/gocode/src/github.com/erikstmartin/go-testdb/examples_test.go is in golang-github-erikstmartin-go-testdb-dev 0.0~git20160219.0.8d10e4a-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package testdb

import (
	"database/sql"
	"database/sql/driver"
	"errors"
	"fmt"
	"strconv"
)

type user struct {
	id      int64
	name    string
	age     int64
	created string
	data    string
}

func ExampleSetOpenFunc() {
	defer Reset()

	SetOpenFunc(func(dsn string) (driver.Conn, error) {
		// Conn() will return the same internal driver.Conn being used by the driver
		return Conn(), errors.New("test error")
	})

	// err only returns from this if it's an unknown driver, we are stubbing opening a connection
	db, _ := sql.Open("testdb", "foo")
	_, err := db.Driver().Open("foo")

	if err != nil {
		fmt.Println("Stubbed error returned as expected: " + err.Error())
	}

	// Output:
	// Stubbed error returned as expected: test error
}

func ExampleRowsFromCSVString() {
	columns := []string{"id", "name", "age", "created"}
	result := `
  1,tim,20,2012-10-01 01:00:01
  2,joe,25,2012-10-02 02:00:02
  3,bob,30,2012-10-03 03:00:03
  `
	rows := RowsFromCSVString(columns, result)

	fmt.Println(rows.Columns())

	// Output:
	// [id name age created]
}

func ExampleStubQuery() {
	defer Reset()

	db, _ := sql.Open("testdb", "")

	sql := "select id, name, age from users"
	columns := []string{"id", "name", "age", "created"}
	result := `
  1,tim,20,2012-10-01 01:00:01
  2,joe,25,2012-10-02 02:00:02
  3,bob,30,2012-10-03 03:00:03
  `
	StubQuery(sql, RowsFromCSVString(columns, result))

	res, _ := db.Query(sql)

	for res.Next() {
		var u = new(user)
		res.Scan(&u.id, &u.name, &u.age, &u.created)

		fmt.Println(u.name + " - " + strconv.FormatInt(u.age, 10))
	}

	// Output:
	// tim - 20
	// joe - 25
	// bob - 30
}

func ExampleStubQuery_queryRow() {
	defer Reset()

	db, _ := sql.Open("testdb", "")

	sql := "select id, name, age from users"
	columns := []string{"id", "name", "age", "created"}
	result := `
  1,tim,20,2012-10-01 01:00:01
  `
	StubQuery(sql, RowsFromCSVString(columns, result))

	row := db.QueryRow(sql)

	u := new(user)
	row.Scan(&u.id, &u.name, &u.age, &u.created)

	fmt.Println(u.name + " - " + strconv.FormatInt(u.age, 10))

	// Output:
	// tim - 20
}

func ExampleStubQueryError() {
	defer Reset()

	db, _ := sql.Open("testdb", "")

	sql := "select count(*) from error"

	StubQueryError(sql, errors.New("test error"))

	_, err := db.Query(sql)

	if err != nil {
		fmt.Println("Error returned: " + err.Error())
	}

	// Output:
	// Error returned: test error
}

func ExampleSetQueryFunc() {
	defer Reset()

	columns := []string{"id", "name", "age", "created"}
	rows := "1,tim,20,2012-10-01 01:00:01\n2,joe,25,2012-10-02 02:00:02\n3,bob,30,2012-10-03 03:00:03"

	SetQueryFunc(func(query string) (result driver.Rows, err error) {
		return RowsFromCSVString(columns, rows), nil
	})

	db, _ := sql.Open("testdb", "")

	res, _ := db.Query("SELECT foo FROM bar")

	for res.Next() {
		var u = new(user)
		res.Scan(&u.id, &u.name, &u.age, &u.created)

		fmt.Println(u.name + " - " + strconv.FormatInt(u.age, 10))
	}

	// Output:
	// tim - 20
	// joe - 25
	// bob - 30
}

func ExampleSetQueryFunc_queryRow() {
	defer Reset()

	columns := []string{"id", "name", "age", "created"}
	rows := "1,tim,20,2012-10-01 01:00:01"

	SetQueryFunc(func(query string) (result driver.Rows, err error) {
		return RowsFromCSVString(columns, rows), nil
	})

	db, _ := sql.Open("testdb", "")

	row := db.QueryRow("SELECT foo FROM bar")

	var u = new(user)
	row.Scan(&u.id, &u.name, &u.age, &u.created)

	fmt.Println(u.name + " - " + strconv.FormatInt(u.age, 10))

	// Output:
	// tim - 20
}

func ExampleSetQueryWithArgsFunc() {
	defer Reset()

	SetQueryWithArgsFunc(func(query string, args []driver.Value) (result driver.Rows, err error) {
		columns := []string{"id", "name", "age", "created"}

		rows := ""
		if args[0] == "joe" {
			rows = "2,joe,25,2012-10-02 02:00:02"
		}
		return RowsFromCSVString(columns, rows), nil
	})

	db, _ := sql.Open("testdb", "")

	res, _ := db.Query("SELECT foo FROM bar WHERE name = $1", "joe")

	for res.Next() {
		var u = new(user)
		res.Scan(&u.id, &u.name, &u.age, &u.created)

		fmt.Println(u.name + " - " + strconv.FormatInt(u.age, 10))
	}

	// Output:
	// joe - 25
}

type testResult struct {
	lastId       int64
	affectedRows int64
}

func (r testResult) LastInsertId() (int64, error) {
	return r.lastId, nil
}

func (r testResult) RowsAffected() (int64, error) {
	return r.affectedRows, nil
}

func ExampleSetExecWithArgsFunc() {
	defer Reset()

	SetExecWithArgsFunc(func(query string, args []driver.Value) (result driver.Result, err error) {
		if args[0] == "joe" {
			return testResult{1, 1}, nil
		}
		return testResult{1, 0}, nil
	})

	db, _ := sql.Open("testdb", "")

	res, _ := db.Exec("UPDATE bar SET name = 'foo' WHERE name = ?", "joe")

	rowsAffected, _ := res.RowsAffected()
	fmt.Println("RowsAffected =", rowsAffected)

	// Output:
	// RowsAffected = 1
}

func ExampleSetBeginFunc() {
	defer Reset()

	commitCalled := false
	rollbackCalled := false
	SetBeginFunc(func() (txn driver.Tx, err error) {
		t := &Tx{}
		t.SetCommitFunc(func() error {
			commitCalled = true
			return nil
		})
		t.SetRollbackFunc(func() error {
			rollbackCalled = true
			return nil
		})
		return t, nil
	})

	db, _ := sql.Open("testdb", "")
	tx, _ := db.Begin()
	tx.Commit()

	fmt.Println("CommitCalled =", commitCalled)
	fmt.Println("RollbackCalled =", rollbackCalled)

	// Output:
	// CommitCalled = true
	// RollbackCalled = false
}

func ExampleSetCommitFunc() {
	defer Reset()

	SetCommitFunc(func() error {
		return errors.New("commit failed")
	})

	db, _ := sql.Open("testdb", "")
	tx, _ := db.Begin()

	fmt.Println("CommitResult =", tx.Commit())

	// Output:
	// CommitResult = commit failed
}

func ExampleSetRollbackFunc() {
	defer Reset()

	SetRollbackFunc(func() error {
		return errors.New("rollback failed")
	})

	db, _ := sql.Open("testdb", "")
	tx, _ := db.Begin()

	fmt.Println("RollbackResult =", tx.Rollback())

	// Output:
	// RollbackResult = rollback failed
}