Example #1
0
def test_lock_fqfield_template_empty_placeholder(json_client, data):
    create_and_update_model(json_client, "a/1", {"f__s": 1}, {"f__s": 2})
    data["locked_fields"]["a/1/f_$_s"] = 1

    response = json_client.post(WRITE_URL, data)
    assert_response_code(response, 201)
    assert_model("a/2", {}, 3)
Example #2
0
def create_model(json_client, data, redis_connection, reset_redis_data):
    fields = copy.deepcopy(data["events"][0]["fields"])
    response = json_client.post(WRITE_URL, data)
    assert_response_code(response, 201)
    assert_model("a/1", fields, 1)
    assert_modified_fields(redis_connection, {"a/1": list(fields.keys())})
    reset_redis_data()
Example #3
0
def test_update_delete_restore_update(json_client, data, redis_connection,
                                      reset_redis_data):
    create_model(json_client, data, redis_connection, reset_redis_data)

    data["events"][0] = {
        "type": "update",
        "fqid": "a/1",
        "fields": {
            "another": "value",
            "f": None
        },
    }
    data["events"].append({"type": "delete", "fqid": "a/1"})
    data["events"].append({"type": "restore", "fqid": "a/1"})
    data["events"].append({
        "type": "update",
        "fqid": "a/1",
        "fields": {
            "third_field": ["my", "list"]
        }
    })
    response = json_client.post(WRITE_URL, data)
    assert_response_code(response, 201)
    assert_model("a/1", {"another": "value", "third_field": ["my", "list"]}, 2)
    assert_modified_fields(redis_connection,
                           {"a/1": ["f", "another", "third_field"]})
def test_lock_collectionfield_with_and_filter(json_client, data):
    create_and_update_model(json_client, "a/1", {
        "f1": 1,
        "f3": False
    }, {"f2": 2})
    data["locked_fields"]["a/f1"] = {
        "position": 1,
        "filter": {
            "and_filter": [
                {
                    "field": "f1",
                    "operator": "=",
                    "value": 1,
                },
                {
                    "field": "f3",
                    "operator": "=",
                    "value": False,
                },
            ],
        },
    }
    response = json_client.post(WRITE_URL, data)
    assert_response_code(response, 201)
    assert_model("a/2", {}, 3)
Example #5
0
def test_create_delete_restore(json_client, data, redis_connection):
    data["events"].append({"type": "delete", "fqid": "a/1"})
    data["events"].append({"type": "restore", "fqid": "a/1"})
    response = json_client.post(WRITE_URL, data)
    assert_response_code(response, 201)
    assert_model("a/1", {"f": 1}, 1)
    assert_modified_fields(redis_connection, {"a/1": ["f"]})
Example #6
0
def test_lock_fqfield_ok_2(json_client, data):
    create_and_update_model(json_client, "a/1", {"f1": 1}, {"f2": 2})
    data["locked_fields"]["a/1/f2"] = 2

    response = json_client.post(WRITE_URL, data)
    assert_response_code(response, 201)
    assert_model("a/2", {}, 3)
def test_lock_fqfield_template_other_field_with_suffix_locked_3(
        json_client, data):
    create_and_update_model(json_client, "a/1", {"f_$11_s": 1}, {"f_$11_s": 2})
    data["locked_fields"]["a/1/f_$1_s"] = 1

    response = json_client.post(WRITE_URL, data)
    assert_response_code(response, 201)
    assert_model("a/2", {}, 3)
Example #8
0
def test_write_none(json_client, data, redis_connection, reset_redis_data):
    create_model(json_client, data, redis_connection, reset_redis_data)

    data["events"][0] = {"type": "update", "fqid": "a/1", "fields": {"f": None}}
    response = json_client.post(WRITE_URL, data)
    assert_response_code(response, 201)
    assert_model("a/1", {}, 2)
    assert_modified_fields(redis_connection, {"a/1": ["f"]}, meta_deleted=False)
Example #9
0
def test_two_write_requests(json_client, data, redis_connection, reset_redis_data):
    create_model(json_client, data, redis_connection, reset_redis_data)

    data["events"][0] = {"type": "update", "fqid": "a/1", "fields": {"f": None}}
    data2 = copy.deepcopy(data)
    data2["events"][0] = {"type": "update", "fqid": "a/1", "fields": {"f2": 1}}
    response = json_client.post(WRITE_URL, [data, data2])
    assert_response_code(response, 201)
    assert_model("a/1", {"f2": 1}, 3)
    assert_modified_fields(redis_connection, {"a/1": ["f", "f2"]}, meta_deleted=False)
Example #10
0
def test_create_double_assert_increased_id_sequence(
    json_client, data, redis_connection, reset_redis_data, db_cur
):
    create_model(json_client, data, redis_connection, reset_redis_data)
    data["events"][0]["fqid"] = "a/3"
    response = json_client.post(WRITE_URL, data)
    assert_response_code(response, 201)
    db_cur.execute("select id from id_sequences where collection = %s", ["a"])
    id = db_cur.fetchone()[0]
    assert id == 4
