KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_database_lib_settings.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
22#include <json_common.h>
23
24#include <wx/utils.h>
25
26#include <filesystem>
27#include <fstream>
28#include <sstream>
29
30namespace fs = std::filesystem;
31
32
33BOOST_AUTO_TEST_SUITE( DatabaseLibSettings )
34
35
36
40BOOST_AUTO_TEST_CASE( UnknownKeysSurviveAnEdit )
41{
42 fs::path dir = fs::temp_directory_path() / ( "kicad_dblib_settings_" + std::to_string( wxGetProcessId() ) );
43 fs::path path = dir / "qa_dblib.kicad_dbl";
44
45 fs::remove_all( dir );
46 fs::create_directories( dir );
47 fs::copy_file( fs::path( KI_TEST::GetTestDataRootDir() ) / "dblib" / "qa_dblib.kicad_dbl", path );
48
49 auto readJson = [&]()
50 {
51 std::ifstream in( path, std::ios::binary );
52 std::stringstream buffer;
53 buffer << in.rdbuf();
54 return nlohmann::json::parse( buffer.str() );
55 };
56
57 nlohmann::json json = readJson();
58
59 json["libraries"][0]["x_vendor"] = 1;
60 json["libraries"][0]["fields"][0]["x_vendor"] = 2;
61 json["libraries"][0]["properties"]["x_vendor"] = 3;
62
63 {
64 std::ofstream out( path, std::ios::binary | std::ios::trunc );
65 out << json.dump( 2 );
66 }
67
68 {
69 std::unique_ptr<DATABASE_LIB_SETTINGS> settings =
70 LoadDatabaseLibSettingsReadOnly( wxString::FromUTF8( path.string() ) );
71
72 settings->m_Tables[0].key_col = "MPN";
73 settings->SetReadOnly( false );
74 BOOST_REQUIRE( settings->SaveToFile() );
75 }
76
77 nlohmann::json saved = readJson();
78
79 BOOST_CHECK_EQUAL( saved["libraries"][0]["key"].get<std::string>(), "MPN" );
80 BOOST_CHECK_EQUAL( saved["libraries"][0]["x_vendor"].get<int>(), 1 );
81 BOOST_CHECK_EQUAL( saved["libraries"][0]["fields"][0]["x_vendor"].get<int>(), 2 );
82 BOOST_CHECK_EQUAL( saved["libraries"][0]["properties"]["x_vendor"].get<int>(), 3 );
83
84 fs::remove_all( dir );
85}
86
87
std::unique_ptr< DATABASE_LIB_SETTINGS > LoadDatabaseLibSettingsReadOnly(const wxString &aPath, bool *aLoaded)
Loads a database library settings file without allowing the load itself (e.g.
nlohmann::json json
Definition gerbview.cpp:50
std::string GetTestDataRootDir()
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
BOOST_AUTO_TEST_SUITE_END()
std::string path
BOOST_CHECK_EQUAL(result, "25.4")