-
Notifications
You must be signed in to change notification settings - Fork 0
/
memory.go
85 lines (68 loc) · 1.95 KB
/
memory.go
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
package sqltesting
import (
"context"
"database/sql"
"io"
"log"
"testing"
"time"
"github.com/hugocorbucci/multitest-go-example/internal/storage"
// Import sqlite only for memory usage
_ "github.com/mattn/go-sqlite3"
)
var _ storage.Repository = storage.NewSQLStore(nil)
// MemoryStore is a SQLite in-memory representation of a Repository.
type MemoryStore struct {
DB *sql.DB
Store storage.Repository
}
// NewMemoryStore returns a sqlite "in-memory" repository for local tests.
func NewMemoryStore(ctx context.Context, l *log.Logger) (*MemoryStore, error) {
db, err := NewSQLiteDB(ctx, l)
if err != nil {
return nil, err
}
return &MemoryStore{
DB: db,
Store: storage.NewSQLStore(db),
}, nil
}
// NewSQLiteDB creates a SQLite DB with the default structure
func NewSQLiteDB(ctx context.Context, l *log.Logger) (*sql.DB, error) {
db, err := sql.Open("sqlite3", "file::memory:?mode=memory&cache=shared")
if err != nil {
return nil, err
}
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
_, err = db.ExecContext(ctx, MemoryStructure)
if err != nil {
return nil, err
}
return db, nil
}
type testingLogWriter struct {
t *testing.T
}
var _ io.Writer = &testingLogWriter{}
func (w *testingLogWriter) Write(b []byte) (int, error) {
w.t.Log(string(b))
return len(b), nil
}
// NewTestingLog creates a logger that outputs to the testing framework
func NewTestingLog(t *testing.T) *log.Logger {
return log.New(&testingLogWriter{t}, "", 0)
}
// MemoryStructure is the database structure for a the sqlite "memory" database.
const MemoryStructure = `
PRAGMA synchronous = OFF;
PRAGMA journal_mode = OFF;
CREATE TABLE IF NOT EXISTS url_mapping (
id integer NOT NULL PRIMARY KEY AUTOINCREMENT,
url VARCHAR(255) NOT NULL,
short_url VARCHAR(12) NOT NULL,
updated_at DEFAULT CURRENT_TIMESTAMP,
created_at DEFAULT CURRENT_TIMESTAMP
);
CREATE UNIQUE INDEX IF NOT EXISTS "idx_url_mapping_short_url" ON "url_mapping" (short_url);
`