Skip to content

Commit

Permalink
tests: add more filter tests
Browse files Browse the repository at this point in the history
  • Loading branch information
double-beep committed Aug 20, 2024
1 parent 727fc34 commit 3566e09
Show file tree
Hide file tree
Showing 7 changed files with 158 additions and 43 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>2.0.13</version>
<version>2.0.15</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ public boolean isHit() {
&& post.getBody() != null
&& "question".equals(post.getPostType())
) {
return !CheckUtils.checkIfNoCodeBlock(post.getLastBody())
&& CheckUtils.checkIfNoCodeBlock(post.getBody());
return CheckUtils.containsCode(post.getLastBody())
&& !CheckUtils.containsCode(post.getBody());
}

return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ public static String checkForLongWords(String target) {
return null;
}

public static boolean checkIfNoCodeBlock(String target) {
return !target.contains("<code>");
public static boolean containsCode(String target) {
return target.contains("<code>");
}

public static double getJaroWinklerScore(String original, String target, double percentage) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,54 +3,52 @@
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(
Post post1 = FilterTestUtils.getSamplePost(
"This was my question. PROBLEM SOLVED: do this",
"This is my question. It is quite big.",
"title",
"title",
"edit"
"edit",
"question"
);

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

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

// more than one blacklisted words
Post post4 = getSamplePost(
Post post4 = FilterTestUtils.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"
"problem fixed, approval overriden",
"question"
);

assertEquals(new BlacklistedFilter(0, post1).isHit(), true);
Expand All @@ -62,26 +60,4 @@ public void hitTest() throws IOException {
// 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
@@ -0,0 +1,64 @@
package bugs.stackoverflow.belisarius.filters;

import java.io.IOException;

import bugs.stackoverflow.belisarius.models.Post;

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

import org.junit.jupiter.api.Test;

public class CodeRemovedFilterTest {
@Test
public void hitTest() throws IOException {
// body is null
Post post1 = FilterTestUtils.getSamplePost(
null,
"don't care what <blockquote>this</blockquote> text <code>says</code>",
"title1",
"title",
"edit",
"question"
);

// last body is null
Post post2 = FilterTestUtils.getSamplePost(
"don't care what this text says",
null,
"title",
null,
"edit",
"question"
);

// code removed in answer (shouldn't be considered)
Post post3 = FilterTestUtils.getSamplePost(
"This is the new answer body.",
"This was the last answer body.",
"title",
null,
"edit",
"answer"
);

// code removed in question
Post post4 = FilterTestUtils.getSamplePost(
"<p>Oops! All code was removed!</p>",
"<p>Question text includes <code>some inline code</code>"
+ ", but some <pre><code>blocks of code</code></pre>, too</p>",
"title",
null,
"removed 20 characters from body",
"question"
);

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

CodeRemovedFilter filter4 = new CodeRemovedFilter(0, post4);
assertEquals(filter4.isHit(), true);
// total score is always 1
assertEquals(filter4.getTotalScore(), 1.0);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package bugs.stackoverflow.belisarius.filters;

import java.io.IOException;

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

import com.google.gson.JsonObject;

public class FilterTestUtils {
public static final ApiService apiService = new ApiService("stackoverflow");
public static JsonObject json;

public static Post getSamplePost(
String body,
String lastBody,
String title,
String lastTitle,
String summary,
String postType
) throws IOException {
setJson();

JsonObject clone = json.deepCopy();

if (body == null) {
clone.remove("body");
} else {
clone.addProperty("body", body);
}

if (lastBody == null) {
clone.remove("last_body");
} else {
clone.addProperty("last_body", lastBody);
}

if (title == null) {
clone.remove("title");
} else {
clone.addProperty("title", title);
}

if (lastTitle == null) {
clone.remove("last_title");
} else {
clone.addProperty("last_title", lastTitle);
}

if (summary == null) {
clone.remove("comment");
} else {
clone.addProperty("comment", summary);
}

if (postType == null) {
clone.remove("post_type");
} else {
clone.addProperty("post_type", postType);
}

return PostUtils.getPost(clone, "stackoverflow", "title");
}

private static void setJson() throws IOException {
if (json != null) {
return;
}

json = apiService.getLatestRevisions("2276572", 1)
.get("items").getAsJsonArray()
.get(2).getAsJsonObject();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ public void fewUniqueCharactersReasonTest() {

@Test
public void checkIfNoCodeBlockTest() {
assertTrue(CheckUtils.checkIfNoCodeBlock("Not a code block"));
assertFalse(CheckUtils.checkIfNoCodeBlock("<code>This is a code block</code>"));
assertFalse(CheckUtils.containsCode("Not a code block"));
assertTrue(CheckUtils.containsCode("<code>This is a code block</code>"));
}

@Test
Expand Down

0 comments on commit 3566e09

Please sign in to comment.