Skip to content

Instantly share code, notes, and snippets.

@rt4914
Last active September 4, 2020 12:44
Show Gist options
  • Save rt4914/9ca5798836e783ec29215dead870bc5b to your computer and use it in GitHub Desktop.
Save rt4914/9ca5798836e783ec29215dead870bc5b to your computer and use it in GitHub Desktop.
Alpha Dummy Progress
package org.oppia.domain.topic
import org.oppia.app.model.ProfileId
import java.util.Date
import javax.inject.Inject
private const val EIGHT_DAYS_IN_MS = 8 * 24 * 60 * 60 * 1000
/** This helper allows tests to easily create dummy progress per profile-basis. */
class StoryProgressTestHelper @Inject constructor(
private val storyProgressController: StoryProgressController
) {
private fun getCurrentTimestamp(): Long {
return Date().time
}
// Returns a timestamp which is atleast a week old than current.
private fun getOldTimestamp(): Long {
return Date().time - EIGHT_DAYS_IN_MS
}
/** Creates a partial story progress for a particular profile. */
fun markAlphaDummyProgress(profileId: ProfileId, timestampOlderThanAWeek: Boolean) {
val timestamp = if (!timestampOlderThanAWeek) {
getCurrentTimestamp()
} else {
getOldTimestamp()
}
storyProgressController.recordCompletedChapter(
profileId,
"iX9kYCjnouWN",
"RRVMHsZ5Mobh",
"K645IfRNzpKy",
timestamp
)
storyProgressController.recordCompletedChapter(
profileId,
"iX9kYCjnouWN",
"RRVMHsZ5Mobh",
"Knvx24p24qPO",
timestamp
)
storyProgressController.recordCompletedChapter(
profileId,
"iX9kYCjnouWN",
"RRVMHsZ5Mobh",
"aAkDKVDR53cG",
timestamp
)
storyProgressController.recordCompletedChapter(
profileId,
"iX9kYCjnouWN",
"RRVMHsZ5Mobh",
"avwshGklKLJE",
timestamp
)
storyProgressController.recordCompletedChapter(
profileId,
"C4fqwrvqWpRm",
"vfJDB3JAdwIx",
"R7WpsSfmDQPV",
timestamp
)
storyProgressController.recordCompletedChapter(
profileId,
"C4fqwrvqWpRm",
"vfJDB3JAdwIx",
"zIBYaqfDJrJC",
timestamp
)
storyProgressController.recordCompletedChapter(
profileId,
"C4fqwrvqWpRm",
"vfJDB3JAdwIx",
"1904tpP0CYwY",
timestamp
)
storyProgressController.recordCompletedChapter(
profileId,
"C4fqwrvqWpRm",
"vfJDB3JAdwIx",
"cQDibOXQbpi7",
timestamp
)
storyProgressController.recordCompletedChapter(
profileId,
"C4fqwrvqWpRm",
"vfJDB3JAdwIx",
"MRJeVrKafW6G",
timestamp
)
storyProgressController.recordCompletedChapter(
profileId,
"C4fqwrvqWpRm",
"vfJDB3JAdwIx",
"hNOP3TwRJhsz",
timestamp
)
storyProgressController.recordCompletedChapter(
profileId,
"C4fqwrvqWpRm",
"vfJDB3JAdwIx",
"zTg2hzTz37jP",
timestamp
)
}
/** Creates a partial story progress for a particular profile. */
fun markPartialStoryProgressForFractions(profileId: ProfileId, timestampOlderThanAWeek: Boolean) {
val timestamp = if (!timestampOlderThanAWeek) {
getCurrentTimestamp()
} else {
getOldTimestamp()
}
storyProgressController.recordCompletedChapter(
profileId,
FRACTIONS_TOPIC_ID,
FRACTIONS_STORY_ID_0,
FRACTIONS_EXPLORATION_ID_0,
timestamp
)
}
/** Creates a partial topic progress for a particular profile. */
fun markPartialTopicProgressForFractions(profileId: ProfileId, timestampOlderThanAWeek: Boolean) {
val timestamp = if (!timestampOlderThanAWeek) {
getCurrentTimestamp()
} else {
getOldTimestamp()
}
storyProgressController.recordCompletedChapter(
profileId,
FRACTIONS_TOPIC_ID,
FRACTIONS_STORY_ID_0,
FRACTIONS_EXPLORATION_ID_0,
timestamp
)
}
/** Marks full story progress for a particular profile. */
fun markFullStoryProgressForFractions(profileId: ProfileId, timestampOlderThanAWeek: Boolean) {
val timestamp = if (!timestampOlderThanAWeek) {
getCurrentTimestamp()
} else {
getOldTimestamp()
}
storyProgressController.recordCompletedChapter(
profileId,
FRACTIONS_TOPIC_ID,
FRACTIONS_STORY_ID_0,
FRACTIONS_EXPLORATION_ID_0,
timestamp
)
storyProgressController.recordCompletedChapter(
profileId,
FRACTIONS_TOPIC_ID,
FRACTIONS_STORY_ID_0,
FRACTIONS_EXPLORATION_ID_1,
timestamp
)
}
/** Marks full topic progress for a particular profile. */
fun markFullTopicProgressForFractions(profileId: ProfileId, timestampOlderThanAWeek: Boolean) {
val timestamp = if (!timestampOlderThanAWeek) {
getCurrentTimestamp()
} else {
getOldTimestamp()
}
storyProgressController.recordCompletedChapter(
profileId,
FRACTIONS_TOPIC_ID,
FRACTIONS_STORY_ID_0,
FRACTIONS_EXPLORATION_ID_0,
timestamp
)
storyProgressController.recordCompletedChapter(
profileId,
FRACTIONS_TOPIC_ID,
FRACTIONS_STORY_ID_0,
FRACTIONS_EXPLORATION_ID_1,
timestamp
)
}
/** Marks one story progress full in ratios exploration for a particular profile. */
fun markFullStoryPartialTopicProgressForRatios(
profileId: ProfileId,
timestampOlderThanAWeek: Boolean
) {
val timestamp = if (!timestampOlderThanAWeek) {
getCurrentTimestamp()
} else {
getOldTimestamp()
}
storyProgressController.recordCompletedChapter(
profileId,
RATIOS_TOPIC_ID,
RATIOS_STORY_ID_0,
RATIOS_EXPLORATION_ID_0,
timestamp
)
storyProgressController.recordCompletedChapter(
profileId,
RATIOS_TOPIC_ID,
RATIOS_STORY_ID_0,
RATIOS_EXPLORATION_ID_1,
timestamp
)
}
/** Marks two partial story progress in ratios exploration for a particular profile. */
fun markTwoPartialStoryProgressForRatios(profileId: ProfileId, timestampOlderThanAWeek: Boolean) {
val timestamp = if (!timestampOlderThanAWeek) {
getCurrentTimestamp()
} else {
getOldTimestamp()
}
storyProgressController.recordCompletedChapter(
profileId,
RATIOS_TOPIC_ID,
RATIOS_STORY_ID_0,
RATIOS_EXPLORATION_ID_0,
timestamp
)
storyProgressController.recordCompletedChapter(
profileId,
RATIOS_TOPIC_ID,
RATIOS_STORY_ID_1,
RATIOS_EXPLORATION_ID_2,
timestamp
)
}
/** Marks exploration [FRACTIONS_EXPLORATION_ID_0] as recently played for a particular profile. */
fun markRecentlyPlayedForFractionsStory0Exploration0(
profileId: ProfileId,
timestampOlderThanAWeek: Boolean
) {
val timestamp = if (!timestampOlderThanAWeek) {
getCurrentTimestamp()
} else {
getOldTimestamp()
}
storyProgressController.recordRecentlyPlayedChapter(
profileId,
FRACTIONS_TOPIC_ID,
FRACTIONS_STORY_ID_0,
FRACTIONS_EXPLORATION_ID_0,
timestamp
)
}
/** Marks exploration [RATIOS_EXPLORATION_ID_0] as recently played for a particular profile. */
fun markRecentlyPlayedForRatiosStory0Exploration0(
profileId: ProfileId,
timestampOlderThanAWeek: Boolean
) {
val timestamp = if (!timestampOlderThanAWeek) {
getCurrentTimestamp()
} else {
getOldTimestamp()
}
storyProgressController.recordRecentlyPlayedChapter(
profileId,
RATIOS_TOPIC_ID,
RATIOS_STORY_ID_0,
RATIOS_EXPLORATION_ID_0,
timestamp
)
}
/** Marks first exploration in both stories of Ratios as recently played for a particular profile. */
fun markRecentlyPlayedForRatiosStory0Exploration0AndStory1Exploration2(
profileId: ProfileId,
timestampOlderThanAWeek: Boolean
) {
val timestamp = if (!timestampOlderThanAWeek) {
getCurrentTimestamp()
} else {
getOldTimestamp()
}
storyProgressController.recordRecentlyPlayedChapter(
profileId,
RATIOS_TOPIC_ID,
RATIOS_STORY_ID_0,
RATIOS_EXPLORATION_ID_0,
timestamp
)
storyProgressController.recordRecentlyPlayedChapter(
profileId,
RATIOS_TOPIC_ID,
RATIOS_STORY_ID_1,
RATIOS_EXPLORATION_ID_2,
timestamp
)
}
/** Marks first exploration in all stories of Ratios & Fractions as recently played for a particular profile. */
fun markRecentlyPlayedForFirstExplorationInAllStoriesInFractionsAndRatios(
profileId: ProfileId,
timestampOlderThanAWeek: Boolean
) {
val timestamp = if (!timestampOlderThanAWeek) {
getCurrentTimestamp()
} else {
getOldTimestamp()
}
storyProgressController.recordRecentlyPlayedChapter(
profileId,
FRACTIONS_TOPIC_ID,
FRACTIONS_STORY_ID_0,
FRACTIONS_EXPLORATION_ID_0,
timestamp
)
storyProgressController.recordRecentlyPlayedChapter(
profileId,
RATIOS_TOPIC_ID,
RATIOS_STORY_ID_0,
RATIOS_EXPLORATION_ID_0,
timestamp
)
storyProgressController.recordRecentlyPlayedChapter(
profileId,
RATIOS_TOPIC_ID,
RATIOS_STORY_ID_1,
RATIOS_EXPLORATION_ID_2,
timestamp
)
}
}
@BenHenning
Copy link

