def test_basic_total_infection(self): """ Basic test of the total infection algorithm. There are 3 users such that user 1 coaches user 2 and user 2 coaches user 3. All of the users should be infected. """ u1 = User("u1", 1) u2 = User("u2", 1) u3 = User("u3", 1) u1.add_student(u2) u2.add_student(u3) network = UserNetwork() network.add_user(u1) network.add_user(u2) network.add_user(u3) network.total_infection(u1, 2) self.assertEqual(u1.version, 2) self.assertEqual(u2.version, 2) self.assertEqual(u3.version, 2) network.total_infection(u2, 3) self.assertEqual(u1.version, 3) self.assertEqual(u2.version, 3) self.assertEqual(u3.version, 3) network.total_infection(u2, 4) self.assertEqual(u1.version, 4) self.assertEqual(u2.version, 4) self.assertEqual(u3.version, 4)
def test_user_creation(self): u = User('A') self.assertEqual(u.get_name(), 'A') self.assertEqual(u.get_status(), 'Clean') v = User('B') u.add_student(v) self.assertTrue(v in u.get_students())
def test_basic_limited_infection(self): """ Tests the limited infection algorithm on a network of 3 users such that user 1 coaches user 2 and user 2 coaches user 3. """ u1 = User("u1", 1) u2 = User("u2", 1) u3 = User("u3", 1) u1.add_student(u2) u2.add_student(u3) network = UserNetwork() network.add_user(u1) network.add_user(u2) network.add_user(u3) network.limited_infection(u1, 2, 2) self.assertEqual(u1.version, 2) self.assertEqual(u2.version, 2) self.assertEqual(u3.version, 1) network.limited_infection(u2, 2, 3) self.assertEqual(u1.version, 2) self.assertEqual(u2.version, 3) self.assertEqual(u3.version, 3) # Make sure it finds user w/ most infected students network.limited_infection(u3, 2, 4) self.assertEqual(u1.version, 2) self.assertEqual(u2.version, 4) self.assertEqual(u3.version, 4)
def add_student(self, other): """ Adds student to self.students and adds an edge for the connection on the graph :param other: Student of self :type other: VisualUser :return: None """ User.add_student(self, other) self.edges.append(Edge(self.node, other.node, weight=1, stroke=VisualUser.EDGE_COLOR, strokewidth=2))
def test_user_add(self): a = User() b = User() c = User() d = User() a.add_student(b) b.add_coach(c) c.add_students([d]) c.add_students([d]) self.assertEqual(a.get_num_students(), 1) self.assertEqual(c.get_num_students(), 2) self.assertEqual(b.get_num_coaches(), 2) self.assertEqual(d.get_num_coaches(), 1)
def test_user_add_single_student(self): """ Tests adding a single student to a user's students. """ u1 = User("u1", 1) u2 = User("u2", 1) u1.add_student(u2) self.assertEqual(len(u1.students), 1) self.assertTrue(u2 in u1.students) self.assertEqual(len(u2.coaches), 1) self.assertTrue(u1 in u2.coaches)
def test_user_double_remove_student(self): """ Tests that an error is raised when double removing a student """ u1 = User("u1", 1) u2 = User("u2", 1) u1.add_student(u2) u1.remove_student(u2) self.assertRaises(MissingStudentError, u1.remove_student, u2) self.assertEqual(len(u1.students), 0) self.assertFalse(u2 in u1.students) self.assertFalse(u1 in u2.coaches)
def test_user_add_duplicate_students(self): """ Tests that an error is raised when duplicate students are added """ u1 = User("u1", 1) u2 = User("u2", 1) u1.add_student(u2) self.assertRaises(DuplicateStudentError, u1.add_student, u2) self.assertEqual(len(u1.students), 1) self.assertTrue(u2 in u1.students) self.assertEqual(len(u2.coaches), 1) self.assertTrue(u1 in u2.coaches)
def test_user_remove_single_student(self): """ Tests removing a student from a user's students """ u1 = User("u1", 1) u2 = User("u2", 1) u1.add_student(u2) u1.remove_student(u2) self.assertEqual(len(u1.students), 0) self.assertFalse(u2 in u1.students) self.assertEqual(len(u2.coaches), 0) self.assertFalse(u1 in u2.coaches)
def test_user_num_infected_students(self): """ Tests the calculation of the number of infected students of a user. """ u1 = User("u1", 1) u2 = User("u2", 2) u3 = User("u3", 2) u4 = User("u4", 1) u1.add_student(u2) u1.add_student(u3) u1.add_student(u4) num_infected = u1.num_infected_students(2) self.assertEqual(len(u1.students), 3) self.assertEqual(num_infected, 2) num_infected = u1.num_infected_students(1) self.assertEqual(num_infected, 1)
def test_user_add_multiple_students(self): """ Tests adding multiple students to a user's students """ u1 = User("u1", 1) u2 = User("u2", 1) u3 = User("u3", 1) u1.add_student(u2) u1.add_student(u3) self.assertEqual(len(u1.students), 2) self.assertTrue(u2 in u1.students) self.assertTrue(u3 in u1.students) self.assertEqual(len(u2.coaches), 1) self.assertTrue(u1 in u2.coaches) self.assertEqual(len(u3.coaches), 1) self.assertTrue(u1 in u3.coaches)
def test_total_infection_rand(self): users = [] user_num = 50 for i in xrange(user_num): user = User(i) users.append(user) coaching = random.randint(0, 1) if i: related_user = users[random.randint(0, i - 1)] if coaching: user.add_student(related_user) else: user.add_coach(related_user) for user in users: self.assertEqual(user.get_status(), 'Clean') user = users[random.randint(0, user_num - 1)] infections.total_infection(user) for user in users: self.assertEqual(user.get_status(), 'Infected')
def test_total_infection1(self): a = User() b = User() c = User() d = User() e = User() f = User() e.add_student(f) a.add_coach(b) b.add_student(c) d.add_student(a) self.assertEqual(a.get_status(), 'Clean') self.assertEqual(b.get_status(), 'Clean') self.assertEqual(c.get_status(), 'Clean') self.assertEqual(d.get_status(), 'Clean') self.assertEqual(e.get_status(), 'Clean') self.assertEqual(f.get_status(), 'Clean') infections.total_infection(a) self.assertEqual(a.get_status(), 'Infected') self.assertEqual(b.get_status(), 'Infected') self.assertEqual(c.get_status(), 'Infected') self.assertEqual(d.get_status(), 'Infected') self.assertEqual(e.get_status(), 'Clean') self.assertEqual(f.get_status(), 'Clean') infections.total_infection(f) self.assertEqual(e.get_status(), 'Infected') self.assertEqual(f.get_status(), 'Infected')
def test_exact_infection1(self): a = User('A') b = User('B') c = User('C') d = User('D') e = User('E') f = User('F') user_lst = [a, b, c, d] e.add_student(f) a.add_coach(b) b.add_student(c) d.add_student(a) self.assertEqual(a.get_status(), 'Clean') self.assertEqual(b.get_status(), 'Clean') self.assertEqual(c.get_status(), 'Clean') self.assertEqual(d.get_status(), 'Clean') self.assertEqual(e.get_status(), 'Clean') self.assertEqual(f.get_status(), 'Clean') result = infections.exact_infection(a, 4) self.assertTrue(result) infected_count = 0 for user in user_lst: infected_count += user.get_status_num() self.assertEqual(infected_count, 4) result = infections.exact_infection(f, 10) self.assertFalse(result) self.assertEqual(e.get_status(), 'Clean') self.assertEqual(f.get_status(), 'Clean')
def test_limited_infection1(self): a = User('A') b = User('B') c = User('C') d = User('D') e = User('E') f = User('F') e.add_student(f) a.add_coach(b) b.add_student(c) d.add_student(a) self.assertEqual(a.get_status(), 'Clean') self.assertEqual(b.get_status(), 'Clean') self.assertEqual(c.get_status(), 'Clean') self.assertEqual(d.get_status(), 'Clean') self.assertEqual(e.get_status(), 'Clean') self.assertEqual(f.get_status(), 'Clean') infections.limited_infection(a, 3) self.assertEqual(a.get_status(), 'Infected') self.assertEqual(b.get_status(), 'Infected') self.assertEqual(c.get_status(), 'Clean') self.assertEqual(d.get_status(), 'Infected') self.assertEqual(e.get_status(), 'Clean') self.assertEqual(f.get_status(), 'Clean') infections.limited_infection(f, 0) self.assertEqual(e.get_status(), 'Clean') self.assertEqual(f.get_status(), 'Clean') infections.limited_infection(f, 10) self.assertEqual(e.get_status(), 'Infected') self.assertEqual(f.get_status(), 'Infected')
def test_user_remove_multiple_students_same_name(self): """ Tests removing students with the same name """ u1 = User("u1", 1) u2 = User("u2", 1) u3 = User("u2", 1) u1.add_student(u2) u1.add_student(u3) u1.remove_student(u2) self.assertEqual(len(u1.students), 1) self.assertTrue(u3 in u1.students) self.assertFalse(u2 in u1.students) self.assertTrue(u1 in u3.coaches) self.assertFalse(u1 in u2.coaches) u1.remove_student(u3) self.assertEqual(len(u1.students), 0) self.assertFalse(u3 in u1.students) self.assertFalse(u2 in u1.students) self.assertFalse(u1 in u3.coaches) self.assertFalse(u1 in u2.coaches)
def case_1_setup(self): A = User("A", 1) B = User("B", 1) C = User("C", 1) D = User("D", 1) E = User("E", 1) F = User("F", 1) G = User("G", 1) H = User("H", 1) I = User("I", 1) J = User("J", 1) A.add_student(B) A.add_student(C) A.add_student(D) B.add_student(E) B.add_student(F) C.add_student(F) D.add_student(G) J.add_coach(H) J.add_coach(I) network = UserNetwork() network.add_user(A) network.add_user(B) network.add_user(C) network.add_user(D) network.add_user(E) network.add_user(F) network.add_user(G) network.add_user(H) network.add_user(I) network.add_user(J) return {"A": A, "B": B, "C": C, "D": D, "E": E, "F": F, "G": G, "H": H, "I": I, "J": J}, network
def test_some_connected_total_infection(self): """ Tests total infection of a network where only some of the users are connected to each other. Each time the algorithm is called, only one connected portion of the network should be infected. """ u1 = User("u1", 1) u2 = User("u2", 1) u3 = User("u3", 1) u4 = User("u4", 1) u5 = User("u5", 1) u6 = User("u6", 1) u1.add_student(u2) u1.add_student(u3) u2.add_student(u4) u5.add_student(u6) network = UserNetwork() network.add_user(u1) network.add_user(u2) network.add_user(u3) network.add_user(u4) network.add_user(u5) network.add_user(u6) network.total_infection(u1, 2) self.assertEqual(u1.version, 2) self.assertEqual(u2.version, 2) self.assertEqual(u3.version, 2) self.assertEqual(u4.version, 2) self.assertEqual(u5.version, 1) self.assertEqual(u6.version, 1) network.total_infection(u2, 3) self.assertEqual(u1.version, 3) self.assertEqual(u2.version, 3) self.assertEqual(u3.version, 3) self.assertEqual(u4.version, 3) self.assertEqual(u5.version, 1) self.assertEqual(u6.version, 1) network.total_infection(u4, 4) self.assertEqual(u1.version, 4) self.assertEqual(u2.version, 4) self.assertEqual(u3.version, 4) self.assertEqual(u4.version, 4) self.assertEqual(u5.version, 1) self.assertEqual(u6.version, 1) network.total_infection(u5, 5) self.assertEqual(u1.version, 4) self.assertEqual(u2.version, 4) self.assertEqual(u3.version, 4) self.assertEqual(u4.version, 4) self.assertEqual(u5.version, 5) self.assertEqual(u6.version, 5)
def test_self_referential(self): user = User() user.add_student(user) self.assertEqual(user.students, [])
def test_add_student(self): user0 = User() user1 = User() user0.add_student(user1) self.assertEqual(user0.students[0], user1) self.assertEqual(user1.coaches[0], user0)