1+ /*
2+ * QQQ - Low-code Application Framework for Engineers.
3+ * Copyright (C) 2021-2024. Kingsrook, LLC
4+ * 651 N Broad St Ste 205 # 6917 | Middletown DE 19709 | United States
5+ * contact@kingsrook.com
6+ * https://github.com/Kingsrook/
7+ *
8+ * This program is free software: you can redistribute it and/or modify
9+ * it under the terms of the GNU Affero General Public License as
10+ * published by the Free Software Foundation, either version 3 of the
11+ * License, or (at your option) any later version.
12+ *
13+ * This program is distributed in the hope that it will be useful,
14+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+ * GNU Affero General Public License for more details.
17+ *
18+ * You should have received a copy of the GNU Affero General Public License
19+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
20+ */
21+
22+ package com .kingsrook .qqq .backend .core .actions .tables .helpers ;
23+
24+
25+ import java .io .Serializable ;
26+ import java .util .List ;
27+ import java .util .Map ;
28+ import com .kingsrook .qqq .backend .core .BaseTest ;
29+ import com .kingsrook .qqq .backend .core .actions .tables .InsertAction ;
30+ import com .kingsrook .qqq .backend .core .context .QContext ;
31+ import com .kingsrook .qqq .backend .core .exceptions .QException ;
32+ import com .kingsrook .qqq .backend .core .model .actions .tables .insert .InsertInput ;
33+ import com .kingsrook .qqq .backend .core .model .actions .tables .insert .InsertOutput ;
34+ import com .kingsrook .qqq .backend .core .model .data .QRecord ;
35+ import com .kingsrook .qqq .backend .core .model .metadata .tables .QTableMetaData ;
36+ import com .kingsrook .qqq .backend .core .modules .backend .implementations .memory .MemoryRecordStore ;
37+ import com .kingsrook .qqq .backend .core .utils .TestUtils ;
38+ import org .junit .jupiter .api .AfterAll ;
39+ import org .junit .jupiter .api .AfterEach ;
40+ import org .junit .jupiter .api .BeforeAll ;
41+ import org .junit .jupiter .api .BeforeEach ;
42+ import org .junit .jupiter .api .Test ;
43+ import static org .junit .jupiter .api .Assertions .assertEquals ;
44+
45+
46+ /*******************************************************************************
47+ ** Unit test for UniqueKeyHelper
48+ *******************************************************************************/
49+ class UniqueKeyHelperTest extends BaseTest
50+ {
51+ private static Integer originalPageSize ;
52+
53+ /*******************************************************************************
54+ **
55+ *******************************************************************************/
56+ @ BeforeAll
57+ static void beforeAll ()
58+ {
59+ originalPageSize = UniqueKeyHelper .getPageSize ();
60+ UniqueKeyHelper .setPageSize (5 );
61+ }
62+
63+
64+
65+ /*******************************************************************************
66+ **
67+ *******************************************************************************/
68+ @ AfterAll
69+ static void afterAll ()
70+ {
71+ UniqueKeyHelper .setPageSize (originalPageSize );
72+ }
73+
74+
75+
76+ /*******************************************************************************
77+ **
78+ *******************************************************************************/
79+ @ BeforeEach
80+ @ AfterEach
81+ void beforeAndAfterEach ()
82+ {
83+ MemoryRecordStore .fullReset ();
84+ }
85+
86+
87+
88+ /*******************************************************************************
89+ **
90+ *******************************************************************************/
91+ @ Test
92+ void testUniqueKey () throws QException
93+ {
94+ List <QRecord > recordsWithKey1Equals1AndKey2In1Through10 = List .of (
95+ new QRecord ().withValue ("key1" , 1 ).withValue ("key2" , 1 ),
96+ new QRecord ().withValue ("key1" , 1 ).withValue ("key2" , 2 ),
97+ new QRecord ().withValue ("key1" , 1 ).withValue ("key2" , 3 ),
98+ new QRecord ().withValue ("key1" , 1 ).withValue ("key2" , 4 ),
99+ new QRecord ().withValue ("key1" , 1 ).withValue ("key2" , 5 ),
100+ new QRecord ().withValue ("key1" , 1 ).withValue ("key2" , 6 ),
101+ new QRecord ().withValue ("key1" , 1 ).withValue ("key2" , 7 ),
102+ new QRecord ().withValue ("key1" , 1 ).withValue ("key2" , 8 ),
103+ new QRecord ().withValue ("key1" , 1 ).withValue ("key2" , 9 ),
104+ new QRecord ().withValue ("key1" , 1 ).withValue ("key2" , 10 )
105+ );
106+
107+ InsertInput insertInput = new InsertInput ();
108+ insertInput .setTableName (TestUtils .TABLE_NAME_TWO_KEYS );
109+ insertInput .setRecords (recordsWithKey1Equals1AndKey2In1Through10 );
110+ InsertOutput insertOutput = new InsertAction ().execute (insertInput );
111+
112+ MemoryRecordStore .resetStatistics ();
113+ MemoryRecordStore .setCollectStatistics (true );
114+
115+ QTableMetaData table = QContext .getQInstance ().getTable (TestUtils .TABLE_NAME_TWO_KEYS );
116+ Map <List <Serializable >, Serializable > existingKeys = UniqueKeyHelper .getExistingKeys (null , table , recordsWithKey1Equals1AndKey2In1Through10 , table .getUniqueKeys ().get (0 ), false );
117+ assertEquals (recordsWithKey1Equals1AndKey2In1Through10 .size (), existingKeys .size ());
118+
119+ assertEquals (2 , MemoryRecordStore .getStatistics ().get (MemoryRecordStore .STAT_QUERIES_RAN ));
120+ }
121+
122+
123+ }
0 commit comments