KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_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 (C) 2022 Jon Evans <[email protected]>
5* Copyright The KiCad Developers, see AUTHORS.TXT for contributors.
6*
7* This program is free software; you can redistribute it and/or
8* modify it under the terms of the GNU General Public License
9* as published by the Free Software Foundation; either version 2
10* of the License, or (at your option) any later version.
11*
12* This program is distributed in the hope that it will be useful,
13* but WITHOUT ANY WARRANTY; without even the implied warranty of
14* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15* GNU General Public License for more details.
16*
17* You should have received a copy of the GNU General Public License
18* along with this program. If not, see <https://www.gnu.org/licenses/>.
19*/
20
21#include <filesystem>
22#include <random>
23
24#include <fmt/format.h>
25#include <boost/test/unit_test.hpp>
26#include <nanodbc/nanodbc.h>
27
29
30BOOST_AUTO_TEST_SUITE( Database )
31
32
34{
35 std::string cs = fmt::format( "Driver={{SQLite3}};Database={}/database.sqlite",
36 QA_DATABASE_FILE_LOCATION );
37
38 // Construct and connect
39 DATABASE_CONNECTION dc( cs, 2, false );
40 BOOST_CHECK_NO_THROW( dc.Connect() );
41 BOOST_CHECK( dc.IsConnected() );
42
43 dc.Disconnect();
44 BOOST_CHECK( !dc.IsConnected() );
45
46 dc.Connect();
47 BOOST_CHECK( dc.IsConnected() );
48
49 dc.Disconnect();
50
51 // Scoped connection should self-disconnect
52 {
53 DATABASE_CONNECTION dc2( cs, 2 );
54 }
55
56 dc.Connect();
57 dc.CacheTableInfo( "Resistors", { "Part ID", "MPN" } );
58 dc.CacheTableInfo( "Capacitors", { "Part ID", "Cost" } );
59 BOOST_CHECK( dc.IsConnected() );
60
62
63 BOOST_CHECK( dc.SelectOne( "Resistors", std::make_pair( "Part ID", "RES-001" ), result ) );
64
65 BOOST_CHECK( !result.empty() );
66 BOOST_CHECK( result.count( "MPN" ) );
67 BOOST_CHECK_NO_THROW( std::any_cast<std::string>( result.at( "MPN" ) ) );
68 BOOST_CHECK_EQUAL( std::any_cast<std::string>( result.at( "MPN" ) ), "RC0603FR-0710KL" );
69
70 BOOST_CHECK( dc.SelectOne( "Capacitors", std::make_pair( "Part ID", "CAP-003" ), result ) );
71
72 BOOST_CHECK( !result.empty() );
73 BOOST_CHECK( result.count( "Cost" ) );
74 BOOST_CHECK_NO_THROW( std::any_cast<std::string>( result.at( "Cost" ) ) );
75 BOOST_CHECK_EQUAL( std::any_cast<std::string>( result.at( "Cost" ) ), "1.95" );
76}
77
78
88BOOST_AUTO_TEST_CASE( GeneratedColumns )
89{
90 std::string cs = fmt::format( "Driver={{SQLite3}};Database={}/database.sqlite",
91 QA_DATABASE_FILE_LOCATION );
92
93 DATABASE_CONNECTION dc( cs, 2 );
94 BOOST_CHECK( dc.IsConnected() );
95
96 // Cache table info including the generated column "FullDesc"
97 // The SQLite ODBC driver won't report this column via SQLColumns,
98 // but our fix should add it to the cache anyway
99 dc.CacheTableInfo( "GeneratedTest", { "part id", "value", "resistance", "fulldesc" } );
100
102
103 BOOST_CHECK( dc.SelectOne( "GeneratedTest", std::make_pair( "Part ID", "GEN-001" ), result ) );
104 BOOST_CHECK( !result.empty() );
105
106 // Verify we can read the generated column value
107 BOOST_CHECK( result.count( "FullDesc" ) );
108 BOOST_CHECK_NO_THROW( std::any_cast<std::string>( result.at( "FullDesc" ) ) );
109 BOOST_CHECK_EQUAL( std::any_cast<std::string>( result.at( "FullDesc" ) ), "10K - 10000ohm" );
110
111 // Also verify regular columns still work
112 BOOST_CHECK( result.count( "Value" ) );
113 BOOST_CHECK_EQUAL( std::any_cast<std::string>( result.at( "Value" ) ), "10K" );
114}
115
116
119{
121 m_path( std::filesystem::temp_directory_path()
122 / fmt::format( "kicad_qa_{}.sqlite", std::random_device()() ) )
123 {
124 }
125
127 {
128 std::error_code ec;
129 std::filesystem::remove( m_path, ec );
130 }
131
132 std::string ConnectionString() const
133 {
134 return fmt::format( "Driver={{SQLite3}};Database={}", m_path.string() );
135 }
136
137 void Execute( const std::string& aSql ) const
138 {
139 std::string cs = ConnectionString();
140 nanodbc::connection conn( nanodbc::string( cs.begin(), cs.end() ) );
141 nanodbc::execute( conn, nanodbc::string( aSql.begin(), aSql.end() ) );
142 }
143
144 std::filesystem::path m_path;
145};
146
147
148// Embedded quote characters in table and column names must be doubled in the generated SQL
149BOOST_AUTO_TEST_CASE( QuoteCharactersInIdentifiers )
150{
152 db.Execute( "CREATE TABLE \"Part \"\"A\"\"\" (\"Part ID\" TEXT, \"Va\"\"lue\" TEXT)" );
153 db.Execute( "INSERT INTO \"Part \"\"A\"\"\" VALUES ('P1', '10K'), ('P2', '22K')" );
154
155 const std::string table = "Part \"A\"";
156 const std::string column = "Va\"lue";
157
158 {
161 BOOST_REQUIRE( dc.CacheTableInfo( table, { "Part ID", column } ) );
162
163 std::vector<DATABASE_CONNECTION::ROW> rows;
164 BOOST_REQUIRE_MESSAGE( dc.SelectAll( table, "Part ID", rows ), dc.GetLastError() );
165 BOOST_CHECK_EQUAL( rows.size(), 2 );
166 }
167
168 {
171 BOOST_REQUIRE( dc.CacheTableInfo( table, { "Part ID", column } ) );
172
173 // A zero-size row cache forces the direct query
174 dc.SetCacheParams( 0, 0 );
175
177 BOOST_REQUIRE_MESSAGE( dc.SelectOne( table, std::make_pair( "Part ID", "P2" ), result ), dc.GetLastError() );
178 BOOST_CHECK_EQUAL( std::any_cast<std::string>( result.at( column ) ), "22K" );
179 }
180}
181
182
183// A table name is a catalog search pattern, so underscore and percent must match only themselves
184BOOST_AUTO_TEST_CASE( PatternCharactersInTableNames )
185{
187
188 // The distractors come first and match the unescaped pattern "Part_%Data"
189 db.Execute( "CREATE TABLE PartAData (\"Part ID\" TEXT, Value TEXT)" );
190 db.Execute( "CREATE TABLE PartABCData (\"Part ID\" TEXT, Value TEXT)" );
191 db.Execute( "CREATE TABLE \"Part_%Data\" (\"Part ID\" TEXT, Value TEXT)" );
192 db.Execute( "INSERT INTO PartAData VALUES ('P1', 'wrong A')" );
193 db.Execute( "INSERT INTO PartABCData VALUES ('P1', 'wrong ABC')" );
194 db.Execute( "INSERT INTO \"Part_%Data\" VALUES ('P1', 'right')" );
195
198 BOOST_REQUIRE( dc.CacheTableInfo( "Part_%Data", { "Part ID", "Value" } ) );
199
201 BOOST_REQUIRE_MESSAGE( dc.SelectOne( "Part_%Data", std::make_pair( "Part ID", "P1" ), result ), dc.GetLastError() );
202 BOOST_CHECK_EQUAL( std::any_cast<std::string>( result.at( "Value" ) ), "right" );
203}
204
205
std::map< std::string, std::any > ROW
void SetCacheParams(int aMaxSize, int aMaxAge)
bool SelectAll(const std::string &aTable, const std::string &aKey, std::vector< ROW > &aResults)
Retrieves all rows from a database table.
bool SelectOne(const std::string &aTable, const std::pair< std::string, std::string > &aWhere, ROW &aResult)
Retrieves a single row from a database table.
std::string GetLastError() const
bool CacheTableInfo(const std::string &aTable, const std::set< std::string > &aRequiredColumns, const std::set< std::string > &aOptionalColumns={})
Caches schema information for a table so that subsequent queries know which columns exist.
STL namespace.
A throwaway SQLite database whose tables are created by the test.
void Execute(const std::string &aSql) const
std::filesystem::path m_path
std::string ConnectionString() const
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_AUTO_TEST_CASE(Connect)
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
BOOST_AUTO_TEST_SUITE_END()
wxString result
Test unit parsing edge cases and error handling.
BOOST_CHECK_EQUAL(result, "25.4")