Fixed issue with party info having an issue serializing with null values (#293)

* Fixed issue with party info having an issue serializing with null values

* Add a unit test
This commit is contained in:
Steve Maier 2022-06-03 01:08:32 -04:00 коммит произвёл GitHub
Родитель 4d1e609e82
Коммит bf01e3e3fe
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
2 изменённых файлов: 62 добавлений и 3 удалений

Просмотреть файл

@ -213,10 +213,21 @@ namespace electionguard
{
// TODO: other cases
if (j.contains("name") && !j["name"].is_null()) {
string abbreviation;
string color;
string logo_uri;
if (!j["abbreviation"].is_null()) {
abbreviation = j["abbreviation"].get<string>();
}
if (!j["color"].is_null()) {
color = j["color"].get<string>();
}
if (!j["logo_uri"].is_null()) {
logo_uri = j["logo_uri"].get<string>();
}
return make_unique<Party>(j["object_id"].get<string>(),
internationalizedTextFromJson(j["name"]),
j["abbreviation"].get<string>(), j["color"].get<string>(),
j["logo_uri"].get<string>());
internationalizedTextFromJson(j["name"]), abbreviation, color,
logo_uri);
}
return make_unique<Party>(j["object_id"].get<string>());
}

Просмотреть файл

@ -12,6 +12,7 @@ bool test_simple_manifest_is_valid(void);
bool test_can_construct_ballot_style(void);
bool test_can_construct_internationalized_text(void);
static bool test_can_deserialize_election_description(void);
static bool test_can_deserialize_election_description_with_nulls(void);
static bool test_can_construct_internal_election_description_from_election_description(void);
static bool test_can_deserlialze_internal_election_description(void);
@ -20,6 +21,7 @@ bool test_manifest(void)
printf("\n -------- test_manifest.c --------- \n");
return test_simple_manifest_is_valid() && test_can_construct_internationalized_text() &&
test_can_construct_ballot_style() && test_can_deserialize_election_description() &&
test_can_deserialize_election_description_with_nulls() &&
test_can_construct_internal_election_description_from_election_description() &&
test_can_deserlialze_internal_election_description();
}
@ -46,6 +48,28 @@ char *election_json =
"unit-id\",\"type\":\"unknown\"}],\"parties\":[{\"object_id\":\"some-party-id-1\"},{\"object_"
"id\":\"some-party-id-2\"}],\"start_date\":\"2021-02-04T13:30:10Z\",\"type\":\"unknown\"}";
char *election_json_no_logo =
"{\"ballot_styles\":[{\"geopolitical_unit_ids\":[\"some-geopoltical-unit-id\"],\"object_id\":"
"\"some-ballot-style-id\"}],\"candidates\":[{\"object_id\":\"some-candidate-id-1\"},{"
"\"object_id\":\"some-candidate-id-2\"},{\"object_id\":\"some-candidate-id-3\"}],"
"\"contests\":[{\"ballot_selections\":[{\"candidate_id\":\"some-candidate-id-1\",\"object_"
"id\":\"some-object-id-affirmative\",\"sequence_order\":0},{\"candidate_id\":\"some-"
"candidate-id-2\",\"object_id\":\"some-object-id-negative\",\"sequence_order\":1}],"
"\"electoral_district_id\":\"some-geopoltical-unit-id\",\"name\":\"some-referendum-contest-"
"name\",\"number_elected\":1,\"object_id\":\"some-referendum-contest-object-id\",\"sequence_"
"order\":0,\"vote_variation\":\"one_of_m\"},{\"ballot_selections\":[{\"candidate_id\":\"some-"
"candidate-id-1\",\"object_id\":\"some-object-id-candidate-1\",\"sequence_order\":0},{"
"\"candidate_id\":\"some-candidate-id-2\",\"object_id\":\"some-object-id-candidate-2\","
"\"sequence_order\":1},{\"candidate_id\":\"some-candidate-id-3\",\"object_id\":\"some-object-"
"id-candidate-3\",\"sequence_order\":2}],\"electoral_district_id\":\"some-geopoltical-unit-"
"id\",\"name\":\"some-candidate-contest-name\",\"number_elected\":2,\"object_id\":\"some-"
"candidate-contest-object-id\",\"sequence_order\":1,\"vote_variation\":\"one_of_m\"}],"
"\"election_scope_id\":\"some-scope-id\",\"end_date\":\"2021-02-04T13:30:10Z\","
"\"geopolitical_units\":[{\"name\":\"some-gp-unit-name\",\"object_id\":\"some-geopoltical-"
"unit-id\",\"type\":\"unknown\"}],\"parties\":[{\"object_id\":\"some-party-id-1\", \"logo_uri\": "
"null},{\"object_id\":\"some-party-id-2\"}],\"start_date\":\"2021-02-04T13:30:10Z\",\"type\":"
"\"unknown\"}";
bool test_simple_manifest_is_valid(void)
{
printf("\n -------- test_simple_manifest_is_valid -------- \n");
@ -95,6 +119,30 @@ bool test_can_deserialize_election_description(void)
return true;
}
bool test_can_deserialize_election_description_with_nulls(void)
{
printf("\n -------- test_can_deserialize_election_description_with_nulls -------- \n");
// Act
eg_election_manifest_t *result = NULL;
if (eg_election_manifest_from_json(election_json_no_logo, &result)) {
assert(false);
}
// Assert
char *election_scope_id;
if (eg_election_manifest_get_election_scope_id(result, &election_scope_id)) {
assert(false);
}
assert(strings_are_equal("some-scope-id", election_scope_id) == true);
// Clean Up
eg_election_manifest_free(result);
free(election_scope_id);
return true;
}
bool test_can_construct_internationalized_text(void)
{
printf("\n -------- test_can_construct_internationalized_text -------- \n");