Skip to content

Commit 88a59c1

Browse files
committed
Replace hardcoded files with data files created on the fly
Signed-off-by: Aleksandr Motsjonov <[email protected]>
1 parent 1bae9e3 commit 88a59c1

File tree

4 files changed

+81
-68
lines changed

4 files changed

+81
-68
lines changed

tests/materials/test-database/camera/Cheburashka_Model1_380_780_5.json

Lines changed: 0 additions & 19 deletions
This file was deleted.

tests/materials/test-database/camera/Karamba_M2_380_780_5.json

Lines changed: 0 additions & 19 deletions
This file was deleted.

tests/materials/test-database/illuminant/iso4242_380_780_5.json

Lines changed: 0 additions & 18 deletions
This file was deleted.

tests/test_image_converter.cpp

Lines changed: 81 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <filesystem>
1212
#include <fstream>
1313
#include <iostream>
14+
#include <nlohmann/json.hpp>
1415
#include <sstream>
1516
#include <vector>
1617
#include <sys/stat.h> // for mkfifo
@@ -61,6 +62,10 @@ class TestDirectory
6162
std::to_string( std::time( nullptr ) ) ) )
6263
.string();
6364
std::filesystem::create_directories( test_dir );
65+
66+
// Create database directory for test data
67+
database_dir = test_dir + "/test-database";
68+
std::filesystem::create_directories( database_dir );
6469
}
6570

6671
~TestDirectory() { std::filesystem::remove_all( test_dir ); }
@@ -70,6 +75,7 @@ class TestDirectory
7075
TestDirectory &operator=( const TestDirectory & ) = delete;
7176

7277
const std::string &path() const { return test_dir; }
78+
const std::string &get_database_path() const { return database_dir; }
7379

7480
void create_test_files()
7581
{
@@ -120,8 +126,51 @@ class TestDirectory
120126
}
121127
}
122128

129+
/// Creates a test data file (camera or illuminant) with the specified header data
130+
/// @param type The type of test data to create (e.g. camera or illuminant)
131+
/// @param header_data JSON object containing the header data to include
132+
/// @return The full path to the created file
133+
std::string create_test_data_file(
134+
const std::string &type, const nlohmann::json &header_data )
135+
{
136+
// Generate random filename
137+
static int file_counter = 0;
138+
std::string filename = "test_data_" + std::to_string( ++file_counter ) +
139+
"_" + std::to_string( std::time( nullptr ) ) +
140+
".json";
141+
142+
// Create target directory dynamically based on type
143+
std::string target_dir = database_dir + "/" + type;
144+
std::filesystem::create_directories( target_dir );
145+
std::string file_path = target_dir + "/" + filename;
146+
147+
// Create JSON object using nlohmann/json
148+
nlohmann::json json_data;
149+
150+
// Start with default header and merge user data
151+
nlohmann::json header = header_data;
152+
153+
// Build spectral_data object
154+
nlohmann::json spectral_data = { { "units", "relative" },
155+
{ "index",
156+
{ { "main", { "R", "G", "B" } } } },
157+
{ "data", nlohmann::json::object() } };
158+
159+
// Assemble final JSON
160+
json_data["header"] = header;
161+
json_data["spectral_data"] = spectral_data;
162+
163+
// Write to file with pretty formatting
164+
std::ofstream file( file_path );
165+
file << json_data.dump( 4 ) << std::endl;
166+
file.close();
167+
168+
return file_path;
169+
}
170+
123171
private:
124172
std::string test_dir;
173+
std::string database_dir;
125174
};
126175

