예제 #1
0
class  MatcherTestCase(unittest.TestCase):
    """Tests the various Matcher subclasses"""

    all_matchers = [GeogMatcher]

    def setUp(self):
        self.sim = Simulator()
        self.sim.simulate(10, 13579)
        

    def test_matcher(self):
        """Test various matcher subclasses"""
        def test_empty_matcher(MatcherSub):
            """Test empty Matcher returns valid empty results"""
            ## Test that an empty set returns valid, emtpy results
            matcher_sub = MatcherSub(set([]))
            unmatched = matcher_sub.get_unmatched()
            best_matches = matcher_sub.get_best_matches()
            self.assertEqual(unmatched, set([]));
            self.assertEqual(best_matches, set([]));

        def test_matcher_matches(MatcherSub, expected_matches, expected_unmatches):
            """Spin up Matcher subclass and confirm expected results"""
            users = self.sim.get_users()
            self.assertEqual(len(users),10, "Sanity check simulator user count")
            matcher_sub = MatcherSub(users,seed=1234)
            best_matches = set([(tup[0].user_id, tup[1].user_id) for tup in matcher_sub.get_best_matches() ])
            unmatched = set([user.user_id for user in matcher_sub.get_unmatched()])
            print best_matches
            print unmatched

            for match in expected_matches:
                self.assertIn(match, best_matches, "Expected match not found - perhaps the algo or simulator changed?")

            for unmatch in expected_unmatches:
                self.assertIn(unmatch, unmatched, "Expected unmatched not found - perhaps the algo or simulator changed?")

        ## Test empty matcher return valid results
        for MatcherSub in self.all_matchers:
            test_empty_matcher(MatcherSub)

        ## For each Matcher subclass, work out what the answers should be
        ## given the simulator's users, and record the answers here
        geog_matched = [(7736498, 2837615), (5072324, 795755), (4454245, 3882723)]
        geog_unmatched = [1510961, 8148259, 6505708, 2432669]

        ## Feed the expected results into 3-tuples below to be tested
        ## (MatcherSubclass , expected_match_results, expected_unmatched_results)
        test_cases = [
            (GeogMatcher, geog_matched,geog_unmatched)
            ]

        ## Test full matchers return correct results
        for match_case in test_cases:
            test_matcher_matches(*match_case)
예제 #2
0
    def get_users(self):
        """Read in the survey data or simulate new data"""
        self.users = self.get_users_parser(APIParser, self.survey_api_survey_id)
        print self.users
        if self.users is None:
            if os.path.isfile(self.survey_data_filename):
                self.users = self.get_users_parser(TextParser, self.survey_data_filename)
            else:
                print "Simulating data"
                ## Simulate some number of survey responses
                sim = Simulator()
                sim.simulate(200) ## By adding a seed parameter, you can have reproducible sims
                self.users = sim.get_users()

    #    ## Print some summaries
    #    for user in users:
    #        user.to_string()

        # Print some descriptive stats
        print "# Users", len(self.users), [u.name for u in self.users]
예제 #3
0
class MatcherTestCase(unittest.TestCase):
    """Tests the various Matcher subclasses"""

    all_matchers = [GeogMatcher]

    def setUp(self):
        self.sim = Simulator()
        self.sim.simulate(10, 13579)

    def test_matcher(self):
        """Test various matcher subclasses"""
        def test_empty_matcher(MatcherSub):
            """Test empty Matcher returns valid empty results"""
            ## Test that an empty set returns valid, emtpy results
            matcher_sub = MatcherSub(set([]))
            unmatched = matcher_sub.get_unmatched()
            best_matches = matcher_sub.get_best_matches()
            self.assertEqual(unmatched, set([]))
            self.assertEqual(best_matches, set([]))

        def test_matcher_matches(MatcherSub, expected_matches,
                                 expected_unmatches):
            """Spin up Matcher subclass and confirm expected results"""
            users = self.sim.get_users()
            self.assertEqual(len(users), 10,
                             "Sanity check simulator user count")
            matcher_sub = MatcherSub(users, seed=1234)
            best_matches = set([(tup[0].user_id, tup[1].user_id)
                                for tup in matcher_sub.get_best_matches()])
            unmatched = set(
                [user.user_id for user in matcher_sub.get_unmatched()])
            print best_matches
            print unmatched

            for match in expected_matches:
                self.assertIn(
                    match, best_matches,
                    "Expected match not found - perhaps the algo or simulator changed?"
                )

            for unmatch in expected_unmatches:
                self.assertIn(
                    unmatch, unmatched,
                    "Expected unmatched not found - perhaps the algo or simulator changed?"
                )

        ## Test empty matcher return valid results
        for MatcherSub in self.all_matchers:
            test_empty_matcher(MatcherSub)

        ## For each Matcher subclass, work out what the answers should be
        ## given the simulator's users, and record the answers here
        geog_matched = [(7736498, 2837615), (5072324, 795755),
                        (4454245, 3882723)]
        geog_unmatched = [1510961, 8148259, 6505708, 2432669]

        ## Feed the expected results into 3-tuples below to be tested
        ## (MatcherSubclass , expected_match_results, expected_unmatched_results)
        test_cases = [(GeogMatcher, geog_matched, geog_unmatched)]

        ## Test full matchers return correct results
        for match_case in test_cases:
            test_matcher_matches(*match_case)
