-
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 #5 from perebaj/save_site_structure
✨ save/get site structure
- Loading branch information
Showing
3 changed files
with
159 additions
and
39 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 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 |
---|---|---|
|
@@ -21,34 +21,35 @@ func TestNLStorageSaveNewsletter(t *testing.T) { | |
database := client.Database(DBName) | ||
collection := database.Collection("newsletter") | ||
|
||
NLStorage := NewNLStorage(client, DBName) | ||
err := NLStorage.SaveNewsletter(ctx, Newsletter{ | ||
want := Newsletter{ | ||
UserEmail: "[email protected]", | ||
URLs: []string{"https://www.google.com"}, | ||
}) | ||
} | ||
|
||
NLStorage := NewNLStorage(client, DBName) | ||
err := NLStorage.SaveNewsletter(ctx, want) | ||
|
||
if err != nil { | ||
t.Fatal("error saving newsletter", err) | ||
} | ||
|
||
var nls []Newsletter | ||
var got []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 { | ||
if err := cursor.All(ctx, &got); 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"}, | ||
}) | ||
if len(got) == 1 { | ||
if !reflect.DeepEqual(got[0], want) { | ||
t.Fatalf("got %v, want %v", got[0], want) | ||
} | ||
} else { | ||
t.Fatal("expected 1 newsletter, got", len(nls)) | ||
t.Fatal("expected 1 newsletter, got", len(got)) | ||
} | ||
|
||
t.Cleanup(teardown(ctx, client, DBName)) | ||
|
@@ -61,28 +62,107 @@ func TestNLStorageNewsletter(t *testing.T) { | |
database := client.Database(DBName) | ||
collection := database.Collection("newsletter") | ||
|
||
_, err := collection.InsertOne(ctx, Newsletter{ | ||
want := Newsletter{ | ||
UserEmail: "[email protected]", | ||
URLs: []string{"https://www.google.com"}, | ||
}) | ||
} | ||
_, err := collection.InsertOne(ctx, want) | ||
|
||
if err != nil { | ||
t.Fatal("error saving newsletter", err) | ||
} | ||
|
||
NLStorage := NewNLStorage(client, DBName) | ||
nls, err := NLStorage.Newsletter() | ||
got, 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"}, | ||
}) | ||
if len(got) == 1 { | ||
if !reflect.DeepEqual(got[0], want) { | ||
t.Fatalf("got %v, want %v", got[0], want) | ||
} | ||
} else { | ||
t.Fatal("expected 1 newsletter, got", len(got)) | ||
} | ||
|
||
t.Cleanup(teardown(ctx, client, DBName)) | ||
} | ||
|
||
func TestNLStorageSaveSite(t *testing.T) { | ||
ctx := context.Background() | ||
client, DBName := setup(ctx, t) | ||
|
||
database := client.Database(DBName) | ||
collection := database.Collection("sites") | ||
|
||
want := []Site{ | ||
{UserEmail: "[email protected]", URL: "https://www.google.com", Content: "HTML", ScrapeDatetime: time.Date(2023, time.August, 14, 15, 30, 0, 0, time.UTC)}, | ||
{UserEmail: "[email protected]", URL: "https://www.google.com", Content: "HTML", ScrapeDatetime: time.Date(2023, time.August, 14, 15, 30, 0, 0, time.UTC)}, | ||
{UserEmail: "[email protected]", URL: "https://www.jj.com", Content: "HTML", ScrapeDatetime: time.Date(2023, time.August, 14, 15, 30, 0, 0, time.UTC)}, | ||
} | ||
|
||
NLStorage := NewNLStorage(client, DBName) | ||
err := NLStorage.SaveSite(ctx, want) | ||
|
||
if err != nil { | ||
t.Fatal("error saving site", err) | ||
} | ||
|
||
var got []Site | ||
cursor, err := collection.Find(context.Background(), bson.M{}) | ||
if err != nil { | ||
t.Fatal("error finding site", err) | ||
} | ||
|
||
if err := cursor.All(ctx, &got); err != nil { | ||
t.Fatal("error decoding site", err) | ||
} | ||
|
||
if len(got) == 3 { | ||
if !reflect.DeepEqual(got, want) { | ||
t.Fatalf("got %v, want %v", got, want) | ||
} | ||
} else { | ||
t.Fatal("expected 2 sites, got", len(got)) | ||
} | ||
|
||
t.Cleanup(teardown(ctx, client, DBName)) | ||
} | ||
|
||
func TestNLStorageSites(t *testing.T) { | ||
ctx := context.Background() | ||
client, DBName := setup(ctx, t) | ||
|
||
want := []Site{ | ||
{UserEmail: "[email protected]", URL: "https://www.google.com", Content: "HTML", ScrapeDatetime: time.Date(2023, time.August, 13, 15, 30, 0, 0, time.UTC)}, | ||
{UserEmail: "[email protected]", URL: "https://www.google.com", Content: "HTML", ScrapeDatetime: time.Date(2023, time.August, 12, 15, 30, 0, 0, time.UTC)}, | ||
{UserEmail: "[email protected]", URL: "https://www.google.com", Content: "HTML", ScrapeDatetime: time.Date(2023, time.August, 11, 15, 30, 0, 0, time.UTC)}, | ||
} | ||
|
||
NLStorage := NewNLStorage(client, DBName) | ||
err := NLStorage.SaveSite(ctx, want) | ||
if err != nil { | ||
t.Fatal("error saving site", err) | ||
} | ||
|
||
got, err := NLStorage.Sites("[email protected]", "https://www.google.com") | ||
if err != nil { | ||
t.Fatal("error getting site", err) | ||
} | ||
|
||
if len(got) == 2 { | ||
assert(t, got[0].UserEmail, want[0].UserEmail) | ||
assert(t, got[0].URL, want[0].URL) | ||
assert(t, got[0].Content, want[0].Content) | ||
assert(t, got[0].ScrapeDatetime, want[0].ScrapeDatetime) | ||
|
||
assert(t, got[1].UserEmail, want[1].UserEmail) | ||
assert(t, got[1].URL, want[1].URL) | ||
assert(t, got[1].Content, want[1].Content) | ||
assert(t, got[1].ScrapeDatetime, want[1].ScrapeDatetime) | ||
} else { | ||
t.Fatal("expected 1 newsletter, got", len(nls)) | ||
t.Fatal("expected 2 sites, got", len(got)) | ||
} | ||
|
||
t.Cleanup(teardown(ctx, client, DBName)) | ||
|