extralite-bundle 2.4 → 2.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,161 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'helper'
4
+
5
+ require 'date'
6
+ require 'tempfile'
7
+
8
+ class ChangesetTest < MiniTest::Test
9
+ def setup
10
+ @db = Extralite::Database.new(':memory:')
11
+ skip if !@db.respond_to?(:track_changes)
12
+
13
+ @db.execute('create table if not exists t (x integer primary key, y, z)')
14
+ end
15
+
16
+ def test_each
17
+ changeset = Extralite::Changeset.new
18
+
19
+ changeset.track(@db, [nil]) do
20
+ @db.execute('insert into t values (1, 2, 3)')
21
+ end
22
+ changes = []
23
+ changeset.each { |*o| changes << o }
24
+ assert_equal [
25
+ [:insert, 't', nil, [1, 2, 3]]
26
+ ], changes
27
+
28
+
29
+ changeset.track(@db, [nil]) do
30
+ @db.execute('update t set y = 22 where x = 1')
31
+ end
32
+ changes = []
33
+ changeset.each { |*o| changes << o }
34
+ assert_equal [
35
+ [:update, 't', [1, 2, nil], [nil, 22, nil]]
36
+ ], changes
37
+
38
+
39
+ changeset.track(@db, [nil]) do
40
+ @db.execute('delete from t where x = 1')
41
+ end
42
+ changes = []
43
+ changeset.each { |*o| changes << o }
44
+ assert_equal [
45
+ [:delete, 't', [1, 22, 3], nil]
46
+ ], changes
47
+ end
48
+
49
+ def test_to_a
50
+ changeset = Extralite::Changeset.new
51
+
52
+ changeset.track(@db, [nil]) do
53
+ @db.execute('insert into t values (1, 2, 3)')
54
+ @db.execute('insert into t values (4, 5, 6)')
55
+ end
56
+ assert_equal [
57
+ [:insert, 't', nil, [1, 2, 3]],
58
+ [:insert, 't', nil, [4, 5, 6]]
59
+ ], changeset.to_a
60
+
61
+
62
+ changeset.track(@db, [nil]) do
63
+ @db.execute('update t set y = 22.22 where z < 10')
64
+ end
65
+ assert_equal [
66
+ [:update, 't', [1, 2, nil], [nil, 22.22, nil]],
67
+ [:update, 't', [4, 5, nil], [nil, 22.22, nil]]
68
+ ], changeset.to_a
69
+
70
+
71
+ changeset.track(@db, [nil]) do
72
+ @db.execute('delete from t where x = 1')
73
+ end
74
+ assert_equal [
75
+ [:delete, 't', [1, 22.22, 3], nil]
76
+ ], changeset.to_a
77
+ end
78
+
79
+ def test_apply
80
+ changeset = Extralite::Changeset.new
81
+
82
+ changeset.track(@db, [:t]) do
83
+ @db.execute('insert into t values (1, 2, 3)')
84
+ @db.execute('insert into t values (4, 5, 6)')
85
+ end
86
+
87
+ db2 = Extralite::Database.new(':memory:')
88
+ db2.execute('create table if not exists t (x integer primary key, y, z)')
89
+
90
+ changeset.apply(db2)
91
+
92
+ assert_equal [
93
+ { x: 1, y: 2, z: 3 },
94
+ { x: 4, y: 5, z: 6 }
95
+ ], db2.query('select * from t')
96
+ end
97
+
98
+ def test_invert
99
+ changeset = Extralite::Changeset.new
100
+
101
+ changeset.track(@db, [:t]) do
102
+ @db.execute('insert into t values (1, 2, 3)')
103
+ @db.execute('insert into t values (4, 5, 6)')
104
+ end
105
+
106
+ db2 = Extralite::Database.new(':memory:')
107
+ db2.execute('create table if not exists t (x integer primary key, y, z)')
108
+
109
+ changeset.apply(db2)
110
+
111
+ assert_equal [
112
+ { x: 1, y: 2, z: 3 },
113
+ { x: 4, y: 5, z: 6 }
114
+ ], db2.query('select * from t')
115
+
116
+ db2.execute('insert into t values (7, 8, 9)')
117
+ inverted = changeset.invert
118
+
119
+ assert_kind_of Extralite::Changeset, inverted
120
+ refute_equal inverted, changeset
121
+
122
+ assert_equal [
123
+ [:delete, 't', [1, 2, 3], nil],
124
+ [:delete, 't', [4, 5, 6], nil],
125
+ ], inverted.to_a
126
+
127
+ inverted.apply(@db)
128
+ assert_equal [], @db.query('select * from t')
129
+ end
130
+
131
+ def test_blob
132
+ changeset = Extralite::Changeset.new
133
+ assert_equal "", changeset.to_blob
134
+
135
+ changeset.track(@db, [:t]) do
136
+ @db.execute('insert into t values (1, 2, 3)')
137
+ @db.execute('insert into t values (4, 5, 6)')
138
+ end
139
+
140
+ blob = changeset.to_blob
141
+ assert_kind_of String, blob
142
+ assert_equal Encoding::ASCII_8BIT, blob.encoding
143
+ assert !blob.empty?
144
+
145
+ c2 = Extralite::Changeset.new
146
+ c2.load(blob)
147
+ assert_equal c2.to_blob, blob
148
+
149
+ assert_equal [
150
+ [:insert, 't', nil, [1, 2, 3]],
151
+ [:insert, 't', nil, [4, 5, 6]]
152
+ ], c2.to_a
153
+ end
154
+
155
+ def test_empty_blob
156
+ changeset = Extralite::Changeset.new
157
+ changeset.load('')
158
+
159
+ assert_raises(Extralite::Error) { changeset.to_a }
160
+ end
161
+ end