コード例 #1
0
    def test_datetime_conversion(self):
        """ tests json_date_to_datetime with different formats """
        json_date = "2015-01-31"
        self.assertTrue(json_date_to_datetime(json_date), datetime(2015, 1, 31))

        json_datetime = "2015-01-31T07:30:28"
        self.assertTrue(json_date_to_datetime(json_datetime), datetime(2015, 1, 31, 7, 30, 28))

        json_datetime = "2015-01-31T07:30:28.65785"
        self.assertTrue(json_date_to_datetime(json_datetime), datetime(2015, 1, 31, 7, 30, 28, 65785))

        json_datetime = "2015-01-31T07:30:28Z"
        self.assertTrue(json_date_to_datetime(json_datetime), datetime(2015, 1, 31, 7, 30, 28))

        json_datetime = "2015-01-31T07:30:28.65785Z"
        self.assertTrue(json_date_to_datetime(json_datetime), datetime(2015, 1, 31, 7, 30, 28, 65785))
コード例 #2
0
    def test_file_value_formats(self):
        """ test the format of values that write/read from the file """
        # json serialization removes microseconds part of the datetime object, so
        # we strip it at the beginning to allow equality comparison to be correct
        this_moment = datetime.utcnow().replace(microsecond=0)
        self.searcher.index([
            {
                "content": {
                    "name": "How did 11 of 12 balls get deflated during the game"
                },
                "my_date_value": this_moment,
                "my_integer_value": 172,
                "my_float_value": 57.654,
                "my_string_value": "If the officials just blew it, would they come out and admit it?"
            },
        ])

        # now search should be successful
        response = self.searcher.search(query_string="deflated")
        self.assertEqual(response["total"], 1)

        # and values should be what we desire
        returned_result = response["results"][0]["data"]
        self.assertEqual(
            json_date_to_datetime(returned_result["my_date_value"]),
            this_moment
        )
        self.assertEqual(returned_result["my_integer_value"], 172)
        self.assertEqual(returned_result["my_float_value"], 57.654)
        self.assertEqual(
            returned_result["my_string_value"],
            "If the officials just blew it, would they come out and admit it?"
        )
コード例 #3
0
    def test_file_value_formats(self):
        """ test the format of values that write/read from the file """
        # json serialization removes microseconds part of the datetime object, so
        # we strip it at the beginning to allow equality comparison to be correct
        this_moment = datetime.utcnow().replace(microsecond=0)
        test_object = {
            "content": {
                "name": "How did 11 of 12 balls get deflated during the game"
            },
            "my_date_value": this_moment,
            "my_integer_value": 172,
            "my_float_value": 57.654,
            "my_string_value": "If the officials just blew it, would they come out and admit it?"
        }

        self.searcher.index("test_doc", [test_object])

        # now search should be successful
        response = self.searcher.search(query_string="deflated")
        self.assertEqual(response["total"], 1)

        # and values should be what we desire
        returned_result = response["results"][0]["data"]
        self.assertEqual(json_date_to_datetime(returned_result["my_date_value"]), this_moment)
        self.assertEqual(returned_result["my_integer_value"], 172)
        self.assertEqual(returned_result["my_float_value"], 57.654)
        self.assertEqual(
            returned_result["my_string_value"],
            "If the officials just blew it, would they come out and admit it?"
        )
コード例 #4
0
    def test_datetime_conversion(self):
        """ tests json_date_to_datetime with different formats """
        json_date = "2015-01-31"
        self.assertTrue(json_date_to_datetime(json_date), datetime(2015, 1, 31))

        json_datetime = "2015-01-31T07:30:28"
        self.assertTrue(json_date_to_datetime(json_datetime), datetime(2015, 1, 31, 7, 30, 28))

        json_datetime = "2015-01-31T07:30:28.65785"
        self.assertTrue(json_date_to_datetime(json_datetime), datetime(2015, 1, 31, 7, 30, 28, 65785))

        json_datetime = "2015-01-31T07:30:28Z"
        self.assertTrue(json_date_to_datetime(json_datetime), datetime(2015, 1, 31, 7, 30, 28))

        json_datetime = "2015-01-31T07:30:28.65785Z"
        self.assertTrue(json_date_to_datetime(json_datetime), datetime(2015, 1, 31, 7, 30, 28, 65785))