예제 #4
0
class  SimulatorTestCase(unittest.TestCase):
    def setUp(self):
        self.sim = Simulator()

    def tearDown(self):
        self.sim = None

    def test_simulator(self):
        self.assertEqual(self.sim.get_users(),set([]))
        self.assertEqual(self.sim.CAREER_STAGES, {"Student": 10, "Entry": 15, "Early":25, "Middle": 40, "Late": 10})

    def test_simulate(self):
        """Tests the simulate and clear_users methods
        
        Since multiple calls to simulate continue to grow the group,
        the test demonstrates that the group grows.  It also tests that
        the users set can be cleared.
        """

        ## Simulate the first 5 users
        self.sim.simulate(5,1234)
        users = list(sorted(
            self.sim.get_users(),
            cmp=lambda x,y : cmp(x.user_id,y.user_id)
            ))
        print users
        expected_results = {
            4 : (8698081,"Melinda Thompson"),
            3 : (8453420, "Freddie Bahlmann"),
            2 : (8198783, "Gary Paddock"),
            1 : (3966593, "Kevin Wilson"),
            0 : (67423, "Scott Lopez")
        }
        for i in range(5):
            self.assertEqual((users[i].user_id,users[i].name),expected_results[i])
        
        ## Clear the simulated users, and resimulate the same users
        self.sim.clear_users()
        self.sim.simulate(5,1234)
        users = list(sorted(
            self.sim.get_users(),
            cmp=lambda x,y : cmp(x.user_id,y.user_id)
            ))
        print users
        expected_results = {
            4 : (8698081,"Melinda Thompson"),
            3 : (8453420, "Freddie Bahlmann"),
            2 : (8198783, "Gary Paddock"),
            1 : (3966593, "Kevin Wilson"),
            0 : (67423, "Scott Lopez")
        }
        for i in range(5):
            self.assertEqual((users[i].user_id,users[i].name),expected_results[i])

        ## Add 5 more users on top of existing users.
        ## Since the same seed is used, it should be tempted
        ## to duplicate the previous users, but the simulate
        ## method should prevent this from happening
        self.sim.simulate(5,1234)
        users = list(sorted(
            self.sim.get_users(),
            cmp=lambda x,y : cmp(x.user_id,y.user_id)
            ))
        print users
        expected_results = {
            9 : (8698081,"Melinda Thompson"),
            8 : (8698080, "Angela Jones"),
            7 : (8453420, "Freddie Bahlmann"),
            6 : (8453419, "Marilynn Orr"),
            5 : (8198783, "Gary Paddock"),
            4 : (8198781, "Tiffany Mchaney"),
            3 : (3966593, "Kevin Wilson"),
            2 : (3966592, "Donna Yu"),
            1 : (67424, "Patrice Newell"),
            0 : (67423, "Scott Lopez")
        }
        for i in range(5):
            self.assertEqual((users[i].user_id,users[i].name),expected_results[i])

    def test_blank_simulator(self):
        blankSim = Simulator()
        self.assertEqual(blankSim.get_users(),set([]))
        self.assertEqual(blankSim.CAREER_STAGES, {"Student": 10, "Entry": 15, "Early":25, "Middle": 40, "Late": 10})