127176
/// Verifies that collect_image_files can traverse a directory, identify valid RAW image files,
@@ -545,15 +594,18 @@ struct ParseParametersTestResult
545594
///
546595
/// @param args Vector of command-line arguments to pass to parse_parameters (excluding program name).
547596
/// For example, {"--list-cameras"} or {"--list-illuminants", "--verbose"}.
597+
/// @param database_path Path to the test database directory (optional, uses default if not provided)
548598
///
549599
/// @return ParseParametersTestResult containing:
550600
/// - success: true if parse_parameters executed successfully, false if argument parsing failed
551601
/// - output: captured stdout output from the parse_parameters execution
552-
ParseParametersTestResult
553-
run_parse_parameters_test( const std::vector<std::string> &args )
602+
ParseParametersTestResult run_parse_parameters_test(
603+
const std::vector<std::string> &args,
604+
const std::string &database_path = "" )
554605
{
555-
// Set up test data path to use the test database
556-
std::string test_data_path = get_test_database_path();
606+
// Set up test data path to use the provided database path or default
607+
std::string test_data_path =
608+
database_path.empty() ? get_test_database_path() : database_path;
557609
set_env_var( "RAWTOACES_DATA_PATH", test_data_path.c_str() );
558610

559611
// Create ImageConverter instance
@@ -604,8 +656,18 @@ void test_parse_parameters_list_cameras()
604656
std::cout << std::endl
605657
<< "test_parse_parameters_list_cameras()" << std::endl;
606658

607-
// Run the test with --list-cameras argument
608-
auto result = run_parse_parameters_test( { "--list-cameras" } );
659+
// Create test directory with dynamic database
660+
TestDirectory test_dir;
661+
662+
// Create test camera data files
663+
test_dir.create_test_data_file(
664+
"camera", { { "manufacturer", "Canon" }, { "model", "EOS R6" } } );
665+
test_dir.create_test_data_file(
666+
"camera", { { "manufacturer", "Mamiya" }, { "model", "Mamiya 7" } } );
667+
668+
// Run the test with --list-cameras argument using the dynamic database
669+
auto result = run_parse_parameters_test(
670+
{ "--list-cameras" }, test_dir.get_database_path() );
609671

610672
// The method should return true (though it calls exit in real usage)
611673
OIIO_CHECK_EQUAL( result.success, true );
@@ -620,10 +682,9 @@ void test_parse_parameters_list_cameras()
620682
// Verify that actual camera names from test data are present
621683
// The format is "manufacturer / model" as defined in supported_cameras()
622684
OIIO_CHECK_EQUAL(
623-
result.output.find( "cheburashka / model 1" ) != std::string::npos,
624-
true );
685+
result.output.find( "Canon / EOS R6" ) != std::string::npos, true );
625686
OIIO_CHECK_EQUAL(
626-
result.output.find( "karamba / M2" ) != std::string::npos, true );
687+
result.output.find( "Mamiya / Mamiya 7" ) != std::string::npos, true );
627688

628689
// Count occurrences of " / " to verify we have 2 camera entries
629690
size_t camera_count = 0;
@@ -643,8 +704,16 @@ void test_parse_parameters_list_illuminants()
643704
std::cout << std::endl
644705
<< "test_parse_parameters_list_illuminants()" << std::endl;
645706

646-
// Run the test with --list-illuminants argument
647-
auto result = run_parse_parameters_test( { "--list-illuminants" } );
707+
// Create test directory with dynamic database
708+
TestDirectory test_dir;
709+
710+
// Create test illuminant data file
711+
test_dir.create_test_data_file(
712+
"illuminant", { { "illuminant", "my-illuminant" } } );
713+
714+
// Run the test with --list-illuminants argument using the dynamic database
715+
auto result = run_parse_parameters_test(
716+
{ "--list-illuminants" }, test_dir.get_database_path() );
648717

649718
// The method should return true (though it calls exit in real usage)
650719
OIIO_CHECK_EQUAL( result.success, true );
@@ -667,7 +736,7 @@ void test_parse_parameters_list_illuminants()
667736

668737
// Verify that the specific illuminant from our test data is present
669738
OIIO_CHECK_EQUAL(
670-
result.output.find( "iso4242" ) != std::string::npos, true );
739+
result.output.find( "my-illuminant" ) != std::string::npos, true );
671740

672741
// Verify we have exactly 3 illuminants total (2 hardcoded + 1 from test data)
673742
// Count newlines in the illuminant list section to verify count

0 commit comments

Comments
 (0)