예제 #1
0
    def test_get_session_info(self):
        """
        Test a valid retrieval of a complex IQR session state.
        """
        rank_relevancy_with_feedback = mock.MagicMock(
            spec=RankRelevancyWithFeedback)
        iqrs = IqrSession(rank_relevancy_with_feedback, session_uid='abc')

        ep, en, p1, p2, p3, n1, n2, d1, d2, n3 = [
            DescriptorMemoryElement('test', uid)
            for uid in ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
            # ep   en   p1   p2   p3   n1   n2   d1   d2   n3
        ]  # C              C         C    C    C    C
        #     ^Contributing^

        # Current adjudications
        iqrs.external_positive_descriptors = {ep}
        iqrs.positive_descriptors = {p1, p2, p3}
        iqrs.external_negative_descriptors = {en}
        iqrs.negative_descriptors = {n1, n2, n3}
        # "Last Refine" adjudications
        # - simulating that "currently" neutral descriptors were previous
        #   adjudicated.
        iqrs.rank_contrib_pos = {p2, d1}
        iqrs.rank_contrib_pos_ext = {ep}
        iqrs.rank_contrib_neg = {n1, n3, d2}
        iqrs.rank_contrib_neg_ext = set()
        # mock working set with
        iqrs.working_set.add_many_descriptors([p1, p2, p3, n1, n2, d1, d2, n3])

        self.app.controller.add_session(iqrs)

        with self.app.test_client() as tc:
            #: :type: flask.wrappers.Response
            r = tc.get('/session?sid=abc')
            self.assertStatusCode(r, 200)
            r_json = r.json
            assert r_json['sid'] == 'abc'
            # That everything included in "current" adjudications is included
            # here.
            assert set(r_json['uuids_pos_ext']) == {'a'}
            assert set(r_json['uuids_pos']) == {'c', 'd', 'e'}
            assert set(r_json['uuids_neg_ext']) == {'b'}
            assert set(r_json['uuids_neg']) == {'f', 'g', 'j'}
            # That those marked as "contributing" are included here
            assert set(r_json['uuids_pos_in_model']) == {'d', 'h'}
            assert set(r_json['uuids_pos_ext_in_model']) == {'a'}
            assert set(r_json['uuids_neg_in_model']) == {'f', 'j', 'i'}
            assert set(r_json['uuids_neg_ext_in_model']) == set()
            # IQR working set expected size
            assert r_json['wi_count'] == 8
예제 #2
0
    def test_get_unadjudicated_relevancy_no_cache_has_results(self):
        """
        Test that we get the non-adjudicated DescriptorElements and their
        scores correctly from a non-cached state with known results.
        """
        iqrs = IqrSession()

        d0 = DescriptorMemoryElement('', 0).set_vector([0])
        d1 = DescriptorMemoryElement('', 1).set_vector([1])
        d2 = DescriptorMemoryElement('', 2).set_vector([2])
        d3 = DescriptorMemoryElement('', 3).set_vector([3])

        # Simulate a populated contributing adjudication state (there must be
        # some positives for a simulated post-refine state to be valid).
        iqrs.rank_contrib_pos = {d1}
        iqrs.rank_contrib_neg = {d0}

        # Simulate post-refine results map.
        iqrs.results = {
            d0: 0.1,
            d1: 0.8,
            d2: 0.2,
            d3: 0.4,
        }

        # Cache should be initially empty
        assert iqrs._ordered_non_adj is None

        # Test that the appropriate sorting actually occurs.
        with mock.patch('smqtk.iqr.iqr_session.sorted',
                        side_effect=sorted) as m_sorted:
            actual1 = iqrs.get_unadjudicated_relevancy()
            m_sorted.assert_called_once()

        expected = [(d3, 0.4), (d2, 0.2)]
        assert actual1 == expected

        # Calling the method a second time should not result in a ``sorted``
        # operation due to caching.
        with mock.patch('smqtk.iqr.iqr_session.sorted',
                        side_effect=sorted) as m_sorted:
            actual2 = iqrs.get_unadjudicated_relevancy()
            m_sorted.assert_not_called()

        assert actual2 == expected
        # Both returns should be shallow copies, thus not the same list
        # instances.
        assert id(actual1) != id(actual2)