KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_sch_io_database.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
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU 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
20#include <algorithm>
21#include <memory>
22#include <set>
23#include <vector>
24
26
28#include <eda_pattern_match.h>
29#include <ki_exception.h>
30#include <lib_symbol.h>
34
35
36BOOST_AUTO_TEST_SUITE( SchIoDatabase )
37
38
39
53BOOST_AUTO_TEST_CASE( UnchangedDataReusesCache )
54{
55 LIBRARY_MANAGER manager;
56 SYMBOL_LIBRARY_ADAPTER adapter( manager );
57
58 SCH_IO_DATABASE plugin;
59 plugin.SetLibraryManagerAdapter( &adapter );
60
61 wxString libPath( QA_DBLIB_SETTINGS_PATH );
62
63 std::vector<LIB_SYMBOL*> first;
64 BOOST_REQUIRE_NO_THROW( plugin.EnumerateSymbolLib( first, libPath ) );
65 BOOST_REQUIRE( !first.empty() );
66
67 // Defeat the age throttle so the next call actually re-queries and compares signatures rather
68 // than short-circuiting on the cache timestamp.
69 plugin.Settings()->m_Cache.max_age = 0;
70
71 std::vector<LIB_SYMBOL*> second;
72 BOOST_REQUIRE_NO_THROW( plugin.EnumerateSymbolLib( second, libPath ) );
73
74 BOOST_REQUIRE_EQUAL( first.size(), second.size() );
75
76 std::set<wxString> firstNames;
77
78 for( LIB_SYMBOL* symbol : first )
79 {
80 firstNames.insert( symbol->GetName() );
81 delete symbol;
82 }
83
84 for( LIB_SYMBOL* symbol : second )
85 {
86 BOOST_CHECK( firstNames.count( symbol->GetName() ) );
87 delete symbol;
88 }
89}
90
91
102BOOST_AUTO_TEST_CASE( FieldMappedDescriptionMatchesShownDescription )
103{
104 const std::string dblPath =
105 KI_TEST::GetTestDataRootDir() + "dblib/qa_dblib_field_description.kicad_dbl";
106
107 LIBRARY_MANAGER manager;
108 SYMBOL_LIBRARY_ADAPTER adapter( manager );
109
110 SCH_IO_DATABASE plugin;
111 plugin.SetLibraryManagerAdapter( &adapter );
112
113 LIB_SYMBOL* symbol = nullptr;
114
115 try
116 {
117 symbol = plugin.LoadSymbol( dblPath, wxS( "RES-001" ), nullptr );
118 }
119 catch( const IO_ERROR& ioe )
120 {
121 // The database connection needs a working ODBC SQLite driver; skip when it is unavailable
122 // so the test does not fail spuriously on minimal CI images.
123 BOOST_TEST_MESSAGE( "Skipping: database library unavailable: " + ioe.What().ToStdString() );
124 return;
125 }
126
127 BOOST_REQUIRE( symbol );
128
129 std::unique_ptr<LIB_SYMBOL> owned( symbol );
130
131 const wxString liveDescription = owned->GetField( FIELD_T::DESCRIPTION )->GetShownText( FOR_GUI );
132
133 // The database row for RES-001 carries a non-empty Description.
134 BOOST_REQUIRE( !liveDescription.IsEmpty() );
135
136 // The chooser tree / search path must agree with the live field the details panel shows.
137 BOOST_CHECK_EQUAL( owned->GetShownDescription( FOR_GUI ), liveDescription );
138 BOOST_CHECK_EQUAL( owned->GetDesc(), liveDescription );
139
140 // The search-term cache feeds chooser filtering and is built from the shown description, so it
141 // must reflect the database value too.
142 const std::vector<SEARCH_TERM>& terms = owned->GetSearchTerms();
143 bool descriptionTermPresent = std::any_of( terms.begin(), terms.end(),
144 [&]( const SEARCH_TERM& aTerm )
145 {
146 return aTerm.Text == liveDescription;
147 } );
148
149 BOOST_CHECK( descriptionTermPresent );
150}
151
152
157BOOST_AUTO_TEST_CASE( CreateConnectionExpandsCwdAndPrefersConnectionString )
158{
159 const std::string dblPath = KI_TEST::GetTestDataRootDir() + "dblib/qa_dblib_field_description.kicad_dbl";
160
161 DATABASE_LIB_SETTINGS settings( dblPath );
162 settings.SetReadOnly( true );
163 settings.LoadFromFile();
164
165 BOOST_REQUIRE( settings.m_Source.connection_string.find( "${CWD}" ) != std::string::npos );
166
167 // The database file is only found if ${CWD} became the settings folder
168 settings.m_Source.dsn = "qa_nonexistent_dsn";
169
170 std::unique_ptr<DATABASE_CONNECTION> conn = SCH_IO_DATABASE::CreateConnection( settings );
171
172 BOOST_REQUIRE( conn );
173 BOOST_CHECK_MESSAGE( conn->IsConnected(), conn->GetLastError() );
174
175 settings.m_Source.connection_string.clear();
176 conn = SCH_IO_DATABASE::CreateConnection( settings );
177
178 BOOST_REQUIRE( conn );
179 BOOST_CHECK( !conn->IsConnected() );
180}
181
182
187BOOST_AUTO_TEST_CASE( PreviewSymbolsMatchCachedNames )
188{
189 LIBRARY_MANAGER manager;
190 SYMBOL_LIBRARY_ADAPTER adapter( manager );
191
192 SCH_IO_DATABASE plugin;
193 plugin.SetLibraryManagerAdapter( &adapter );
194
195 std::vector<LIB_SYMBOL*> listed;
196 BOOST_REQUIRE_NO_THROW( plugin.EnumerateSymbolLib( listed, wxString( QA_DBLIB_SETTINGS_PATH ) ) );
197
198 std::set<wxString> expected;
199
200 for( LIB_SYMBOL* symbol : listed )
201 {
202 if( symbol->GetName().StartsWith( wxS( "Resistors/" ) ) )
203 expected.insert( symbol->GetName() );
204
205 delete symbol;
206 }
207
208 BOOST_REQUIRE( !expected.empty() );
209
210 const DATABASE_LIB_SETTINGS& settings = *plugin.Settings();
211 auto table = std::find_if( settings.m_Tables.begin(), settings.m_Tables.end(),
212 []( const DATABASE_LIB_TABLE& aTable )
213 {
214 return aTable.name == "Resistors";
215 } );
216 BOOST_REQUIRE( table != settings.m_Tables.end() );
217
218 std::unique_ptr<DATABASE_CONNECTION> conn = SCH_IO_DATABASE::CreateConnection( settings );
219 std::vector<std::string> columns;
220 std::vector<DATABASE_CONNECTION::ROW> rows;
221 BOOST_REQUIRE( conn->SelectSample( table->table, 100000, columns, rows ) );
222
223 std::set<wxString> previewed;
224
225 for( const std::unique_ptr<LIB_SYMBOL>& symbol :
226 SCH_IO_DATABASE::BuildPreviewSymbols( &adapter, *table, settings.m_GloballyUniqueKeys, rows, nullptr ) )
227 {
228 previewed.insert( symbol->GetName() );
229 }
230
231 BOOST_CHECK( previewed == expected );
232}
233
234
std::vector< DATABASE_LIB_TABLE > m_Tables
DATABASE_CACHE_SETTINGS m_Cache
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
virtual const wxString What() const
A composite of Problem() and Where()
virtual bool LoadFromFile(const wxString &aDirectory="")
Loads the backing file from disk and then calls Load()
void SetReadOnly(bool aReadOnly)
Define a library symbol object.
Definition lib_symbol.h:114
A KiCad database library provides both symbol and footprint metadata, so there are "shim" plugins on ...
static std::unique_ptr< DATABASE_CONNECTION > CreateConnection(const DATABASE_LIB_SETTINGS &aSettings, bool aConnectNow=true, const wxString &aBaseDir=wxEmptyString)
Opens a connection described by aSettings, expanding ${CWD} in a connection string to the directory h...
void EnumerateSymbolLib(wxArrayString &aSymbolNameList, const wxString &aLibraryPath, const std::map< std::string, UTF8 > *aProperties=nullptr) override
Populate a list of LIB_SYMBOL alias names contained within the library aLibraryPath.
DATABASE_LIB_SETTINGS * Settings() const
static std::vector< std::unique_ptr< LIB_SYMBOL > > BuildPreviewSymbols(SYMBOL_LIBRARY_ADAPTER *aLibs, const DATABASE_LIB_TABLE &aTable, bool aGloballyUniqueKeys, const std::vector< DATABASE_CONNECTION::ROW > &aRows, PREVIEW_ISSUES *aIssues)
Builds symbols from already-fetched rows exactly as the library would name and load them,...
void SetLibraryManagerAdapter(SYMBOL_LIBRARY_ADAPTER *aAdapter) override
Some library plugins need to interface with other loaded libraries.
LIB_SYMBOL * LoadSymbol(const wxString &aLibraryPath, const wxString &aAliasName, const std::map< std::string, UTF8 > *aProperties=nullptr) override
Load a LIB_SYMBOL object having aPartName from the aLibraryPath containing a library format that this...
An interface to the global shared library manager that is schematic-specific and linked to one projec...
@ FOR_GUI
Definition common.h:93
Abstract pattern-matching tool and implementations.
std::string GetTestDataRootDir()
int max_age
Max age of a cached table before it expires, in seconds.
A database library table will be mapped to a sub-library provided by the database library entry in th...
std::string connection_string
A structure for storing weighted search terms.
@ DESCRIPTION
Field Description of part, i.e. "1/4W 1% Metal Film Resistor".
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
BOOST_AUTO_TEST_SUITE_END()
VECTOR3I expected(15, 30, 45)
BOOST_AUTO_TEST_CASE(UnchangedDataReusesCache)
SCH_IO_DATABASE::cacheLib() must not rebuild its materialized LIB_SYMBOL cache when a re-query return...
BOOST_TEST_MESSAGE("Polyline has "<< chain.PointCount()<< " points")
BOOST_CHECK_EQUAL(result, "25.4")