def test_upsert_with_mapping(self): stmt = upsert_entity( indexes=["id"], keys_mapping={ "id": "id", "updated_at": "updated_at" }, new_data={ "id": "1", "updated_at": func.now() }, table=self.some_table, ) assert ( format_statement(stmt) == "INSERT INTO upsert_model (id, updated_at) VALUES (1, now()) ON CONFLICT (id) DO UPDATE SET updated_at = excluded.updated_at WHERE excluded.updated_at > upsert_model.updated_at" )
def test_does_nothing_if_do_nothing_action_was_passed(self): stmt = upsert_entity( indexes=["id"], keys_mapping={ "id": "id", "updated_at": "updated_at" }, new_data={ "id": "1", "updated_at": func.now() }, table=self.some_table, action=ConflictAction.DO_NOTHING, ) assert ( format_statement(stmt) == "INSERT INTO upsert_model (id, updated_at) VALUES (1, now()) ON CONFLICT (id) DO NOTHING" )
def test_adds_returning_clause_if_returning_specified(self): stmt = upsert_entity( indexes=["id"], keys_mapping={ "id": "id", "updated_at": "updated_at" }, new_data={ "id": "1", "updated_at": func.now() }, table=self.some_table, returning=[self.some_table.c.id], ) assert ( format_statement(stmt) == "INSERT INTO upsert_model (id, updated_at) VALUES (1, now()) ON CONFLICT (id) DO UPDATE SET updated_at = excluded.updated_at WHERE excluded.updated_at > upsert_model.updated_at RETURNING upsert_model.id" )