Example #11
0
def test_list_update_add_string(json_client, data, redis_connection, reset_redis_data):
    create_model(json_client, data, redis_connection, reset_redis_data)

    data["events"][0] = {
        "type": "update",
        "fqid": "a/1",
        "list_fields": {"add": {"field": ["str"]}},
    }
    response = json_client.post(WRITE_URL, data)
    assert_response_code(response, 201)
    assert_model("a/1", {"f": 1, "field": ["str"]}, 2)
    assert_modified_fields(redis_connection, {"a/1": ["field"]}, meta_deleted=False)
Example #12
0
def test_list_update_with_create(json_client, data, redis_connection, reset_redis_data):
    data["events"].append(
        {
            "type": "update",
            "fqid": "a/1",
            "list_fields": {"add": {"g": [2]}},
        }
    )
    response = json_client.post(WRITE_URL, data)
    assert_response_code(response, 201)
    assert_model("a/1", {"f": 1, "g": [2]}, 1)
    assert_modified_fields(redis_connection, {"a/1": ["f", "g"]})
Example #13
0
def test_restore_without_delete(json_client, data, redis_connection, reset_redis_data):
    response = json_client.post(WRITE_URL, data)
    assert_response_code(response, 201)
    assert_model("a/1", {"f": 1}, 1)
    assert_modified_fields(redis_connection, {"a/1": ["f"]})
    reset_redis_data()

    data["events"] = [{"type": "restore", "fqid": "a/1"}]
    response = json_client.post(WRITE_URL, data)
    assert_error_response(response, ERROR_CODES.MODEL_NOT_DELETED)
    assert_model("a/1", {"f": 1}, 1)
    assert_no_modified_fields(redis_connection)
Example #14
0
def test_single_restore(json_client, data, redis_connection, reset_redis_data):
    data["events"].append({"type": "delete", "fqid": "a/1"})
    response = json_client.post(WRITE_URL, data)
    assert_response_code(response, 201)
    assert_no_model("a/1")
    assert_modified_fields(redis_connection, {"a/1": ["f"]})
    reset_redis_data()

    data["events"] = [{"type": "restore", "fqid": "a/1"}]
    response = json_client.post(WRITE_URL, data)
    assert_response_code(response, 201)
    assert_model("a/1", {"f": 1}, 2)
    assert_modified_fields(redis_connection, {"a/1": ["f"]})
Example #15
0
def test_create_update(json_client, data, redis_connection):
    field_data = [True, None, {"test": "value"}]
    data["events"].append(
        {
            "type": "update",
            "fqid": "a/1",
            "fields": {"f": None, "another_field": field_data},
        }
    )
    response = json_client.post(WRITE_URL, data)
    assert_response_code(response, 201)
    assert_model("a/1", {"another_field": field_data}, 1)
    assert_modified_fields(redis_connection, {"a/1": ["f", "another_field"]})
Example #16
0
def test_list_update_add_remove(json_client, data, redis_connection, reset_redis_data):
    data["events"][0]["fields"]["f"] = [1]
    data["events"][0]["fields"]["f2"] = ["test"]
    create_model(json_client, data, redis_connection, reset_redis_data)

    data["events"][0] = {
        "type": "update",
        "fqid": "a/1",
        "list_fields": {"add": {"f": [2]}, "remove": {"f2": ["test"]}},
    }
    response = json_client.post(WRITE_URL, data)
    assert_response_code(response, 201)
    assert_model("a/1", {"f": [1, 2], "f2": []}, 2)
    assert_modified_fields(redis_connection, {"a/1": ["f", "f2"]}, meta_deleted=False)
Example #17
0
def test_single_update(json_client, data, redis_connection, reset_redis_data):
    create_model(json_client, data, redis_connection, reset_redis_data)

    field_data = [True, None, {"test": "value"}]
    data["events"][0] = {
        "type": "update",
        "fqid": "a/1",
        "fields": {"f": None, "another_field": field_data},
    }
    response = json_client.post(WRITE_URL, data)
    assert_response_code(response, 201)
    assert_model("a/1", {"another_field": field_data}, 2)
    assert_modified_fields(
        redis_connection, {"a/1": ["f", "another_field"]}, meta_deleted=False
    )
def _set_model(json_client, fqid, payload, mode):
    response = json_client.post(
        WRITE_URL,
        {
            "user_id": 1,
            "information": {},
            "locked_fields": {},
            "events": [{
                "type": mode,
                "fqid": fqid,
                "fields": payload
            }],
        },
    )
    assert_response_code(response, 201)
Example #19
0
def test_read_db_is_updated_before_redis_fires(json_client, data):
    messaging = injector.get(Messaging)
    connection_handler = injector.get(ConnectionHandler)

    def assert_read_db_data(*args, **kwargs):
        connection = psycopg2.connect(**connection_handler.get_connection_params())
        with connection.cursor() as cursor:
            cursor.execute("select * from models where fqid = 'a/1'")
            result = cursor.fetchone()

            # assert the model exists
            assert result

    with patch.object(messaging, "handle_events", new=assert_read_db_data):
        response = json_client.post(WRITE_URL, data)
        assert_response_code(response, 201)
