|
| 1 | +require "test_helper" |
| 2 | + |
| 3 | +class MysqlSpecificTest < ActiveSupport::TestCase |
| 4 | + if defined?(Mysql2) |
| 5 | + def setup |
| 6 | + @instance = ModelWithAllMysqlColumnTypes.new( |
| 7 | + text_field: "some-text", |
| 8 | + integer_field: 2, |
| 9 | + date_field: Date.parse("2026-06-03"), |
| 10 | + time_field: (Time.parse("2026-07-04 04:05:06") if !ActiveSnapshot.config.storage_method_yaml?), |
| 11 | + timestamp_field: Time.parse("2026-08-05 01:02:03"), |
| 12 | + json_field: {string: "bar", number: 2, array: [1,2,3]}, |
| 13 | + #binary_field: "0010010", # binary fields have issues currently |
| 14 | + boolean_field: true, |
| 15 | + enum_field: "bar", |
| 16 | + ) |
| 17 | + |
| 18 | + @instance.save! |
| 19 | + end |
| 20 | + |
| 21 | + def teardown |
| 22 | + end |
| 23 | + |
| 24 | + def test_restore_snapshot_handles_column_serialization |
| 25 | + snapshot = @instance.create_snapshot! |
| 26 | + |
| 27 | + snapshot_item = snapshot.snapshot_items.first! |
| 28 | + |
| 29 | + snapshot_item.restore_item! |
| 30 | + |
| 31 | + assert_equal( |
| 32 | + { |
| 33 | + text_field: "some-text", |
| 34 | + integer_field: 2, |
| 35 | + date_field: Date.parse("2026-06-03"), |
| 36 | + json_field: {"string" => "bar", "number" => 2, "array" => [1,2,3]}, |
| 37 | + binary_field: nil, |
| 38 | + boolean_field: true, |
| 39 | + enum_field: "bar", |
| 40 | + }, |
| 41 | + snapshot_item.item.attributes.symbolize_keys.except(:id, :time_field, :timestamp_field) |
| 42 | + ) |
| 43 | + |
| 44 | + assert_time_match(Time.parse("2026-08-05 01:02:03"), snapshot_item.item.timestamp_field) |
| 45 | + |
| 46 | + if !ActiveSnapshot.config.storage_method_yaml? |
| 47 | + assert_time_match(Time.parse("2000-01-01 04:05:06"), snapshot_item.item.time_field) |
| 48 | + end |
| 49 | + end |
| 50 | + |
| 51 | + def test_diff_handles_column_serialization |
| 52 | + snapshot = @instance.create_snapshot! |
| 53 | + |
| 54 | + @instance.assign_attributes( |
| 55 | + text_field: "other-text", |
| 56 | + integer_field: 3, |
| 57 | + date_field: Date.parse("2026-01-03"), |
| 58 | + time_field: (Time.parse("2026-07-04 07:08:09") if !ActiveSnapshot.config.storage_method_yaml?), |
| 59 | + timestamp_field: Time.parse("2026-03-05 04:05:06"), |
| 60 | + json_field: {string: "foo", number: 1, array: [4,5,6]}, |
| 61 | + #binary_field: "0101010101", |
| 62 | + boolean_field: false, |
| 63 | + enum_field: "foo", |
| 64 | + ) |
| 65 | + |
| 66 | + @instance.save! |
| 67 | + |
| 68 | + diff = ActiveSnapshot::Snapshot.diff(snapshot, @instance) |
| 69 | + |
| 70 | + assert_equal(["some-text","other-text"], diff.first[:changes][:text_field]) |
| 71 | + assert_equal([2,3], diff.first[:changes][:integer_field]) |
| 72 | + assert_equal([Date.parse("2026-06-03"), Date.parse("2026-01-03")], diff.first[:changes][:date_field]) |
| 73 | + assert_equal([{"string" => "bar", "number" => 2, "array" => [1, 2, 3]}, {"string" => "foo", "number" => 1, "array" => [4, 5, 6]}], diff.first[:changes][:json_field]) |
| 74 | + assert_equal(nil, diff.first[:changes][:binary_field]) |
| 75 | + assert_equal([true, false], diff.first[:changes][:boolean_field]) |
| 76 | + assert_equal(["bar", "foo"], diff.first[:changes][:enum_field]) |
| 77 | + |
| 78 | + assert_time_match(Time.parse("2026-08-05 01:02:03"), diff.first[:changes][:timestamp_field].first) |
| 79 | + assert_time_match(Time.parse("2026-03-05 04:05:06"), diff.first[:changes][:timestamp_field].last) |
| 80 | + |
| 81 | + if !ActiveSnapshot.config.storage_method_yaml? |
| 82 | + assert_time_match(Time.parse("2000-01-01 04:05:06"), diff.first[:changes][:time_field].first) |
| 83 | + assert_time_match(Time.parse("2000-01-01 07:08:09"), diff.first[:changes][:time_field].last) |
| 84 | + end |
| 85 | + end |
| 86 | + end |
| 87 | +end |
0 commit comments