Skip to content

Commit

Permalink
Merge pull request #4 from perebaj/save_nl
Browse files Browse the repository at this point in the history
✨ nl basic operations
  • Loading branch information
perebaj authored Jan 5, 2024
2 parents 8efa29e + 37699ea commit dab8450
Show file tree
Hide file tree
Showing 6 changed files with 196 additions and 66 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ test:

.PHONY: integration-test
integration-test:
go test -timeout 5s -tags=integration ./...
go test -timeout 5s -tags=integration ./... -v

## builds the service
.PHONY: service
Expand Down
15 changes: 12 additions & 3 deletions mongodb/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,21 @@ type Config struct {
URI string
}

// Connect connects to the MongoDB instance.
func Connect(ctx context.Context, cfg Config) (*mongo.Client, error) {
client, err := mongo.Connect(ctx, options.Client().ApplyURI(cfg.URI))
// OpenDB connects to the MongoDB instance.
func OpenDB(ctx context.Context, cfg Config) (*mongo.Client, error) {
bsonOpts := &options.BSONOptions{
UseJSONStructTags: true,
NilSliceAsEmpty: true,
}

client, err := mongo.Connect(ctx, options.Client().
ApplyURI(cfg.URI).
SetBSONOptions(bsonOpts))

if err != nil {
return nil, err
}

err = client.Ping(ctx, nil)
if err != nil {
return nil, err
Expand Down
51 changes: 0 additions & 51 deletions mongodb/db_test.go

This file was deleted.

65 changes: 65 additions & 0 deletions mongodb/newsletter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package mongodb

import (
"context"
"time"

"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
)

// Newsletter is the struct that gather what websites to scrape for an user email
type Newsletter struct {
UserEmail string `bson:"user_email"`
URLs []string `bson:"urls"`
}

// Site is the struct that gather the scraped content of a website
type Site struct {
UserEmail string `bson:"user_email"`
URL string `bson:"url"`
Content string `bson:"content"`
ScrapeDate time.Time `bson:"scrape_date"`
}

// NLStorage joins the Mongo operations for the Newsletter collection
type NLStorage struct {
client *mongo.Client
DBName string
}

// NewNLStorage initializes a new NLStorage
func NewNLStorage(client *mongo.Client, DBName string) *NLStorage {
return &NLStorage{
client: client,
DBName: DBName,
}
}

// SaveNewsletter saves a newsletter in the database
func (m *NLStorage) SaveNewsletter(ctx context.Context, newsletter Newsletter) error {
database := m.client.Database(m.DBName)
collection := database.Collection("newsletter")
_, err := collection.InsertOne(ctx, newsletter)
if err != nil {
return err
}
return nil
}

// Newsletter returns all the newsletters in the database
func (m *NLStorage) Newsletter() ([]Newsletter, error) {
var newsletters []Newsletter
database := m.client.Database(m.DBName)
collection := database.Collection("newsletter")
cursor, err := collection.Find(context.Background(), bson.M{})
if err != nil {
return nil, err
}

if err = cursor.All(context.Background(), &newsletters); err != nil {
return nil, err
}

return newsletters, nil
}
118 changes: 118 additions & 0 deletions mongodb/newsletter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
//go:build integration
// +build integration

package mongodb

import (
"context"
"fmt"
"reflect"
"testing"
"time"

"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
)

func TestNLStorageSaveNewsletter(t *testing.T) {
ctx := context.Background()
client, DBName := setup(ctx, t)

database := client.Database(DBName)
collection := database.Collection("newsletter")

NLStorage := NewNLStorage(client, DBName)
err := NLStorage.SaveNewsletter(ctx, Newsletter{
UserEmail: "[email protected]",
URLs: []string{"https://www.google.com"},
})

if err != nil {
t.Fatal("error saving newsletter", err)
}

var nls []Newsletter
cursor, err := collection.Find(context.Background(), bson.M{})

if err != nil {
t.Fatal("error finding newsletter", err)
}

if err := cursor.All(ctx, &nls); err != nil {
t.Fatal("error decoding newsletter", err)
}

if len(nls) == 1 {
reflect.DeepEqual(nls[0], Newsletter{
UserEmail: "[email protected]",
URLs: []string{"https://www.google.com"},
})
} else {
t.Fatal("expected 1 newsletter, got", len(nls))
}

t.Cleanup(teardown(ctx, client, DBName))
}

func TestNLStorageNewsletter(t *testing.T) {
ctx := context.Background()
client, DBName := setup(ctx, t)

database := client.Database(DBName)
collection := database.Collection("newsletter")

_, err := collection.InsertOne(ctx, Newsletter{
UserEmail: "[email protected]",
URLs: []string{"https://www.google.com"},
})

if err != nil {
t.Fatal("error saving newsletter", err)
}

NLStorage := NewNLStorage(client, DBName)
nls, err := NLStorage.Newsletter()
if err != nil {
t.Fatal("error getting newsletter", err)
}

if len(nls) == 1 {
reflect.DeepEqual(nls[0], Newsletter{
UserEmail: "[email protected]",
URLs: []string{"https://www.google.com"},
})
} else {
t.Fatal("expected 1 newsletter, got", len(nls))
}

t.Cleanup(teardown(ctx, client, DBName))
}

func assert(t testing.TB, got, want interface{}) {
t.Helper()
if got != want {
t.Fatalf("got %v, want %v", got, want)
}
}

func teardown(ctx context.Context, client *mongo.Client, DBName string) func() {
return func() {
if err := client.Database(DBName).Drop(ctx); err != nil {
panic(err)
}
}
}

func setup(ctx context.Context, t testing.TB) (*mongo.Client, string) {
// TODO: Receive the URI from the environment variable
URI := "mongodb://root:root@mongodb:27017/"
client, err := OpenDB(ctx, Config{
URI: URI,
})
if err != nil {
panic(err)
}

DBName := t.Name() + fmt.Sprintf("%d", time.Now().UnixNano())
return client, DBName
}
11 changes: 0 additions & 11 deletions scrape_test.go

This file was deleted.

0 comments on commit dab8450

Please sign in to comment.