Readable unit test - lists of complex objects

Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
3
down vote
favorite
Goal: Writing more readable tests.
I have a couple of functions, which basically merge and converts two lists of Datasets together, written using Scala and Spark. Each of these Datasets has a lot of fields inside it. For testing, I'm creating three Datasets: New records, existing records, and expected result.
The problem is, tests are long and hard to read. An example:
test("Merging Movies") {
val newMovies: Dataset[ATMMovie] = Seq(
ATMMovie(
id = 123L,
utc_insert_timestamp = Some(1524522274),
movie_title = Some("New movie from ATM"),
censor_rating_id = Some(0),
release_year = Some(2018),
release_date = Some(1524522000),
primary_language_id = Some(0),
distributor_id = Some(0),
internal_pos_movie_code = Some("P1"),
internal_pos_movie_id = Some("ID1"),
temporary = 0,
utc_last_modified_timestamp = None,
force_update = 0,
utc_last_import_attempt_timestamp = None,
import_attempts = 0
),
ATMMovie(
id = 456L,
utc_insert_timestamp = Some(34567522274L),
movie_title = Some("Title updated"),
censor_rating_id = Some(0),
release_year = Some(2016),
release_date = Some(1524522000),
primary_language_id = Some(0),
distributor_id = Some(0),
internal_pos_movie_code = Some("Movie2"),
internal_pos_movie_id = Some("MovieID2"),
temporary = 0,
utc_last_modified_timestamp = None,
force_update = 0,
utc_last_import_attempt_timestamp = None,
import_attempts = 3
)
).toDS
val existingMovies: Dataset[ODSMovie] = Seq(
ODSMovie(
movie_row_id = 2L,
movie_source_id = Some("234"),
movie_entity_id = 7777L,
utc_insert_timestamp = Some(1524522000),
movie_title = Some("Old ODS Movie"),
censor_rating_id = Some(0),
release_year = Some(2017),
release_date = Some(1524522987),
primary_language_id = Some(1),
distributor_id = Some(5),
internal_pos_movie_id = Some("Movie 1"),
temporary = 0,
utc_Last_modified_timestamp = Some(1524522666),
force_update = 0,
utc_last_import_attempt_timestamp = None,
import_attempts = 1
),
ODSMovie(
movie_row_id = 764L,
movie_entity_id = 658L,
utc_insert_timestamp = Some(94567522333L),
movie_title = Some("Old title"),
censor_rating_id = Some(0),
release_year = Some(2016),
release_date = Some(1524522000),
primary_language_id = Some(0),
distributor_id = Some(0),
movie_source_id = Some("Movie2"),
internal_pos_movie_id = Some("MovieID2-old"),
temporary = 0,
utc_Last_modified_timestamp = None,
force_update = 0,
utc_last_import_attempt_timestamp = None,
import_attempts = 0
)
).toDS
val expectedODSMovies: Dataset[ODSMovie] = Seq(
ODSMovie(
movie_row_id = 765L,
movie_source_id = Some("P1"),
movie_entity_id = 123L,
utc_insert_timestamp = Some(1524522274),
movie_title = Some("New movie from ATM"),
censor_rating_id = Some(0),
release_year = Some(2018),
release_date = Some(1524522000),
primary_language_id = Some(0),
distributor_id = Some(0),
internal_pos_movie_id = Some("ID1"),
temporary = 0,
utc_Last_modified_timestamp = None,
force_update = 0,
utc_last_import_attempt_timestamp = None,
import_attempts = 0
),
ODSMovie(
movie_row_id = 764L,
movie_entity_id = 456L,
utc_insert_timestamp = Some(34567522274L),
movie_title = Some("Title updated"),
censor_rating_id = Some(0),
release_year = Some(2016),
release_date = Some(1524522000),
primary_language_id = Some(0),
distributor_id = Some(0),
movie_source_id = Some("Movie2"),
internal_pos_movie_id = Some("MovieID2"),
temporary = 0,
utc_Last_modified_timestamp = None,
force_update = 0,
utc_last_import_attempt_timestamp = None,
import_attempts = 3
),
ODSMovie( // Movie we had.
movie_row_id = 2L,
movie_source_id = Some("234"),
movie_entity_id = 7777L,
utc_insert_timestamp = Some(1524522000),
movie_title = Some("Old ODS Movie"),
censor_rating_id = Some(0),
release_year = Some(2017),
release_date = Some(1524522987),
primary_language_id = Some(1),
distributor_id = Some(5),
internal_pos_movie_id = Some("Movie 1"),
temporary = 0,
utc_Last_modified_timestamp = Some(1524522666),
force_update = 0,
utc_last_import_attempt_timestamp = None,
import_attempts = 1
)
).toDS
As you see, each test is very hard to read and follow. I'm looking to find a better way to write these tests.
unit-testing scala
add a comment |Â
up vote
3
down vote
favorite
Goal: Writing more readable tests.
I have a couple of functions, which basically merge and converts two lists of Datasets together, written using Scala and Spark. Each of these Datasets has a lot of fields inside it. For testing, I'm creating three Datasets: New records, existing records, and expected result.
The problem is, tests are long and hard to read. An example:
test("Merging Movies") {
val newMovies: Dataset[ATMMovie] = Seq(
ATMMovie(
id = 123L,
utc_insert_timestamp = Some(1524522274),
movie_title = Some("New movie from ATM"),
censor_rating_id = Some(0),
release_year = Some(2018),
release_date = Some(1524522000),
primary_language_id = Some(0),
distributor_id = Some(0),
internal_pos_movie_code = Some("P1"),
internal_pos_movie_id = Some("ID1"),
temporary = 0,
utc_last_modified_timestamp = None,
force_update = 0,
utc_last_import_attempt_timestamp = None,
import_attempts = 0
),
ATMMovie(
id = 456L,
utc_insert_timestamp = Some(34567522274L),
movie_title = Some("Title updated"),
censor_rating_id = Some(0),
release_year = Some(2016),
release_date = Some(1524522000),
primary_language_id = Some(0),
distributor_id = Some(0),
internal_pos_movie_code = Some("Movie2"),
internal_pos_movie_id = Some("MovieID2"),
temporary = 0,
utc_last_modified_timestamp = None,
force_update = 0,
utc_last_import_attempt_timestamp = None,
import_attempts = 3
)
).toDS
val existingMovies: Dataset[ODSMovie] = Seq(
ODSMovie(
movie_row_id = 2L,
movie_source_id = Some("234"),
movie_entity_id = 7777L,
utc_insert_timestamp = Some(1524522000),
movie_title = Some("Old ODS Movie"),
censor_rating_id = Some(0),
release_year = Some(2017),
release_date = Some(1524522987),
primary_language_id = Some(1),
distributor_id = Some(5),
internal_pos_movie_id = Some("Movie 1"),
temporary = 0,
utc_Last_modified_timestamp = Some(1524522666),
force_update = 0,
utc_last_import_attempt_timestamp = None,
import_attempts = 1
),
ODSMovie(
movie_row_id = 764L,
movie_entity_id = 658L,
utc_insert_timestamp = Some(94567522333L),
movie_title = Some("Old title"),
censor_rating_id = Some(0),
release_year = Some(2016),
release_date = Some(1524522000),
primary_language_id = Some(0),
distributor_id = Some(0),
movie_source_id = Some("Movie2"),
internal_pos_movie_id = Some("MovieID2-old"),
temporary = 0,
utc_Last_modified_timestamp = None,
force_update = 0,
utc_last_import_attempt_timestamp = None,
import_attempts = 0
)
).toDS
val expectedODSMovies: Dataset[ODSMovie] = Seq(
ODSMovie(
movie_row_id = 765L,
movie_source_id = Some("P1"),
movie_entity_id = 123L,
utc_insert_timestamp = Some(1524522274),
movie_title = Some("New movie from ATM"),
censor_rating_id = Some(0),
release_year = Some(2018),
release_date = Some(1524522000),
primary_language_id = Some(0),
distributor_id = Some(0),
internal_pos_movie_id = Some("ID1"),
temporary = 0,
utc_Last_modified_timestamp = None,
force_update = 0,
utc_last_import_attempt_timestamp = None,
import_attempts = 0
),
ODSMovie(
movie_row_id = 764L,
movie_entity_id = 456L,
utc_insert_timestamp = Some(34567522274L),
movie_title = Some("Title updated"),
censor_rating_id = Some(0),
release_year = Some(2016),
release_date = Some(1524522000),
primary_language_id = Some(0),
distributor_id = Some(0),
movie_source_id = Some("Movie2"),
internal_pos_movie_id = Some("MovieID2"),
temporary = 0,
utc_Last_modified_timestamp = None,
force_update = 0,
utc_last_import_attempt_timestamp = None,
import_attempts = 3
),
ODSMovie( // Movie we had.
movie_row_id = 2L,
movie_source_id = Some("234"),
movie_entity_id = 7777L,
utc_insert_timestamp = Some(1524522000),
movie_title = Some("Old ODS Movie"),
censor_rating_id = Some(0),
release_year = Some(2017),
release_date = Some(1524522987),
primary_language_id = Some(1),
distributor_id = Some(5),
internal_pos_movie_id = Some("Movie 1"),
temporary = 0,
utc_Last_modified_timestamp = Some(1524522666),
force_update = 0,
utc_last_import_attempt_timestamp = None,
import_attempts = 1
)
).toDS
As you see, each test is very hard to read and follow. I'm looking to find a better way to write these tests.
unit-testing scala
I don't know about Scala syntax, but have you considered migrating data to JSON and parse it from the code instead of having it everything in the code?
â Xtreme Biker
May 3 at 6:31
Without seeing the code that performs this "merging" I find it hard to tell what's going on in here. Without understanding what are you testing, it's hard to suggest improvements for the tests.
â Rene Saarsoo
May 3 at 12:25
1
My team and I typically find that having a "defaults" file in our API test package to be very useful when writing data-oriented tests. If these domain objects need to be used in future, we can rely on the fact that they're determinstic and centralized.
â erip
May 11 at 11:42
add a comment |Â
up vote
3
down vote
favorite
up vote
3
down vote
favorite
Goal: Writing more readable tests.
I have a couple of functions, which basically merge and converts two lists of Datasets together, written using Scala and Spark. Each of these Datasets has a lot of fields inside it. For testing, I'm creating three Datasets: New records, existing records, and expected result.
The problem is, tests are long and hard to read. An example:
test("Merging Movies") {
val newMovies: Dataset[ATMMovie] = Seq(
ATMMovie(
id = 123L,
utc_insert_timestamp = Some(1524522274),
movie_title = Some("New movie from ATM"),
censor_rating_id = Some(0),
release_year = Some(2018),
release_date = Some(1524522000),
primary_language_id = Some(0),
distributor_id = Some(0),
internal_pos_movie_code = Some("P1"),
internal_pos_movie_id = Some("ID1"),
temporary = 0,
utc_last_modified_timestamp = None,
force_update = 0,
utc_last_import_attempt_timestamp = None,
import_attempts = 0
),
ATMMovie(
id = 456L,
utc_insert_timestamp = Some(34567522274L),
movie_title = Some("Title updated"),
censor_rating_id = Some(0),
release_year = Some(2016),
release_date = Some(1524522000),
primary_language_id = Some(0),
distributor_id = Some(0),
internal_pos_movie_code = Some("Movie2"),
internal_pos_movie_id = Some("MovieID2"),
temporary = 0,
utc_last_modified_timestamp = None,
force_update = 0,
utc_last_import_attempt_timestamp = None,
import_attempts = 3
)
).toDS
val existingMovies: Dataset[ODSMovie] = Seq(
ODSMovie(
movie_row_id = 2L,
movie_source_id = Some("234"),
movie_entity_id = 7777L,
utc_insert_timestamp = Some(1524522000),
movie_title = Some("Old ODS Movie"),
censor_rating_id = Some(0),
release_year = Some(2017),
release_date = Some(1524522987),
primary_language_id = Some(1),
distributor_id = Some(5),
internal_pos_movie_id = Some("Movie 1"),
temporary = 0,
utc_Last_modified_timestamp = Some(1524522666),
force_update = 0,
utc_last_import_attempt_timestamp = None,
import_attempts = 1
),
ODSMovie(
movie_row_id = 764L,
movie_entity_id = 658L,
utc_insert_timestamp = Some(94567522333L),
movie_title = Some("Old title"),
censor_rating_id = Some(0),
release_year = Some(2016),
release_date = Some(1524522000),
primary_language_id = Some(0),
distributor_id = Some(0),
movie_source_id = Some("Movie2"),
internal_pos_movie_id = Some("MovieID2-old"),
temporary = 0,
utc_Last_modified_timestamp = None,
force_update = 0,
utc_last_import_attempt_timestamp = None,
import_attempts = 0
)
).toDS
val expectedODSMovies: Dataset[ODSMovie] = Seq(
ODSMovie(
movie_row_id = 765L,
movie_source_id = Some("P1"),
movie_entity_id = 123L,
utc_insert_timestamp = Some(1524522274),
movie_title = Some("New movie from ATM"),
censor_rating_id = Some(0),
release_year = Some(2018),
release_date = Some(1524522000),
primary_language_id = Some(0),
distributor_id = Some(0),
internal_pos_movie_id = Some("ID1"),
temporary = 0,
utc_Last_modified_timestamp = None,
force_update = 0,
utc_last_import_attempt_timestamp = None,
import_attempts = 0
),
ODSMovie(
movie_row_id = 764L,
movie_entity_id = 456L,
utc_insert_timestamp = Some(34567522274L),
movie_title = Some("Title updated"),
censor_rating_id = Some(0),
release_year = Some(2016),
release_date = Some(1524522000),
primary_language_id = Some(0),
distributor_id = Some(0),
movie_source_id = Some("Movie2"),
internal_pos_movie_id = Some("MovieID2"),
temporary = 0,
utc_Last_modified_timestamp = None,
force_update = 0,
utc_last_import_attempt_timestamp = None,
import_attempts = 3
),
ODSMovie( // Movie we had.
movie_row_id = 2L,
movie_source_id = Some("234"),
movie_entity_id = 7777L,
utc_insert_timestamp = Some(1524522000),
movie_title = Some("Old ODS Movie"),
censor_rating_id = Some(0),
release_year = Some(2017),
release_date = Some(1524522987),
primary_language_id = Some(1),
distributor_id = Some(5),
internal_pos_movie_id = Some("Movie 1"),
temporary = 0,
utc_Last_modified_timestamp = Some(1524522666),
force_update = 0,
utc_last_import_attempt_timestamp = None,
import_attempts = 1
)
).toDS
As you see, each test is very hard to read and follow. I'm looking to find a better way to write these tests.
unit-testing scala
Goal: Writing more readable tests.
I have a couple of functions, which basically merge and converts two lists of Datasets together, written using Scala and Spark. Each of these Datasets has a lot of fields inside it. For testing, I'm creating three Datasets: New records, existing records, and expected result.
The problem is, tests are long and hard to read. An example:
test("Merging Movies") {
val newMovies: Dataset[ATMMovie] = Seq(
ATMMovie(
id = 123L,
utc_insert_timestamp = Some(1524522274),
movie_title = Some("New movie from ATM"),
censor_rating_id = Some(0),
release_year = Some(2018),
release_date = Some(1524522000),
primary_language_id = Some(0),
distributor_id = Some(0),
internal_pos_movie_code = Some("P1"),
internal_pos_movie_id = Some("ID1"),
temporary = 0,
utc_last_modified_timestamp = None,
force_update = 0,
utc_last_import_attempt_timestamp = None,
import_attempts = 0
),
ATMMovie(
id = 456L,
utc_insert_timestamp = Some(34567522274L),
movie_title = Some("Title updated"),
censor_rating_id = Some(0),
release_year = Some(2016),
release_date = Some(1524522000),
primary_language_id = Some(0),
distributor_id = Some(0),
internal_pos_movie_code = Some("Movie2"),
internal_pos_movie_id = Some("MovieID2"),
temporary = 0,
utc_last_modified_timestamp = None,
force_update = 0,
utc_last_import_attempt_timestamp = None,
import_attempts = 3
)
).toDS
val existingMovies: Dataset[ODSMovie] = Seq(
ODSMovie(
movie_row_id = 2L,
movie_source_id = Some("234"),
movie_entity_id = 7777L,
utc_insert_timestamp = Some(1524522000),
movie_title = Some("Old ODS Movie"),
censor_rating_id = Some(0),
release_year = Some(2017),
release_date = Some(1524522987),
primary_language_id = Some(1),
distributor_id = Some(5),
internal_pos_movie_id = Some("Movie 1"),
temporary = 0,
utc_Last_modified_timestamp = Some(1524522666),
force_update = 0,
utc_last_import_attempt_timestamp = None,
import_attempts = 1
),
ODSMovie(
movie_row_id = 764L,
movie_entity_id = 658L,
utc_insert_timestamp = Some(94567522333L),
movie_title = Some("Old title"),
censor_rating_id = Some(0),
release_year = Some(2016),
release_date = Some(1524522000),
primary_language_id = Some(0),
distributor_id = Some(0),
movie_source_id = Some("Movie2"),
internal_pos_movie_id = Some("MovieID2-old"),
temporary = 0,
utc_Last_modified_timestamp = None,
force_update = 0,
utc_last_import_attempt_timestamp = None,
import_attempts = 0
)
).toDS
val expectedODSMovies: Dataset[ODSMovie] = Seq(
ODSMovie(
movie_row_id = 765L,
movie_source_id = Some("P1"),
movie_entity_id = 123L,
utc_insert_timestamp = Some(1524522274),
movie_title = Some("New movie from ATM"),
censor_rating_id = Some(0),
release_year = Some(2018),
release_date = Some(1524522000),
primary_language_id = Some(0),
distributor_id = Some(0),
internal_pos_movie_id = Some("ID1"),
temporary = 0,
utc_Last_modified_timestamp = None,
force_update = 0,
utc_last_import_attempt_timestamp = None,
import_attempts = 0
),
ODSMovie(
movie_row_id = 764L,
movie_entity_id = 456L,
utc_insert_timestamp = Some(34567522274L),
movie_title = Some("Title updated"),
censor_rating_id = Some(0),
release_year = Some(2016),
release_date = Some(1524522000),
primary_language_id = Some(0),
distributor_id = Some(0),
movie_source_id = Some("Movie2"),
internal_pos_movie_id = Some("MovieID2"),
temporary = 0,
utc_Last_modified_timestamp = None,
force_update = 0,
utc_last_import_attempt_timestamp = None,
import_attempts = 3
),
ODSMovie( // Movie we had.
movie_row_id = 2L,
movie_source_id = Some("234"),
movie_entity_id = 7777L,
utc_insert_timestamp = Some(1524522000),
movie_title = Some("Old ODS Movie"),
censor_rating_id = Some(0),
release_year = Some(2017),
release_date = Some(1524522987),
primary_language_id = Some(1),
distributor_id = Some(5),
internal_pos_movie_id = Some("Movie 1"),
temporary = 0,
utc_Last_modified_timestamp = Some(1524522666),
force_update = 0,
utc_last_import_attempt_timestamp = None,
import_attempts = 1
)
).toDS
As you see, each test is very hard to read and follow. I'm looking to find a better way to write these tests.
unit-testing scala
edited May 2 at 22:08
Sam Onela
5,77461543
5,77461543
asked May 2 at 21:47
Aidin
1162
1162
I don't know about Scala syntax, but have you considered migrating data to JSON and parse it from the code instead of having it everything in the code?
â Xtreme Biker
May 3 at 6:31
Without seeing the code that performs this "merging" I find it hard to tell what's going on in here. Without understanding what are you testing, it's hard to suggest improvements for the tests.
â Rene Saarsoo
May 3 at 12:25
1
My team and I typically find that having a "defaults" file in our API test package to be very useful when writing data-oriented tests. If these domain objects need to be used in future, we can rely on the fact that they're determinstic and centralized.
â erip
May 11 at 11:42
add a comment |Â
I don't know about Scala syntax, but have you considered migrating data to JSON and parse it from the code instead of having it everything in the code?
â Xtreme Biker
May 3 at 6:31
Without seeing the code that performs this "merging" I find it hard to tell what's going on in here. Without understanding what are you testing, it's hard to suggest improvements for the tests.
â Rene Saarsoo
May 3 at 12:25
1
My team and I typically find that having a "defaults" file in our API test package to be very useful when writing data-oriented tests. If these domain objects need to be used in future, we can rely on the fact that they're determinstic and centralized.
â erip
May 11 at 11:42
I don't know about Scala syntax, but have you considered migrating data to JSON and parse it from the code instead of having it everything in the code?
â Xtreme Biker
May 3 at 6:31
I don't know about Scala syntax, but have you considered migrating data to JSON and parse it from the code instead of having it everything in the code?
â Xtreme Biker
May 3 at 6:31
Without seeing the code that performs this "merging" I find it hard to tell what's going on in here. Without understanding what are you testing, it's hard to suggest improvements for the tests.
â Rene Saarsoo
May 3 at 12:25
Without seeing the code that performs this "merging" I find it hard to tell what's going on in here. Without understanding what are you testing, it's hard to suggest improvements for the tests.
â Rene Saarsoo
May 3 at 12:25
1
1
My team and I typically find that having a "defaults" file in our API test package to be very useful when writing data-oriented tests. If these domain objects need to be used in future, we can rely on the fact that they're determinstic and centralized.
â erip
May 11 at 11:42
My team and I typically find that having a "defaults" file in our API test package to be very useful when writing data-oriented tests. If these domain objects need to be used in future, we can rely on the fact that they're determinstic and centralized.
â erip
May 11 at 11:42
add a comment |Â
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f193503%2freadable-unit-test-lists-of-complex-objects%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
I don't know about Scala syntax, but have you considered migrating data to JSON and parse it from the code instead of having it everything in the code?
â Xtreme Biker
May 3 at 6:31
Without seeing the code that performs this "merging" I find it hard to tell what's going on in here. Without understanding what are you testing, it's hard to suggest improvements for the tests.
â Rene Saarsoo
May 3 at 12:25
1
My team and I typically find that having a "defaults" file in our API test package to be very useful when writing data-oriented tests. If these domain objects need to be used in future, we can rely on the fact that they're determinstic and centralized.
â erip
May 11 at 11:42