# Path: psqlextra/query.py
# class PostgresQuerySet(models.QuerySet):
#     def __init__(self, model=None, query=None, using=None, hints=None):
#     def annotate(self, **annotations):
#     def rename_annotations(self, **annotations):
#     def on_conflict(
#         self,
#         fields: ConflictTarget,
#         action: ConflictAction,
#         index_predicate: Optional[Union[Expression, Q, str]] = None,
#         update_condition: Optional[Union[Expression, Q, str]] = None,
#     ):
#     def bulk_insert(
#         self,
#         rows: List[dict],
#         return_model: bool = False,
#         using: Optional[str] = None,
#     ):
#     def insert(self, using: Optional[str] = None, **fields):
#     def insert_and_get(self, using: Optional[str] = None, **fields):
#     def upsert(
#         self,
#         conflict_target: ConflictTarget,
#         fields: dict,
#         index_predicate: Optional[Union[Expression, Q, str]] = None,
#         using: Optional[str] = None,
#         update_condition: Optional[Union[Expression, Q, str]] = None,
#     ) -> int:
#     def upsert_and_get(
#         self,
#         conflict_target: ConflictTarget,
#         fields: dict,
#         index_predicate: Optional[Union[Expression, Q, str]] = None,
#         using: Optional[str] = None,
#         update_condition: Optional[Union[Expression, Q, str]] = None,
#     ):
#     def bulk_upsert(
#         self,
#         conflict_target: ConflictTarget,
#         rows: Iterable[Dict],
#         index_predicate: Optional[Union[Expression, Q, str]] = None,
#         return_model: bool = False,
#         using: Optional[str] = None,
#         update_condition: Optional[Union[Expression, Q, str]] = None,
#     ):
#         def is_empty(r):
#     def _create_model_instance(
#         self, field_values: dict, using: str, apply_converters: bool = True
#     ):
#     def _build_insert_compiler(
#         self, rows: Iterable[Dict], using: Optional[str] = None
#     ):
#     def _is_magical_field(self, model_instance, field, is_insert: bool):
#     def _get_upsert_fields(self, kwargs):
# 
# Path: tests/fake_model.py
# def get_fake_model(fields=None, model_base=PostgresModel, meta_options={}):
#     """Defines a fake model and creates it in the database."""
# 
#     model = define_fake_model(fields, model_base, meta_options)
# 
#     with connection.schema_editor() as schema_editor:
#         schema_editor.create_model(model)
# 
#     return model


import uuid
import pytest
from django.db import models
from psqlextra.query import ConflictAction
from ..fake_model import get_fake_model

        for row in rows:
            model.objects.on_conflict(["field"], ConflictAction.UPDATE).insert(
                **row
            )

    benchmark(_native_upsert, model, random_values)


@pytest.mark.benchmark()
def test_upsert_bulk_native(benchmark):
    model = get_fake_model(
        {"field": models.CharField(max_length=255, unique=True)}
    )

    rows = []
    random_values = []
    for i in range(0, ROW_COUNT):
        random_value = str(uuid.uuid4())
        random_values.append(random_value)
        rows.append(model(field=random_value))

    model.objects.bulk_create(rows)

    def _native_upsert(model, random_values):
        """Performs a concurrency safe upsert using the native PostgreSQL
        upsert."""

        rows = [dict(field=random_value) for random_value in random_values]


# Path: psqlextra/query.py
# class PostgresQuerySet(models.QuerySet):
#     def __init__(self, model=None, query=None, using=None, hints=None):
#     def annotate(self, **annotations):
#     def rename_annotations(self, **annotations):
#     def on_conflict(
#         self,
#         fields: ConflictTarget,
#         action: ConflictAction,
#         index_predicate: Optional[Union[Expression, Q, str]] = None,
#         update_condition: Optional[Union[Expression, Q, str]] = None,
#     ):
#     def bulk_insert(
#         self,
#         rows: List[dict],
#         return_model: bool = False,
#         using: Optional[str] = None,
#     ):
#     def insert(self, using: Optional[str] = None, **fields):
#     def insert_and_get(self, using: Optional[str] = None, **fields):
#     def upsert(
#         self,
#         conflict_target: ConflictTarget,
#         fields: dict,
#         index_predicate: Optional[Union[Expression, Q, str]] = None,
#         using: Optional[str] = None,
#         update_condition: Optional[Union[Expression, Q, str]] = None,
#     ) -> int:
#     def upsert_and_get(
#         self,
#         conflict_target: ConflictTarget,
#         fields: dict,
#         index_predicate: Optional[Union[Expression, Q, str]] = None,
#         using: Optional[str] = None,
#         update_condition: Optional[Union[Expression, Q, str]] = None,
#     ):
#     def bulk_upsert(
#         self,
#         conflict_target: ConflictTarget,
#         rows: Iterable[Dict],
#         index_predicate: Optional[Union[Expression, Q, str]] = None,
#         return_model: bool = False,
#         using: Optional[str] = None,
#         update_condition: Optional[Union[Expression, Q, str]] = None,
#     ):
#         def is_empty(r):
#     def _create_model_instance(
#         self, field_values: dict, using: str, apply_converters: bool = True
#     ):
#     def _build_insert_compiler(
#         self, rows: Iterable[Dict], using: Optional[str] = None
#     ):
#     def _is_magical_field(self, model_instance, field, is_insert: bool):
#     def _get_upsert_fields(self, kwargs):
# 
# Path: tests/fake_model.py
# def get_fake_model(fields=None, model_base=PostgresModel, meta_options={}):
#     """Defines a fake model and creates it in the database."""
# 
#     model = define_fake_model(fields, model_base, meta_options)
# 
#     with connection.schema_editor() as schema_editor:
#         schema_editor.create_model(model)
# 
#     return model


# Path: tests/benchmarks/test_upsert_bulk.py
import uuid
import pytest
from django.db import models
from psqlextra.query import ConflictAction
from ..fake_model import get_fake_model


        for row in rows:
            model.objects.on_conflict(["field"], ConflictAction.UPDATE).insert(
                **row
            )

    benchmark(_native_upsert, model, random_values)


@pytest.mark.benchmark()
def test_upsert_bulk_native(benchmark):
    model = get_fake_model(
        {"field": models.CharField(max_length=255, unique=True)}
    )

    rows = []
    random_values = []
    for i in range(0, ROW_COUNT):
        random_value = str(uuid.uuid4())
        random_values.append(random_value)
        rows.append(model(field=random_value))

    model.objects.bulk_create(rows)

    def _native_upsert(model, random_values):
        """Performs a concurrency safe upsert using the native PostgreSQL
        upsert."""

        rows = [dict(field=random_value) for random_value in random_values]


        model.objects.on_conflict(["field"], ConflictAction.UPDATE).bulk_insert(