def test_doc_set_timestamp(self):
     doc1 = OJAIDocument().set('test_timestamp', OTimestamp(millis_since_epoch=29877132000)).set_id('121212')
     doc2 = OJAIDocument().set('test_timestamp', OTimestamp(year=1970, month_of_year=12, day_of_month=12,
                                                            hour_of_day=12, minute_of_hour=12, second_of_minute=12,
                                                            millis_of_second=12)).set_id('121212')
     doc3 = OJAIDocument().set('test_timestamp', OTimestamp(date=datetime(year=1970, month=12, day=12, hour=12,
                                                                          minute=12, second=12))).set_id('121212')
     self.assertEqual(doc1.as_json_str(), json.dumps({"test_timestamp": {"$date": "1970-12-12T19:12:12.000000Z"},
                                                      "_id": "121212"}))
     self.assertEqual(doc2.as_json_str(), json.dumps({"test_timestamp": {"$date": "1970-12-12T12:12:12.012000Z"},
                                                      "_id": "121212"}))
     self.assertEqual(doc3.as_json_str(),
                      json.dumps({"test_timestamp": {"$date": "1970-12-12T12:12:12.000000Z"}, "_id": "121212"}))
Beispiel #2
0
 def test_o_timestamp_from_millis_epoch(self):
     o_timestamp = OTimestamp(millis_since_epoch=8598355000)
     self.assertEqual(o_timestamp.get_year(), 1970)
     self.assertEqual(o_timestamp.get_month(), 4)
     self.assertEqual(o_timestamp.get_day_of_month(), 10)
     self.assertEqual(o_timestamp.get_hour(), 12)
     self.assertEqual(o_timestamp.get_minute(), 25)
     self.assertEqual(o_timestamp.get_second(), 55)
    def test_insert(self):
        dict_stream = [{'_id': "id001", 'test_int': 51, 'test_str': 'strstr'},
                       {'_id': 'id002', 'mystr': 'str', 'test_int': 51, 'test_str': 'strstr'},
                       {'_id': 'id003', 'test_int': 51, 'test_otime': OTime(timestamp=1518689532),
                        'test_str': 'strstr'},
                       {'_id': 'id004', 'test_int': 51, 'test_timestamp': OTimestamp(millis_since_epoch=29877132000),
                        'test_str': 'strstr'},
                       {'_id': 'id005', 'test_int': 51, 'test_bool': True, 'test_str': 'strstr'},
                       {'_id': 'id006', 'test_int': 51, 'test_str': 'strstr'},
                       {'_id': 'id007', 'test_int': 51, 'test_str': 'strstr'},
                       {'_id': 'id008', 'test_int': 51, 'test_str': 'strstr', 'test_dict': {'test_int': 5}},
                       {'_id': 'id009', 'test_int': 51, 'test_str': 'strstr', 'test_list': [5, 6]},
                       {'_id': 'id010', 'test_int': 51, 'test_str': 'strstr', 'test_null': None}]

        connection = ConnectionFactory.get_connection(connection_str=CONNECTION_STR,
                                                      options=CONNECTION_OPTIONS)

        # should raise an error if exit is not 0
        if connection.is_store_exists(store_path='/test-store6'):
            document_store = connection.get_store(store_path='/test-store6')
        else:
            document_store = connection.create_store(store_path='/test-store6')
        check_store = connection.is_store_exists(store_path='/test-store6')
        self.assertTrue(check_store)
        self.assertTrue(isinstance(document_store, OJAIDocumentStore))
        for doc in dict_stream:
            document = connection.new_document(dictionary=doc)
            # print('Insert document with ID: ' + str(document.get_id()))
            document_store.insert(doc=document)
        #
        drop_store = connection.delete_store(store_path='/test-store6')
        self.assertTrue(drop_store)
