Skip to content
Snippets Groups Projects
Commit f9e13ab6 authored by Christian Pointner's avatar Christian Pointner
Browse files

added rant about database/sql and gorm ...

parent 9b676607
No related branches found
No related tags found
No related merge requests found
......@@ -25,11 +25,12 @@
package store
import (
"database/sql"
"fmt"
"os"
"os/user"
"testing"
"github.com/jinzhu/gorm"
)
var (
......@@ -81,7 +82,7 @@ func TestOpen(t *testing.T) {
t.Fatalf("unexpected error: %v", err)
}
/// exisiting but non-database file (only possible with sqlite...
/// exisiting but non-database file (only possible with sqlite...)
// if f, err := os.Create(testDBConnection); err != nil {
// t.Fatalf("unexpected error: %v", err)
// } else {
......@@ -126,60 +127,44 @@ func TestMigrations(t *testing.T) {
// TODO: needs more testing!!!!
}
func TestDBConstraints(t *testing.T) {
db, err := sql.Open(testDBType, testDBConnection)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if err = db.Ping(); err != nil {
t.Fatalf("unexpected error: %v", err)
}
res, err := db.Exec("INSERT INTO groups (name) VALUES ($1)", testGroup1)
if err != nil {
t.Fatalf("adding group '%s' shouldn't fail: %v", testGroup1, err)
}
t.Fatalf("%v", res)
// /* should fail */
// insert into files (size) values(100);
// insert into files (group_name, size) values('invalid', 101);
// /* should succeed */
// insert into files (group_name, size) values('test', 101);
// insert into files (group_name, size) values('test', 102);
// select * from files;
// /* should fail */
// insert into playlists (created_at) values ('2018-06-17 02:38:17');
// /* should succeed */
// insert into playlists (created_at, group_name) values('2018-06-17 02:38:17', 'test');
// /* should fail */
// insert into playlist_entries (uri) values ('http://stream.example.com/live.mp3');
// insert into playlist_entries (playlist_id, uri) values (1, 'http://stream.example.com/live.mp3');
// insert into playlist_entries (playlist_id, line_num, uri) values (17, 1, 'http://stream.example.com/live.mp3');
// /* should succeed */
// insert into playlist_entries (playlist_id, line_num, uri) values (1, 1, 'http://stream.example.com/live.mp3');
// /* should fail */
// insert into playlist_entries (playlist_id, line_num, uri) values (1, 1, 'http://stream.example.com/other.mp3');
// insert into playlist_entries (playlist_id, line_num, file_id) values (1, 4, 23);
// /* should succeed */
// insert into playlist_entries (playlist_id, line_num, file_id) values (1, 4, 2);
// delete from files where id = 1;
// /* should fail */
// delete from files where id = 2;
//// Arrrrgh!!!!!
//// both database/sql and gorm are incredibly stupid!!!!
//// using database/sql alone it is needlessly hard to write portable sql queries:
//// postgres: db.Exec("INSERT INTO groups (name) VALUES ($1)", testGroup1)
//// mysql: db.Exec("INSERT INTO groups (name) VALUES (?)", testGroup1)
////
//// using gorm db.Exec() will deal with that but i couldn't find a good way to get
//// the last inserted id without using the model code of gorm and we specifically
//// don't want to use the model code since is not clear whether the constraints
//// are actually enforced by the DBMS or by some gorm callbacks...
////
// func TestDBConstraints(t *testing.T) {
// // we don't want to use the model but hand written SQL commands here since we want to check
// // whether the constraints are really enforced by the DBMS and not by some magic bullshit in gorm!
// // This is even worse since gorm.AutoUpdate will not deal with foreign key constraints and we
// // need do it ourselves at the migration scripts. It would be really nice to be able to check
// // whether the migrations do the right thing!!!
// db, err := gorm.Open(testDBType, testDBConnection)
// if err != nil {
// t.Fatalf("unexpected error: %v", err)
// }
// if err = db.DB().Ping(); err != nil {
// t.Fatalf("unexpected error: %v", err)
// }
// /* should succeed */
// delete from playlists where id = 1;
// delete from files where id = 2;
// res := db.Exec("INSERT INTO groups (name) VALUES (?)", testGroup1)
// if res.Error != nil {
// t.Fatalf("adding group '%s' shouldn't fail: %v", testGroup1, res.Error)
// }
// if res = db.Exec("INSERT INTO groups (name) VALUES (?)", testGroup1); res.Error == nil {
// t.Fatalf("re-adding the same group should fail")
// }
}
// if res = db.Exec("INSERT INTO files (group_name, size) VALUES (?, ?)", testGroup1, 500); res.Error != nil {
// t.Fatalf("re-adding the same group should fail")
// }
// }
// // Groups
// //
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment