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_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 })
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 } })
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_set_list_instead_of_list(self): doc = OJAIDocument() field = 'list_field' doc.set(field, value=[1, 1]) doc.set(field, value=[2, 2]) self.assertEqual(doc.as_dictionary(), {field: [2, 2]}) doc.set(field, 5) self.assertEqual(doc.as_dictionary(), {field: 5}) doc.set(field, value=[1, 1]) self.assertEqual(doc.as_dictionary(), {field: [1, 1]})
def test_doc_delete_first_level(self): doc = OJAIDocument().set_id('121212') \ .set('test_int', 123) \ .set('test_float', 11.1) self.assertEqual(doc.as_dictionary(), { '_id': '121212', 'test_int': 123, 'test_float': 11.1 }) doc.delete('test_timestamp') self.assertEqual(doc.as_dictionary(), { '_id': '121212', 'test_int': 123, 'test_float': 11.1 })
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_document_change_value_type(self): doc = OJAIDocument().set_id('121212') \ .set('test_int', 123) \ .set('test_float', 11.1) self.assertEqual(doc.as_dictionary(), { '_id': '121212', 'test_int': 123, 'test_float': 11.1 }) doc.set('test_int', 12.2) self.assertEqual(doc.as_dictionary(), { '_id': '121212', 'test_int': 12.2, 'test_float': 11.1 })
def test_doc_set_bool(self): doc = OJAIDocument() \ .set('test_bool', True) \ .set('test_boolean_false', False) self.assertEqual(doc.as_dictionary(), { 'test_bool': True, 'test_boolean_false': False }) doc.set('test_int', 11) \ .set('test_long', int(123)) self.assertEqual( doc.as_dictionary(), { 'test_bool': True, 'test_boolean_false': False, 'test_int': 11, 'test_long': 123 })
def test_doc_insert_id(self): doc = OJAIDocument() doc.set_id("75") self.assertEqual(doc.get_id(), "75") self.assertIsInstance(doc.get_id(), str) doc.set_id(str("75")) self.assertEqual(doc.get_id(), "75") self.assertIsInstance(doc.get_id(), str) self.assertEqual(doc.as_dictionary(), {'_id': "75"})
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_set_array_element(self): doc = OJAIDocument().set('a.b.c', [1, 2, 3]) self.assertEqual(doc.as_dictionary(), {'a': {'b': {'c': [1, 2, 3]}}}) doc.set('a.b.c[1]', 9) self.assertEqual(doc.as_dictionary(), {'a': {'b': {'c': [1, 9, 3]}}}) doc.set('a.b.c[12]', 55) self.assertEqual( doc.as_dictionary(), { 'a': { 'b': { 'c': [ 1, 9, 3, None, None, None, None, None, None, None, None, None, 55 ] } } }) self.assertEqual(doc.get('a.b.c[12]'), 55) self.assertEqual(doc.get('a.b.c[0]'), 1) self.assertEqual(doc.get('a.b.c[1]'), 9) self.assertEqual(doc.get('a.b.c[3]'), None)
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_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 }]] ] })
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_int(self): doc = OJAIDocument().set('test_int', 123).set_id('121212') self.assertEqual(doc.as_dictionary(), { '_id': '121212', 'test_int': 123 })
def test_empty_doc(self): doc = OJAIDocument() self.assertTrue(doc.empty()) self.assertEqual(doc.as_dictionary(), {})
def test_doc_set_none(self): doc = OJAIDocument().set('test_none', None) self.assertEqual(doc.as_dictionary(), {'test_none': None})