Beispiel #4
0
 def test_parse_timestamp_to_o_timestamp(self):
     o_timestamp = OTimestamp.parse("April 10 1970 12:25:55")
     self.assertEqual(o_timestamp.get_year(), 1970)
     self.assertEqual(o_timestamp.get_month(), 4)
     self.assertEqual(o_timestamp.get_day_of_month(), 10)
     self.assertEqual(o_timestamp.get_hour(), 12)
     self.assertEqual(o_timestamp.get_minute(), 25)
     self.assertEqual(o_timestamp.get_second(), 55)
    def test_document_change_value_type(self):
        doc = OJAIDocument().set_id('121212') \
            .set('test_int', 123) \
            .set('test_float', 11.1)
        self.assertEqual(doc.as_json_str(), json.dumps({"_id": "121212", "test_int": {"$numberLong": 123},
                                                        "test_float": {"$numberFloat": 11.1}}))
        doc.set('test_int', OTimestamp(millis_since_epoch=29877132000))

        self.assertEqual(doc.as_json_str(),
                         json.dumps({"_id": "121212", "test_int": {"$date": "1970-12-12T19:12:12.000000Z"},
                                     "test_float": {"$numberFloat": 11.1}}))
 def test_doc_set_interval(self):
     doc = OJAIDocument() \
         .set('test_interval', OInterval(milli_seconds=172800000))
     self.assertEqual(doc.as_json_str(), '{"test_interval": {"$interval": 172800000}}')
     doc.set_id('121212') \
         .set('test_int', 123) \
         .set('test_timestamp', OTimestamp(millis_since_epoch=29877132000)) \
         .set('test_float', 11.1)
     json.loads(doc.as_json_str())
     self.assertEqual(doc.as_json_str(), json.dumps({"test_interval": {"$interval": 172800000}, "_id": "121212",
                                                     "test_int": {"$numberLong": 123},
                                                     "test_timestamp": {"$date": "1970-12-12T19:12:12.000000Z"},
                                                     "test_float": {"$numberFloat": 11.1}}))
    def test_doc_set_doc(self):
        doc_to_set = OJAIDocument().set_id('121212') \
            .set('test_int', 123) \
            .set('test_timestamp', OTimestamp(millis_since_epoch=29877132000)) \
            .set('test_float', 11.1)

        doc = OJAIDocument().set('test_int_again', 55).set('internal_doc', doc_to_set)
        self.assertEqual(doc.as_json_str(), json.dumps({"test_int_again": {"$numberLong": 55},
                                                        "internal_doc": {"_id": "121212",
                                                                         "test_int": {"$numberLong": 123},
                                                                         "test_timestamp": {
                                                                             "$date": "1970-12-12T19:12:12.000000Z"},
                                                                         "test_float": {"$numberFloat": 11.1}}}))
Beispiel #8
0
 def generate_o_types(str_type, str_value):
     if str_type == '$interval':
         from ojai.types.OInterval import OInterval
         return OInterval(milli_seconds=str_value)
     elif str_type == '$date':
         from ojai.types.OTimestamp import OTimestamp
         return OTimestamp.parse(str_value)
     elif str_type == '$dateDay':
         from ojai.types.ODate import ODate
         return ODate.parse(str_value)
     else:
         from ojai.types.OTime import OTime
         return OTime.parse(str_value)
Beispiel #9
0
 def test_o_timestamp_from_date(self):
     import datetime
     date = datetime.datetime(year=1970,
                              month=4,
                              day=10,
                              hour=12,
                              minute=25,
                              second=55)
     o_timestamp = OTimestamp(date=date)
     self.assertEqual(o_timestamp.get_year(), 1970)
     self.assertEqual(o_timestamp.get_month(), 4)
     self.assertEqual(o_timestamp.get_day_of_month(), 10)
     self.assertEqual(o_timestamp.get_hour(), 12)
     self.assertEqual(o_timestamp.get_minute(), 25)
     self.assertEqual(o_timestamp.get_second(), 55)
Beispiel #10
0
    def test_document_to_json_str_without_tags(self):
        doc = OJAIDocument().set_id('121212') \
            .set('test_int', 123) \
            .set('first.test_int', 1235) \
            .set('first.test_long', 123456789) \
            .set('first.test_float', 123456789.123) \
            .set('first.test_time', OTime(timestamp=1518689532)) \
            .set('test_float', 11.1) \
            .set('first.test_timestamp', OTimestamp(millis_since_epoch=29877132000)) \
            .set('first.test_date', ODate(days_since_epoch=3456)) \
            .set('first.test_bool', True) \
            .set('first.test_bool_false', False) \
            .set('first.test_invalid', ODate(days_since_epoch=3457)) \
            .set('first.test_str', 'strstr') \
            .set('first.test_dict', {'a': 1, 'b': 2}) \
            .set('first.test_dict2', {}) \
            .set('first.test_list', [1, 2, 'str', False, ODate(days_since_epoch=3457)])\
            .set('test_bytearray', bytearray(b'\x06\x06'))

        self.assertEqual(
            {
                "_id": "121212",
                "test_int": 123,
                "first": {
                    "test_int": 1235,
                    "test_long": 123456789,
                    "test_float": 123456789.123,
                    "test_time": "12:12:12",
                    "test_timestamp": "1970-12-12T19:12:12.000000Z",
                    "test_date": "1979-06-19",
                    "test_bool": True,
                    "test_bool_false": False,
                    "test_invalid": "1979-06-20",
                    "test_str": "strstr",
                    "test_dict": {
                        "a": 1,
                        "b": 2
                    },
                    "test_dict2": {},
                    "test_list": [1, 2, "str", False, "1979-06-20"]
                },
                "test_float": 11.1,
                "test_bytearray": "\u0006\u0006"
            }, json.loads(doc.as_json_str(with_tags=False)))
