@@ -38,6 +38,108 @@ class DatabaseTests: XCTestCase {
3838 XCTAssertTrue ( database. supportsJSON)
3939 }
4040
41+ func testAutoVacuumMode( ) throws {
42+ XCTAssertEqual ( . none, database. autoVacuumMode)
43+
44+ database. autoVacuumMode = . full
45+ XCTAssertEqual ( . full, database. autoVacuumMode)
46+
47+ database. autoVacuumMode = . incremental
48+ XCTAssertEqual ( . incremental, database. autoVacuumMode)
49+ }
50+
51+ func testIncrementalVacuumDoesNotThrowIfModeIsNotIncremental( ) throws {
52+ XCTAssertEqual ( . none, database. autoVacuumMode)
53+ try database. incrementalVacuum ( )
54+ }
55+
56+ func testIncrementalVacuum( ) throws {
57+ func getPageCount( ) throws -> Int {
58+ let result = try database. execute ( raw: " PRAGMA page_count; " ) . first
59+ return try XCTUnwrap ( result ? [ " page_count " ] ? . intValue)
60+ }
61+
62+ database. autoVacuumMode = . incremental
63+
64+ XCTAssertEqual ( 1 , try getPageCount ( ) )
65+
66+ try database. execute ( raw: _createTableWithBlob)
67+
68+ try database. inTransaction { ( db) -> Void in
69+ try ( 0 ..< 1000 ) . forEach { ( index) in
70+ let args : SQLiteArguments = [
71+ " id " : . integer( Int64 ( index) ) , " data " : . data( _textData)
72+ ]
73+ try db. write ( _insertIDAndData, arguments: args)
74+ }
75+ }
76+
77+ XCTAssertGreaterThan ( try getPageCount ( ) , 3 )
78+
79+ try database. write ( " DELETE FROM test; " , arguments: [ : ] )
80+ try database. incrementalVacuum ( )
81+
82+ XCTAssertEqual ( 3 , try getPageCount ( ) )
83+ }
84+
85+ func testIncrementalVacuumWithPageCount( ) throws {
86+ func getPageCount( ) throws -> Int {
87+ let result = try database. execute ( raw: " PRAGMA page_count; " ) . first
88+ return try XCTUnwrap ( result ? [ " page_count " ] ? . intValue)
89+ }
90+
91+ database. autoVacuumMode = . incremental
92+
93+ XCTAssertEqual ( 1 , try getPageCount ( ) )
94+
95+ try database. execute ( raw: _createTableWithBlob)
96+
97+ try database. inTransaction { ( db) -> Void in
98+ try ( 0 ..< 1000 ) . forEach { ( index) in
99+ let args : SQLiteArguments = [
100+ " id " : . integer( Int64 ( index) ) , " data " : . data( _textData)
101+ ]
102+ try db. write ( _insertIDAndData, arguments: args)
103+ }
104+ }
105+
106+ let pageCount = try getPageCount ( )
107+ XCTAssertGreaterThan ( pageCount, 3 )
108+
109+ try database. write ( " DELETE FROM test; " , arguments: [ : ] )
110+ try database. incrementalVacuum ( 2 )
111+
112+ XCTAssertEqual ( pageCount - 2 , try getPageCount ( ) )
113+ }
114+
115+ func testVacuum( ) throws {
116+ func getPageCount( ) throws -> Int {
117+ let result = try database. execute ( raw: " PRAGMA page_count; " ) . first
118+ return try XCTUnwrap ( result ? [ " page_count " ] ? . intValue)
119+ }
120+
121+ XCTAssertEqual ( . none, database. autoVacuumMode)
122+ XCTAssertEqual ( 0 , try getPageCount ( ) )
123+
124+ try database. execute ( raw: _createTableWithBlob)
125+
126+ try database. inTransaction { ( db) -> Void in
127+ try ( 0 ..< 1000 ) . forEach { ( index) in
128+ let args : SQLiteArguments = [
129+ " id " : . integer( Int64 ( index) ) , " data " : . data( _textData)
130+ ]
131+ try db. write ( _insertIDAndData, arguments: args)
132+ }
133+ }
134+
135+ XCTAssertGreaterThan ( try getPageCount ( ) , 2 )
136+
137+ try database. write ( " DELETE FROM test; " , arguments: [ : ] )
138+ try database. vacuum ( )
139+
140+ XCTAssertEqual ( 2 , try getPageCount ( ) )
141+ }
142+
41143 func testCreateTable( ) throws {
42144 XCTAssertNoThrow ( try database. execute ( raw: _createTableWithBlob) )
43145 let tableNames = try database. tables ( )
0 commit comments