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())
Example #2
0
 def __get_doc_str(doc, _id=None):
     if not isinstance(doc, (OJAIDocument, dict)):
         raise IllegalArgumentError(m="Invalid type of the doc parameter.")
     if isinstance(doc, dict):
         doc = OJAIDocument().from_dict(doc)
     if _id is not None:
         doc.set_id(_id=_id)
     return doc.as_json_str()
 def test_doc_set_list(self):
     nested_doc = OJAIDocument().set('nested_int', 11).set('nested_str', 'strstr')
     doc = OJAIDocument().set('test_list', [1, 2, 3, 4, False, 'mystr', [{}, {}, [7, 8, 9, nested_doc]]])
     self.assertEqual(doc.as_json_str(), json.dumps({"test_list": [{"$numberLong": 1}, {"$numberLong": 2},
                                                                   {"$numberLong": 3}, {"$numberLong": 4}, False,
                                                                   "mystr",
                                                                   [{}, {}, [{"$numberLong": 7}, {"$numberLong": 8},
                                                                             {"$numberLong": 9},
                                                                             {"nested_int": {"$numberLong": 11},
                                                                              "nested_str": "strstr"}]]]}))
Example #4
0
    def test_doc_delete_nested(self):
        doc = OJAIDocument().set_id('121212') \
            .set('test_int', 123) \
            .set('first.test_int', 1235) \
            .set('first.test_float', 12.2) \
            .set('test_float', 11.1)

        self.assertEqual(
            doc.as_dictionary(), {
                '_id': '121212',
                'test_int': 123,
                'first': {
                    'test_float': 12.2,
                    'test_int': 1235
                },
                'test_float': 11.1
            })

        doc.delete('first.test_int')
        self.assertEqual(
            doc.as_dictionary(), {
                '_id': '121212',
                'test_int': 123,
                'first': {
                    'test_float': 12.2
                },
                'test_float': 11.1
            })
        doc.delete('first.test_float')
        self.assertEqual(doc.as_dictionary(), {
            '_id': '121212',
            'test_int': 123,
            'first': {},
            'test_float': 11.1
        })
Example #5
0
 def test_doc_set_dict(self):
     test_dict = {'field_one': 12, 'field_two': 14}
     doc = OJAIDocument().set('test_dict', test_dict)
     self.assertEqual(doc.as_dictionary(),
                      {'test_dict': {
                          'field_one': 12,
                          'field_two': 14
                      }})
     doc.set_id('50')
     self.assertEqual(doc.as_dictionary(), {
         '_id': '50',
         'test_dict': {
             'field_one': 12,
             'field_two': 14
         }
     })
     doc.set('test_dict.insert', 90)
     self.assertEqual(
         doc.as_dictionary(), {
             '_id': '50',
             'test_dict': {
                 'field_one': 12,
                 'field_two': 14,
                 'insert': 90
             }
         })
Example #6
0
 def test_doc_set_float(self):
     doc = OJAIDocument() \
         .set('test_float', 11.1) \
         .set('test_float_two', 12.34)
     self.assertEqual(doc.as_dictionary(), {
         'test_float': 11.1,
         'test_float_two': 12.34
     })
     doc.set('test_int', 999).set('test_long', int(51233123))
     self.assertEqual(
         doc.as_dictionary(), {
             'test_float': 11.1,
             'test_float_two': 12.34,
             'test_long': 51233123,
             'test_int': 999
         })
     doc.set('test_bool', False)
     self.assertEqual(
         doc.as_dictionary(), {
             'test_float': 11.1,
             'test_float_two': 12.34,
             'test_long': 51233123,
             'test_int': 999,
             'test_bool': False
         })
 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_list_with_nested_dict(self):
     test_doc_dict = {"_id": "some_id", "list": [{"name": 55,
                                                  "surname": "Surname",
                                                  "city": "City"}]}
     doc = OJAIDocument().from_dict(test_doc_dict)
     self.assertEqual(doc.as_dictionary(),
                      {'_id': 'some_id', 'list': [{'city': 'City', 'surname': 'Surname', 'name': 55}]})
     self.assertEqual(doc.as_json_str(),
                      json.dumps({"_id": "some_id", "list": [{"name": {"$numberLong": 55},
                                                              "surname": "Surname", "city": "City"}]}))
     self.assertEqual(doc.as_json_str(with_tags=False),
                      json.dumps({"_id": "some_id", "list": [{"name": 55, "surname": "Surname", "city": "City"}]}))
    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}}}))
