Skip to content

Commit

Permalink
feat: add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeKleinsorge committed Jun 25, 2023
1 parent 445a42c commit 971169b
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 4 deletions.
25 changes: 25 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: CI

on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.16

- name: Run tests
run: go test ./...
6 changes: 5 additions & 1 deletion .github/workflows/remind.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@ jobs:
with:
go-version: 1.16

- name: Get dependencies
run: |
go get -v -t -d ./...
- name: Build
run: go build
run: go build .

- name: Send email
env:
Expand Down
7 changes: 4 additions & 3 deletions remind.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package remind

import (
"bytes"
Expand Down Expand Up @@ -66,6 +66,7 @@ func main() {
log.Println("Email sent successfully!")
}

// Parse the clippings from the Kindle file
func parseClippings(data string) []Clipping {
// Split the data into individual clippings
delimiter := "=========="
Expand Down Expand Up @@ -117,7 +118,7 @@ func parseClippings(data string) []Clipping {
return clippingSlice
}

// selects a given number of random clips from the clippings.
// Selects a given number of random clips from the clippings
func selectRandomClippings(clippings []Clipping, count int) []Clipping {
// Set the random seed
rand.Seed(time.Now().UnixNano())
Expand All @@ -142,7 +143,7 @@ func extractSubmatch(clipping string, regex *regexp.Regexp, label string) string
return match[1]
}

// sendEmail sends an email with the selected quotes.
// Send an email with the selected clippings
func sendEmail(apiKey, senderEmail, recipientEmail, emailContent string) error {

message := mail.NewSingleEmail(
Expand Down
93 changes: 93 additions & 0 deletions remind_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package remind

import (
"reflect"
"testing"
)

func TestParseClippings(t *testing.T) {
data := "clipping 1(Author 1) on page 1 Added on 2023-06-01\nHighlight 1\n==========\nclipping 2(Author 2) on page 2 Added on 2023-06-02\nHighlight 2\n"
expected := []Clipping{
{
Title: "clipping 1",
Author: "Author 1",
Page: "1",
When: "2023-06-01",
Highlight: "Highlight 1",
},
{
Title: "clipping 2",
Author: "Author 2",
Page: "2",
When: "2023-06-02",
Highlight: "Highlight 2",
},
}
result := parseClippings(data)

if len(result) != len(expected) {
t.Errorf("Unexpected number of clippings. Got: %d, Expected: %d", len(result), len(expected))
}

for i := range result {
if result[i].Title != expected[i].Title ||
result[i].Author != expected[i].Author ||
result[i].Page != expected[i].Page ||
result[i].When != expected[i].When ||
result[i].Highlight != expected[i].Highlight {
t.Errorf("Unexpected clipping at index %d. Got: %+v, Expected: %+v", i, result[i], expected[i])
}
}
}

func TestSelectRandomClippings(t *testing.T) {
clippings := []Clipping{
{
Title: "clipping 1",
Author: "Author 1",
Page: "1",
When: "2023-06-01",
Highlight: "Highlight 1",
},
{
Title: "clipping 2",
Author: "Author 2",
Page: "2",
When: "2023-06-02",
Highlight: "Highlight 2",
},
{
Title: "clipping 3",
Author: "Author 3",
Page: "3",
When: "2023-06-03",
Highlight: "Highlight 3",
},
{
Title: "clipping 4",
Author: "Author 4",
Page: "4",
When: "2023-06-04",
Highlight: "Highlight 4",
},
}

selectedClippings := selectRandomClippings(clippings, 2)

if len(selectedClippings) != 2 {
t.Errorf("Unexpected number of selected clippings. Got: %d, Expected: 2", len(selectedClippings))
}

for _, clip := range selectedClippings {
found := false
for _, c := range clippings {
if reflect.DeepEqual(clip, c) {
found = true
break
}
}
if !found {
t.Errorf("Selected clipping not found in the original clippings. Clipping: %v", clip)
}
}
}

0 comments on commit 971169b

Please sign in to comment.