def test_get_any_array_fields(self): mapping = { 'db': { 'col': { 'field1': {'type': '_ARRAY'}, 'field2': {'type': '_ARRAY_OF_SCALARS'} } } } got = utils.get_any_array_fields(mapping, 'db', 'col', {}) self.assertEqual(got, []) doc = {'field1': [{}]} got = utils.get_any_array_fields(mapping, 'db', 'col', doc) self.assertEqual(got, ['field1']) doc = {'field2': [1, 2, 3]} got = utils.get_any_array_fields(mapping, 'db', 'col', doc) self.assertEqual(got, ['field2']) doc = { 'field1': [{}], 'field2': [1, 2, 3] } got = utils.get_any_array_fields(mapping, 'db', 'col', doc) self.assertIn('field1', got) self.assertIn('field2', got)
def test_get_any_array_fields(self): mapping = { 'db': { 'col': { 'field1': {'type': '_ARRAY'}, 'field2': {'type': '_ARRAY_OF_SCALARS'} } } } got = utils.get_any_array_fields(mapping, 'db', 'col', {}) self.assertEqual(got, []) doc = {'field1': [{}]} got = utils.get_any_array_fields(mapping, 'db', 'col', doc) self.assertEqual(got, ['field1']) doc = {'field2': [1, 2, 3]} got = utils.get_any_array_fields(mapping, 'db', 'col', doc) self.assertEqual(got, ['field2']) doc = { 'field1': [{}], 'field2': [1, 2, 3] } got = utils.get_any_array_fields(mapping, 'db', 'col', doc) self.assertIn('field1', got) self.assertIn('field2', got)
def update(self, document_id, update_spec, namespace, timestamp): db, collection = db_and_collection(namespace) updated_document = self.get_document_by_id(db, collection, document_id) primary_key = self.mappings[db][collection]['pk'] mapped_field = self.mappings[db][collection].get(primary_key, {}) field_type = mapped_field.get('type') doc_id = to_sql_value(document_id, vtype=field_type) if updated_document is None: return for arrayField in get_any_array_fields(self.mappings, db, collection, updated_document): dest = self.mappings[db][collection][arrayField]['dest'] fk = self.mappings[db][collection][arrayField]['fk'] sql_delete_rows_where( self.pgsql.cursor(), dest, "{0} = {1}".format(fk, doc_id) ) self._upsert(namespace, updated_document, self.pgsql.cursor(), timestamp) self.commit()
def update(self, document_id, update_spec, namespace, timestamp): db, collection = db_and_collection(namespace) updated_document = self.get_document_by_id(db, collection, document_id) if updated_document is None: return for arrayField in get_any_array_fields(self.mappings, db, collection, updated_document): dest = self.mappings[db][collection][arrayField]['dest'] fk = self.mappings[db][collection][arrayField]['fk'] sql_delete_rows_where(self.pgsql.cursor(), dest, "{0} = {1}".format(fk, to_sql_value(document_id))) self._upsert(namespace, updated_document, self.pgsql.cursor(), timestamp) self.commit()
def update(self, document_id, update_spec, namespace, timestamp): # TODO update this to grab doc, apply_update, and then update (return None if doc not in Postgres) db, collection = db_and_collection(namespace) updated_document = self.get_document_by_id(db, collection, document_id) if updated_document is None: return for arrayField in get_any_array_fields(self.mappings, db, collection, updated_document): dest = self.mappings[db][collection][arrayField]['dest'] fk = self.mappings[db][collection][arrayField]['fk'] sql_delete_rows_where(self.pgsql.cursor(), dest, "{0} = {1}".format(fk, to_sql_value(document_id))) self._upsert(namespace, updated_document, self.pgsql.cursor(), timestamp) self.commit()