CSC/ECE 517 Spring 2014/final doc updated qyx: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
Line 300: Line 300:
** this method should be deleted here. No test
** this method should be deleted here. No test


== Possible Refacotring ==
== Possible Refactoring ==
=== advertise_for_team_controller.rb ===
=== advertise_for_team_controller.rb ===
* ''update'' should be ''edit'' (this is mentioned in the comments of code)
* ''update'' should be ''edit'' (this is mentioned in the comments of code)

Revision as of 17:59, 25 April 2014

E1414: Testing classes related to teams

Please be advised that we do not have deployment link, since our work are all back-end tests

Introduction

This project will be an extension of our previous E1406 project. For the previous project, we did some refactoring and testing for the controllers related to teams. This time, we are going to reuse most of our test environment, but extend the tests to other team-related classes. We also expect to examine and report the code that may require refactoring.

Test Environment

Framework
Rails default test framework
Sample data
Fixtures (under /test/fixtures)
Coverage tool
SimpleCov

Background

teams_users.rb & teams_users_controller.rb

  • Teams_users.rb and teams_users_controller.rb are used for Instructor/TA to manage the members of teams.
  • There is no functional test for teams_users_controller.rb and unit test for teams_users.rb in the current version of expertiza codes.

  • This controller takes over the advertisement function from the student_team_controller, and make the class more RESTful.
  • It seems like there has been a functional test, but the class name and set up looks like the other class. It may be necessary to re-run, and even re-write the test

team.rb

  • This model defines the relationship between team object and other objects. It also has other methods to handle related functions about team operation
  • There exists a unit test for team model, but there are only two test cases. Also, some of the methods under team.rb are duplicated, or either marked as not working. Further research may be required before starting to do the test.

assignment_team.rb & course_team.rb

  • Both assignment_team class and course_team class are inherited from team class.
  • These two classes have similar methods, which could be used to manage teams for assignment and course more effectively.

Changes made to original classes

team.rb

  • line 10 def get_participants removed
    • This is commented out because the same method is defined again at line 26
  • line 38 def self.generate_team_name() removed
    • This is commented out because the same method is defined again at line 150
  • line 100 def self.create_node_object(name, parent_id)
    • see the TODO at line 99, the method will not work.

Test Classes

app/models/teams_users.rb

The teams_users.rb is a model belonging to both users and teams. It is used when a user is added to a team.

app/controllers/teams_users_controller.rb

The teams_users_controller support team member management for instructor/TA accounts Instruction/TA can:

  • get a user name by inputting part of the name
  • list team members of a team
  • add a user to a team
  • remove a team member from a team
  • remove all members belonging to an item

app/controllers/advertise_for_partners_controller.rb

The advertise_for_partners_controller work with the student_team_controllers, allowing student manage advertisement for their teams.

A student can:

  • create a new advertisement for the team
  • edit the advertisement content
  • remove advertisement record from the table for the specific team

app/models/team.rb

