提交 9a90da8a 编写于 作者: A Adam Berlin 提交者: Adam Berlin

Extract shared library for making assertions about rows.

上级 cb1ebf73
......@@ -51,8 +51,10 @@ test_dependencies = bdd-library/upgrade-bdd.o \
$(scenario_objs) \
$(CMOCKERY_OBJS)
debugging_flags = -Og -g
greenplum_five_to_greenplum_six_upgrade_test_suite.t: greenplum_five_to_greenplum_six_upgrade_test_suite.o $(test_dependencies)
$(CC) $(CFLAGS) $^ $(libpq_pgport) $(LDFLAGS) -o $@
$(CC) $(CFLAGS) $(debugging_flags) $^ $(libpq_pgport) $(LDFLAGS) -o $@
clean:
rm -f $(OBJS) $(EXS)
......@@ -13,12 +13,16 @@
#include "utilities/gpdb6-cluster.h"
#include "utilities/test-helpers.h"
#include "utilities/row-assertions.h"
static void
setup(void **state)
{
resetGpdbFiveDataDirectories();
resetGpdbSixDataDirectories();
matcher = NULL;
match_failed = NULL;
}
static void
......
......@@ -19,70 +19,56 @@
#include "heap_table.h"
#include "utilities/row-assertions.h"
typedef struct UserData
{
int id;
char *name;
} User;
} User;
static bool
users_match(const User * expected_user, const User * actual_user)
users_match(void * expected, void * actual)
{
return
expected_user->id == actual_user->id &&
strncmp(expected_user->name, actual_user->name, strlen(expected_user->name)) == 0
;
}
User *expected_user = (User *) expected;
User *actual_user = (User *) actual;
typedef struct Rows
{
int size;
User rows[10];
} Rows;
return expected_user->id == actual_user->id &&
strncmp(expected_user->name, actual_user->name, strlen(expected_user->name)) == 0;
}
static void
assert_rows_contain_users(const Rows *expected_rows, const Rows *rows)
{
bool found = false;
user_match_failed(void *expected) {
User *expected_user = (User *) expected;
for (int j = 0; j < expected_rows->size; ++j)
{
found = false;
const User *expected_user = &expected_rows->rows[j];
for (int i = 0; i < rows->size; ++i)
{
const User *current_user = &rows->rows[i];
if (users_match(expected_user, current_user))
{
found = true;
break;
}
}
assert_true(found);
}
assert_true(found);
printf("==========> expected {.id=%d, .name=%s}\n",
expected_user->id,
expected_user->name);
}
static void
extract_user_rows(PGresult *result, Rows *rows)
static Rows *
extract_user_rows(PGresult *result)
{
int number_of_rows = PQntuples(result);
int number_of_rows = PQntuples(result);
Rows *rows = calloc(1, sizeof(Rows));
const int i_id = PQfnumber(result, "id");
const int i_name = PQfnumber(result, "name");
for (int i = 0; i < number_of_rows; i++)
{
User *user = &rows->rows[i];
User *user = calloc(1, sizeof(User));
user->id = atoi(PQgetvalue(result, i, i_id));
user->name = PQgetvalue(result, i, i_name);
user->name = strdup(PQgetvalue(result, i, i_name));
rows->rows[i] = user;
}
rows->size = number_of_rows;
return rows;
}
static void
......@@ -173,22 +159,20 @@ heapTableShouldHaveDataUpgradedToSixCluster()
executeQuery(connection, "set search_path to five_to_six_upgrade;");
PGresult *result = executeQuery(connection, "select * from users;");
Rows rows = {};
extract_user_rows(result, &rows);
Rows *rows = extract_user_rows(result);
assert_int_equal(3, rows.size);
assert_int_equal(3, rows->size);
const Rows expected_users = {
User jane = {.id = 1,.name = "Jane"};
User john = {.id = 2,.name = "John"};
User joe = {.id = 3,.name = "Joe"};
const Rows expected_users = {
.size = 3,
.rows = {
{.id = 1,.name = "Jane"},
{.id = 2,.name = "John"},
{.id = 3,.name = "Joe"}
}
.rows = {&jane, &john, &joe}
};
assert_rows_contain_users(&expected_users, &rows);
assert_rows(rows, expected_users);
PQfinish(connection);
}
......@@ -241,6 +225,9 @@ createHeapTableWithDataInFiveCluster(void)
void
test_a_heap_table_with_data_can_be_upgraded(void **state)
{
matcher = users_match;
match_failed = user_match_failed;
given(createHeapTableWithDataInFiveCluster);
when(anAdministratorPerformsAnUpgrade);
then(heapTableShouldHaveDataUpgradedToSixCluster);
......
......@@ -13,20 +13,17 @@
#include "utilities/upgrade-helpers.h"
#include "utilities/test-helpers.h"
#include "utilities/query-helpers.h"
#include "utilities/row-assertions.h"
#include "subpartitioned_heap_table.h"
typedef struct UserData
{
int id;
int age;
} User;
typedef struct RowsData
{
int size;
User *users[10];
} Rows;
static Rows *
extract_rows(PGresult *result)
......@@ -44,7 +41,7 @@ extract_rows(PGresult *result)
user->id = atoi(PQgetvalue(result, i, id_column_index));
user->age = atoi(PQgetvalue(result, i, age_column_index));
rows->users[i] = user;
rows->rows[i] = user;
}
rows->size = number_of_rows;
......@@ -66,45 +63,25 @@ queryForRows(char *queryString)
}
static bool
users_match(User *first_user, User *second_user)
users_match(void *expected, void *actual)
{
User *first_user = (User *) expected;
User *second_user = (User *) actual;
return first_user->age == second_user->age &&
first_user->id == second_user->id;
}
static bool
row_in(User *expected_user, Rows *actual_rows)
static void match_failed_for_user(void *expected_row)
{
for (int i = 0; i < actual_rows->size; i++)
{
User *current_user = actual_rows->users[i];
User *expected_user = (User*) expected_row;
if (users_match(current_user, expected_user))
return true;
}
return false;
printf("==============> expected {.id=%d, .age=%d} to be in actual rows\n",
expected_user->id,
expected_user->age);
}
static void
assert_row_in(User *expected_user, Rows *actual_rows)
{
bool found = row_in(expected_user, actual_rows);
if (!found)
printf("==============> expected {.id=%d, .age=%d} to be in actual rows\n",
expected_user->id,
expected_user->age);
assert_true(found);
}
static void
assert_rows(Rows *actual_rows, Rows expected_rows)
{
for (int i = 0; i < expected_rows.size; i++)
assert_row_in(expected_rows.users[i], actual_rows);
}
static void
aSubpartitionedHeapTableHasDataInAGpdbFiveCluster(void)
......@@ -129,6 +106,9 @@ anAdministratorPerformsAnUpgrade(void)
static void
theSubpartitionShouldExistWithDataInTheGpdbSixCluster(void)
{
matcher = users_match;
match_failed = match_failed_for_user;
Rows *rows_in_partition_a = queryForRows("select id, age from users_1_prt_partition_id_2_prt_subpartition_age_first");
Rows *rows_in_partition_b = queryForRows("select id, age from users_1_prt_partition_id_2_prt_subpartition_age_second");
......@@ -137,19 +117,19 @@ theSubpartitionShouldExistWithDataInTheGpdbSixCluster(void)
assert_rows(rows_in_partition_a, (Rows) {
.size = 1,
.users = {&expected_user}
.rows = {&expected_user}
});
assert_rows(rows_in_partition_b, (Rows) {
.size = 1,
.users = {&other_expected_user}
.rows = {&other_expected_user}
});
Rows *all_rows = queryForRows("select id, age from users;");
assert_rows(all_rows, (Rows) {
.size = 2,
.users = {&expected_user, &other_expected_user}
.rows = {&expected_user, &other_expected_user}
});
}
......
#include "stdbool.h"
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <stdio.h>
#include "cmockery.h"
#include "row-assertions.h"
bool (*matcher)(void *expected, void*actual);
void (*match_failed)(void *expected);
static bool
row_in(void *expected_user, Rows *actual_rows)
{
for (int i = 0; i < actual_rows->size; i++)
{
void *current_user = actual_rows->rows[i];
if (matcher(current_user, expected_user))
return true;
}
return false;
}
static void
assert_row_in(void *expected_row, Rows *actual_rows)
{
bool found = row_in(expected_row, actual_rows);
if (!found)
match_failed(expected_row);
assert_true(found);
}
void
assert_rows(Rows *actual_rows, Rows expected_rows)
{
if (matcher == NULL)
printf("expected matcher() function to be configured, was NULL");
if (match_failed == NULL)
printf("expected match_failed() function to be configured, was NULL");
for (int i = 0; i < expected_rows.size; i++)
assert_row_in(expected_rows.rows[i], actual_rows);
}
#ifndef PG_UPGRADE_TEST_UTILITIES_ROW_ASSERTIONS_H
#define PG_UPGRADE_TEST_UTILITIES_ROW_ASSERTIONS_H
#include "stdbool.h"
typedef struct RowsData
{
int size;
void *rows[10];
} Rows;
/*
* Extension points
*/
extern bool (*matcher)(void *expected, void*actual);
extern void (*match_failed)(void *expected);
/*
* Library function
*/
extern void assert_rows(Rows *actual_rows, Rows expected_rows);
#endif /* PG_UPGRADE_TEST_UTILITIES_ROW_ASSERTIONS_H */
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册