-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from perebaj/save_nl
✨ nl basic operations
- Loading branch information
Showing
6 changed files
with
196 additions
and
66 deletions.
There are no files selected for viewing
This file contains 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 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 was deleted.
Oops, something went wrong.
This file contains 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,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 | ||
} |
This file contains 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,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 | ||
} |
This file was deleted.
Oops, something went wrong.