Skip to content

Conversation

@anirudh711
Copy link

PR Overview

The PR proposes a fix for the following tests -
com.p2sdev.jsonToOrmMapper.AppTest#testValidJSONObject

Build Project

  • To build the project :
mvn clean install
  • To run the test :
mvn test
  • To run the nondex tool on this test :
mvn edu.illinois:nondex-maven-plugin:2.1.1:nondex -Dtest=<path.to.test#testName>

Problem

This flakiness was identified by the nondex tool created by the researchers of UIUC. The above eight tests fail when run on the nondex tool.
This test is failing because the order is not maintained in the assertion although it is the same JSON object.

public void testValidJSONObject() {
String expected = "from django.db import modelsclass" +
" Person(models.Model):" +
"person_id = models.AutoField(primary_key=True)" +
"f_name = models.CharField(max_length=100)" +
"p_id = models.IntegerField()" +
"l_name = models.CharField(max_length=100)";
expected = expected.replaceAll("\\n|\\r\\n|\\r|\\t", "");

This problem of flakiness starts with the JSONToConverter method that creates the object and builds the fields inside it (called columns here). This is being done when a new DjangoModel is being initialized and the function of the class buildClass is where the flakiness lies.

Map<String, JSONTypes> tableColumnsDef = table.getTableColumnsDef();
for(String key : tableColumnsDef.keySet()) {
switch(tableColumnsDef.get(key)) {
case BOOLEAN:
modelClass.append(key)
.append(Model.BOOLEAN.getValue())
.append("\n\t");

Here, the buildClass function initializes tableColumnsDef which is a map, a map does not maintain order which causes the reordering of values and the consequent fail in the assertion.

Fix:

The proposed fix is to use a TreeMap to maintain the order of insertion. A Treemap is used instead of a LinkedHashMap because to maintain the order of insertion of the nested elements and not only the base-level JSON elements.

Note: There is a change in the base test code, shifting the values of l_name and p_id to match with the assertion since a TreeMap will maintain order

@anirudh711 anirudh711 changed the title fix flaky test fix flaky test testValidJSONObject() Nov 22, 2023
@anirudh711 anirudh711 changed the title fix flaky test testValidJSONObject() Fix flaky test testValidJSONObject() Nov 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant