Skip to content

Commit

Permalink
(#102) LinkGenerator: more test stubs
Browse files Browse the repository at this point in the history
  • Loading branch information
ForNeVeR committed Oct 5, 2021
1 parent c3ab60d commit 68b4d18
Show file tree
Hide file tree
Showing 7 changed files with 160 additions and 18 deletions.
5 changes: 5 additions & 0 deletions Emulsion.Database/DataStorage.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module Emulsion.Database.DataStorage

open Emulsion.Database.Models

let getById (databaseSettings: unit) (id: string): Async<TelegramContent> = failwithf "TODO"
1 change: 1 addition & 0 deletions Emulsion.Database/Emulsion.Database.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<Compile Include="EmulsionDbContext.fs" />
<Compile Include="Migrations\*.fs" />
<Compile Include="Initializer.fs" />
<Compile Include="DataStorage.fs" />
</ItemGroup>

<ItemGroup>
Expand Down
6 changes: 4 additions & 2 deletions Emulsion.Database/Models.fs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
namespace Emulsion.Database.Models

open System
open System.ComponentModel.DataAnnotations

[<CLIMutable>]
type TelegramContent = {
[<Key>] Id: Guid
[<Key>] Id: string
ChatUsername: string
MessageId: int64
FileId: string
}
2 changes: 1 addition & 1 deletion Emulsion.Tests/Telegram/FunogramTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ let private forwardingUser = createUser (Some "forwardingUser") "" None

module ReadMessageTests =
let private readMessage m =
let links = Async.RunSynchronously(LinkGenerator.gatherLinks m)
let links = Async.RunSynchronously(LinkGenerator.gatherLinks () m)
MessageConverter.read selfUserId (m, links)

[<Fact>]
Expand Down
160 changes: 147 additions & 13 deletions Emulsion.Tests/Telegram/LinkGeneratorTests.fs
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
module Emulsion.Tests.Telegram.LinkGeneratorTests

open Emulsion.Telegram
open Funogram.Telegram.Types
open Xunit

open Emulsion.Database
open Emulsion.Telegram

let private databaseSettings = ()
let private linkBase = "https://example.com"
let private chatName = "test_chat"
let private fileId = "123456"

let private messageTemplate =
{ defaultMessage with
Chat =
Expand All @@ -14,19 +20,147 @@ let private messageTemplate =
}
}

let private messageWithDocument =
{ messageTemplate with
Document = Some {
FileId = fileId
Thumb = None
FileName = None
MimeType = None
FileSize = None
}
}

let private messageWithAudio =
{ messageTemplate with
Audio = Some {
FileId = fileId
Duration = 0
Performer = None
Title = None
MimeType = None
FileSize = None
Thumb = None
}
}

let private messageWithAnimation =
{ messageTemplate with
Animation = Some {
FileId = fileId
Width = 0
Height = 0
Duration = 0
Thumb = None
FileName = None
MimeType = None
FileSize = None
}
}

let private messageWithPhoto =
{ messageTemplate with
Photo = Some(upcast [|{
FileId = fileId
Width = 0
Height = 0
FileSize = None
}|])
}

let private messageWithSticker =
{ messageTemplate with
Sticker = Some {
FileId = fileId
Width = 0
Height = 0
IsAnimated = false
Thumb = None
Emoji = None
SetName = None
MaskPosition = None
FileSize = None
}
}

let private messageWithVideo =
{ messageTemplate with
Video = Some {
FileId = fileId
Width = 0
Height = 0
Duration = 0
Thumb = None
MimeType = None
FileSize = None
}
}

let private messageWithVoice =
{ messageTemplate with
Voice = Some {
FileId = fileId
Duration = 0
MimeType = None
FileSize = None
}
}

let private messageWithVideoNote =
{ messageTemplate with
VideoNote = Some {
FileId = fileId
Length = 0
Duration = 0
Thumb = None
FileSize = None
}
}

let private doBasicLinkTest message =
let links = Async.RunSynchronously(LinkGenerator.gatherLinks message)
let links = Async.RunSynchronously(LinkGenerator.gatherLinks () message)
Assert.Equal(Some $"https://t.me/{chatName}/{message.MessageId}", links.ContentLink)

let private doDatabaseLinkTest fileId message =
Async.RunSynchronously <| async {
let! links = LinkGenerator.gatherLinks databaseSettings message
let link = Option.get links.ContentLink
Assert.StartsWith(linkBase, link)
let id = link.Substring(linkBase.Length + 1)
let! content = DataStorage.getById databaseSettings id

Assert.Equal(message.MessageId, content.MessageId)
Assert.Equal(message.Chat.Username, Some content.ChatUsername)
Assert.Equal(fileId, content.FileId)
}


[<Fact>]
let documentLinkTest(): unit =
doBasicLinkTest
{ messageTemplate with
Document = Some {
FileId = ""
Thumb = None
FileName = None
MimeType = None
FileSize = None
}
}
let documentLinkTest(): unit = doBasicLinkTest messageWithDocument

[<Fact>]
let databaseDocumentTest(): unit = doDatabaseLinkTest fileId messageWithDocument

[<Fact>]
let databaseAudioTest(): unit = doDatabaseLinkTest fileId messageWithAudio

[<Fact>]
let databaseAnimationTest(): unit = doDatabaseLinkTest fileId messageWithAnimation

[<Fact>]
let databasePhotoTest(): unit = doDatabaseLinkTest fileId messageWithPhoto

[<Fact>]
let databaseStickerTest(): unit = doDatabaseLinkTest fileId messageWithSticker

[<Fact>]
let databaseVideoTest(): unit = doDatabaseLinkTest fileId messageWithVideo

[<Fact>]
let databaseVoiceTest(): unit = doDatabaseLinkTest fileId messageWithVoice

[<Fact>]
let databaseVideoNoteTest(): unit = doDatabaseLinkTest fileId messageWithVideoNote

[<Fact>]
let multiplePhotosTest(): unit = Assert.True false
2 changes: 1 addition & 1 deletion Emulsion/Telegram/Funogram.fs
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ let internal processSendResult(result: Result<'a, ApiResponseError>): unit =
failwith $"Telegram API Call processing error {e.ErrorCode}: {e.Description}"

let private extractLinkData message =
message, Async.RunSynchronously(gatherLinks message)
message, Async.RunSynchronously(gatherLinks () message)

let internal processMessage (context: {| SelfUserId: int64; GroupId: int64 |})
(message: FunogramMessage): Message option =
Expand Down
2 changes: 1 addition & 1 deletion Emulsion/Telegram/LinkGenerator.fs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ let private gatherMessageLink(message: FunogramMessage) =
| { Text = Some _} | { Poll = Some _ } -> None
| _ -> getMessageLink message

let gatherLinks(message: FunogramMessage): Async<TelegramThreadLinks> = async {
let gatherLinks (databaseSettings: Unit) (message: FunogramMessage): Async<TelegramThreadLinks> = async {
return {
ContentLink = gatherMessageLink message
ReplyToContentLink = message.ReplyToMessage |> Option.bind gatherMessageLink
Expand Down

0 comments on commit 68b4d18

Please sign in to comment.