The “team” object represents a team that consists students who are either participants of course or assignment. a team will response to the request on:

  • get_participants : get participant in the team
  • delete : delete a team member in the team
  • get_node_type : will return “TeamNode”
  • get_author_name : will return team’s name
  • generate_team_name() : serialize the automatically generated team name (e.g. team 1, team 2, etc.)
  • get_possible_team_members(name) : find a team member by name
  • has_user(user : find a team member by user object
  • add_member(user, assignment_id) : add a user to an assignment team
  • copy_members(new_team) : copy current member in the team to the new team
  • create_node_object(name, parent_id) : ‘’there is a TODO on this method indicated that this is not working’’
  • check_for_exisiting(parent, name, team_type) : check if the team name under the same assignment or course is in use.
  • delete_all_by_parent(parent) : delete all teams under the same course/assignment
  • randomize_all_by_parent(parent, team_type, team_size) : randomly assign team members
  • generate_team_name(teamnameprefix) : generate team name with prefix (e.g. prefix_Team1)
  • import_team_members(starting_index, row) : import user from specific index of users and length

app/unit/assignment_team.rb

  • includes?(participant): check if this assignment team include this participant
  • assign_reviewer(reviewer): assign a reviewer for this assignment team
  • reviewed_by?(reviewer):check if this assignment team is reviewed by this reviewer
  • topic: get the topic picked up by this team
  • has_submissions?: check if this team submitted work or not
  • reviewed_contributor?(contributor)
  • participants: get all the participants
  • delete: delete the signed up users
  • self.get_first_member(team_id): get the first participant
  • get_hyperlinks
  • get_path
  • get_submitted_files
  • get_review_map_type: return TeamReviewResponseMap
  • self.handle_duplicate(team, name, assignment_id, handle_duplicates): handle duplicate team in different cases
  • self.import(row,session,assignment_id,options): import team members to a exist team or a new team
  • email: get fist team user's email
  • get_participant_type: return AssignmentParticipant
  • get_parent_model:return Assignment
  • fullname
  • get_participants
  • copy(course_id): copy a team from course team
  • add_participant(assignment_id, user): add participant to AssignmentParticipant
  • assignment
  • get_scores(questions): return a hash of scores that the team has received for the questions
  • self.get_team(participant): get this participant's team
  • self.export(csv, parent_id, options): export assignment and team members
  • self.get_export_fields(options)
  • self.create_team_and_node(assignment_id)
  • self.remove_team_by_id(id): Remove a team given the team id

app/unit/course_team.rb

  • get_participant_type: return CourseParticipant
  • get_parent_model:return Course
  • get_node_type: return TeamNode
  • assignment_id: return nil
  • copy(assignment_id): copy a team from assignment team
  • add_participant(course_id, user): add a user to a course. Note: this is deprecated because the functionality belongs to course
  • export_participants: export the participants of a course team
  • export(team_name_only): export team name, participants and course
  • self.handle_duplicate(team, name, assignment_id, handle_duplicates): handle duplicate team in different cases
  • self.import(row,session,assignment_id,options): import team members to a exist team or a new team
  • self.export(csv, parent_id, options): export course and team members
  • self.get_export_fields(options)
  • self.export_all_assignment_team_related_to_course(csv, parent_id, options): this method is deprecated
  • self.create_team_and_node(assignment_id)

Setup

To set up our testing for the models and controllers, we need some fixtures to support. Fixtures needed for corresponding classes are listed below However, all of the fixtures are required to be in the fixutre folder, otherwise it will raise error.

fixtures use in common

  • teams.yml
  • users.yml
  • teams_users.yml
  • courses.yml
  • assignments.yml
  • teams_participant.yml
  • join_team_requests.yml
  • participants.yml
  • nodes.yml
  • questions.yml
  • signed_up_users.yml

Use Case Diagram

Testing Scenarios

functional/teams_users_controller_test.rb

Based on the user story of the teams_users_controller.rb, there are also several sub-scenarios for each method in the user story.

  • Auto_complete_for_user_name:
    • Test auto_complete_for_user_name with valid user name
    • Test auto_complete_for_user_name with invalid user name
  • List
    • Test List with valid team id
    • Test List with invalid team id
  • New
    • Test New with valid team id
    • Test New with invalid team id
  • Create
    • Test Create with valid team id
      • Test Create with valid user id and maximum member number is not reached
      • Test Create with invalid user id and maximum member number is not reached
      • Test Create with valid user id and maximum member number is reached
      • Test Create with invalid user id and maximum member number is reached
    • Test Create with invalid team id
      • Test Create with valid user id and maximum member number is not reached
      • Test Create with invalid user id and maximum member number is not reached
      • Test Create with valid user id and maximum member number is reached
      • Test Create with invalid user id and maximum member number is reached
  • Delete
    • Test Delete with valid user id
    • Test Delete with invalid user id
  • Delete_select
    • Test Delete_select with valid item id
    • Test Delete_select with invalid item id

unit/teams_users_test.rb

  • Delete
    • Test Delete to check if it could delete the team node and the team user
  • Remove_team
    • Test Remove_team with valid user_id and valid team_id
    • Test Remove_team with valid user_id and invalid team_id
    • Test Remove_team with invalid user_id and valid team_id
    • Test Remove_team with invalid user_id and invalid team_id
  • Is_team_empty
    • Test Is_team_empty with empty team
    • Test Is_team_empty with non-empty team
  • Add_member_to_invited_team
    • Test Add_member_to_invited_team with valid user_id and valid team_id
    • Test Add_member_to_invited_team with valid user_id and invalid team_id
    • Test Add_member_to_invited_team with invalid user_id and valid team_id
    • Test Add_member_to_invited_team with invalid user_id and invalid team_id

functional/advertise_for_partner_controller_test.rb

  • test "test remove advertisement should set advertise_for_partner false" do
  • test "test remove advertisement should remove advertisement comments" do
  • test "test remove advertisement should redirect to view" do
  • test "test create advertisement should set advertise_for_partner true" do
  • test "test create advertisement should set comment for advertisement" do
  • test "test create advertisement should redirect to view" do
  • test "test update advertisement successfully should show notice" do
  • test "test update advertisement successfully should change comment" do
  • test "test update advertisement successfully should redirect to view" do
  • # test "test update advertisement fail" do
    • should show flash for fail message when team.save fail, but there is no really a good way to fail team.save, so this is not implemented
  • test "test edit advertisement" do
    • only assign team

unit/team_test.rb

Most of test should be straightforward, so only the test with concerns will be listed below

  • test "get_participants from assignment team" do
    • Please notice that this will actually call the get_participants under AssignmentTeam
  • test "generate_team_name" do
    • Please notice that this method (self.generate_team_name)is modified
  • test "add member for course should create teams user" do
    • In the original code, it allow nil to be passed as parameter, and checks for assignment.id = nil, but rails will actually raise runtime error when nil is passed.
  • #test "randomize_all_by_parent" do
    • Commented out because the bug in the code make it raise error because assignment id not passed. (wrong argument)
  • #test "import team members successfully" do
    • problem: add_member are not passing correct assignment id at line 169 in team.rb

unit/assignment_team_test.rb

  • includes?(participant)
    • test one assignment team include a participant return true
  • assign_reviewer(reviewer)
    • assign a reviewer for this assignment team, and TeamReviewResponseMap.count increases
  • reviewed_by?(reviewer)
    • TeamReviewResponseMap is empty, so test this method will return false
  • has_submissions?
    • test one team has a submission return true
  • reviewed_contributor?(contributor)
    • TeamReviewResponseMap is empty, so test this method will return false
  • participants
    • participants are not nil
  • delete
    • this method has bug. No test
  • self.get_first_member(team_id)
    • test first menber not nil
  • get_hyperlinks
    • test hyperlinks are not nil
  • get_path
    • test path is not nil
  • get_submitted_files
    • submitted_file must be empty, expect nil
  • get_review_map_type
    • return TeamReviewResponseMap
  • self.handle_duplicate(team, name, assignment_id, handle_duplicates)
    • test in different cases(team is nil, option is "ignore", option is "rename")
  • self.import(row,session,assignment_id,options)
    • test in different cases(argument error, has_column_name is true, has_column_name is false)
  • email
    • this method has bug because there is no method named get_team_users. No test
  • get_participant_type
    • return AssignmentParticipant
  • get_parent_model
    • return Assignment
  • fullname
    • return value should equal what we expect
  • get_participants
    • test the participants are not nil
  • copy(course_id)
    • TeamUserNode.count and TeamsUser.count increase
  • add_participant(assignment_id, user)
    • AssignmentParticipant.count increases
  • assignment
    • return value should equal what we expect
  • get_scores(questions)
    • return output's total score is not nil and the assignment team is what we expect
  • self.get_team(participant)
    • return team is what we expect
  • self.export(csv, parent_id, options)
    • test in different cases(team_name is true, team_name is false)
  • self.get_export_fields(options)
    • test if the fields' name is right
  • self.create_team_and_node(assignment_id)
    • assignent team and team node are not nil
  • self.remove_team_by_id(id)
    • check the removed team cannot be found in AssignmentTeam

unit/course_team_test.rb

Because most of the methods in course_team.rb are similar with that in assignment_team.rb if they have the same name. So I skip the same name method. Take the test/unit/course_team_test.rb as a reference.

  • self.export_all_assignment_team_related_to_course(csv, parent_id, options)
    • this method should be deleted here. No test

Possible Refactoring

  • update should be edit (this is mentioned in the comments of code)
  • edit should be reexamined since it is only retrieving the team object
  • new is empty, so it should be removed

team.rb

  • There are two def get_participants, the second one overwritten the first one, so the first one should be deleted.

teams_participant.rb & teams_participants_controller.rb

  • These two files are never used, no tests will cover them.

teams_user.rb

  • In delete method, the statements about team.delete should be commented out due to the recursion it causes.
  • elf.is_team_empty method is rewritten since the original code will cause fatal error.

teams_users_controller.rb

  • delete_select method will not work since the params are not in accordance with the method called inside.

Running Tests

Please note that all of our work will be under the /test folder, which means they will not show any change on the view. To see the result of tests, run

rake test TEST=test/unit/<filename> # for models

and

rake test TEST=test/functional/<filename> # for controllers

You can also run multiple tests

 rake test:prepare
 bundle exec ruby -I.:test -e "ARGV.each{|f| require f}" test/functional/teams_users_controller_test.rb test/functional/advertise_for_partner_controller_test.rb test/unit/course_team_test.rb test/unit/assignment_team_test.rb test/unit/team_test.rb test/unit/team_user_test.rb

running the whole test suite with rake test is not recommended, as the schema change overtime has broke some of the previous tests.

Sample Test Code

We show two pieces of test code for two controllers from our previous work to explain how our test works

   test "create_student team with name in use" do
       sessionVars = session_for(users(:student8))
       post(:create, {'team' => { 'name' => 'IntelligentTeam2'}, 'id' => participants(:par21).id, "commit" => "Create Team"}, sessionVars, nil)
       assert_equal 'Team name is already in use.', flash[:notice]
       assert_redirected_to :controller => 'student_team', :action => 'view', :id => participants(:par21).id
   end
   test "create_should_increase_number_of_teams_course" do
       sessionVars = session_for(users(:instructor1))
       sessionVars[:team_type] = "Course"
       assert_difference 'Team.count' do
         get :create, {'id' => @testCourse,'team' => {'name' => "Random"}},sessionVars
       end
   end

The first piece of code is from student_team_controller.rb. It tests if there is already a same team name when student8 want to create a team. The second piece of code is from teams_controller_test.rb. It tests if the team number decreases when instructor1 creates a new team for a course.

As we can see from the code, when we want to use post or get, several parameters are needed. Usually we use four parameters: First, the method we are requesting. Second, an hash of request parameters for the method. Third, an optional hash of session variables. Forth, an optional hash of flash values. If we do not want to pass in the optional hash, we can set it to nil as the first piece of code shows, or we can just leave it blank as the second piece of code shows.

When we run the command rake test TEST=test/functional/your_test_file.rb, you will see if the test is passed or not.

References

<references/>