Skip to content

Commit f2cb9bd

Browse files
aydinomer00jomei
authored andcommitted
Add DownloadableFileBlock interface for Pdf, File, and Image blocks
This change: - Adds new interface to unify common functionality - Implements interface for PdfBlock, FileBlock, and ImageBlock - Adds comprehensive test coverage
1 parent 6609d69 commit f2cb9bd

File tree

2 files changed

+195
-0
lines changed

2 files changed

+195
-0
lines changed

downloadable_interface.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package notionapi
2+
3+
import "time"
4+
5+
// DownloadableFileBlock is an interface for blocks that can be downloaded
6+
// such as Pdf, FileBlock, and Image
7+
type DownloadableFileBlock interface {
8+
Block
9+
GetURL() string
10+
GetExpiryTime() *time.Time
11+
}
12+
13+
// GetURL implements DownloadableFileBlock interface for PdfBlock
14+
func (b *PdfBlock) GetURL() string {
15+
if b.Pdf.File != nil {
16+
return b.Pdf.File.URL
17+
}
18+
if b.Pdf.External != nil {
19+
return b.Pdf.External.URL
20+
}
21+
return ""
22+
}
23+
24+
// GetExpiryTime implements DownloadableFileBlock interface for PdfBlock
25+
func (b *PdfBlock) GetExpiryTime() *time.Time {
26+
if b.Pdf.File != nil {
27+
return b.Pdf.File.ExpiryTime
28+
}
29+
return nil
30+
}
31+
32+
// GetURL implements DownloadableFileBlock interface for FileBlock
33+
func (b *FileBlock) GetURL() string {
34+
if b.File.File != nil {
35+
return b.File.File.URL
36+
}
37+
if b.File.External != nil {
38+
return b.File.External.URL
39+
}
40+
return ""
41+
}
42+
43+
// GetExpiryTime implements DownloadableFileBlock interface for FileBlock
44+
func (b *FileBlock) GetExpiryTime() *time.Time {
45+
if b.File.File != nil {
46+
return b.File.File.ExpiryTime
47+
}
48+
return nil
49+
}
50+
51+
// GetURL implements DownloadableFileBlock interface for ImageBlock
52+
func (b *ImageBlock) GetURL() string {
53+
return b.Image.GetURL()
54+
}
55+
56+
// GetExpiryTime implements DownloadableFileBlock interface for ImageBlock
57+
func (b *ImageBlock) GetExpiryTime() *time.Time {
58+
if b.Image.File != nil {
59+
return b.Image.File.ExpiryTime
60+
}
61+
return nil
62+
}
63+
64+
// Verify that types implement DownloadableFileBlock interface
65+
var (
66+
_ DownloadableFileBlock = (*PdfBlock)(nil)
67+
_ DownloadableFileBlock = (*FileBlock)(nil)
68+
_ DownloadableFileBlock = (*ImageBlock)(nil)
69+
)

downloadable_interface_test.go

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
package notionapi
2+
3+
import (
4+
"testing"
5+
"time"
6+
)
7+
8+
func TestPdfBlockImplementsDownloadableFileBlock(t *testing.T) {
9+
// Test setup
10+
now := time.Now()
11+
pdfBlock := &PdfBlock{
12+
Pdf: Pdf{
13+
File: &FileObject{
14+
URL: "https://example.com/file.pdf",
15+
ExpiryTime: &now,
16+
},
17+
},
18+
}
19+
20+
// Test GetURL
21+
if url := pdfBlock.GetURL(); url != "https://example.com/file.pdf" {
22+
t.Errorf("Expected URL to be 'https://example.com/file.pdf', got %s", url)
23+
}
24+
25+
// Test GetExpiryTime
26+
if expiry := pdfBlock.GetExpiryTime(); expiry != &now {
27+
t.Errorf("Expected expiry time to be %v, got %v", now, expiry)
28+
}
29+
}
30+
31+
func TestFileBlockImplementsDownloadableFileBlock(t *testing.T) {
32+
// Test setup
33+
now := time.Now()
34+
fileBlock := &FileBlock{
35+
File: BlockFile{
36+
File: &FileObject{
37+
URL: "https://example.com/file.txt",
38+
ExpiryTime: &now,
39+
},
40+
},
41+
}
42+
43+
// Test GetURL
44+
if url := fileBlock.GetURL(); url != "https://example.com/file.txt" {
45+
t.Errorf("Expected URL to be 'https://example.com/file.txt', got %s", url)
46+
}
47+
48+
// Test GetExpiryTime
49+
if expiry := fileBlock.GetExpiryTime(); expiry != &now {
50+
t.Errorf("Expected expiry time to be %v, got %v", now, expiry)
51+
}
52+
}
53+
54+
func TestImageBlockImplementsDownloadableFileBlock(t *testing.T) {
55+
// Test setup
56+
now := time.Now()
57+
imageBlock := &ImageBlock{
58+
Image: Image{
59+
File: &FileObject{
60+
URL: "https://example.com/image.jpg",
61+
ExpiryTime: &now,
62+
},
63+
},
64+
}
65+
66+
// Test GetURL
67+
if url := imageBlock.GetURL(); url != "https://example.com/image.jpg" {
68+
t.Errorf("Expected URL to be 'https://example.com/image.jpg', got %s", url)
69+
}
70+
71+
// Test GetExpiryTime
72+
if expiry := imageBlock.GetExpiryTime(); expiry != &now {
73+
t.Errorf("Expected expiry time to be %v, got %v", now, expiry)
74+
}
75+
}
76+
77+
func TestExternalURLCases(t *testing.T) {
78+
// Test External URLs for each block type
79+
testCases := []struct {
80+
name string
81+
block DownloadableFileBlock
82+
expected string
83+
}{
84+
{
85+
name: "PDF with external URL",
86+
block: &PdfBlock{
87+
Pdf: Pdf{
88+
External: &FileObject{
89+
URL: "https://external.com/file.pdf",
90+
},
91+
},
92+
},
93+
expected: "https://external.com/file.pdf",
94+
},
95+
{
96+
name: "File with external URL",
97+
block: &FileBlock{
98+
File: BlockFile{
99+
External: &FileObject{
100+
URL: "https://external.com/file.txt",
101+
},
102+
},
103+
},
104+
expected: "https://external.com/file.txt",
105+
},
106+
{
107+
name: "Image with external URL",
108+
block: &ImageBlock{
109+
Image: Image{
110+
External: &FileObject{
111+
URL: "https://external.com/image.jpg",
112+
},
113+
},
114+
},
115+
expected: "https://external.com/image.jpg",
116+
},
117+
}
118+
119+
for _, tc := range testCases {
120+
t.Run(tc.name, func(t *testing.T) {
121+
if url := tc.block.GetURL(); url != tc.expected {
122+
t.Errorf("Expected URL to be '%s', got '%s'", tc.expected, url)
123+
}
124+
})
125+
}
126+
}

0 commit comments

Comments
 (0)