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 (C) 2022 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, you may find one here:
19* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20* or you may search the http://www.gnu.org website for the version 2 license,
21* or you may write to the Free Software Foundation, Inc.,
22* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23*/
24
25#include <fmt/core.h>
26#include <boost/test/unit_test.hpp>
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() );
42
43 dc.Disconnect();
44 BOOST_CHECK( !dc.IsConnected() );
45
46 dc.Connect();
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" } );
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
78BOOST_AUTO_TEST_SUITE_END()
bool CacheTableInfo(const std::string &aTable, const std::set< std::string > &aColumns)
std::map< std::string, std::any > ROW
bool SelectOne(const std::string &aTable, const std::pair< std::string, std::string > &aWhere, ROW &aResult)
Retrieves a single row from a database table.
BOOST_CHECK(box.ClosestPointTo(VECTOR2D(0, 0))==VECTOR2D(1, 2))
Test suite for KiCad math code.
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_AUTO_TEST_CASE(Connect)