mirror of
https://github.com/aykhans/slash-e.git
synced 2025-06-15 04:17:50 +00:00
feat: add user store tests
This commit is contained in:
@ -19,9 +19,6 @@ import (
|
||||
//go:embed migration
|
||||
var migrationFS embed.FS
|
||||
|
||||
//go:embed seed
|
||||
var seedFS embed.FS
|
||||
|
||||
type DB struct {
|
||||
profile *profile.Profile
|
||||
// sqlite db connection instance
|
||||
@ -122,9 +119,6 @@ func (db *DB) Open(ctx context.Context) (err error) {
|
||||
if err := db.applyLatestSchema(ctx); err != nil {
|
||||
return fmt.Errorf("failed to apply latest schema: %w", err)
|
||||
}
|
||||
if err := db.seed(ctx); err != nil {
|
||||
return fmt.Errorf("failed to seed: %w", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -191,28 +185,6 @@ func (db *DB) applyMigrationForMinorVersion(ctx context.Context, minorVersion st
|
||||
return tx.Commit()
|
||||
}
|
||||
|
||||
func (db *DB) seed(ctx context.Context) error {
|
||||
filenames, err := fs.Glob(seedFS, fmt.Sprintf("%s/*.sql", "seed"))
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to read seed files, err: %w", err)
|
||||
}
|
||||
|
||||
sort.Strings(filenames)
|
||||
|
||||
// Loop over all seed files and execute them in order.
|
||||
for _, filename := range filenames {
|
||||
buf, err := seedFS.ReadFile(filename)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to read seed file, filename=%s err=%w", filename, err)
|
||||
}
|
||||
stmt := string(buf)
|
||||
if err := db.execute(ctx, stmt); err != nil {
|
||||
return fmt.Errorf("seed error: statement:%s err=%w", stmt, err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// execute runs a single SQL statement within a transaction.
|
||||
func (db *DB) execute(ctx context.Context, stmt string) error {
|
||||
tx, err := db.DBInstance.Begin()
|
||||
|
@ -1,11 +0,0 @@
|
||||
DELETE FROM shortcut;
|
||||
|
||||
DELETE FROM workspace_user;
|
||||
|
||||
DELETE FROM user_setting;
|
||||
|
||||
DELETE FROM user;
|
||||
|
||||
DELETE FROM workspace_setting;
|
||||
|
||||
DELETE FROM workspace;
|
@ -1,35 +0,0 @@
|
||||
INSERT INTO
|
||||
user (
|
||||
`id`,
|
||||
`username`,
|
||||
`nickname`,
|
||||
`email`,
|
||||
`password_hash`
|
||||
)
|
||||
VALUES
|
||||
(
|
||||
11,
|
||||
'frank',
|
||||
'Frank',
|
||||
'frank@shortify.demo',
|
||||
-- raw password: secret
|
||||
'$2a$14$ajq8Q7fbtFRQvXpdCq7Jcuy.Rx1h/L4J60Otx.gyNLbAYctGMJ9tK'
|
||||
);
|
||||
|
||||
INSERT INTO
|
||||
user (
|
||||
`id`,
|
||||
`username`,
|
||||
`nickname`,
|
||||
`email`,
|
||||
`password_hash`
|
||||
)
|
||||
VALUES
|
||||
(
|
||||
12,
|
||||
'bob',
|
||||
'Bob',
|
||||
'bob@shortify.demo',
|
||||
-- raw password: secret
|
||||
'$2a$14$ajq8Q7fbtFRQvXpdCq7Jcuy.Rx1h/L4J60Otx.gyNLbAYctGMJ9tK'
|
||||
);
|
@ -1,14 +0,0 @@
|
||||
INSERT INTO
|
||||
workspace (
|
||||
`id`,
|
||||
`name`,
|
||||
`title`,
|
||||
`description`
|
||||
)
|
||||
VALUES
|
||||
(
|
||||
1,
|
||||
'minecraft',
|
||||
'minecraft',
|
||||
''
|
||||
);
|
@ -1,25 +0,0 @@
|
||||
INSERT INTO
|
||||
workspace_user (
|
||||
`workspace_id`,
|
||||
`user_id`,
|
||||
`role`
|
||||
)
|
||||
VALUES
|
||||
(
|
||||
1,
|
||||
11,
|
||||
'ADMIN'
|
||||
);
|
||||
|
||||
INSERT INTO
|
||||
workspace_user (
|
||||
`workspace_id`,
|
||||
`user_id`,
|
||||
`role`
|
||||
)
|
||||
VALUES
|
||||
(
|
||||
1,
|
||||
12,
|
||||
'USER'
|
||||
);
|
@ -1,56 +0,0 @@
|
||||
INSERT INTO
|
||||
shortcut (
|
||||
`creator_id`,
|
||||
`workspace_id`,
|
||||
`name`,
|
||||
`link`,
|
||||
`description`,
|
||||
`visibility`
|
||||
)
|
||||
VALUES
|
||||
(
|
||||
11,
|
||||
1,
|
||||
'baidu',
|
||||
'https://baidu.com',
|
||||
'百度搜索',
|
||||
'WORKSPACE'
|
||||
);
|
||||
|
||||
INSERT INTO
|
||||
shortcut (
|
||||
`creator_id`,
|
||||
`workspace_id`,
|
||||
`name`,
|
||||
`link`,
|
||||
`description`,
|
||||
`visibility`
|
||||
)
|
||||
VALUES
|
||||
(
|
||||
12,
|
||||
1,
|
||||
'bl',
|
||||
'https://bilibili.com',
|
||||
'B站',
|
||||
'PUBLIC'
|
||||
);
|
||||
|
||||
INSERT INTO
|
||||
shortcut (
|
||||
`creator_id`,
|
||||
`workspace_id`,
|
||||
`name`,
|
||||
`link`,
|
||||
`description`,
|
||||
`visibility`
|
||||
)
|
||||
VALUES
|
||||
(
|
||||
11,
|
||||
1,
|
||||
'ph',
|
||||
'https://producthunt.com',
|
||||
'PH',
|
||||
'PRIVATE'
|
||||
);
|
Reference in New Issue
Block a user