Alternative that let's you monitor the records finishing (which lets you block the HomeFragment until the record is completed):

package org.oppia.domain.topic

import androidx.lifecycle.LiveData
import androidx.lifecycle.MediatorLiveData
import org.oppia.app.model.ProfileId
import org.oppia.util.data.AsyncResult
import java.util.Date
import java.util.concurrent.atomic.AtomicInteger
import javax.inject.Inject

private const val EIGHT_DAYS_IN_MS = 8 * 24 * 60 * 60 * 1000

/** This helper allows tests to easily create dummy progress per profile-basis. */
class StoryProgressTestHelper @Inject constructor(
  private val storyProgressController: StoryProgressController
) {

  private fun getCurrentTimestamp(): Long {
    return Date().time
  }

  // Returns a timestamp which is atleast a week old than current.
  private fun getOldTimestamp(): Long {
    return Date().time - EIGHT_DAYS_IN_MS
  }

  /** Creates a partial story progress for a particular profile. */
  fun markPartialStoryProgressForFractions(profileId: ProfileId, timestampOlderThanAWeek: Boolean) {
    val timestamp = if (!timestampOlderThanAWeek) {
      getCurrentTimestamp()
    } else {
      getOldTimestamp()
    }
    storyProgressController.recordCompletedChapter(
      profileId,
      FRACTIONS_TOPIC_ID,
      FRACTIONS_STORY_ID_0,
      FRACTIONS_EXPLORATION_ID_0,
      timestamp
    )
  }

  /** Creates a partial topic progress for a particular profile. */
  fun markPartialTopicProgressForFractions(profileId: ProfileId, timestampOlderThanAWeek: Boolean) {
    val timestamp = if (!timestampOlderThanAWeek) {
      getCurrentTimestamp()
    } else {
      getOldTimestamp()
    }
    storyProgressController.recordCompletedChapter(
      profileId,
      FRACTIONS_TOPIC_ID,
      FRACTIONS_STORY_ID_0,
      FRACTIONS_EXPLORATION_ID_0,
      timestamp
    )
  }

  fun markAlphaDummyProgress(profileId: ProfileId, timestampOlderThanAWeek: Boolean): LiveData<Boolean> {
    val timestamp = if (!timestampOlderThanAWeek) {
      getCurrentTimestamp()
    } else {
      getOldTimestamp()
    }

    data class ChapterToComplete(val topicId: String, val storyId: String, val expId: String)
    val chapters = listOf(
      ChapterToComplete("iX9kYCjnouWN", "RRVMHsZ5Mobh", "K645IfRNzpKy"),
      ChapterToComplete("iX9kYCjnouWN", "RRVMHsZ5Mobh", "Knvx24p24qPO"),
      ChapterToComplete("iX9kYCjnouWN", "RRVMHsZ5Mobh", "aAkDKVDR53cG"),
      ChapterToComplete("iX9kYCjnouWN", "RRVMHsZ5Mobh", "avwshGklKLJE"),
      ChapterToComplete("C4fqwrvqWpRm", "vfJDB3JAdwIx", "R7WpsSfmDQPV"),
      ChapterToComplete("C4fqwrvqWpRm", "vfJDB3JAdwIx", "zIBYaqfDJrJC"),
      ChapterToComplete("C4fqwrvqWpRm", "vfJDB3JAdwIx", "1904tpP0CYwY"),
      ChapterToComplete("C4fqwrvqWpRm", "vfJDB3JAdwIx", "cQDibOXQbpi7"),
      ChapterToComplete("C4fqwrvqWpRm", "vfJDB3JAdwIx", "MRJeVrKafW6G"),
      ChapterToComplete("C4fqwrvqWpRm", "vfJDB3JAdwIx", "hNOP3TwRJhsz"),
      ChapterToComplete("C4fqwrvqWpRm", "vfJDB3JAdwIx", "zTg2hzTz37jP")
    )

    val combinedLiveData = MediatorLiveData<Boolean>()
    val countDown = AtomicInteger(chapters.size)
    fun updateLiveData(result: AsyncResult<Any?>): Boolean {
      if (result.isSuccess()) {
        if (countDown.decrementAndGet() == 0) {
          combinedLiveData.value = true
        }
        return true
      }
      return false
    }

    chapters.forEach { (topicId, storyId, explorationId) ->
      val recordStatusLiveData = storyProgressController.recordCompletedChapter(
        profileId, topicId, storyId, explorationId, timestamp
      )
      combinedLiveData.addSource(recordStatusLiveData) {
        if (updateLiveData(it)) {
          combinedLiveData.removeSource(recordStatusLiveData)
        }
      }
    }
    return combinedLiveData
  }

  /** Marks full story progress for a particular profile. */
  fun markFullStoryProgressForFractions(profileId: ProfileId, timestampOlderThanAWeek: Boolean) {
    val timestamp = if (!timestampOlderThanAWeek) {
      getCurrentTimestamp()
    } else {
      getOldTimestamp()
    }
    storyProgressController.recordCompletedChapter(
      profileId,
      FRACTIONS_TOPIC_ID,
      FRACTIONS_STORY_ID_0,
      FRACTIONS_EXPLORATION_ID_0,
      timestamp
    )

    storyProgressController.recordCompletedChapter(
      profileId,
      FRACTIONS_TOPIC_ID,
      FRACTIONS_STORY_ID_0,
      FRACTIONS_EXPLORATION_ID_1,
      timestamp
    )
  }

  /** Marks full topic progress for a particular profile. */
  fun markFullTopicProgressForFractions(profileId: ProfileId, timestampOlderThanAWeek: Boolean) {
    val timestamp = if (!timestampOlderThanAWeek) {
      getCurrentTimestamp()
    } else {
      getOldTimestamp()
    }
    storyProgressController.recordCompletedChapter(
      profileId,
      FRACTIONS_TOPIC_ID,
      FRACTIONS_STORY_ID_0,
      FRACTIONS_EXPLORATION_ID_0,
      timestamp
    )

    storyProgressController.recordCompletedChapter(
      profileId,
      FRACTIONS_TOPIC_ID,
      FRACTIONS_STORY_ID_0,
      FRACTIONS_EXPLORATION_ID_1,
      timestamp
    )
  }

  /** Marks one story progress full in ratios exploration for a particular profile. */
  fun markFullStoryPartialTopicProgressForRatios(
    profileId: ProfileId,
    timestampOlderThanAWeek: Boolean
  ) {
    val timestamp = if (!timestampOlderThanAWeek) {
      getCurrentTimestamp()
    } else {
      getOldTimestamp()
    }
    storyProgressController.recordCompletedChapter(
      profileId,
      RATIOS_TOPIC_ID,
      RATIOS_STORY_ID_0,
      RATIOS_EXPLORATION_ID_0,
      timestamp
    )

    storyProgressController.recordCompletedChapter(
      profileId,
      RATIOS_TOPIC_ID,
      RATIOS_STORY_ID_0,
      RATIOS_EXPLORATION_ID_1,
      timestamp
    )
  }

  /** Marks two partial story progress in ratios exploration for a particular profile. */
  fun markTwoPartialStoryProgressForRatios(profileId: ProfileId, timestampOlderThanAWeek: Boolean) {
    val timestamp = if (!timestampOlderThanAWeek) {
      getCurrentTimestamp()
    } else {
      getOldTimestamp()
    }
    storyProgressController.recordCompletedChapter(
      profileId,
      RATIOS_TOPIC_ID,
      RATIOS_STORY_ID_0,
      RATIOS_EXPLORATION_ID_0,
      timestamp
    )

    storyProgressController.recordCompletedChapter(
      profileId,
      RATIOS_TOPIC_ID,
      RATIOS_STORY_ID_1,
      RATIOS_EXPLORATION_ID_2,
      timestamp
    )
  }

  /** Marks exploration [FRACTIONS_EXPLORATION_ID_0] as recently played for a particular profile. */
  fun markRecentlyPlayedForFractionsStory0Exploration0(
    profileId: ProfileId,
    timestampOlderThanAWeek: Boolean
  ) {
    val timestamp = if (!timestampOlderThanAWeek) {
      getCurrentTimestamp()
    } else {
      getOldTimestamp()
    }
    storyProgressController.recordRecentlyPlayedChapter(
      profileId,
      FRACTIONS_TOPIC_ID,
      FRACTIONS_STORY_ID_0,
      FRACTIONS_EXPLORATION_ID_0,
      timestamp
    )
  }

  /** Marks exploration [RATIOS_EXPLORATION_ID_0] as recently played for a particular profile. */
  fun markRecentlyPlayedForRatiosStory0Exploration0(
    profileId: ProfileId,
    timestampOlderThanAWeek: Boolean
  ) {
    val timestamp = if (!timestampOlderThanAWeek) {
      getCurrentTimestamp()
    } else {
      getOldTimestamp()
    }
    storyProgressController.recordRecentlyPlayedChapter(
      profileId,
      RATIOS_TOPIC_ID,
      RATIOS_STORY_ID_0,
      RATIOS_EXPLORATION_ID_0,
      timestamp
    )
  }

  /** Marks first exploration in both stories of Ratios as recently played for a particular profile. */
  fun markRecentlyPlayedForRatiosStory0Exploration0AndStory1Exploration2(
    profileId: ProfileId,
    timestampOlderThanAWeek: Boolean
  ) {
    val timestamp = if (!timestampOlderThanAWeek) {
      getCurrentTimestamp()
    } else {
      getOldTimestamp()
    }

    storyProgressController.recordRecentlyPlayedChapter(
      profileId,
      RATIOS_TOPIC_ID,
      RATIOS_STORY_ID_0,
      RATIOS_EXPLORATION_ID_0,
      timestamp
    )

    storyProgressController.recordRecentlyPlayedChapter(
      profileId,
      RATIOS_TOPIC_ID,
      RATIOS_STORY_ID_1,
      RATIOS_EXPLORATION_ID_2,
      timestamp
    )
  }

  /** Marks first exploration in all stories of Ratios & Fractions as recently played for a particular profile. */
  fun markRecentlyPlayedForFirstExplorationInAllStoriesInFractionsAndRatios(
    profileId: ProfileId,
    timestampOlderThanAWeek: Boolean
  ) {
    val timestamp = if (!timestampOlderThanAWeek) {
      getCurrentTimestamp()
    } else {
      getOldTimestamp()
    }

    storyProgressController.recordRecentlyPlayedChapter(
      profileId,
      FRACTIONS_TOPIC_ID,
      FRACTIONS_STORY_ID_0,
      FRACTIONS_EXPLORATION_ID_0,
      timestamp
    )

    storyProgressController.recordRecentlyPlayedChapter(
      profileId,
      RATIOS_TOPIC_ID,
      RATIOS_STORY_ID_0,
      RATIOS_EXPLORATION_ID_0,
      timestamp
    )

    storyProgressController.recordRecentlyPlayedChapter(
      profileId,
      RATIOS_TOPIC_ID,
      RATIOS_STORY_ID_1,
      RATIOS_EXPLORATION_ID_2,
      timestamp
    )
  }
}

@rt4914
Copy link
Author

rt4914 commented Sep 4, 2020

That's actually much clean. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment