def test_get_with_position(reader: ReaderService,
                           read_db: SqlReadDatabaseBackendService):
    fqid = "c/1"
    model = MagicMock()
    reader.filter_fqids_by_deleted_status = MagicMock(return_value=[fqid])
    read_db.build_model_ignore_deleted = bmid = MagicMock(return_value=model)
    reader.apply_mapped_fields = amf = MagicMock(return_value=model)

    request = GetRequest(fqid, ["field"], 42)

    assert reader.get(request) == model

    bmid.assert_called_with(fqid, 42)
    amf.assert_called_with(model, ["field"])
def test_apply_mapped_fields_multi(reader: ReaderService):
    result = {
        "a/1": {
            "f1": "a",
            "f2": "b",
            "f": "c"
        },
        "b/1": {
            "f3": "a",
            "f4": "b",
            "f": "c"
        },
    }
    assert reader.apply_mapped_fields_multi(result, {
        "a/1": ["f1", "f"],
        "b/1": ["f3", "f"]
    }) == {
        "a/1": {
            "f1": "a",
            "f": "c"
        },
        "b/1": {
            "f3": "a",
            "f": "c"
        }
    }
def test_get_many_with_position_deleted_no_deleted(
        reader: ReaderService, read_db: SqlReadDatabaseBackendService):
    deleted_map = {"a/1": True, "b/1": False}
    read_db.get_deleted_status = gds = MagicMock(return_value=deleted_map)
    read_db.build_models_ignore_deleted = bmid = MagicMock()
    reader.apply_mapped_fields_multi = MagicMock()

    parts = [
        GetManyRequestPart("a", "1", ["field1"]),
        GetManyRequestPart("b", "1", ["field2"]),
    ]
    request = GetManyRequest(parts, ["field"], 42)

    reader.get_many(request)

    gds.assert_called_with(["a/1", "b/1"], 42)
    bmid.assert_called_with(["b/1"], 42)
def test_get(reader: ReaderService, read_db: SqlReadDatabaseBackendService):
    model = MagicMock()
    read_db.get = get = MagicMock(return_value=model)

    request = GetRequest("c/1", ["field"])

    assert reader.get(request) == model

    get.assert_called_with("c/1", ["field"], DeletedModelsBehaviour.NO_DELETED)
def test_exists_false(reader: ReaderService):
    reader.count = count = MagicMock(return_value={"count": 0, "position": 0})

    filter_operator = FilterOperator("field", "=", "data")
    request = AggregateRequest("collection", filter_operator)

    assert reader.exists(request) == {"exists": False, "position": 0}

    count.assert_called_with(request)
def test_get_everything(reader: ReaderService,
                        read_db: SqlReadDatabaseBackendService):
    result = MagicMock()
    read_db.get_everything = get_everything = MagicMock(return_value=result)

    request = GetEverythingRequest(DeletedModelsBehaviour.ALL_MODELS)

    assert reader.get_everything(request) == result

    get_everything.assert_called_with(DeletedModelsBehaviour.ALL_MODELS)
def test_max(reader: ReaderService, read_db: SqlReadDatabaseBackendService):
    result = MagicMock()
    read_db.aggregate = aggregate = MagicMock(return_value=result)

    filter_operator = FilterOperator("field", "=", "data")
    request = MinMaxRequest("collection", filter_operator, "field")
    params = AggregateFilterQueryFieldsParameters("max", "field", "int")

    assert reader.max(request) == result

    aggregate.assert_called_with("collection", filter_operator, params)
def test_count(reader: ReaderService, read_db: SqlReadDatabaseBackendService):
    result = MagicMock()
    read_db.aggregate = aggregate = MagicMock(return_value=result)

    filter_operator = FilterOperator("field", "=", "data")
    request = AggregateRequest("collection", filter_operator)

    assert reader.count(request) == result

    aggregate.assert_called_with("collection", filter_operator,
                                 CountFilterQueryFieldsParameters())
def test_get_all(reader: ReaderService,
                 read_db: SqlReadDatabaseBackendService):
    result = MagicMock()
    read_db.get_all = get_all = MagicMock(return_value=result)

    request = GetAllRequest("collection", ["field"])

    assert reader.get_all(request) == result

    get_all.assert_called_with("collection", ["field"],
                               DeletedModelsBehaviour.NO_DELETED)
def test_filter(reader: ReaderService, read_db: SqlReadDatabaseBackendService):
    result = MagicMock()
    read_db.filter = filter = MagicMock(return_value=result)
    read_db.get_position = get_pos = MagicMock(return_value=42)

    filter_operator = FilterOperator("field", "=", "data")
    request = FilterRequest("collection", filter_operator, ["field"])

    assert reader.filter(request) == {"data": result, "position": 42}

    filter.assert_called_with("collection", filter_operator, ["field"])
    get_pos.assert_called()
def test_get_many(reader: ReaderService,
                  read_db: SqlReadDatabaseBackendService):
    model = MagicMock()
    result = {"c/1": model}
    read_db.get_many = get_many = MagicMock(return_value=result)

    parts = [
        GetManyRequestPart("a", "1", ["field1"]),
        GetManyRequestPart("b", "1", ["field2"]),
    ]
    request = GetManyRequest(parts, ["field"])

    assert reader.get_many(request) == {"a": {}, "b": {}, "c": {1: model}}

    get_many.assert_called_with(
        ["a/1", "b/1"],
        {
            "a/1": ["field1", "field"],
            "b/1": ["field2", "field"]
        },
        DeletedModelsBehaviour.NO_DELETED,
    )
def test_get_many_with_position(reader: ReaderService,
                                read_db: SqlReadDatabaseBackendService):
    model = MagicMock()
    result = {"c/1": model}
    deleted_map = {"a/1": False, "b/1": False}
    read_db.get_deleted_status = gds = MagicMock(return_value=deleted_map)
    read_db.build_models_ignore_deleted = bmid = MagicMock(return_value=result)
    reader.apply_mapped_fields_multi = amfm = MagicMock(return_value=result)

    parts = [
        GetManyRequestPart("a", "1", ["field1"]),
        GetManyRequestPart("b", "1", ["field2"]),
    ]
    request = GetManyRequest(parts, ["field"], 42)

    assert reader.get_many(request) == {"a": {}, "b": {}, "c": {1: model}}

    gds.assert_called_with(["a/1", "b/1"], 42)
    bmid.assert_called_with(["a/1", "b/1"], 42)
    amfm.assert_called_with(result, {
        "a/1": ["field1", "field"],
        "b/1": ["field2", "field"]
    })
def test_apply_mapped_fields_multi_no_fields(reader: ReaderService):
    result = MagicMock()
    assert reader.apply_mapped_fields_multi(result, {}) == result
def test_apply_mapped_fields_no_fields(reader: ReaderService):
    model = MagicMock()
    assert reader.apply_mapped_fields(model, []) == model
def test_apply_mapped_fields(reader: ReaderService):
    model = {"f1": "a", "f2": "b"}
    assert reader.apply_mapped_fields(model, ["f1"]) == {"f1": "a"}
def test_filter_fqids_by_deleted_status(reader: ReaderService):
    fqids = MagicMock()
    res = reader.filter_fqids_by_deleted_status(
        fqids, 42, DeletedModelsBehaviour.ALL_MODELS)
    assert res == fqids