Skip to content

Commit

Permalink
tests: add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
double-beep committed Aug 9, 2024
1 parent 2e2ac7f commit 727fc34
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package bugs.stackoverflow.belisarius.filters;

import java.io.IOException;

import bugs.stackoverflow.belisarius.models.Post;
import bugs.stackoverflow.belisarius.utils.PostUtils;
import bugs.stackoverflow.belisarius.services.ApiService;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;

import com.google.gson.JsonObject;

public class BlacklistedFilterTest {
private final ApiService apiService = new ApiService("stackoverflow");

@Test
public void hitTest() throws IOException {
// blacklisted word added after edit
Post post1 = getSamplePost(
"This was my question. PROBLEM SOLVED: do this",
"This is my question. It is quite big.",
"title",
"title",
"edit"
);

// blacklisted word existed before edit:
Post post2 = getSamplePost(
"Minor edit, This is my question, Minor edit. PrObLEM fIXeD: do this",
"This is my question. PrObLEM fIXeD: do this",
"title",
"title",
"edit"
);

// blacklisted word inside HTML tag
Post post3 = getSamplePost(
"This was my question. <code>PROBLEM SOLVED</code>: do this",
"This is my question. It is quite big.",
"title",
"title",
"edit"
);

// more than one blacklisted words
Post post4 = getSamplePost(
"This was my question. problem solved. answer: do this",
"This is my question. It is quite big.",
"title",
"[SOLVED] title",
"problem fixed, approval overriden"
);

assertEquals(new BlacklistedFilter(0, post1).isHit(), true);
assertEquals(new BlacklistedFilter(0, post2).isHit(), false);
assertEquals(new BlacklistedFilter(0, post3).isHit(), false);

BlacklistedFilter filter4 = new BlacklistedFilter(0, post4);
assertEquals(filter4.isHit(), true);
// 1 (title) + 1 (edit summary) + 2 (post body) = 4
assertEquals(filter4.getTotalScore(), 4.0);
}

private Post getSamplePost(
String body,
String lastBody,
String title,
String lastTitle,
String summary
) throws IOException {
// choosing this post because it is locked
// if a new revision appears, edit .get(2) accordingly
JsonObject json = apiService.getLatestRevisions("2276572", 1)
.get("items").getAsJsonArray()
.get(2).getAsJsonObject();

json.addProperty("body", body);
json.addProperty("last_body", lastBody);
json.addProperty("title", title);
json.addProperty("last_title", lastTitle);
json.addProperty("comment", summary);

return PostUtils.getPost(json, "stackoverflow", "title");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,21 @@ public void getPostObjectTest() throws IOException {
assertEquals("question", postObject.getPostType());
assertEquals("test", postObject.getTitle());
assertEquals("ABCDE12345", postObject.getPreviousRevisionGuid());

Post locked = belisarius.getPost("2276572");
assertEquals(
locked.getBody(),
"<p>How do I comment a block of lines in YAML?</p>\n"
);
assertEquals(
locked.getLastBody(),
"<p>Does anyone know how to comment a block of lines in yaml?</p>\n"
);
assertEquals(locked.getRevisionGuid(), "ec79a719-0ebf-48ca-ae89-27739738f2b7".toUpperCase());
assertEquals(locked.getPreviousRevisionGuid(), "0db0a737-a330-4625-a537-2bf9e73916a3".toUpperCase());
assertEquals(locked.getComment(), "Active reading [&lt;http://en.wikipedia.org/wiki/YAML&gt;].");
assertEquals(locked.getTitle(), "How do you do block comments in YAML?");
assertEquals(locked.getLastTitle(), "How do you do block comment in yaml?");
}

@Test
Expand Down

0 comments on commit 727fc34

Please sign in to comment.