Example #10
0
    def test_doc_set_byte_array(self):
        byte_array = bytearray([0x13, 0x00, 0x00, 0x00, 0x08, 0x00])
        doc = OJAIDocument().set('test_byte_array', byte_array)
        self.assertEqual(
            doc.as_dictionary(),
            {'test_byte_array': bytearray(b'\x13\x00\x00\x00\x08\x00')})
        self.assertEqual(doc.as_json_str(),
                         '{"test_byte_array": {"$binary": "EwAAAAgA"}}')

        recreated_doc = OJAIDocumentCreator().create_document(
            doc.as_json_str())
        self.assertEqual(
            recreated_doc.as_dictionary(),
            {'test_byte_array': bytearray(b'\x13\x00\x00\x00\x08\x00')})
 def test_set_list_instead_of_list(self):
     doc = OJAIDocument()
     field = 'list_field'
     doc.set(field, value=[1, 1])
     self.assertEqual(doc.as_json_str(), json.dumps({"list_field": [{"$numberLong": 1}, {"$numberLong": 1}]}))
     doc.set(field, value=[2, 2])
     self.assertEqual(doc.as_json_str(), json.dumps({"list_field": [{"$numberLong": 2}, {"$numberLong": 2}]}))
 def test_doc_set_dict(self):
     test_dict = {'field_one': 12, 'field_two': 14}
     doc = OJAIDocument().set('test_dict', test_dict)
     self.assertEqual(doc.as_json_str(), json.dumps({"test_dict": {"field_one": {"$numberLong": 12}, "field_two": {
         "$numberLong": 14}}}))
     doc.set_id('50')
     self.assertEqual(doc.as_json_str(), json.dumps({"test_dict": {"field_one": {"$numberLong": 12},
                                                                   "field_two": {"$numberLong": 14}}, "_id": "50"}))
     doc.set('test_dict.insert', 90)
     self.assertEqual(doc.as_json_str(), json.dumps({"test_dict": {"field_one": {"$numberLong": 12},
                                                                   "field_two": {"$numberLong": 14},
                                                                   "insert": {"$numberLong": 90}}, "_id": "50"}))
Example #13
0
 def increment(self, _id, field, inc):
     str_doc = OJAIDocument().set_id(_id=_id).as_json_str()
     from mapr.ojai.document.OJAIDocumentMutation import \
         OJAIDocumentMutation
     str_mutation = self.__get_str_mutation(
         OJAIDocumentMutation().increment(field_path=field, inc=inc))
     self.__execute_update(_id=str_doc, mutation=str_mutation)
    def set_list(value, tags=False):
        from mapr.ojai.ojai.OJAITagsBuilder import OJAITagsBuilder
        ojai_list = []
        if tags:
            dump_document = OJAITagsBuilder()
        else:
            from mapr.ojai.ojai.OJAIDocument import OJAIDocument
            dump_document = OJAIDocument()

        for elem in value:
            if isinstance(elem, list):
                if isinstance(dump_document, OJAITagsBuilder):
                    nested_list = OJAIList.set_list(elem, tags=True)
                else:
                    nested_list = OJAIList.set_list(elem)
                ojai_list.append(nested_list)
            elif isinstance(elem, dict) and bool(elem):
                tmp_dict = {}
                for k, v in list(elem.items()):
                    if isinstance(v, list):
                        tmp_dict[k] = OJAIList.set_list(v)
                    else:
                        internal_value = dump_document.set(
                            'dump', v).as_dictionary()['dump']
                        tmp_dict[k] = internal_value
                ojai_list.append(tmp_dict)
            else:
                ojai_list.append(
                    dump_document.set('dump', elem).as_dictionary()['dump'])
            dump_document.clear()
        return ojai_list
 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"}))
Example #16
0
 def test_doc_set_list(self):
     nested_doc = OJAIDocument().set('nested_int',
                                     11).set('nested_str', 'strstr')
     doc = OJAIDocument().set(
         'test_list',
         [1, 2, 3, 4, False, 'mystr', [{}, {}, [7, 8, 9, nested_doc]]])
     self.assertEqual(
         doc.as_dictionary(), {
             'test_list': [
                 1, 2, 3, 4, False, 'mystr',
                 [{}, {},
                  [7, 8, 9, {
                      'nested_str': 'strstr',
                      'nested_int': 11
                  }]]
             ]
         })
Example #17
0
    def __delete_document(self, document):
        if not isinstance(document, (OJAIDocument, dict)):
            raise IllegalArgumentError(m="Invalid type of the doc parameter.")

        if isinstance(document, OJAIDocument):
            self.__evaluate_delete(document.as_json_str())
        else:
            self.__evaluate_delete(
                OJAIDocument().from_dict(document_dict=document).as_json_str())
Example #18
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)))
    def test_nested_doc_insert(self):
        nested_doc = OJAIDocument().set('nested_int', 11).set('nested_str', 'strstr')
        doc = OJAIDocument().set('test_list',
                                 [1, 2, 3, 4,
                                  False, 'mystr',
                                  [{}, {},
                                   [7, 8, 9, nested_doc]]]) \
            .set_id('testid001')
        connection = ConnectionFactory.get_connection(connection_str=CONNECTION_STR,
                                                      options=CONNECTION_OPTIONS)
        if connection.is_store_exists(store_path='/test-store8'):
            document_store = connection.get_store(store_path='/test-store8')
        else:
            document_store = connection.create_store(store_path='/test-store8')
        check_store = connection.is_store_exists(store_path='/test-store8')
        self.assertTrue(check_store)
        self.assertTrue(isinstance(document_store, OJAIDocumentStore))

        document_store.insert_or_replace(doc=doc)
