this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

chore: update tests

+317 -147
+53 -75
src/db/repo/album.test.odin
··· 1 - 2 1 package repo 3 2 4 3 import db_pkg "../" 5 4 import sqlite "../../../vendor/sqlite" 6 5 import types "../../core" 7 - import "core:fmt" 8 - import "core:mem" 9 6 import "core:testing" 10 - 11 7 12 8 @(test) 13 9 should_create_new_album :: proc(t: ^testing.T) { 14 - 15 - 16 - track: mem.Tracking_Allocator 17 - mem.tracking_allocator_init(&track, context.allocator) 18 - context.allocator = mem.tracking_allocator(&track) 19 - defer { 20 - if len(track.allocation_map) > 0 { 21 - fmt.eprintf("=== %v allocations not freed: ===\n", len(track.allocation_map)) 22 - for _, entry in track.allocation_map { 23 - fmt.eprintf("- %v bytes @ %v\n", entry.size, entry.location) 24 - } 25 - } 26 - if len(track.bad_free_array) > 0 { 27 - fmt.eprintf("=== %v incorrect frees: ===\n", len(track.bad_free_array)) 28 - for entry in track.bad_free_array { 29 - fmt.eprintf("- %p @ %v\n", entry.memory, entry.location) 30 - } 31 - } 32 - mem.tracking_allocator_destroy(&track) 33 - } 10 + db := open_test_db(t) 11 + defer sqlite.close(db) 34 12 13 + title := db_pkg.gen_id("album_title") 14 + defer delete(title) 35 15 36 - db: ^sqlite.Connection 16 + album := types.Album{title = title} 17 + new_id, err := new_album(db, album) 18 + defer delete(string(new_id)) 37 19 38 - if rc := sqlite.open(db_pkg.db_url, &db); rc != .Ok { 39 - fmt.panicf("failed to open database. result code {}", rc) 40 - } 41 - fmt.printfln("connected to database") 20 + testing.expect(t, err == .None) 21 + testing.expect(t, len(string(new_id)) > 0) 22 + } 42 23 43 - defer { 44 - sqlite.close(db) 45 - fmt.printfln("\nconnection closed") 46 - } 24 + @(test) 25 + should_get_album_by_id :: proc(t: ^testing.T) { 26 + db := open_test_db(t) 27 + defer sqlite.close(db) 47 28 48 - album := types.Album { 49 - id = "test", 50 - title = "Test title", 51 - mb_id = "asdf", 52 - mb_rg_id = "asdf", 53 - } 29 + title := db_pkg.gen_id("album_by_id") 30 + defer delete(title) 54 31 32 + album := types.Album{title = title} 55 33 new_id, err := new_album(db, album) 56 - fmt.printfln("Error: %v", err) 57 - defer delete(string(new_id)) 58 34 testing.expect(t, err == .None) 59 35 60 - } 36 + found, ok := get_album_by_id(db, string(new_id)) 37 + defer if ok do types.delete_album(found) 38 + defer delete(string(new_id)) 61 39 40 + testing.expect(t, ok) 41 + testing.expect(t, found.title == title) 42 + testing.expect(t, string(found.id) == string(new_id)) 43 + } 62 44 63 45 @(test) 64 - should_get_album_by_id :: proc(t: ^testing.T) { 46 + should_get_album_by_title :: proc(t: ^testing.T) { 47 + db := open_test_db(t) 48 + defer sqlite.close(db) 65 49 50 + title := db_pkg.gen_id("album_lookup") 51 + defer delete(title) 66 52 67 - track: mem.Tracking_Allocator 68 - mem.tracking_allocator_init(&track, context.allocator) 69 - context.allocator = mem.tracking_allocator(&track) 70 - defer { 71 - if len(track.allocation_map) > 0 { 72 - fmt.eprintf("=== %v allocations not freed: ===\n", len(track.allocation_map)) 73 - for _, entry in track.allocation_map { 74 - fmt.eprintf("- %v bytes @ %v\n", entry.size, entry.location) 75 - } 76 - } 77 - if len(track.bad_free_array) > 0 { 78 - fmt.eprintf("=== %v incorrect frees: ===\n", len(track.bad_free_array)) 79 - for entry in track.bad_free_array { 80 - fmt.eprintf("- %p @ %v\n", entry.memory, entry.location) 81 - } 82 - } 83 - mem.tracking_allocator_destroy(&track) 84 - } 53 + album := types.Album{title = title} 54 + new_id, err := new_album(db, album) 55 + testing.expect(t, err == .None) 56 + defer delete(string(new_id)) 85 57 58 + found, ok := get_album_by_title(db, title) 59 + defer if ok do types.delete_album(found) 86 60 87 - db: ^sqlite.Connection 61 + testing.expect(t, ok) 62 + testing.expect(t, found.title == title) 63 + } 88 64 89 - if rc := sqlite.open(db_pkg.db_url, &db); rc != .Ok { 90 - fmt.panicf("failed to open database. result code {}", rc) 91 - } 92 - fmt.printfln("connected to database") 65 + @(test) 66 + should_get_or_create_album :: proc(t: ^testing.T) { 67 + db := open_test_db(t) 68 + defer sqlite.close(db) 93 69 94 - defer { 95 - sqlite.close(db) 96 - fmt.printfln("\nconnection closed") 97 - } 70 + title := db_pkg.gen_id("album_upsert") 71 + defer delete(title) 98 72 73 + album := types.Album{title = title} 99 74 100 - input := "album_019cf0fc-b57a-7f58-a298-ad3166101dd9" 75 + first_id, first_ok := get_or_create_album(db, album) 76 + testing.expect(t, first_ok) 77 + testing.expect(t, len(string(first_id)) > 0) 101 78 102 - album, ok := get_album_by_id(db, input) 103 - testing.expect(t, ok) 104 - testing.expect(t, album.title == "Test title") 79 + second_id, second_ok := get_or_create_album(db, album) 80 + testing.expect(t, second_ok) 81 + testing.expect(t, string(second_id) == string(first_id)) 105 82 106 - fmt.printfln("Album: %#v", album) 83 + delete(string(first_id)) 84 + delete(string(second_id)) 107 85 }
+52 -72
src/db/repo/artist.test.odin
··· 3 3 import db_pkg "../" 4 4 import sqlite "../../../vendor/sqlite" 5 5 import types "../../core" 6 - import "core:fmt" 7 - import "core:mem" 8 6 import "core:testing" 9 7 10 - 11 8 @(test) 12 9 should_create_new_artist :: proc(t: ^testing.T) { 10 + db := open_test_db(t) 11 + defer sqlite.close(db) 13 12 14 - track: mem.Tracking_Allocator 15 - mem.tracking_allocator_init(&track, context.allocator, context.allocator) 16 - context.allocator = mem.tracking_allocator(&track) 17 - defer { 18 - if len(track.allocation_map) > 0 { 19 - fmt.eprintf("=== %v allocations not freed: ===\n", len(track.allocation_map)) 20 - for _, entry in track.allocation_map { 21 - fmt.eprintf("- %v bytes @ %v\n", entry.size, entry.location) 22 - } 23 - } 24 - if len(track.bad_free_array) > 0 { 25 - fmt.eprintf("=== %v incorrect frees: ===\n", len(track.bad_free_array)) 26 - for entry in track.bad_free_array { 27 - fmt.eprintf("- %p @ %v\n", entry.memory, entry.location) 28 - } 29 - } 30 - mem.tracking_allocator_destroy(&track) 13 + artist := types.Artist{ 14 + name = db_pkg.gen_id("artist_name"), 31 15 } 32 16 17 + new_id, err := new_artist(db, artist) 18 + defer delete(string(new_id)) 19 + defer delete(artist.name) 33 20 34 - db: ^sqlite.Connection 35 - 36 - if rc := sqlite.open(db_pkg.db_url, &db); rc != .Ok { 37 - fmt.panicf("failed to open database. result code {}", rc) 38 - } 39 - fmt.printfln("connected to database") 21 + testing.expect(t, err == .None) 22 + testing.expect(t, len(string(new_id)) > 0) 23 + } 40 24 41 - defer { 42 - sqlite.close(db) 43 - fmt.printfln("\nconnection closed") 44 - } 25 + @(test) 26 + should_create_new_artist_batch :: proc(t: ^testing.T) { 27 + db := open_test_db(t) 28 + defer sqlite.close(db) 45 29 46 - artist := types.Artist { 47 - id = "test", 48 - name = "PinkPantheres", 49 - mb_id = "asdf", 50 - acoust_id = "123", 30 + artists := []types.Artist{ 31 + {name = "artist_batch_1"}, 32 + {name = "artist_batch_2"}, 33 + {name = "artist_batch_3"}, 51 34 } 52 35 53 - new_id, err := new_artist(db, artist, context.allocator) 54 - testing.expect(t, err == .None) 36 + rc := new_artist_batch(db, artists) 37 + testing.expect(t, rc == .Ok) 55 38 } 56 39 57 40 @(test) 58 - should_create_new_artist_batch :: proc(t: ^testing.T) { 41 + should_get_artist_by_name :: proc(t: ^testing.T) { 42 + db := open_test_db(t) 43 + defer sqlite.close(db) 59 44 45 + name := db_pkg.gen_id("artist_lookup") 46 + defer delete(name) 60 47 61 - track: mem.Tracking_Allocator 62 - mem.tracking_allocator_init(&track, context.allocator, context.allocator) 63 - context.allocator = mem.tracking_allocator(&track) 64 - defer { 65 - if len(track.allocation_map) > 0 { 66 - fmt.eprintf("=== %v allocations not freed: ===\n", len(track.allocation_map)) 67 - for _, entry in track.allocation_map { 68 - fmt.eprintf("- %v bytes @ %v\n", entry.size, entry.location) 69 - } 70 - } 71 - if len(track.bad_free_array) > 0 { 72 - fmt.eprintf("=== %v incorrect frees: ===\n", len(track.bad_free_array)) 73 - for entry in track.bad_free_array { 74 - fmt.eprintf("- %p @ %v\n", entry.memory, entry.location) 75 - } 76 - } 77 - mem.tracking_allocator_destroy(&track) 78 - } 48 + artist := types.Artist{name = name} 49 + new_id, err := new_artist(db, artist) 50 + defer delete(string(new_id)) 51 + testing.expect(t, err == .None) 79 52 53 + found, ok := get_artist_by_name(db, name) 54 + defer if ok do types.delete_artist(found) 80 55 81 - db: ^sqlite.Connection 56 + testing.expect(t, ok) 57 + testing.expect(t, found.name == name) 58 + testing.expect(t, len(string(found.id)) > 0) 59 + } 82 60 83 - if rc := sqlite.open(db_pkg.db_url, &db); rc != .Ok { 84 - fmt.panicf("failed to open database. result code {}", rc) 85 - } 86 - fmt.printfln("connected to database") 61 + @(test) 62 + should_get_or_create_artist :: proc(t: ^testing.T) { 63 + db := open_test_db(t) 64 + defer sqlite.close(db) 87 65 88 - defer { 89 - sqlite.close(db) 90 - fmt.printfln("connection closed") 91 - } 66 + name := db_pkg.gen_id("artist_upsert") 67 + defer delete(name) 68 + 69 + artist := types.Artist{name = name} 70 + 71 + first_id, first_ok := get_or_create_artist(db, artist) 72 + testing.expect(t, first_ok) 73 + testing.expect(t, len(string(first_id)) > 0) 92 74 93 - artists := []types.Artist { 94 - {id = "test1", name = "PinkPantheres1", mb_id = "asdf", acoust_id = "123"}, 95 - {id = "test2", name = "PinkPantheres2", mb_id = "asdf", acoust_id = "123"}, 96 - {id = "test3", name = "PinkPantheres3", mb_id = "asdf", acoust_id = "123"}, 97 - } 75 + second_id, second_ok := get_or_create_artist(db, artist) 76 + testing.expect(t, second_ok) 77 + testing.expect(t, string(second_id) == string(first_id)) 98 78 99 - rc := new_artist_batch(db, artists, context.allocator) 100 - testing.expect(t, rc == .Ok) 79 + delete(string(first_id)) 80 + delete(string(second_id)) 101 81 }
+59
src/db/repo/junctions.test.odin
··· 1 + package repo 2 + 3 + import db_pkg "../" 4 + import sqlite "../../../vendor/sqlite" 5 + import types "../../core" 6 + import "core:testing" 7 + 8 + @(test) 9 + should_create_artist_album_junction :: proc(t: ^testing.T) { 10 + db := open_test_db(t) 11 + defer sqlite.close(db) 12 + 13 + artist_name := db_pkg.gen_id("junction_artist") 14 + defer delete(artist_name) 15 + artist := types.Artist{name = artist_name} 16 + artist_id, artist_err := new_artist(db, artist) 17 + testing.expect(t, artist_err == .None) 18 + defer delete(string(artist_id)) 19 + 20 + album_title := db_pkg.gen_id("junction_album") 21 + defer delete(album_title) 22 + album := types.Album{title = album_title} 23 + album_id, album_err := new_album(db, album) 24 + testing.expect(t, album_err == .None) 25 + defer delete(string(album_id)) 26 + 27 + link := types.ArtistAlbum{artist_id = artist_id, album_id = album_id} 28 + 29 + err := new_artist_album(db, link) 30 + testing.expect(t, err == .None) 31 + } 32 + 33 + @(test) 34 + should_ignore_duplicate_artist_album_junction :: proc(t: ^testing.T) { 35 + db := open_test_db(t) 36 + defer sqlite.close(db) 37 + 38 + artist_name := db_pkg.gen_id("junction_artist_dupe") 39 + defer delete(artist_name) 40 + artist := types.Artist{name = artist_name} 41 + artist_id, artist_err := new_artist(db, artist) 42 + testing.expect(t, artist_err == .None) 43 + defer delete(string(artist_id)) 44 + 45 + album_title := db_pkg.gen_id("junction_album_dupe") 46 + defer delete(album_title) 47 + album := types.Album{title = album_title} 48 + album_id, album_err := new_album(db, album) 49 + testing.expect(t, album_err == .None) 50 + defer delete(string(album_id)) 51 + 52 + link := types.ArtistAlbum{artist_id = artist_id, album_id = album_id} 53 + 54 + first_err := new_artist_album(db, link) 55 + second_err := new_artist_album(db, link) 56 + 57 + testing.expect(t, first_err == .None) 58 + testing.expect(t, second_err == .None) 59 + }
+54
src/db/repo/test_utils.test.odin
··· 1 + package repo 2 + 3 + import sqlite "../../../vendor/sqlite" 4 + import sa "../../../vendor/sqlite/addons" 5 + import "core:os" 6 + import "core:slice" 7 + import "core:strings" 8 + import "core:testing" 9 + 10 + open_test_db :: proc(t: ^testing.T) -> ^sqlite.Connection { 11 + db: ^sqlite.Connection 12 + rc := sqlite.open(":memory:", &db) 13 + testing.expect(t, rc == .Ok) 14 + 15 + apply_test_migrations(t, db) 16 + return db 17 + } 18 + 19 + apply_test_migrations :: proc(t: ^testing.T, db: ^sqlite.Connection) { 20 + migration_dir := "src/db/migrations" 21 + 22 + dir_entries, err := os.read_all_directory_by_path(migration_dir, context.allocator) 23 + testing.expect(t, err == nil) 24 + defer os.file_info_slice_delete(dir_entries, context.allocator) 25 + 26 + only_sql := slice.filter(dir_entries, proc(x: os.File_Info) -> bool { 27 + return strings.has_suffix(x.fullpath, ".sql") 28 + }) 29 + defer delete(only_sql) 30 + 31 + slice.sort_by(only_sql, proc(a, b: os.File_Info) -> bool { 32 + return strings.compare(a.fullpath, b.fullpath) < 0 33 + }) 34 + 35 + for migration in only_sql { 36 + apply_sql_file(t, db, migration.fullpath) 37 + } 38 + } 39 + 40 + apply_sql_file :: proc(t: ^testing.T, db: ^sqlite.Connection, path: string) { 41 + data, err := os.read_entire_file_from_path(path, context.allocator) 42 + testing.expect(t, err == nil) 43 + defer delete(data, context.allocator) 44 + 45 + text := string(data) 46 + expressions := strings.split(text, ";") 47 + defer delete(expressions) 48 + 49 + for exp in expressions { 50 + trimmed := strings.trim_space(exp) 51 + if len(trimmed) == 0 do continue 52 + testing.expect(t, sa.execute(db, trimmed) == .Ok) 53 + } 54 + }
+99
src/db/repo/track.test.odin
··· 1 + package repo 2 + 3 + import db_pkg "../" 4 + import sqlite "../../../vendor/sqlite" 5 + import types "../../core" 6 + import "core:testing" 7 + 8 + create_album_for_track_test :: proc(t: ^testing.T, db: ^sqlite.Connection) -> types.Album_Id { 9 + title := db_pkg.gen_id("track_album") 10 + defer delete(title) 11 + 12 + album := types.Album{title = title} 13 + album_id, err := new_album(db, album) 14 + testing.expect(t, err == .None) 15 + 16 + return album_id 17 + } 18 + 19 + @(test) 20 + should_create_new_track :: proc(t: ^testing.T) { 21 + db := open_test_db(t) 22 + defer sqlite.close(db) 23 + 24 + album_id := create_album_for_track_test(t, db) 25 + defer delete(string(album_id)) 26 + 27 + track_title := db_pkg.gen_id("track_title") 28 + defer delete(track_title) 29 + 30 + track := types.Track{ 31 + title = track_title, 32 + track_number = 1, 33 + album_id = album_id, 34 + } 35 + 36 + track_id, err := new_track(db, track) 37 + defer delete(string(track_id)) 38 + 39 + testing.expect(t, err == .None) 40 + testing.expect(t, len(string(track_id)) > 0) 41 + } 42 + 43 + @(test) 44 + should_get_track_by_title :: proc(t: ^testing.T) { 45 + db := open_test_db(t) 46 + defer sqlite.close(db) 47 + 48 + album_id := create_album_for_track_test(t, db) 49 + defer delete(string(album_id)) 50 + 51 + track_title := db_pkg.gen_id("track_lookup") 52 + defer delete(track_title) 53 + 54 + track := types.Track{ 55 + title = track_title, 56 + track_number = 2, 57 + album_id = album_id, 58 + } 59 + 60 + track_id, err := new_track(db, track) 61 + testing.expect(t, err == .None) 62 + defer delete(string(track_id)) 63 + 64 + found, ok := get_track_by_title(db, track_title, album_id) 65 + defer if ok do types.delete_track(found) 66 + 67 + testing.expect(t, ok) 68 + testing.expect(t, found.title == track_title) 69 + testing.expect(t, string(found.album_id) == string(album_id)) 70 + } 71 + 72 + @(test) 73 + should_get_or_create_track :: proc(t: ^testing.T) { 74 + db := open_test_db(t) 75 + defer sqlite.close(db) 76 + 77 + album_id := create_album_for_track_test(t, db) 78 + defer delete(string(album_id)) 79 + 80 + track_title := db_pkg.gen_id("track_upsert") 81 + defer delete(track_title) 82 + 83 + track := types.Track{ 84 + title = track_title, 85 + track_number = 3, 86 + album_id = album_id, 87 + } 88 + 89 + first_id, first_ok := get_or_create_track(db, track) 90 + testing.expect(t, first_ok) 91 + testing.expect(t, len(string(first_id)) > 0) 92 + 93 + second_id, second_ok := get_or_create_track(db, track) 94 + testing.expect(t, second_ok) 95 + testing.expect(t, string(second_id) == string(first_id)) 96 + 97 + delete(string(first_id)) 98 + delete(string(second_id)) 99 + }