网站首页 网站源码
website
站点相关全部源代码,隐藏了一些关于服务器的信息
using System.Collections.Generic;
using System.Net.Http;
using MongoDB.Bson.Serialization.Attributes;
using MongoDB.Driver.Linq;

namespace Dpz.Core.ServiceTest;

[TestFixture]
public class MusicServiceTest : Basic
{
    [Test]
    public async Task CleanDataAsyncTest()
    {
        var repository = new Repository<Music>(ConnectionString);
        var list = await repository.SearchFor(x => true).ToListAsync();

        var client = new HttpClient();

        var updatedMusics = new List<Music>();
        foreach (var item in list)
        {
            if (string.IsNullOrEmpty(item.LyricUrl))
            {
                continue;
            }

            var request = new HttpRequestMessage(HttpMethod.Get, item.LyricUrl);
            var response = await client.SendAsync(request);

            if (!response.IsSuccessStatusCode)
            {
                continue;
            }

            var lyricContent = await response.Content.ReadAsStringAsync();
            item.LyricContent = lyricContent;
            updatedMusics.Add(item);
        }

        await repository.UpdateAsync(updatedMusics);
    }


    public class MusicTest : BaseEntity
    {
        /// <summary>
        /// 音乐Url
        /// </summary>
        public string? MusicUrl { get; set; }

        /// <summary>
        /// 歌词Url
        /// </summary>
        public string? LyricUrl { get; set; }

        /// <summary>
        /// 音乐源文件名
        /// </summary>
        public string? FileName { get; set; }

        /// <summary>
        /// 音乐标题
        /// </summary>
        public string? Title { get; set; }

        /// <summary>
        /// 音乐时长
        /// </summary>
        public TimeSpan Duration { get; set; }

        /// <summary>
        /// 音乐大小
        /// </summary>
        public long MusicLength { get; set; }

        /// <summary>
        /// 艺术家
        /// </summary>
        public string? Artist { get; set; }

        /// <summary>
        /// 上传时间
        /// </summary>
        [BsonDateTimeOptions(Kind = DateTimeKind.Local)]
        public DateTime UploadTime { get; set; }

        /// <summary>
        /// 最后修改时间
        /// </summary>
        [BsonDateTimeOptions(Kind = DateTimeKind.Local)]
        public DateTime LastUpdateTime { get; set; }

        /// <summary>
        /// 封面ID
        /// </summary>
        public string? CoverUrl { get; set; }

        /// <summary>
        /// 来源
        /// </summary>
        public string? From { get; set; }

        /// <summary>
        /// 分组
        /// </summary>
        public List<string>? Group { get; set; }
    }


    [Test]
    public async Task GetMusicsAsyncTest()
    {
        var repository = new Repository<MusicTest>(ConnectionString, nameof(Music));
        var list = await repository.SearchFor(x => true).ToListAsync();
        
        Assert.IsNotNull(list);
        
        Assert.IsNotEmpty(list);
    }
}
loading