-
Notifications
You must be signed in to change notification settings - Fork 87
Add AudioBlock implementation with tests #187
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
aydinomer00
wants to merge
4
commits into
jomei:main
Choose a base branch
from
aydinomer00:feature/downloadable-interface
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
543752e
Add DownloadableFileBlock interface for Pdf, File, and Image blocks
aydinomer00 8f893fb
Add AudioBlock implementation with tests
aydinomer00 3ee7835
Move BlockTypeAudio constant to be grouped with other media types
aydinomer00 d2b6702
Move BlockTypeAudio constant to be grouped with other media types
aydinomer00 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| package notionapi_test | ||
|
|
||
| import ( | ||
| "github.com/jomei/notionapi" | ||
| "testing" | ||
| "time" | ||
| ) | ||
|
|
||
| func TestAudioBlock_GetURL(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| block *notionapi.AudioBlock | ||
| want string | ||
| }{ | ||
| { | ||
| name: "returns internal file URL", | ||
| block: ¬ionapi.AudioBlock{ | ||
| Audio: notionapi.Audio{ | ||
| File: ¬ionapi.FileObject{ | ||
| URL: "https://example.com/internal.mp3", | ||
| }, | ||
| }, | ||
| }, | ||
| want: "https://example.com/internal.mp3", | ||
| }, | ||
| { | ||
| name: "returns external file URL", | ||
| block: ¬ionapi.AudioBlock{ | ||
| Audio: notionapi.Audio{ | ||
| External: ¬ionapi.FileObject{ | ||
| URL: "https://example.com/external.mp3", | ||
| }, | ||
| }, | ||
| }, | ||
| want: "https://example.com/external.mp3", | ||
| }, | ||
| { | ||
| name: "returns empty string when no URL", | ||
| block: ¬ionapi.AudioBlock{}, | ||
| want: "", | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| if got := tt.block.GetURL(); got != tt.want { | ||
| t.Errorf("AudioBlock.GetURL() = %v, want %v", got, tt.want) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestAudioBlock_GetExpiryTime(t *testing.T) { | ||
| now := time.Now() | ||
| tests := []struct { | ||
| name string | ||
| block *notionapi.AudioBlock | ||
| want *time.Time | ||
| }{ | ||
| { | ||
| name: "returns expiry time for internal file", | ||
| block: ¬ionapi.AudioBlock{ | ||
| Audio: notionapi.Audio{ | ||
| File: ¬ionapi.FileObject{ | ||
| ExpiryTime: &now, | ||
| }, | ||
| }, | ||
| }, | ||
| want: &now, | ||
| }, | ||
| { | ||
| name: "returns nil for external file", | ||
| block: ¬ionapi.AudioBlock{ | ||
| Audio: notionapi.Audio{ | ||
| External: ¬ionapi.FileObject{}, | ||
| }, | ||
| }, | ||
| want: nil, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| got := tt.block.GetExpiryTime() | ||
| if got != tt.want { | ||
| t.Errorf("AudioBlock.GetExpiryTime() = %v, want %v", got, tt.want) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // Compile-time interface check | ||
| var _ notionapi.DownloadableFileBlock = (*notionapi.AudioBlock)(nil) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| package notionapi | ||
|
|
||
| import "time" | ||
|
|
||
| // DownloadableFileBlock is an interface for blocks that can be downloaded | ||
| // such as Pdf, FileBlock, and Image | ||
| type DownloadableFileBlock interface { | ||
| Block | ||
| GetURL() string | ||
| GetExpiryTime() *time.Time | ||
| } | ||
|
|
||
| // GetURL implements DownloadableFileBlock interface for PdfBlock | ||
| func (b *PdfBlock) GetURL() string { | ||
| if b.Pdf.File != nil { | ||
| return b.Pdf.File.URL | ||
| } | ||
| if b.Pdf.External != nil { | ||
| return b.Pdf.External.URL | ||
| } | ||
| return "" | ||
| } | ||
|
|
||
| // GetURL implements DownloadableFileBlock interface for AudioBlock by returning | ||
| // the URL of the audio file. For internal files, this will be a Notion URL with | ||
| // an expiry time. For external files, this will be the external URL. | ||
| func (b *AudioBlock) GetURL() string { | ||
| return b.Audio.GetURL() | ||
| } | ||
|
|
||
| // GetExpiryTime implements DownloadableFileBlock interface for AudioBlock by returning | ||
| // the expiry time of the internal audio file. Returns nil for external files or if no | ||
| // expiry time is set. | ||
| func (b *AudioBlock) GetExpiryTime() *time.Time { | ||
| if b.Audio.File != nil { | ||
| return b.Audio.File.ExpiryTime | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| // GetExpiryTime implements DownloadableFileBlock interface for PdfBlock | ||
| func (b *PdfBlock) GetExpiryTime() *time.Time { | ||
| if b.Pdf.File != nil { | ||
| return b.Pdf.File.ExpiryTime | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| // GetURL implements DownloadableFileBlock interface for FileBlock | ||
| func (b *FileBlock) GetURL() string { | ||
| if b.File.File != nil { | ||
| return b.File.File.URL | ||
| } | ||
| if b.File.External != nil { | ||
| return b.File.External.URL | ||
| } | ||
| return "" | ||
| } | ||
|
|
||
| // GetExpiryTime implements DownloadableFileBlock interface for FileBlock | ||
| func (b *FileBlock) GetExpiryTime() *time.Time { | ||
| if b.File.File != nil { | ||
| return b.File.File.ExpiryTime | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| // GetURL implements DownloadableFileBlock interface for ImageBlock | ||
| func (b *ImageBlock) GetURL() string { | ||
| return b.Image.GetURL() | ||
| } | ||
|
|
||
| // GetExpiryTime implements DownloadableFileBlock interface for ImageBlock | ||
| func (b *ImageBlock) GetExpiryTime() *time.Time { | ||
| if b.Image.File != nil { | ||
| return b.Image.File.ExpiryTime | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| // Verify that types implement DownloadableFileBlock interface | ||
| var ( | ||
| _ DownloadableFileBlock = (*PdfBlock)(nil) | ||
| _ DownloadableFileBlock = (*FileBlock)(nil) | ||
| _ DownloadableFileBlock = (*ImageBlock)(nil) | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| package notionapi | ||
|
|
||
| import ( | ||
| "testing" | ||
| "time" | ||
| ) | ||
|
|
||
| func TestPdfBlockImplementsDownloadableFileBlock(t *testing.T) { | ||
| // Test setup | ||
| now := time.Now() | ||
| pdfBlock := &PdfBlock{ | ||
| Pdf: Pdf{ | ||
| File: &FileObject{ | ||
| URL: "https://example.com/file.pdf", | ||
| ExpiryTime: &now, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| // Test GetURL | ||
| if url := pdfBlock.GetURL(); url != "https://example.com/file.pdf" { | ||
| t.Errorf("Expected URL to be 'https://example.com/file.pdf', got %s", url) | ||
| } | ||
|
|
||
| // Test GetExpiryTime | ||
| if expiry := pdfBlock.GetExpiryTime(); expiry != &now { | ||
| t.Errorf("Expected expiry time to be %v, got %v", now, expiry) | ||
| } | ||
| } | ||
|
|
||
| func TestFileBlockImplementsDownloadableFileBlock(t *testing.T) { | ||
| // Test setup | ||
| now := time.Now() | ||
| fileBlock := &FileBlock{ | ||
| File: BlockFile{ | ||
| File: &FileObject{ | ||
| URL: "https://example.com/file.txt", | ||
| ExpiryTime: &now, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| // Test GetURL | ||
| if url := fileBlock.GetURL(); url != "https://example.com/file.txt" { | ||
| t.Errorf("Expected URL to be 'https://example.com/file.txt', got %s", url) | ||
| } | ||
|
|
||
| // Test GetExpiryTime | ||
| if expiry := fileBlock.GetExpiryTime(); expiry != &now { | ||
| t.Errorf("Expected expiry time to be %v, got %v", now, expiry) | ||
| } | ||
| } | ||
|
|
||
| func TestImageBlockImplementsDownloadableFileBlock(t *testing.T) { | ||
| // Test setup | ||
| now := time.Now() | ||
| imageBlock := &ImageBlock{ | ||
| Image: Image{ | ||
| File: &FileObject{ | ||
| URL: "https://example.com/image.jpg", | ||
| ExpiryTime: &now, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| // Test GetURL | ||
| if url := imageBlock.GetURL(); url != "https://example.com/image.jpg" { | ||
| t.Errorf("Expected URL to be 'https://example.com/image.jpg', got %s", url) | ||
| } | ||
|
|
||
| // Test GetExpiryTime | ||
| if expiry := imageBlock.GetExpiryTime(); expiry != &now { | ||
| t.Errorf("Expected expiry time to be %v, got %v", now, expiry) | ||
| } | ||
| } | ||
|
|
||
| func TestExternalURLCases(t *testing.T) { | ||
| // Test External URLs for each block type | ||
| testCases := []struct { | ||
| name string | ||
| block DownloadableFileBlock | ||
| expected string | ||
| }{ | ||
| { | ||
| name: "PDF with external URL", | ||
| block: &PdfBlock{ | ||
| Pdf: Pdf{ | ||
| External: &FileObject{ | ||
| URL: "https://external.com/file.pdf", | ||
| }, | ||
| }, | ||
| }, | ||
| expected: "https://external.com/file.pdf", | ||
| }, | ||
| { | ||
| name: "File with external URL", | ||
| block: &FileBlock{ | ||
| File: BlockFile{ | ||
| External: &FileObject{ | ||
| URL: "https://external.com/file.txt", | ||
| }, | ||
| }, | ||
| }, | ||
| expected: "https://external.com/file.txt", | ||
| }, | ||
| { | ||
| name: "Image with external URL", | ||
| block: &ImageBlock{ | ||
| Image: Image{ | ||
| External: &FileObject{ | ||
| URL: "https://external.com/image.jpg", | ||
| }, | ||
| }, | ||
| }, | ||
| expected: "https://external.com/image.jpg", | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range testCases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| if url := tc.block.GetURL(); url != tc.expected { | ||
| t.Errorf("Expected URL to be '%s', got '%s'", tc.expected, url) | ||
| } | ||
| }) | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Other constants are grouped. Maybe then put this
BlockTypeAudoafterimageand beforevideoso it's grouped as well?