Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#12048] SeedDb code for student entities #12990

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 35 additions & 2 deletions src/client/java/teammates/client/scripts/sql/SeedDb.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import teammates.storage.entity.Account;
import teammates.storage.entity.AccountRequest;
import teammates.storage.entity.Course;
import teammates.storage.entity.CourseStudent;
import teammates.storage.entity.Notification;
import teammates.test.FileHelper;

Expand All @@ -40,6 +41,7 @@
public class SeedDb extends DatastoreClient {

private static final int MAX_ENTITY_SIZE = 10000;
private static final int MAX_STUDENT_PER_COURSE = 1;
private final LogicExtension logic = new LogicExtension();
private Closeable closeable;

Expand Down Expand Up @@ -121,18 +123,49 @@ private void seedCourse() {

Random rand = new Random();

String courseId = UUID.randomUUID().toString();
try {
String courseName = String.format("Course %s", i);
String courseInstitute = String.format("Institute %s", i);
String courseTimeZone = String.format("Time Zone %s", i);
Course course = new Course(UUID.randomUUID().toString(), courseName, courseTimeZone, courseInstitute,
getRandomInstant(),
Course course = new Course(courseId, courseName, courseTimeZone, courseInstitute, getRandomInstant(),
rand.nextInt(3) > 1 ? null : getRandomInstant(), // set deletedAt randomly at 25% chance
false);
ofy().save().entities(course).now();
} catch (Exception e) {
log(e.toString());
}

seedStudents(i, courseId);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should seedStudents be in the try block? such that if there is an exception then seedStudents will not run

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, could abstract course creation to a function and put both of the methods in the try block

}
}

private void seedStudents(int courseNumber, String courseId) {
Random rand = new Random();

log("Seeding students for course " + courseNumber);
for (int i = 0; i < MAX_STUDENT_PER_COURSE; i++) {

int googleIdNumber = courseNumber * MAX_STUDENT_PER_COURSE + i;
try {
String studentEmail = String.format("Course %s Student %s Email ", courseNumber, i);
String studentName = String.format("Student %s in Course %s", i, courseNumber);
String studentGoogleId = String.format("Account Google ID %s", googleIdNumber);
String studentComments = String.format("Comments for student %s in course %s", i, courseNumber);
String studentTeamName = String.format("Course %s Team %s", courseNumber, i);
String studentSectionName = String.format("Course %s Section %s", courseNumber, i);
String studentRegistrationKey = String.format("Student %s in Course %s Registration Key", i, courseNumber);

CourseStudent student = new CourseStudent(studentEmail, studentName, studentGoogleId, studentComments,
courseId, studentTeamName, studentSectionName);
student.setCreatedAt(getRandomInstant());
student.setLastUpdate(rand.nextInt(3) > 1 ? null : getRandomInstant());
student.setRegistrationKey(studentRegistrationKey);

ofy().save().entities(student).now();
} catch (Exception e) {
log(e.toString());
}
}
}

Expand Down