Skip to content
This repository has been archived by the owner on Feb 1, 2025. It is now read-only.

Commit

Permalink
Increase model and database test coverage (#87)
Browse files Browse the repository at this point in the history
* edit follow test

* Revert "edit follow test"

This reverts commit 4adee47.

* Add get following and followers test

* remove generic database

* Add collection model tests

* Add dates test
  • Loading branch information
MariemBaccari authored Mar 29, 2023
1 parent 8f0277f commit 775b719
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 113 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
*/
public class UserTests {
private final static int STANDARD_USER_TIMEOUT = 10;
private final static int STANDARD_SLEEP_DELAY = 1000;
User user1;
User user2;
User user3;
Expand Down Expand Up @@ -79,73 +80,31 @@ public void followUserAddsUsernameInFollowingAndFollowers() throws ExecutionExce
Database.addUser(user3).get(STANDARD_USER_TIMEOUT, TimeUnit.SECONDS);

Database.followUser(user2.getUsername(), user3.getUsername());
DatabaseReference user1FollowingRef = Database.database.getReference().child(Database.USERS_PATH + user2.getUsername() + Database.FOLLOWING_PATH);
user1FollowingRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {

Map<String, Boolean> following = (Map<String, Boolean>) snapshot.getValue();
assertTrue(following.get(user3.getUsername()));
}

@Override
public void onCancelled(@NonNull DatabaseError error) {
throw error.toException();
}
});

DatabaseReference user2FollowersRef = Database.database.getReference().child(Database.USERS_PATH + user3.getUsername() + Database.FOLLOWERS_PATH);
user2FollowersRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {

Map<String, Boolean> followers = (Map<String, Boolean>) snapshot.getValue();
assertTrue(followers.get(user2.getUsername()));
}

@Override
public void onCancelled(@NonNull DatabaseError error) {
throw error.toException();
}
});
Thread.sleep(STANDARD_SLEEP_DELAY);

List<String> followers = Database.getUser(user3.getUsername()).get(STANDARD_USER_TIMEOUT, TimeUnit.SECONDS).getFollowers();
List<String> following = Database.getUser(user2.getUsername()).get(STANDARD_USER_TIMEOUT, TimeUnit.SECONDS).getFollowing();

assertTrue(followers.contains(user2.getUsername()));
assertTrue(following.contains(user3.getUsername()));

}

@Test
//Tests that unfollowing a user removes the right username from the following list and the followers list
public void unfollowUserRemovesUsernameFromFollowingAndFollowers() throws ExecutionException, InterruptedException, TimeoutException {
Database.addUser(user2).get(STANDARD_USER_TIMEOUT, TimeUnit.SECONDS);
Database.addUser(user3).get(STANDARD_USER_TIMEOUT, TimeUnit.SECONDS);

Database.followUser(user2.getUsername(), user3.getUsername());
Database.unfollowUser(user2.getUsername(), user3.getUsername());
DatabaseReference user1FollowingRef = Database.database.getReference().child(Database.USERS_PATH + user2.getUsername() + Database.FOLLOWING_PATH);
user1FollowingRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {

Map<String, Boolean> following = (Map<String, Boolean>) snapshot.getValue();
assertFalse(following.get(user3.getUsername()));
}

@Override
public void onCancelled(@NonNull DatabaseError error) {
throw error.toException();
}
});

DatabaseReference user2FollowersRef = Database.database.getReference().child(Database.USERS_PATH + user3.getUsername() + Database.FOLLOWERS_PATH);
user2FollowersRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {

Map<String, Boolean> followers = (Map<String, Boolean>) snapshot.getValue();
assertFalse(followers.get(user2.getUsername()));
}

@Override
public void onCancelled(@NonNull DatabaseError error) {
throw error.toException();
}
});
Thread.sleep(STANDARD_SLEEP_DELAY);

List<String> followers = Database.getUser(user3.getUsername()).get(STANDARD_USER_TIMEOUT, TimeUnit.SECONDS).getFollowers();
List<String> following = Database.getUser(user2.getUsername()).get(STANDARD_USER_TIMEOUT, TimeUnit.SECONDS).getFollowing();

assertFalse(followers.contains(user2.getUsername()));
assertFalse(following.contains(user3.getUsername()));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import java.util.concurrent.CancellationException;
import java.util.concurrent.CompletableFuture;

public class Database implements GenericDatabase {
public class Database {

public static final String USERS_PATH = "Users/";

Expand Down
37 changes: 0 additions & 37 deletions app/src/main/java/com/github/sdp/mediato/data/GenericDatabase.java

This file was deleted.

17 changes: 0 additions & 17 deletions app/src/test/java/com/github/sdp/mediato/ExampleUnitTest.java

This file was deleted.

25 changes: 25 additions & 0 deletions app/src/test/java/com/github/sdp/mediato/formats/DatesTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.github.sdp.mediato.formats;

import static org.junit.Assert.assertEquals;

import org.junit.Test;

import java.time.LocalDate;
import java.util.Date;

public class DatesTests {

@Test
//Tests that the Dates class returns today's date with the right format
public void getsTodaysDateProperly(){
LocalDate today = LocalDate.now();
String expected = pad(today.getDayOfMonth()) + "/" + pad(today.getMonthValue()) + "/" + today.getYear();
assertEquals(expected, Dates.getToday());
}

//Adds leading zero to number
public String pad(int number){
if(number < 10) return "0" + number;
else return String.valueOf(number);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.github.sdp.mediato.model;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertThrows;

import com.github.sdp.mediato.model.media.Collection;
import com.github.sdp.mediato.model.media.CollectionType;
import com.github.sdp.mediato.model.media.Movie;

import org.junit.BeforeClass;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

public class CollectionTests {
private static Map<String, Review> reviews = new HashMap<>();
private static String SAMPLE_TITLE = "Harry Potter";
private static String SAMPLE_DESCRIPTION = "Description";
private static String SAMPLE_URL = "Url";
private static int SAMPLE_ID = 1;

private static String SAMPLE_USERNAME = "testUser";
private static final Movie SAMPLE_MOVIE = new Movie(SAMPLE_TITLE , SAMPLE_DESCRIPTION, SAMPLE_URL, SAMPLE_ID);

@BeforeClass
public static void setUp(){
reviews.put(SAMPLE_TITLE, new Review(SAMPLE_USERNAME, SAMPLE_MOVIE));
}

@Test
//Tests that a custom collection is created properly
public void createsCustomCollectionProperly(){
Collection collection = new Collection("MyCustom", reviews);
assertThat(collection.getCollectionType(), is(CollectionType.CUSTOM));
assertThat(collection.getCollectionName(), is("MyCustom"));
assertThat(CollectionType.CUSTOM.toString(), is("Custom"));
}

@Test
//Tests that a favourites collection is created properly
public void createsFavouriteCollectionProperly(){
Collection collection = new Collection(CollectionType.FAVOURITES, reviews);
assertThat(collection.getCollectionType(), is(CollectionType.FAVOURITES));
assertThat(collection.getCollectionName(), is(CollectionType.FAVOURITES.toString()));
}

@Test
//Tests that a recently watched collection is created properly
public void createsRecentlyWatchedCollectionProperly(){
Collection collection = new Collection(CollectionType.RECENTLY_WATCHED, reviews);
assertThat(collection.getCollectionType(), is(CollectionType.RECENTLY_WATCHED));
assertThat(collection.getCollectionName(), is(CollectionType.RECENTLY_WATCHED.toString()));
}

@Test
//Tests that custom collection creation fails when the wrong constructor is used
public void defaultCollectionConstructorFailsWhenTypeIsCustom(){
assertThrows(
IllegalArgumentException.class,
() -> new Collection(CollectionType.CUSTOM, reviews)
);
}
}

0 comments on commit 775b719

Please sign in to comment.