def test_doc_set_time(self):
     doc1 = OJAIDocument().set("test_time", OTime(timestamp=1518689532)).set_id('121212')
     doc2 = OJAIDocument().set("test_time", OTime(hour_of_day=12, minutes=12, seconds=12)).set_id('121212')
     doc3 = OJAIDocument().set("test_time", OTime(date=datetime(year=1999, month=12, day=31, hour=12, minute=12,
                                                                second=12))).set_id("121212")
     self.assertEqual(doc1.as_json_str(), json.dumps({"test_time": {"$time": "12:12:12"}, "_id": "121212"}))
     self.assertEqual(doc2.as_json_str(), json.dumps({"test_time": {"$time": "12:12:12"}, "_id": "121212"}))
     self.assertEqual(doc3.as_json_str(), json.dumps({"test_time": {"$time": "12:12:12"}, "_id": "121212"}))
    def test_find_by_id_as_dict(self):
        connection = ConnectionFactory.get_connection(connection_str=CONNECTION_STR,
                                                      options=CONNECTION_OPTIONS)

        if connection.is_store_exists(store_path='/find-by-id-test-store1'):
            document_store = connection.get_store(store_path='/find-by-id-test-store1')
        else:
            document_store = connection.create_store(store_path='/find-by-id-test-store1')

        self.assertTrue(connection.is_store_exists('/find-by-id-test-store1'))
        doc = document_store.find_by_id('121212')

        document = OJAIDocument().set_id("121212") \
            .set('test_int', 123) \
            .set('first.test_int', 1235) \
            .set('first.test_long', 123456789) \
            .set('first.test_time', OTime(timestamp=1518689532)) \
            .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)])

        self.assertEqual(doc, document.as_dictionary())
    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)
Exemple #4
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)
Exemple #5
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)))
Exemple #6
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_find_by_id(self):
        connection = ConnectionFactory.get_connection(connection_str=CONNECTION_STR,
                                                      options=CONNECTION_OPTIONS)

        if connection.is_store_exists(store_path='/find-by-id-test-store1'):
            document_store = connection.get_store(store_path='/find-by-id-test-store1')
        else:
            document_store = connection.create_store(store_path='/find-by-id-test-store1')
        document = connection.new_document(dictionary={'_id': 'id008',
                                                       'test_int': 51,
                                                       'test_str': 'strstr',
                                                       'test_dict': {'test_int': 5},
                                                       'test_list': [5, 6],
                                                       'test_null': None,
                                                       'test_otime': OTime(timestamp=1518689532)})

        document_store.insert_or_replace(doc=document)

        self.assertTrue(connection.is_store_exists('/find-by-id-test-store1'))
        doc = document_store.find_by_id('id008')

        self.assertEqual(doc, document.as_dictionary())
    def test_insert_find_large_doc(self):
        document = OJAIDocument().set_id("121212") \
            .set('test_int', 123) \
            .set('first.test_int', 1235) \
            .set('first.test_long', 123456789) \
            .set('first.test_time', OTime(timestamp=1518689532)) \
            .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)]) \

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

        if connection.is_store_exists(store_path='/find-by-id-test-store1'):
            document_store = connection.get_store(store_path='/find-by-id-test-store1')
        else:
            document_store = connection.create_store(store_path='/find-by-id-test-store1')

        document_store.insert_or_replace(doc=document)

        self.assertTrue(connection.is_store_exists('/find-by-id-test-store1'))
        doc = document_store.find_by_id('121212', results_as_document=True)

        self.assertEqual(doc.as_json_str(), '{"_id": "121212", "test_int": {"$numberLong": 123}, "first": {'
                                            '"test_invalid": {"$dateDay": "1979-06-20"}, "test_time": {"$time": '
                                            '"12:12:12"}, "test_bool_false": false, "test_list": [{"$numberLong": 1}, '
                                            '{"$numberLong": 2}, "str", false, {"$dateDay": "1979-06-20"}], '
                                            '"test_long": {"$numberLong": 123456789}, "test_dict2": {}, "test_dict": '
                                            '{"a": {"$numberLong": 1}, "b": {"$numberLong": 2}}, "test_bool": true, '
                                            '"test_date": {"$dateDay": "1979-06-19"}, "test_str": "strstr", '
                                            '"test_int": {"$numberLong": 1235}}}')
        self.assertEqual(doc.as_dictionary(), document.as_dictionary())
Exemple #9
0
 def test_o_time_from_date(self):
     import datetime
     o_time = OTime(date=datetime.datetime(year=1970, month=1, day=1, hour=12, minute=25, second=55))
     self.assertEqual(o_time.get_hour(), 12)
     self.assertEqual(o_time.get_minute(), 25)
     self.assertEqual(o_time.get_second(), 55)
     self.assertEqual(o_time.get_millis(), 0)
     parse_o_time = OTime.parse(time_str="12:25:55")
     self.assertEqual(parse_o_time.get_hour(), o_time.get_hour())
     self.assertEqual(parse_o_time.get_minute(), o_time.get_minute())
     self.assertEqual(parse_o_time.get_second(), o_time.get_second())
     self.assertTrue(o_time.__eq__(parse_o_time))
     self.assertEqual(o_time.to_str("%H:%M:%S"), "12:25:55")
     self.assertEqual(o_time.time_to_str(), "12:25:55")
Exemple #10
0
 def test_o_time_from_time(self):
     o_time = OTime(hour_of_day=12, minutes=25, seconds=55)
     self.assertEqual(o_time.get_hour(), 12)
     self.assertEqual(o_time.get_minute(), 25)
     self.assertEqual(o_time.get_second(), 55)
     self.assertEqual(o_time.get_millis(), 0)
     parse_o_time = OTime.parse(time_str="12:25:55")
     self.assertEqual(parse_o_time.get_hour(), o_time.get_hour())
     self.assertEqual(parse_o_time.get_minute(), o_time.get_minute())
     self.assertEqual(parse_o_time.get_second(), o_time.get_second())
     self.assertTrue(o_time.__eq__(parse_o_time))
     self.assertEqual(o_time.to_str("%H:%M:%S"), "12:25:55")
     self.assertEqual(o_time.time_to_str(), "12:25:55")
Exemple #11
0
 def test_days_from_epoch(self):
     epoch = 8587555
     o_time = OTime(timestamp=epoch)
     self.assertEqual(o_time.get_hour(), 12)
     self.assertEqual(o_time.get_minute(), 25)
     self.assertEqual(o_time.get_second(), 55)
     self.assertEqual(o_time.get_millis(), 0)
     parse_o_time = OTime.parse(time_str="12:25:55")
     self.assertEqual(parse_o_time.get_hour(), o_time.get_hour())
     self.assertEqual(parse_o_time.get_minute(), o_time.get_minute())
     self.assertEqual(parse_o_time.get_second(), o_time.get_second())
     self.assertTrue(o_time.__eq__(parse_o_time))
     self.assertEqual(o_time.to_str("%H:%M:%S"), "12:25:55")
     self.assertEqual(o_time.time_to_str(), "12:25:55")
}

# ===== CHECK AND DELETE / CHECK AND REPLACE / DELETE / INSERT REPLACE ======
DICT_STREAM = [{
    '_id': "id01",
    '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'
json_dict = {
    "_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