From ad10ea99e6cc3e21322ebe2de82833e78e7cccef Mon Sep 17 00:00:00 2001 From: aidewoode Date: Wed, 22 Jul 2020 19:36:51 +0800 Subject: [PATCH] add support for various artists album --- app/controllers/albums_controller.rb | 1 - app/controllers/application_controller.rb | 2 +- app/controllers/artists/albums_controller.rb | 20 +++++++++ .../artists/appears_on_albums_controller.rb | 8 ++++ app/controllers/artists_controller.rb | 5 ++- app/lib/black_candy_error.rb | 1 + app/models/artist.rb | 15 ++++++- app/models/media.rb | 15 ++++++- app/models/media_file.rb | 1 + app/views/artists/_albums.html.erb | 17 ++++++++ app/views/artists/_info.html.erb | 18 ++++++++ app/views/artists/albums/index.html.erb | 2 + .../artists/appears_on_albums/index.html.erb | 2 + app/views/artists/show.html.erb | 39 +++++------------- app/views/artists/show.js.erb | 1 - config/locales/en.yml | 3 ++ config/routes.rb | 6 ++- ...0200721071945_add_is_various_to_artists.rb | 5 +++ db/schema.rb | 3 +- .../artists/albums_controller_test.rb | 19 +++++++++ .../appears_on_albums_controller_test.rb | 18 ++++++++ test/fixtures/albums.yml | 3 ++ test/fixtures/artists.yml | 3 ++ test/fixtures/files/artist1_album1.flac | Bin 10001 -> 10001 bytes test/fixtures/files/artist1_album1.m4a | Bin 9312 -> 9312 bytes test/fixtures/files/artist1_album2.mp3 | Bin 38136 -> 38117 bytes test/fixtures/files/artist2_album3.oga | Bin 9862 -> 9885 bytes test/fixtures/files/artist2_album3.ogg | Bin 7075 -> 7074 bytes test/fixtures/files/artist2_album3.opus | Bin 7703 -> 7702 bytes test/fixtures/files/artist2_album3.wav | Bin 1416738 -> 1416738 bytes test/fixtures/files/artist2_album3.wma | Bin 279156 -> 279208 bytes test/fixtures/files/various_artists.mp3 | Bin 0 -> 38117 bytes test/fixtures/songs.yml | 8 ++++ test/models/artist_test.rb | 12 ++++++ test/models/concerns/searchable_test.rb | 3 +- test/models/media_file_test.rb | 11 ++++- test/models/media_test.rb | 13 +++--- 37 files changed, 209 insertions(+), 45 deletions(-) create mode 100644 app/controllers/artists/albums_controller.rb create mode 100644 app/controllers/artists/appears_on_albums_controller.rb create mode 100644 app/views/artists/_albums.html.erb create mode 100644 app/views/artists/_info.html.erb create mode 100644 app/views/artists/albums/index.html.erb create mode 100644 app/views/artists/appears_on_albums/index.html.erb delete mode 100644 app/views/artists/show.js.erb create mode 100644 db/migrate/20200721071945_add_is_various_to_artists.rb create mode 100644 test/controllers/artists/albums_controller_test.rb create mode 100644 test/controllers/artists/appears_on_albums_controller_test.rb create mode 100644 test/fixtures/files/various_artists.mp3 diff --git a/app/controllers/albums_controller.rb b/app/controllers/albums_controller.rb index b2f8332a..1f967c8a 100644 --- a/app/controllers/albums_controller.rb +++ b/app/controllers/albums_controller.rb @@ -13,7 +13,6 @@ def index def show @songs = @album.songs - AttachAlbumImageFromDiscogsJob.perform_later(@album.id) if @album.need_attach_from_discogs? end diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index c6494357..12ff5531 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -5,7 +5,7 @@ class ApplicationController < ActionController::Base before_action :find_current_user - rescue_from ActiveRecord::RecordNotFound do |exception| + rescue_from ActiveRecord::RecordNotFound, BlackCandyError::NotFound do |exception| respond_to do |format| format.js { head :not_found } format.json { head :not_found } diff --git a/app/controllers/artists/albums_controller.rb b/app/controllers/artists/albums_controller.rb new file mode 100644 index 00000000..4053f581 --- /dev/null +++ b/app/controllers/artists/albums_controller.rb @@ -0,0 +1,20 @@ +# frozen_string_literal: true + +class Artists::AlbumsController < ApplicationController + before_action :require_login + before_action :find_artist + before_action :find_albums + + def index + raise BlackCandyError::NotFound if @albums.empty? + end + + private + def find_albums + @albums = @artist.albums + end + + def find_artist + @artist = Artist.find(params[:artist_id]) + end +end diff --git a/app/controllers/artists/appears_on_albums_controller.rb b/app/controllers/artists/appears_on_albums_controller.rb new file mode 100644 index 00000000..003eaff0 --- /dev/null +++ b/app/controllers/artists/appears_on_albums_controller.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +class Artists::AppearsOnAlbumsController < Artists::AlbumsController + private + def find_albums + @albums = @artist.appears_on_albums + end +end diff --git a/app/controllers/artists_controller.rb b/app/controllers/artists_controller.rb index 103274f2..15d5db76 100644 --- a/app/controllers/artists_controller.rb +++ b/app/controllers/artists_controller.rb @@ -1,6 +1,8 @@ # frozen_string_literal: true class ArtistsController < ApplicationController + ALBUMS_COUNT = 10 + include Pagy::Backend before_action :require_login @@ -12,7 +14,8 @@ def index def show @artist = Artist.find(params[:id]) - @pagy, @albums = pagy_countless(@artist.albums) + @albums_pagy, @albums = pagy_countless(@artist.albums, items: ALBUMS_COUNT) + @appears_on_albums_pagy, @appears_on_albums = pagy_countless(@artist.appears_on_albums, items: ALBUMS_COUNT) AttachArtistImageFromDiscogsJob.perform_later(@artist.id) if @artist.need_attach_from_discogs? end diff --git a/app/lib/black_candy_error.rb b/app/lib/black_candy_error.rb index 6b2a6bd6..71a22200 100644 --- a/app/lib/black_candy_error.rb +++ b/app/lib/black_candy_error.rb @@ -2,5 +2,6 @@ module BlackCandyError class Forbidden < StandardError; end + class NotFound < StandardError; end class InvalidFilePath < StandardError; end end diff --git a/app/models/artist.rb b/app/models/artist.rb index 3b9c61ae..9bb19a98 100644 --- a/app/models/artist.rb +++ b/app/models/artist.rb @@ -4,14 +4,17 @@ class Artist < ApplicationRecord include Searchable has_many :albums, dependent: :destroy - has_many :songs, dependent: :destroy + has_many :songs mount_uploader :image, ImageUploader search_by :name def title - is_unknown? ? I18n.t('text.unknown_artist') : name + return I18n.t('text.various_artists') if is_various? + return I18n.t('text.unknown_artist') if is_unknown? + + name end def has_image? @@ -22,6 +25,14 @@ def is_unknown? name.blank? end + def all_albums + Album.joins(:songs).where('albums.artist_id = ? OR songs.artist_id = ?', id, id).distinct + end + + def appears_on_albums + Album.joins(:songs).where('albums.artist_id != ? AND songs.artist_id = ?', id, id).distinct + end + def need_attach_from_discogs? Setting.discogs_token.present? && !has_image? && !is_unknown? end diff --git a/app/models/media.rb b/app/models/media.rb index 49c5ad79..d8b112c0 100644 --- a/app/models/media.rb +++ b/app/models/media.rb @@ -9,7 +9,13 @@ def initialize(file_path) def attach artist = Artist.find_or_create_by(name: file_info[:artist_name]) - album = Album.find_or_create_by(artist: artist, name: file_info[:album_name]) + + if various_artists? + various_artist = Artist.find_or_create_by(is_various: true) + album = Album.find_or_create_by(artist: various_artist, name: file_info[:album_name]) + else + album = Album.find_or_create_by(artist: artist, name: file_info[:album_name]) + end # Attach image from file to the album. AttachAlbumImageFromFileJob.perform_later(album.id, file_info[:file_path]) unless album.has_image? @@ -25,6 +31,11 @@ def song_info file_info.slice(:name, :tracknum, :length, :file_path) end + def various_artists? + albumartist = file_info[:albumartist_name] + albumartist.present? && (albumartist.downcase == 'various artists' || albumartist != file_info[:artist_name]) + end + class << self def sync media_hashes = MediaFile.file_paths.map do |file_path| @@ -46,7 +57,7 @@ def clean_up(media_hashes) # Clean up no content albums and artist. Album.left_outer_joins(:songs).where('songs.id is null').destroy_all - Artist.left_outer_joins(:songs).where('songs.id is null').destroy_all + Artist.left_outer_joins(:songs, :albums).where('songs.album_id is null').where('albums.id is null').destroy_all end end end diff --git a/app/models/media_file.rb b/app/models/media_file.rb index f6373103..a03aee0a 100644 --- a/app/models/media_file.rb +++ b/app/models/media_file.rb @@ -40,6 +40,7 @@ def get_tag_info(file_path) name: tag.title.presence || File.basename(file_path), album_name: tag.album.presence, artist_name: tag.artist.presence, + albumartist_name: tag.albumartist.presence, tracknum: tag.track, length: tag.duration } diff --git a/app/views/artists/_albums.html.erb b/app/views/artists/_albums.html.erb new file mode 100644 index 00000000..ef9a64c3 --- /dev/null +++ b/app/views/artists/_albums.html.erb @@ -0,0 +1,17 @@ +<% if albums.present? %> +
+
+
+
+ <%= title %> +
+
+ <% if local_assigns[:all_albums_link] && all_albums_link.present? %> + <%= link_to t('text.see_all'), all_albums_link %> + <% end %> +
+
+
+ <%= render partial: 'albums/album', collection: albums, cached: true %> +
+<% end %> diff --git a/app/views/artists/_info.html.erb b/app/views/artists/_info.html.erb new file mode 100644 index 00000000..d43f38a7 --- /dev/null +++ b/app/views/artists/_info.html.erb @@ -0,0 +1,18 @@ +<% cache artist do %> +
+
+
+ <%= image_tag image_url_for(artist), class: 'image' %> +
+
+
<%= artist.title %>
+
+ <%= artist.all_albums.size %> <%= t('label.albums') %> + , + <%= artist.songs.size %> <%= t('label.songs')%> +
+
+
+
+
+<% end %> diff --git a/app/views/artists/albums/index.html.erb b/app/views/artists/albums/index.html.erb new file mode 100644 index 00000000..9a22f7c8 --- /dev/null +++ b/app/views/artists/albums/index.html.erb @@ -0,0 +1,2 @@ +<%= render partial: 'artists/info', locals: { artist: @artist } %> +<%= render partial: 'artists/albums', locals: { title: t('label.albums'), albums: @albums } %> diff --git a/app/views/artists/appears_on_albums/index.html.erb b/app/views/artists/appears_on_albums/index.html.erb new file mode 100644 index 00000000..04722ab3 --- /dev/null +++ b/app/views/artists/appears_on_albums/index.html.erb @@ -0,0 +1,2 @@ +<%= render partial: 'artists/info', locals: { artist: @artist } %> +<%= render partial: 'artists/albums', locals: { title: t('label.appears_on'), albums: @albums } %> diff --git a/app/views/artists/show.html.erb b/app/views/artists/show.html.erb index e63069de..9e05b3b2 100644 --- a/app/views/artists/show.html.erb +++ b/app/views/artists/show.html.erb @@ -1,28 +1,11 @@ -<% cache @artist do %> -
-
-
- <%= image_tag image_url_for(@artist), class: 'image' %> -
-
-
<%= @artist.title %>
-
- <%= @albums.size %> <%= t('label.albums') %> - , - <%= @artist.songs.size %> <%= t('label.songs')%> -
-
-
-
-
-<% end %> -
-
- <%= render partial: 'albums/album', collection: @albums, cached: true %> -
-
- <% if @pagy.next %> - <%= loader_tag %> - <% end %> -
-
+<%= render partial: 'info', locals: { artist: @artist} %> + +<%= render partial: 'albums', locals: { + title: t('label.albums'), + albums: @albums, + all_albums_link: @albums_pagy.next.present? ? artist_albums_path(@artist) : '' } %> + +<%= render partial: 'albums', locals: { + title: t('label.appears_on'), + albums: @appears_on_albums, + all_albums_link: @appears_on_albums_pagy.next.present? ? artist_appears_on_albums_path(@artist) : '' } %> diff --git a/app/views/artists/show.js.erb b/app/views/artists/show.js.erb deleted file mode 100644 index 40b080ea..00000000 --- a/app/views/artists/show.js.erb +++ /dev/null @@ -1 +0,0 @@ -App.appedNextPageContentTo('#js-albums-content', '<%= j(render partial: 'albums/album', collection: @albums, cached: true) %>', '<%= pagy_next_url(@pagy) %>') diff --git a/config/locales/en.yml b/config/locales/en.yml index 3f90c95e..6d9f987d 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -52,6 +52,7 @@ en: integration: 'Integration' close: 'Close' more: 'More' + appears_on: 'Appears On' error: login: 'Wrong email or password' media_path_blank: 'Media path is not exist' @@ -75,6 +76,7 @@ en: add_user: 'Add user' create_user: 'Create User' media_path: 'Media path' + various_artists: 'Various Artists' unknown_artist: 'Unknown Artist' unknown_album: 'Unknown Album' go_back: 'Go back' @@ -89,3 +91,4 @@ en: expand_player: 'Expand player' collapse_player: 'Collapse player' personal: 'Personal' + see_all: 'See All' diff --git a/config/routes.rb b/config/routes.rb index 469bbc7a..ddbf5adb 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -4,7 +4,11 @@ resource :session, only: [:new, :create, :destroy] resource :setting, only: [:show, :update] - resources :artists, only: [:index, :show] + resources :artists, only: [:index, :show] do + resources :albums, only: [:index], module: 'artists' + resources :appears_on_albums, only: [:index], module: 'artists' + end + resources :stream, only: [:new] resources :transcoded_stream, only: [:new] resources :songs, only: [:index, :show] diff --git a/db/migrate/20200721071945_add_is_various_to_artists.rb b/db/migrate/20200721071945_add_is_various_to_artists.rb new file mode 100644 index 00000000..f879d63c --- /dev/null +++ b/db/migrate/20200721071945_add_is_various_to_artists.rb @@ -0,0 +1,5 @@ +class AddIsVariousToArtists < ActiveRecord::Migration[6.0] + def change + add_column(:artists, :is_various, :boolean, default: false) + end +end diff --git a/db/schema.rb b/db/schema.rb index b41be1ca..3c0fe5dc 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2020_04_25_023906) do +ActiveRecord::Schema.define(version: 2020_07_21_071945) do # These are extensions that must be enabled in order to support this database enable_extension "pg_trgm" @@ -31,6 +31,7 @@ t.string "image" t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false + t.boolean "is_various", default: false t.index ["name"], name: "index_artists_on_name", unique: true end diff --git a/test/controllers/artists/albums_controller_test.rb b/test/controllers/artists/albums_controller_test.rb new file mode 100644 index 00000000..db1ca0a0 --- /dev/null +++ b/test/controllers/artists/albums_controller_test.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +require 'test_helper' + +class Artists::AlbumsControllerTest < ActionDispatch::IntegrationTest + test 'should get index' do + assert_login_access(url: artist_albums_path(artists :artist1)) do + assert_response :success + end + end + + test 'should redirect to not found page when artists have no albums' do + empty_artist = Artist.create + + assert_login_access(url: artist_albums_path(empty_artist)) do + assert_response :not_found + end + end +end diff --git a/test/controllers/artists/appears_on_albums_controller_test.rb b/test/controllers/artists/appears_on_albums_controller_test.rb new file mode 100644 index 00000000..26dd35c5 --- /dev/null +++ b/test/controllers/artists/appears_on_albums_controller_test.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +require 'test_helper' + +class Artists::AppearsOnAlbumsControllerTest < ActionDispatch::IntegrationTest + test 'should get index' do + assert_login_access(url: artist_appears_on_albums_path(artists :artist1)) do + assert_response :success + end + end + + test 'should redirect to not found page when artists have no albums' do + assert artists(:artist2).appears_on_albums.empty? + assert_login_access(url: artist_appears_on_albums_path(artists :artist2)) do + assert_response :not_found + end + end +end diff --git a/test/fixtures/albums.yml b/test/fixtures/albums.yml index 7b220961..e005243f 100644 --- a/test/fixtures/albums.yml +++ b/test/fixtures/albums.yml @@ -7,3 +7,6 @@ album2: album3: name: 'album3' artist: 'artist2' +album4: + name: 'album4' + artist: 'various_artists' diff --git a/test/fixtures/artists.yml b/test/fixtures/artists.yml index 2a327f79..3f9354c0 100644 --- a/test/fixtures/artists.yml +++ b/test/fixtures/artists.yml @@ -3,3 +3,6 @@ artist1: artist2: name: 'artist2' + +various_artists: + is_various: true diff --git a/test/fixtures/files/artist1_album1.flac b/test/fixtures/files/artist1_album1.flac index b909d84cb91e24dc93d06213aa818cd65be1cb43..abfc5929a872f1ee8f78805d251a356b71edfdc7 100644 GIT binary patch delta 80 zcmbQ}H_>kczaXRhWC1}H5e^0h1|A@G^l=LHwN1=PD$O;VTq$@#S{Nqi7!=|e9AcYT cRFYX-V%W$aAhkJA=nwzo1R=T2zDh?}03sX}-~a#s delta 101 zcmbQ}H_>kczaXRKWC1}HS55{71|A@G^l=LHwN1=PD$O+%f$<%KLOg>*Y~3;|N=l1T ei$zhzjo=cKJp?Z>H!=upb`)Y1*zBuxgarTzxf@jg diff --git a/test/fixtures/files/artist1_album1.m4a b/test/fixtures/files/artist1_album1.m4a index c4b45e2b8aa6338fda69664fcc147ffd756ae31f..0116a948c2c4a0d97630fed7f84709efeec4fcac 100644 GIT binary patch delta 45 zcmaFh@xWt)AqV66$wnN9ndB2E+eyi67U6s=$ZDBZl$yF(P+FC7vVt_vW-}Qr4ghWL B4d(y= delta 60 zcmaFh@xWt)AqV4y$wnN9Wfc+~gF+Y>7$i~>OA>)JBM>vVWmc4w7Nr(%7UX;{$Y`;d NQQCxYvzd$*2LL6a5N-ef diff --git a/test/fixtures/files/artist1_album2.mp3 b/test/fixtures/files/artist1_album2.mp3 index b6f50282cfb1a18e94e836dd9e492b3c93ce4f94..07eb78bf11791d99d81ce11b2a7d78247e348d38 100644 GIT binary patch delta 53 zcmeydlIiJ6CN57GV-^M=(DI(hRljl083Cb?09PZR00$5!7L{Zcml$p?6nw|LIYV?p FJpiSA5F7vi delta 56 zcmaF5lIh1vCN57GV-^M=(2ASLRWImj1Y~kDFfhAiR+N+$r50~&KPRxcQ1A)!H)xO5ikG% diff --git a/test/fixtures/files/artist2_album3.oga b/test/fixtures/files/artist2_album3.oga index b97821250810cc81b3bd8cef27861c383ac7975a..2c2daee86d878b063a2bd0eb772d7782942b92a2 100644 GIT binary patch delta 68 zcmZqko$EUxl`AGdr<}2ig@K`MV!nztD+2=q4-h;0IEDJ!CgvoS<{Asb_>Ms#p1~ou QiA5!u#U(}?m)}IZ36t#vAwDR|NnecnuH$ diff --git a/test/fixtures/files/artist2_album3.ogg b/test/fixtures/files/artist2_album3.ogg index b722b1d8d9122b8749d04576eeb2cc12e3c5f5ce..7f3926f01469285e548ef2dd804ecc85d4ab8845 100644 GIT binary patch delta 58 zcmZ2%zQ}w+0KfZ}o0la2!@!4$@o^ikHgJjyGcYhX`Z$I9ItGP!28Y-t7L{Zcml$mh I5%6h;3p~NoH}05g$kxMZ^v$;_UD1>*^O`JMoIy#EGIpAVncT Wj?UhGp}tP8LAC~lo0S;9$^ZZ`9X=ZX delta 172 zcmbPcGu>u_YrRO+eLji**g=qgL1}SFVtO$z0|P@&W>P+ot6->StY^p$6yO75$Dk0; z;1JuyqLR$w5+gyNNQh^MkE<<6WqfgBZb42e4@jAhQ>d?PVop+Nt}#ekNRXqmw_m8Q ilPgP*t%2dh9ZnJ=FlA69-7+gmN{dp9H!CuJl>q?r6F|%W diff --git a/test/fixtures/files/artist2_album3.wav b/test/fixtures/files/artist2_album3.wav index e5fd98a8c51fd6b74b4a8074ed058783dc8317d6..483120d226e038601be944d49f25d35e17b2dbf3 100644 GIT binary patch delta 85 zcmZ3qCv4H4u!a`K7N!>F7M2#)7Pc1l7LFFq7OocV7M>Q~7QPn#7J(MQ7NHj57LhHY o&pWpN>JYsvAQTedYQ(_6zyZXGMJ1WVB}N+!nOLUZm@Dc90NG+2&j0`b delta 84 zcmZ3qCv4H4u!a`K7N!>F7M2#)7Pc1l7LFFq7OocV7M>Q~7QPn#7J(MQ7NHj57LhHY o&pQNNjTjghI2jn2-7+gmN{dp9w?FL=y)UrQkdbBjjk%&;0GQDms{jB1 diff --git a/test/fixtures/files/artist2_album3.wma b/test/fixtures/files/artist2_album3.wma index 88c821aec8389c22674d779757f31096c8f7aed1..ae81a47704c8f74c09ee3ab4f5c7db83c165d4a2 100644 GIT binary patch delta 110 zcmey;BDkVeP|!eaQ)yq?dBJ5j8CEeQoy++)QSiC#D+UN)VUS}8XYggvXK-Z5VMt;q rWyl4xiWo{5G8u}2BnSvFBqE6zF)%bUGPW~6XH)RB2CLm_sZpz3~ G><9p8*b$ci diff --git a/test/fixtures/files/various_artists.mp3 b/test/fixtures/files/various_artists.mp3 new file mode 100644 index 0000000000000000000000000000000000000000..6c015a75d76d41c36977308f0d1c00464538bfd8 GIT binary patch literal 38117 zcmeI5eQ*@z9mk)&7w!_9l3)ZuZI>g+%b0shh{?%;$>kzRxaJ6lK(kllTr-REV0zy0oKzU)6&UYe&PP$pOUylxv*jD%=8Nfu(V%~$TT!$b$HVzPZ`EvQXkb>Wui90as@saaLh#ZkTvR#uKFr$}Z+S$P>@7(!;iKcaM!c6UOkB?RtpGK?@112ZCdn+$`=8m5FVrF8h53J7E)O#RVut|(PSB6ozO{@@fn&wXNSUph}UoXs9Ch@bE zea9DAwj@v#G)_qOu@HSan9uFUDo330CgDrEs)o+AoYk3J5Q1wyi z*NHAmKVqz{c-XD8ID=_vw*8T(HyX#4Y~6EP^?|OOJiqj4TKdRQw~rn({_Y7ACr!?u zQgCl!k;7H$E_+}`dBtqsoCoL5n_m+M^0jsK!s3`5Z)i+3Eqmw~PP$_|oN1{&nTw|54)tkJ}vB%l(brSQaUJlgI+BLqaj(0sgj;7x2PqfrzkK919QOOVeprcW=QL~HsFf# z0sL1XLpeddp=Cf!+AK1iT+8UV>~ziVFvr6+A1&B*a%YWjruheZbDom+?Q6=}(CW@6 zvj)B~56$ejoNw1!OllACg1kmC*xa@?Uoj_VlYxWj`ScVv*`jt+9%*XH?w)-UaM zm35fq=W9+Zs61I-Gx}6>MfshpqwVcYx-qQ_cV4{kS!LCeE6(5Bq>$E(rn^Y?Ecbx7 zg@^L@!<&@5{rS63sBhAPx3jA%;Z2He*;16V;nIaG-WSKLI9qWS{eT;o7q= z>Q@|#yr+<|^iR*W3|Ee)!)F!sBT6Q0sgEdZ>LW@w4snkezzp168BnhND0riY<_VEH z=rbIw9DE^scn{I)jp-S~M`Vv0edo7KITI$`W47fL6yE2&-{qcB;i;o$u%35jvB#Zfwpp#Zo%9x`usQk`4`EKJ_pv|pM<2a#(;k(p znL#qV3qB-6i15WbgN4Tcf19BR)%=x&=z0=0r=N1e4s+(H>zHn^PCv8vo*v*jr9-q% z?GUZAdWhE9GDPd_8=`eSSbKe4_X?|@+5q*2>-5PV=IskP+9yk&Z(`msZ>lO#2dD$o zLBD?V^Wf}r2HziifAIam_lIHud4N1X9v}~p2iN`1N7K{q5jlz+MUEmzDIkys$OGg7 z@&I{&zKUu9d4N2iT0nn*{s4J^JU||xKcE^w9v}~>7SJD{KR_NJ50D4w52yx^2gn1e z1@s5#50D4Q1LOhv1F8Y!0rG%q0sR5`1LOhn0C|A^fNB7FfIOgDK!1S#0C|8sKpvn! zpc+6PAP=Y(&>x^bKpr3ukO$}ws0NS+$OEbc^atn#| zm1eJk^aiRw|kxW=_Oo;X5z~oZ??P@ zd)>a`2i)`CHxOch-$aMsMpsKqau2&yoA|1WU6sVvPxqCyJIvL@< zF)kbp@w7wtSsQKkIdUO?Je=R> + md5_hash: 'fake_md5' + artist: 'artist1' + album: 'album4' diff --git a/test/models/artist_test.rb b/test/models/artist_test.rb index dced3c4e..c6e777c1 100644 --- a/test/models/artist_test.rb +++ b/test/models/artist_test.rb @@ -6,4 +6,16 @@ class ArtistTest < ActiveSupport::TestCase test 'should have default title when name is empty' do assert_equal 'Unknown Artist', Artist.create(name: nil).title end + + test 'should have default title when is various artist' do + assert_equal 'Various Artists', Artist.create(is_various: true).title + end + + test 'should get all albums' do + assert_equal Album.where(name: %w(album1 album2 album4)).ids.sort, artists(:artist1).all_albums.ids.sort + end + + test 'should get appears on albums' do + assert_equal Album.where(name: %w(album4)).ids.sort, artists(:artist1).appears_on_albums.ids.sort + end end diff --git a/test/models/concerns/searchable_test.rb b/test/models/concerns/searchable_test.rb index 5018fbeb..57440df9 100644 --- a/test/models/concerns/searchable_test.rb +++ b/test/models/concerns/searchable_test.rb @@ -41,7 +41,8 @@ class SearchableTest < ActiveSupport::TestCase album_serach_song_ids = Album.find_by_name('album1').songs.ids + Album.find_by_name('album2').songs.ids + - Album.find_by_name('album3').songs.ids + Album.find_by_name('album3').songs.ids + + Album.find_by_name('album4').songs.ids assert_equal album_serach_song_ids.sort, Song.search('album').ids.sort end diff --git a/test/models/media_file_test.rb b/test/models/media_file_test.rb index 3e2d7943..8d922cf7 100644 --- a/test/models/media_file_test.rb +++ b/test/models/media_file_test.rb @@ -12,7 +12,8 @@ class MediaFileTest < ActiveSupport::TestCase fixtures_file_path('artist2_album3.opus'), fixtures_file_path('artist2_album3.wma'), fixtures_file_path('artist2_album3.oga'), - fixtures_file_path('artist1_album1.m4a') + fixtures_file_path('artist1_album1.m4a'), + fixtures_file_path('various_artists.mp3') ] Setting.media_path = Rails.root.join('test', 'fixtures', 'files') @@ -62,6 +63,7 @@ class MediaFileTest < ActiveSupport::TestCase assert_equal 'mp3_sample', tag_info[:name] assert_equal 'album2', tag_info[:album_name] assert_equal 'artist1', tag_info[:artist_name] + assert_equal 'artist1', tag_info[:albumartist_name] assert_equal 1, tag_info[:tracknum] assert_equal 8, tag_info[:length] end @@ -72,6 +74,7 @@ class MediaFileTest < ActiveSupport::TestCase assert_equal 'flac_sample', tag_info[:name] assert_equal 'album1', tag_info[:album_name] assert_equal 'artist1', tag_info[:artist_name] + assert_equal 'artist1', tag_info[:albumartist_name] assert_equal 1, tag_info[:tracknum] assert_equal 8, tag_info[:length] end @@ -82,6 +85,7 @@ class MediaFileTest < ActiveSupport::TestCase assert_equal 'ogg_sample', tag_info[:name] assert_equal 'album3', tag_info[:album_name] assert_equal 'artist2', tag_info[:artist_name] + assert_equal 'artist2', tag_info[:albumartist_name] assert_equal 1, tag_info[:tracknum] assert_equal 8, tag_info[:length] end @@ -92,6 +96,7 @@ class MediaFileTest < ActiveSupport::TestCase assert_equal 'wav_sample', tag_info[:name] assert_equal 'album3', tag_info[:album_name] assert_equal 'artist2', tag_info[:artist_name] + assert_equal 'artist2', tag_info[:albumartist_name] assert_equal 1, tag_info[:tracknum] assert_equal 8, tag_info[:length] end @@ -102,6 +107,7 @@ class MediaFileTest < ActiveSupport::TestCase assert_equal 'opus_sample', tag_info[:name] assert_equal 'album3', tag_info[:album_name] assert_equal 'artist2', tag_info[:artist_name] + assert_equal 'artist2', tag_info[:albumartist_name] assert_equal 1, tag_info[:tracknum] assert_equal 8, tag_info[:length] end @@ -112,6 +118,7 @@ class MediaFileTest < ActiveSupport::TestCase assert_equal 'm4a_sample', tag_info[:name] assert_equal 'album1', tag_info[:album_name] assert_equal 'artist1', tag_info[:artist_name] + assert_equal 'artist1', tag_info[:albumartist_name] assert_equal 1, tag_info[:tracknum] assert_equal 8, tag_info[:length] end @@ -122,6 +129,7 @@ class MediaFileTest < ActiveSupport::TestCase assert_equal 'oga_sample', tag_info[:name] assert_equal 'album3', tag_info[:album_name] assert_equal 'artist2', tag_info[:artist_name] + assert_equal 'artist2', tag_info[:albumartist_name] assert_equal 1, tag_info[:tracknum] assert_equal 8, tag_info[:length] end @@ -132,6 +140,7 @@ class MediaFileTest < ActiveSupport::TestCase assert_equal 'wma_sample', tag_info[:name] assert_equal 'album3', tag_info[:album_name] assert_equal 'artist2', tag_info[:artist_name] + assert_equal 'artist2', tag_info[:albumartist_name] assert_equal 1, tag_info[:tracknum] assert_equal 8, tag_info[:length] end diff --git a/test/models/media_test.rb b/test/models/media_test.rb index 87b985d8..c4822ea3 100644 --- a/test/models/media_test.rb +++ b/test/models/media_test.rb @@ -11,25 +11,28 @@ class MediaTest < ActiveSupport::TestCase end test 'should create all records in database when synced' do - assert_equal 2, Artist.count - assert_equal 3, Album.count - assert_equal 8, Song.count + assert_equal 3, Artist.count + assert_equal 4, Album.count + assert_equal 9, Song.count end test 'should create associations between artists and albums' do assert_equal Album.where(name: %w(album1 album2)).ids.sort, Artist.find_by_name('artist1').albums.ids.sort assert_equal Album.where(name: 'album3').ids.sort, Artist.find_by_name('artist2').albums.ids.sort + assert_equal Album.where(name: 'album4').ids.sort, Artist.find_by_is_various(true).albums.ids.sort end test 'should create associations between albums and songs' do assert_equal Song.where(name: %w(flac_sample m4a_sample)).ids.sort, Album.find_by_name('album1').songs.ids.sort assert_equal Song.where(name: 'mp3_sample').ids.sort, Album.find_by_name('album2').songs.ids.sort assert_equal Song.where(name: %w(ogg_sample wav_sample opus_sample oga_sample wma_sample)).ids.sort, Album.find_by_name('album3').songs.ids.sort + assert_equal Song.where(name: %w(various_artists_sample)).ids.sort, Album.find_by_name('album4').songs.ids.sort end test 'should create associations between artists and songs' do - assert_equal Song.where(name: %w(flac_sample mp3_sample m4a_sample)).ids.sort, Artist.find_by_name('artist1').songs.ids.sort + assert_equal Song.where(name: %w(flac_sample mp3_sample m4a_sample various_artists_sample)).ids.sort, Artist.find_by_name('artist1').songs.ids.sort assert_equal Song.where(name: %w(ogg_sample wav_sample opus_sample oga_sample wma_sample)).ids.sort, Artist.find_by_name('artist2').songs.ids.sort + assert_equal [], Artist.find_by_is_various(true).songs.ids.sort end test 'should change associations when modify album info on file' do @@ -42,7 +45,7 @@ class MediaTest < ActiveSupport::TestCase end test 'should change associations when modify artist info on file' do - MediaFile.stub(:file_info, media_file_info_stub(file_fixture('artist1_album2.mp3'), artist_name: 'artist2')) do + MediaFile.stub(:file_info, media_file_info_stub(file_fixture('artist1_album2.mp3'), artist_name: 'artist2', albumartist_name: 'artist2')) do Media.sync assert_equal Album.where(name: %w(album2 album3)).ids.sort, Artist.find_by_name('artist2').albums.ids.sort