def test_lock_collectionfield_prefix_not_locked(json_client, data):
    create_and_update_model(json_client, "a_suf/1", {"f1": 1}, {
        "f1": 2,
        "f2": 2
    })
    data["locked_fields"]["a/f1"] = {
        "position": 1,
        "filter": {
            "field": "f1",
            "operator": "=",
            "value": 2,
        },
    }

    response = json_client.post(WRITE_URL, data)
    assert_response_code(response, 201)
    assert_model("a/2", {}, 3)
Example #21
0
def test_create_delete_restore_different_positions(json_client, data,
                                                   redis_connection,
                                                   reset_redis_data):
    create_model(json_client, data, redis_connection, reset_redis_data)

    data["events"][0] = {"type": "delete", "fqid": "a/1"}
    response = json_client.post(WRITE_URL, data)
    assert_response_code(response, 201)
    assert_no_model("a/1")
    assert_modified_fields(redis_connection, {"a/1": ["f"]})
    reset_redis_data()

    data["events"][0] = {"type": "restore", "fqid": "a/1"}

    response = json_client.post(WRITE_URL, data)
    assert_response_code(response, 201)
    assert_model("a/1", {"f": 1}, 3)
    assert_modified_fields(redis_connection, {"a/1": ["f"]})
Example #22
0
def test_single_delete(json_client, data, redis_connection, reset_redis_data):
    create_model(json_client, data, redis_connection, reset_redis_data)

    data["events"][0] = {"type": "delete", "fqid": "a/1"}
    response = json_client.post(WRITE_URL, data)
    assert_response_code(response, 201)
    assert_no_model("a/1")

    # assert the model is still in the lookup table, but marked as deleted
    connection_handler = injector.get(ConnectionHandler)
    with connection_handler.get_connection_context():
        # read from read db
        read_db: ReadDatabase = injector.get(ReadDatabase)
        model = read_db.get("a/1", [], DeletedModelsBehaviour.ONLY_DELETED)
        assert model == {"f": 1, "meta_deleted": True, "meta_position": 2}
        assert read_db.is_deleted("a/1")

    assert_modified_fields(redis_connection, {"a/1": ["f"]})
Example #23
0
def create_and_update_model(json_client, fqid, create_payload, update_payload):
    response = json_client.post(
        WRITE_URL,
        {
            "user_id": 1,
            "information": {},
            "locked_fields": {},
            "events": [{"type": "create", "fqid": fqid, "fields": create_payload}],
        },
    )
    assert_response_code(response, 201)
    response = json_client.post(
        WRITE_URL,
        {
            "user_id": 1,
            "information": {},
            "locked_fields": {},
            "events": [{"type": "update", "fqid": fqid, "fields": update_payload}],
        },
    )
    assert_response_code(response, 201)
Example #24
0
def test_truncate_db(db_connection, db_cur, json_client):
    db_cur.execute("insert into positions (user_id) values ('1')")
    db_cur.execute(
        "insert into events (position, fqid, type) values (1, 'a/1', 'create')"
    )
    db_cur.execute("insert into models_lookup values ('a/1', TRUE)")
    db_cur.execute("insert into id_sequences values ('c', 1)")
    db_cur.execute(
        "insert into collectionfields (collectionfield, position) values ('c/f', 1)"
    )
    db_cur.execute("insert into events_to_collectionfields values (1, 1)")
    db_cur.execute("insert into models values ('c/1', '{}')")
    db_connection.commit()

    response = json_client.post(TRUNCATE_DB_URL, {})
    assert_response_code(response, 204)

    with db_connection.cursor() as cursor:
        for table in ALL_TABLES:
            cursor.execute(f"select * from {table}")
            assert cursor.fetchone() is None
Example #25
0
def test_create_empty_field(json_client, data, redis_connection):
    data["events"][0]["fields"]["empty"] = None
    response = json_client.post(WRITE_URL, data)
    assert_response_code(response, 201)
    assert_model("a/1", {"f": 1}, 1)
    assert_modified_fields(redis_connection, {"a/1": ["f"]})
Example #26
0
def create_model(json_client, data, redis_connection, reset_redis_data):
    response = json_client.post(WRITE_URL, data)
    assert_response_code(response, 201)
    assert_model("a/1", {"f": 1}, 1)
    assert_modified_fields(redis_connection, {"a/1": ["f"]})
    reset_redis_data()
Example #27
0
def test_lock_not_existing_fqfield(json_client, data):
    data["locked_fields"]["b/2/f1"] = 1

    response = json_client.post(WRITE_URL, data)
    assert_response_code(response, 201)
    assert_model("a/2", {}, 1)
def test_not_found_in_non_dev(json_client):
    injector.get(EnvironmentService).set(DATASTORE_DEV_MODE_ENVIRONMENT_VAR,
                                         "0")
    response = json_client.post(Route.GET_EVERYTHING.URL, {})
    assert_response_code(response, 404)
Example #29
0
def test_not_found(json_client):
    del os.environ[DATASTORE_DEV_MODE_ENVIRONMENT_VAR]
    injector.get(EnvironmentService).cache = {}
    response = json_client.post(TRUNCATE_DB_URL, {})
    assert_response_code(response, 404)