cql_code test_all_column_fetchers()

in sources/run_test_client.c [1289:1477]


cql_code test_all_column_fetchers(sqlite3 *db) {
  printf("Running column fetchers test\n");
  tests++;

  // test object
  cql_object_ref set = set_create();
  emit_object_result_set_result_set_ref object_result_set;
  emit_object_result_set_fetch_results(&object_result_set, set);
  E(emit_object_result_set_result_count(object_result_set) == 2, "expected 2 rows from the object union\n");
  for (int32_t row = 0; row <= 1; row++) {
    for (int32_t col = 0; col < 1; col++) {
      cql_bool is_null = cql_result_set_get_is_null_col((cql_result_set_ref)object_result_set, row, col);
      cql_bool is_null_expected = (row == 1) && col == 0;
      E(is_null == is_null_expected, "expected is_null did not match seed data, row %d, col %d\n", row, col);
    }
  }

  cql_object_ref _Nullable object = cql_result_set_get_object_col((cql_result_set_ref)object_result_set, 1, 0);
  E(!object, "expected object to be null\n");
  cql_object_ref new_set = set_create();
  cql_result_set_set_object_col((cql_result_set_ref)object_result_set, 1, 0, new_set);
  object = cql_result_set_get_object_col((cql_result_set_ref)object_result_set, 1, 0);
  E(object, "expected not null object\n");
  cql_result_set_set_object_col((cql_result_set_ref)object_result_set, 1, 0, NULL);

  cql_object_release(set);
  cql_object_release(new_set);
  cql_result_set_release(object_result_set);

  load_all_types_table_result_set_ref result_set;
  SQL_E(load_all_types_table_fetch_results(db, &result_set));
  E(load_all_types_table_result_count(result_set) == 2, "expected 2 rows from result table\n");
  E(cql_result_set_get_meta(result_set)->columnCount == 12, "expected 12 columns from result table\n");
  cql_result_set_ref rs = (cql_result_set_ref)result_set;

  for (int32_t row = 0; row <= 1; row++) {
    for (int32_t col = 0; col < 12; col++) {
      cql_bool is_null = cql_result_set_get_is_null_col(rs, row, col);
      cql_bool is_null_expected = (row == 0) && col < 6;
      E(is_null == is_null_expected, "expected is_null did not match seed data, row %d, col %d\n", row, col);
    }
  }
  for (int32_t row = 0; row <= 1; row++) {
    for (int32_t col = 0; col < 12; col++) {
      cql_bool is_null_expected = (row == 0) && col < 6;
      if (is_null_expected) {
        continue;
      }
      switch (col % 6) {
        case 0:  {
          // bool
          E(cql_result_set_get_bool_col(rs, row, col) == row,
            "expected bool did not match seed data, row %d, col %d\n", row, col);
          cql_nullable_bool new_value;
          cql_set_notnull(new_value, !row);
          cql_result_set_set_bool_col(rs, row, col, new_value);
          E(cql_result_set_get_bool_col(rs, row, col) == !row,
            "expected bool did not match seed data, row %d, col %d\n", !row, col);
          cql_result_set_set_bool_col_not_null(rs, row, col, row);
          E(cql_result_set_get_bool_col(rs, row, col) == row,
            "expected bool did not match seed data, row %d, col %d\n", row, col);
          break;
        }
        case 1: {
          // int32
          E(cql_result_set_get_int32_col(rs, row, col) == row,
            "expected int32 did not match seed data, row %d, col %d\n", row, col);
          cql_nullable_int32 new_value;
          cql_set_notnull(new_value, row + 19);
          cql_result_set_set_int32_col(rs, row, col, new_value);
          E(cql_result_set_get_int32_col(rs, row, col) == row + 19,
            "expected int32 did not match seed data, row %d, col %d\n", row + 19, col);
          cql_result_set_set_int32_col_not_null(rs, row, col, row + 20);
          E(cql_result_set_get_int32_col(rs, row, col) == row + 20,
            "expected int32 did not match seed data, row %d, col %d\n", row + 20, col);
          break;
        }
        case 2: {
          // int64
          E(cql_result_set_get_int64_col(rs, row, col) == row,
            "expected int64 did not match seed data, row %d, col %d\n", row, col);
          cql_nullable_int64 new_value;
          cql_set_notnull(new_value, row + 29);
          cql_result_set_set_int64_col(rs, row, col, new_value);
          E(cql_result_set_get_int64_col(rs, row, col) == row + 29,
            "expected int64 did not match seed data, row %d, col %d\n", row + 29, col);
          cql_result_set_set_int64_col_not_null(rs, row, col, row + 30);
          E(cql_result_set_get_int64_col(rs, row, col) == row + 30,
            "expected int64 did not match seed data, row %d, col %d\n", row + 30, col);
          break;
        }
        case 3: {
          // double
          E(cql_result_set_get_double_col(rs, row, col) == row,
            "expected double did not match seed data, row %d, col %d\n", row, col);
          cql_nullable_double new_value;
          cql_set_notnull(new_value, row + 39);
          cql_result_set_set_double_col(rs, row, col, new_value);
          E(cql_result_set_get_double_col(rs, row, col) == row + 39,
            "expected double did not match seed data, row %d, col %d\n", row + 39, col);
          cql_result_set_set_double_col_not_null(rs, row, col, row + 40);
          E(cql_result_set_get_double_col(rs, row, col) == row + 40,
            "expected double did not match seed data, row %d, col %d\n", row + 40, col);
          break;
        }
        case 4: {
          // string
          // expected results:
          // s1_0  (row 0)  (s0 will be null)
          // s0_1  (row 1)
          // s1_1  (row 1)
          cql_string_ref str = cql_result_set_get_string_col(rs, row, col);
          const char *expected = row == 0 ? "s1_0" : col < 6 ? "s0_1" : "s1_1";
          E(strcmp(str->ptr, expected) == 0, "expected string did not match seed data, row %d, col %d\n", row, col);

          cql_string_ref updated = string_create();
          cql_result_set_set_string_col(rs, row, col, updated);
          str = cql_result_set_get_string_col(rs, row, col);
          E(strcmp(str->ptr, updated->ptr) == 0, "expected string did not match seed data, row %d, col %d\n", row, col);
          cql_string_release(updated);

          cql_result_set_set_string_col(rs, row, col, NULL);
          cql_string_ref _Nullable nullable_str = cql_result_set_get_string_col(rs, row, col);
          E(nullable_str == NULL, "expected string to be nil");
          break;
        }
        case 5: {
          // blob
          // expected results (size, data)
          // 5, bl1_0
          // 5, bl0_1
          // 5, bl1_1
          cql_blob_ref bl = cql_result_set_get_blob_col(rs, row, col);
          const char *expected = row == 0 ? "bl1_0" : col < 6 ? "bl0_1" : "bl1_1";
          E(bl->size == 5 && memcmp(bl->ptr, expected, 5) == 0,
            "expected blob did not match seed data, row %d, col %d\n", row, col);

          cql_string_ref str_blob = string_create();
          cql_blob_ref updated_bl = blob_from_string(str_blob);
          cql_result_set_set_blob_col(rs, row, col, updated_bl);
          bl = cql_result_set_get_blob_col(rs, row, col);
          E(bl->size == 13 && memcmp(bl->ptr, "Hello, world.", 13) == 0,
            "expected blob did not match seed data, row %d, col %d\n", row, col);
          cql_string_release(str_blob);
          cql_blob_release(updated_bl);
          break;
        }
      }
    }
  }

  // check nullability setters
  cql_int32 row = 0;
  // bool
  E(load_all_types_table_get_b0_is_null(result_set, row) == true, "b0 expected to be null at 0\n");
  load_all_types_table_set_b0_value(result_set, row, true);
  E(load_all_types_table_get_b0_is_null(result_set, row) == false, "b0 expected not to be null at 0\n");
  load_all_types_table_set_b0_to_null(result_set, row);
  E(load_all_types_table_get_b0_is_null(result_set, row) == true, "b0 expected to be null after _to_null\n");

  // int32
  cql_int32 new_int32 = 10;
  E(load_all_types_table_get_i0_is_null(result_set, row) == true, "i0 expected to be null at 0\n");
  load_all_types_table_set_i0_value(result_set, row, new_int32);
  E(load_all_types_table_get_i0_is_null(result_set, row) == false, "i0 expected not to be null at 0\n");
  load_all_types_table_set_i0_to_null(result_set, row);
  E(load_all_types_table_get_i0_is_null(result_set, row) == true, "i0 expected to be null after _to_null\n");

  // int64
  cql_int64 new_int64 = 99;
  E(load_all_types_table_get_l0_is_null(result_set, row) == true, "l0 expected to be null at 0\n");
  load_all_types_table_set_l0_value(result_set, row, new_int64);
  E(load_all_types_table_get_l0_is_null(result_set, row) == false, "l0 expected not to be null at 0\n");
  load_all_types_table_set_l0_to_null(result_set, row);
  E(load_all_types_table_get_l0_is_null(result_set, row) == true, "l0 expected to be null after _to_null\n");

  // double
  cql_double new_double = 200;
  E(load_all_types_table_get_d0_is_null(result_set, row) == true, "d0 expected to be null at 0\n");
  load_all_types_table_set_d0_value(result_set, row, new_double);
  E(load_all_types_table_get_d0_is_null(result_set, row) == false, "d0 expected not to be null at 0\n");
  load_all_types_table_set_d0_to_null(result_set, row);
  E(load_all_types_table_get_d0_is_null(result_set, row) == true, "d0 expected to be null after _to_null\n");

  cql_result_set_release(result_set);

  tests_passed++;
  return SQLITE_OK;
}