Ejemplo n.º 1
0
def paginate(query: Query, data: dict) -> Query:
    """
    Update pagination parameters on a :class:`.Query` from request parameters.

    Parameters
    ----------
    query : :class:`.Query`
    data : dict

    Returns
    -------
    :class:`.Query`
    """
    query.page_start = int(data.get('start', 0))
    query.page_size = int(data.get('size', 50))
    return query
Ejemplo n.º 2
0
    def test_physics_is_selected_specific_archive_plus_other_groups(self):
        """The physics group and specified archive are added to the query."""
        class_data = {
            "mathematics": True,
            "physics": True,
            "physics_archives": "hep-ex",
        }
        q = advanced._update_query_with_classification(Query(), class_data)
        self.assertIsInstance(q, Query)
        self.assertIsInstance(q.classification, list)
        self.assertEqual(len(q.classification), 2)

        self.assertEqual(
            q.classification,
            [
                {
                    "archive": {
                        "id": "math"
                    }
                },
                {
                    "group": {
                        "id": "grp_physics"
                    },
                    "archive": {
                        "id": "hep-ex"
                    }
                },
            ],
        )
Ejemplo n.º 3
0
def paginate(query: Query, data: Dict[Any, Any]) -> Query:
    """
    Update pagination parameters on a :class:`.Query` from request parameters.

    Parameters
    ----------
    query : :class:`.Query`
    data : dict

    Returns
    -------
    :class:`.Query`

    """
    query.page_start = max(int(data.get("start", 0)), 0)
    query.size = min(int(data.get("size", 50)), Query.MAXIMUM_size)
    return query
Ejemplo n.º 4
0
 def test_multiple_classifications_are_selected(self):
     """Selected classifications are added to the query."""
     class_data = {'computer_science': True, 'eess': True}
     q = advanced._update_query_with_classification(Query(), class_data)
     self.assertIsInstance(q, Query)
     self.assertIsInstance(q.classification, list)
     self.assertEqual(len(q.classification), 2)
     self.assertIsInstance(q.classification[0], Classification)
     self.assertIsInstance(q.classification[1], Classification)
Ejemplo n.º 5
0
 def test_classification_is_selected(self):
     """Selected classifications are added to the query."""
     class_data = {'computer_science': True}
     q = advanced._update_query_with_classification(Query(), class_data)
     self.assertIsInstance(q, Query)
     self.assertIsInstance(q.classification, list)
     self.assertEqual(len(q.classification), 1)
     self.assertIsInstance(q.classification[0], Classification)
     self.assertEqual(q.classification[0].archive['id'], 'cs')
Ejemplo n.º 6
0
 def test_physics_is_selected_specific_archive(self):
     """The physic group and specified archive are added to the query."""
     class_data = {'physics': True, 'physics_archives': 'hep-ex'}
     q = advanced._update_query_with_classification(Query(), class_data)
     self.assertIsInstance(q, Query)
     self.assertIsInstance(q.classification, list)
     self.assertEqual(len(q.classification), 1)
     self.assertIsInstance(q.classification[0], Classification)
     self.assertEqual(q.classification[0].archive['id'], 'hep-ex')
     self.assertEqual(q.classification[0].group['id'], 'grp_physics')
Ejemplo n.º 7
0
 def test_physics_is_selected_all_archives(self):
     """The physics group is added to the query."""
     class_data = {'physics': True, 'physics_archives': 'all'}
     q = advanced._update_query_with_classification(Query(), class_data)
     self.assertIsInstance(q, Query)
     self.assertIsInstance(q.classification, list)
     self.assertEqual(len(q.classification), 1)
     self.assertIsInstance(q.classification[0], Classification)
     self.assertIsNone(q.classification[0].archive)
     self.assertEqual(q.classification[0].group['id'], 'grp_physics')
Ejemplo n.º 8
0
 def test_terms_are_provided(self):
     """Selected terms are added to the query."""
     terms_data = [{'term': 'muon', 'operator': 'AND', 'field': 'title'}]
     q = advanced._update_query_with_terms(Query(), terms_data)
     self.assertIsInstance(q, Query)
     self.assertIsInstance(q.terms, list)
     self.assertEqual(len(q.terms), 1)
     self.assertIsInstance(q.terms[0], FieldedSearchTerm)
     self.assertEqual(q.terms[0].term, 'muon')
     self.assertEqual(q.terms[0].operator, 'AND')
     self.assertEqual(q.terms[0].field, 'title')
Ejemplo n.º 9
0
 def test_terms_are_provided(self):
     """Selected terms are added to the query."""
     terms_data = [{"term": "muon", "operator": "AND", "field": "title"}]
     q = advanced._update_query_with_terms(Query(), terms_data)
     self.assertIsInstance(q, Query)
     self.assertIsInstance(q.terms, list)
     self.assertEqual(len(q.terms), 1)
     self.assertIsInstance(q.terms[0], FieldedSearchTerm)
     self.assertEqual(q.terms[0].term, "muon")
     self.assertEqual(q.terms[0].operator, "AND")
     self.assertEqual(q.terms[0].field, "title")