Beispiel #11
0
    def test_doc_get(self):
        doc = OJAIDocument().set_id('121212') \
            .set('test_int', 123) \
            .set('first.test_int', 1235) \
            .set('first.test_long', 123456789) \
            .set('first.test_float', 123456789.123) \
            .set('first.test_time', OTime(timestamp=1518689532)) \
            .set('test_float', 11.1) \
            .set('first.test_timestamp', OTimestamp(millis_since_epoch=29877132000)) \
            .set('first.test_date', ODate(days_since_epoch=3456)) \
            .set('first.test_interval', OInterval(milli_seconds=172800000)) \
            .set('first.test_bool', True) \
            .set('first.test_bool_false', False) \
            .set('first.test_invalid', ODate(days_since_epoch=3457)) \
            .set('first.test_str', 'strstr') \
            .set('first.test_dict', {'a': 1, 'b': 2}) \
            .set('first.test_dict2', {}) \
            .set('first.test_list', [1, 2, 'str', False, ODate(days_since_epoch=3457)]) \

        self.assertEqual(doc.get_int('test_int'), 123)
        self.assertEqual(doc.get_int('first.test_int'), 1235)
        self.assertEqual(doc.get_int('first.test_long'), 123456789)
        self.assertEqual(doc.get_long('first.test_long'), 123456789)
        self.assertEqual(doc.get_float('first.test_float'), 123456789.123)
        self.assertEqual(
            doc.get_time('first.test_time').time_to_str(), '12:12:12')
        self.assertEqual(
            doc.get_interval('first.test_interval').time_duration, 172800000)
        self.assertEqual(
            doc.get_timestamp('first.test_timestamp').__str__(),
            '1970-12-12T19:12:12.000000Z')
        self.assertEqual(
            doc.get_date('first.test_date').to_date_str(), '1979-06-19')
        self.assertEqual(doc.get_boolean('first.test_bool'), True)
        self.assertEqual(doc.get_boolean('first.test_bool_false'), False)
        self.assertEqual(doc.get_boolean('first.test_invalid'), None)
        self.assertEqual(doc.get_str('first.test_str'), 'strstr')
        self.assertEqual(doc.get_dictionary('first.test_dict'), {
            'a': 1,
            'b': 2
        })
        self.assertEqual(doc.get_dictionary('first.test_dict2'), {})
    def test_doc_delete_nested(self):
        doc = OJAIDocument().set_id('121212') \
            .set('test_int', 123) \
            .set('first.test_int', 1235) \
            .set('first.test_timestamp', OTimestamp(millis_since_epoch=29877132000)) \
            .set('test_float', 11.1)

        self.assertEqual(doc.as_json_str(), json.dumps({"_id": "121212", "test_int": {"$numberLong": 123},
                                                        "first": {"test_int": {"$numberLong": 1235},
                                                                  "test_timestamp": {
                                                                      "$date": "1970-12-12T19:12:12.000000Z"}},
                                                        "test_float": {"$numberFloat": 11.1}}))

        doc.delete('first.test_int')
        self.assertEqual(doc.as_json_str(), json.dumps({"_id": "121212", "test_int": {"$numberLong": 123},
                                                        "first": {
                                                            "test_timestamp": {"$date": "1970-12-12T19:12:12.000000Z"}},
                                                        "test_float": {"$numberFloat": 11.1}}))
        doc.delete('first.test_timestamp')
        self.assertEqual(doc.as_json_str(), json.dumps({"_id": "121212", "test_int": {"$numberLong": 123},
                                                        "first": {}, "test_float": {"$numberFloat": 11.1}}))
    'test_int': 51,
    'test_str': 'strstr'
}, {
    '_id': 'id02',
    'mystr': 'str',
    'test_int': 51,
    'test_str': 'strstr'
}, {
    '_id': 'id03',
    'test_int': 51,
    'test_otime': OTime(timestamp=1518689532),
    'test_str': 'strstr'
}, {
    '_id': 'id04',
    'test_int': 51,
    'test_timestamp': OTimestamp(millis_since_epoch=29877132000),
    'test_str': 'strstr'
}, {
    '_id': 'id05',
    'test_int': 51,
    'test_bool': True,
    'test_str': 'strstr'
}, {
    '_id': 'id06',
    'test_int': 51,
    'test_str': 'strstr'
}, {
    '_id': 'id07',
    'test_int': 51,
    'test_str': 'strstr'
}, {
Beispiel #14
0
    def test_days_from_epoch(self):
        o_timestamp = OTimestamp(year=1970,
                                 month_of_year=4,
                                 day_of_month=10,
                                 hour_of_day=12,
                                 minute_of_hour=25,
                                 second_of_minute=55,
                                 millis_of_second=600)
        o_timestamp_clone = OTimestamp(year=1970,
                                       month_of_year=4,
                                       day_of_month=10,
                                       hour_of_day=12,
                                       minute_of_hour=25,
                                       second_of_minute=55,
                                       millis_of_second=600)

        self.assertEqual(o_timestamp.get_year(), 1970)
        self.assertEqual(o_timestamp.get_month(), 4)
        self.assertEqual(o_timestamp.get_day_of_month(), 10)
        self.assertEqual(o_timestamp.get_hour(), 12)
        self.assertEqual(o_timestamp.get_minute(), 25)
        self.assertEqual(o_timestamp.get_second(), 55)
        self.assertEqual(o_timestamp.get_millis(), 600)
        self.assertTrue(o_timestamp.__eq__(o_timestamp_clone))
    "_id": "id002",
    "name": "Joe",
    "age": 50,
    "address": {
        "street": "555 Moon Way",
        "city": "Gotham"
    }
}

byte_array = bytearray([0x13, 0x00, 0x00, 0x00, 0x08, 0x00])
doc = OJAIDocument().set_id("id003") \
            .set('test_int', 14) \
            .set('first.test_int', 1235) \
            .set('first.test_long', 123456789) \
            .set('first.test_time', OTime(timestamp=1518689532)) \
            .set('first.test_timestamp', OTimestamp(millis_since_epoch=29877132000)) \
            .set('first.test_date', ODate(days_since_epoch=3456)) \
            .set('first.test_bool', True) \
            .set('first.test_bool_false', False) \
            .set('first.test_invalid', ODate(days_since_epoch=3457)) \
            .set('first.test_str', 'strstr') \
            .set('first.test_dict', {'a': 1, 'b': 2}) \
            .set('first.test_dict2', {}) \
            .set('first.test_list', [1, 2, 'str', False, ODate(days_since_epoch=3457)]) \
            .set('first.test_binary', byte_array)

# Create new document from json_document
new_document = connection.new_document(dictionary=json_dict)
doc.set('first.second.nested_doc', new_document)
# Insert new document into the store
store.insert_or_replace(doc=doc)