Example #20
0
 def __evaluate_doc_stream(self, doc_stream, operation_type):
     LOG.debug('Start sending documents on the server.')
     for doc in doc_stream:
         if isinstance(doc, OJAIDocument):
             self.__validate_dict(doc.as_dictionary())
             doc_str = doc.as_json_str()
         else:
             self.__validate_dict(doc)
             doc_str = OJAIDocument().from_dict(doc).as_json_str()
         self.__evaluate_doc(doc_str=doc_str, operation_type=operation_type)
 def test_doc_set_float(self):
     doc = OJAIDocument() \
         .set('test_float', 11.1) \
         .set('test_float_two', 12.34)
     self.assertEqual(doc.as_json_str(), json.dumps({"test_float": {"$numberFloat": 11.1}, "test_float_two": {
         "$numberFloat": 12.34}}))
     doc.set('test_int', 999).set('test_long', int(51233123))
     self.assertEqual(json.loads(doc.as_json_str()), {"test_float": {"$numberFloat": 11.1},
                                                      "test_long": {"$numberLong": 51233123},
                                                      "test_float_two": {"$numberFloat": 12.34},
                                                      "test_int": {"$numberLong": 999}})
     doc.set('test_bool', False)
     self.assertEqual(doc.as_json_str(), json.dumps({"test_float": {"$numberFloat": 11.1},
                                                     "test_float_two": {"$numberFloat": 12.34},
                                                     "test_int": {"$numberLong": 999},
                                                     "test_long": {"$numberLong": 51233123}, "test_bool": False}))
Example #22
0
 def check_and_update(self, _id, query_condition, mutation):
     str_condition = OJAIDocumentStore.__get_str_condition(query_condition)
     str_doc = OJAIDocument().set_id(_id=_id).as_json_str()
     str_mutation = OJAIDocumentStore.__get_str_mutation(mutation)
     try:
         self.__execute_update(_id=str_doc,
                               mutation=str_mutation,
                               condition=str_condition)
     except DocumentNotFoundError:
         return False
     return True
Example #23
0
    def test_delete_by_index(self):
        doc = OJAIDocument().set('test_list', [1, 2, 3, 4, 5, 6, 7])

        self.assertEqual({'test_list': [1, 2, 3, 4, 5, 6, 7]},
                         doc.as_dictionary())
        doc.delete('test_list[3]')
        self.assertEqual({'test_list': [1, 2, 3, 5, 6, 7]},
                         doc.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())
 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}}))
Example #26
0
    def test_delete_document_stream(self):
        connection = ConnectionFactory.get_connection(connection_str=CONNECTION_STR,
                                                      options=CONNECTION_OPTIONS)

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

        doc_stream = []

        for i in range(1, 5):
            doc_stream.append(OJAIDocument().from_dict(DICT_STREAM[i]))

        document_store.delete(doc_stream=doc_stream)
 def test_doc_set_bool(self):
     doc = OJAIDocument() \
         .set('test_bool', True) \
         .set('test_boolean_false', False)
     self.assertEqual(doc.as_json_str(), '{"test_bool": true, "test_boolean_false": false}')
     doc.set('test_int', 11) \
         .set('test_long', int(123))
     self.assertEqual(doc.as_json_str(), json.dumps({"test_bool": True, "test_boolean_false": False,
                                                     "test_int": {"$numberLong": 11},
                                                     "test_long": {"$numberLong": 123}}))
    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}}))
Example #29
0
 def check_and_delete(self, _id, condition):
     str_condition = OJAIDocumentStore.__get_str_condition(
         condition=condition)
     request = DeleteRequest(
         table_path=self.__store_path,
         payload_encoding=PayloadEncoding.Value('JSON_ENCODING'),
         json_condition=str_condition,
         json_document=OJAIDocument().set_id(_id=_id).as_json_str())
     LOG.debug(
         'Sending CHECK AND DELETE request to the server. Request body: %s',
         request)
     response = self.__connection.Delete(request)
     LOG.debug(
         'Got CHECK AND DELETE response from the server. Response body: %s',
         response)
     self.validate_response(response)
Example #30
0
    def __delete_doc_stream(self, doc_stream):
        LOG.debug('Start deleting documents on the server.')
        if not isinstance(doc_stream, list):
            raise IllegalArgumentError(
                m="Invalid type of the doc_stream parameter.")

        for doc in doc_stream:
            if isinstance(doc, OJAIDocument):
                self.__evaluate_delete(doc.as_json_str())
            elif isinstance(doc, dict):
                self.__evaluate_delete(
                    OJAIDocument().from_dict(document_dict=doc).as_json_str())
            else:
                raise IllegalArgumentError(
                    m="Invalid type of the doc parameter, must be "
                    "OJAIDocument or dict.")