Ejemplo n.º 10
0
 def test_past_12_is_selected(self):
     """Query selects the past twelve months."""
     date_data = {'filter_by': 'past_12', 'date_type': 'submitted_date'}
     q = advanced._update_query_with_dates(Query(), date_data)
     self.assertIsInstance(q, Query)
     self.assertIsInstance(q.date_range, DateRange)
     twelve_months = relativedelta(months=12, days=date.today().day - 1)
     self.assertEqual(
         q.date_range.start_date.date(),
         date.today() - twelve_months,
         "Start date is the first day of the month twelve prior to today.")
Ejemplo n.º 11
0
 def test_physics_is_selected_specific_archive(self):
     """The physic group and specified archive are added to the query."""
     class_data = {'physics': True, 'physics_archives': 'hep-ex'}
     q = advanced._update_query_with_classification(Query(), class_data)
     self.assertEqual(q.classification, [{
         'archive': {
             'id': 'hep-ex'
         },
         'group': {
             'id': 'grp_physics'
         }
     }])
Ejemplo n.º 12
0
 def test_physics_is_selected_specific_archive_plus_other_groups(self):
     """The physics group and specified archive are added to the query."""
     class_data = {
         'mathematics': True,
         'physics': True,
         'physics_archives': 'hep-ex'
     }
     q = advanced._update_query_with_classification(Query(), class_data)
     self.assertIsInstance(q, Query)
     self.assertIsInstance(q.classification, list)
     self.assertEqual(len(q.classification), 2)
     self.assertIsInstance(q.classification[0], Classification)
     self.assertIsInstance(q.classification[1], Classification)
Ejemplo n.º 13
0
 def test_multiple_classifications_are_selected(self):
     """Selected classifications are added to the query."""
     class_data = {'computer_science': True, 'eess': True}
     q = advanced._update_query_with_classification(Query(), class_data)
     self.assertEqual(q.classification, [{
         'archive': {
             'id': 'cs'
         }
     }, {
         'archive': {
             'id': 'eess'
         }
     }])
Ejemplo n.º 14
0
 def test_multiple_terms_are_provided_with_all_field(self):
     """Selected terms are added to the query."""
     terms_data = [
         {'term': 'switch', 'operator': 'AND', 'field': 'all'},
         {'term': 'disk', 'operator': 'OR', 'field': 'all'}
     ]
     q = advanced._update_query_with_terms(Query(), terms_data)
     self.assertIsInstance(q, Query)
     self.assertIsInstance(q.terms, list)
     self.assertEqual(len(q.terms), 2)
     self.assertIsInstance(q.terms[0], FieldedSearchTerm)
     self.assertEqual(q.terms[1].term, 'disk')
     self.assertEqual(q.terms[1].operator, 'OR')
     self.assertEqual(q.terms[1].field, 'all')
Ejemplo n.º 15
0
 def test_physics_is_selected_specific_archive(self):
     """The physic group and specified archive are added to the query."""
     class_data = {"physics": True, "physics_archives": "hep-ex"}
     q = advanced._update_query_with_classification(Query(), class_data)
     self.assertEqual(
         q.classification,
         [{
             "archive": {
                 "id": "hep-ex"
             },
             "group": {
                 "id": "grp_physics"
             }
         }],
     )
Ejemplo n.º 16
0
 def test_multiple_classifications_are_selected(self):
     """Selected classifications are added to the query."""
     class_data = {"computer_science": True, "eess": True}
     q = advanced._update_query_with_classification(Query(), class_data)
     self.assertEqual(
         q.classification,
         [{
             "archive": {
                 "id": "cs"
             }
         }, {
             "archive": {
                 "id": "eess"
             }
         }],
     )
Ejemplo n.º 17
0
 def test_multiple_terms_are_provided_with_all_field(self):
     """Selected terms are added to the query."""
     terms_data = [
         {
             "term": "switch",
             "operator": "AND",
             "field": "all"
         },
         {
             "term": "disk",
             "operator": "OR",
             "field": "all"
         },
     ]
     q = advanced._update_query_with_terms(Query(), terms_data)
     self.assertIsInstance(q, Query)
     self.assertIsInstance(q.terms, list)
     self.assertEqual(len(q.terms), 2)
     self.assertIsInstance(q.terms[0], FieldedSearchTerm)
     self.assertEqual(q.terms[1].term, "disk")
     self.assertEqual(q.terms[1].operator, "OR")
     self.assertEqual(q.terms[1].field, "all")
Ejemplo n.º 18
0
 def test_physics_is_selected_all_archives(self):
     """The physics group is added to the query."""
     class_data = {"physics": True, "physics_archives": "all"}
     q = advanced._update_query_with_classification(Query(), class_data)
     self.assertEqual(q.classification, [{"group": {"id": "grp_physics"}}])
Ejemplo n.º 19
0
 def test_physics_is_selected_all_archives(self):
     """The physics group is added to the query."""
     class_data = {'physics': True, 'physics_archives': 'all'}
     q = advanced._update_query_with_classification(Query(), class_data)
     self.assertEqual(q.classification, [{'group': {'id': 'grp_physics'}}])