|
| 1 | +# encoding: utf-8 |
| 2 | +require "logstash/plugin_mixins/jdbc/value_tracking" |
| 3 | +require "tempfile" |
| 4 | + |
| 5 | +module LogStash module PluginMixins module Jdbc |
| 6 | + describe ValueTracking do |
| 7 | + context "#load_yaml" do |
| 8 | + |
| 9 | + context "with date string" do |
| 10 | + let(:yaml_date_source) { "--- !ruby/object:DateTime '2023-06-15 09:59:30.558000000 +02:00'\n" } |
| 11 | + |
| 12 | + it "should be loaded" do |
| 13 | + parsed_date = LogStash::PluginMixins::Jdbc::ValueTracking.load_yaml(yaml_date_source) |
| 14 | + expect(parsed_date.class).to eq DateTime |
| 15 | + expect(parsed_date.year).to eq 2023 |
| 16 | + expect(parsed_date.month).to eq 6 |
| 17 | + expect(parsed_date.day).to eq 15 |
| 18 | + end |
| 19 | + end |
| 20 | + |
| 21 | + context "with time string" do |
| 22 | + let(:yaml_time_source) { "--- 2023-06-15 15:28:15.227874000 +02:00\n" } |
| 23 | + |
| 24 | + it "should be loaded" do |
| 25 | + parsed_time = LogStash::PluginMixins::Jdbc::ValueTracking.load_yaml(yaml_time_source) |
| 26 | + expect(parsed_time.class).to eq Time |
| 27 | + expect(parsed_time.year).to eq 2023 |
| 28 | + expect(parsed_time.month).to eq 6 |
| 29 | + expect(parsed_time.day).to eq 15 |
| 30 | + expect(parsed_time.hour).to eq 15 |
| 31 | + expect(parsed_time.min).to eq 28 |
| 32 | + expect(parsed_time.sec).to eq 15 |
| 33 | + end |
| 34 | + end |
| 35 | + |
| 36 | + context "with date string" do |
| 37 | + let(:yaml_bigdecimal_source) { "--- !ruby/object:BigDecimal '0:0.1e1'\n" } |
| 38 | + |
| 39 | + it "should be loaded" do |
| 40 | + parsed_bigdecimal = LogStash::PluginMixins::Jdbc::ValueTracking.load_yaml(yaml_bigdecimal_source) |
| 41 | + expect(parsed_bigdecimal.class).to eq BigDecimal |
| 42 | + expect(parsed_bigdecimal.to_i).to eq 1 |
| 43 | + end |
| 44 | + end |
| 45 | + end |
| 46 | + |
| 47 | + context "#build_last_value_tracker" do |
| 48 | + |
| 49 | + let(:plugin) { double("fake plugin") } |
| 50 | + let(:temp_file) { Tempfile.new('last_run_tracker') } |
| 51 | + |
| 52 | + before(:each) do |
| 53 | + allow(plugin).to receive(:record_last_run).and_return(true) |
| 54 | + allow(plugin).to receive(:clean_run).and_return(false) |
| 55 | + allow(plugin).to receive(:last_run_metadata_file_path).and_return(temp_file.path) |
| 56 | + end |
| 57 | + |
| 58 | + context "create numerical tracker" do |
| 59 | + before(:each) do |
| 60 | + allow(plugin).to receive(:use_column_value).and_return(true) |
| 61 | + allow(plugin).to receive(:tracking_column_type).and_return("numeric") |
| 62 | + end |
| 63 | + |
| 64 | + it "should write correctly" do |
| 65 | + tracker = ValueTracking.build_last_value_tracker(plugin) |
| 66 | + tracker.set_value(1) |
| 67 | + tracker.write |
| 68 | + |
| 69 | + temp_file.rewind |
| 70 | + v = ValueTracking.load_yaml(::File.read(temp_file.path)) |
| 71 | + expect(v).to eq 1 |
| 72 | + end |
| 73 | + end |
| 74 | + |
| 75 | + context "create date time tracker" do |
| 76 | + before(:each) do |
| 77 | + allow(plugin).to receive(:use_column_value).and_return(false) |
| 78 | + allow(plugin).to receive(:jdbc_default_timezone).and_return(:something_not_nil) |
| 79 | + end |
| 80 | + |
| 81 | + it "should write correctly" do |
| 82 | + tracker = ValueTracking.build_last_value_tracker(plugin) |
| 83 | + tracker.set_value("2023-06-15T15:28:15+02:00") |
| 84 | + tracker.write |
| 85 | + |
| 86 | + temp_file.rewind |
| 87 | + v = ValueTracking.load_yaml(::File.read(temp_file.path)) |
| 88 | + expect(v.class).to eq DateTime |
| 89 | + expect(v.year).to eq 2023 |
| 90 | + end |
| 91 | + end |
| 92 | + |
| 93 | + context "create time tracker" do |
| 94 | + before(:each) do |
| 95 | + allow(plugin).to receive(:use_column_value).and_return(false) |
| 96 | + allow(plugin).to receive(:jdbc_default_timezone).and_return(nil) |
| 97 | + end |
| 98 | + |
| 99 | + it "should write correctly" do |
| 100 | + tracker = ValueTracking.build_last_value_tracker(plugin) |
| 101 | + tracker.set_value("2023-06-15T15:28:15+02:00") |
| 102 | + tracker.write |
| 103 | + |
| 104 | + temp_file.rewind |
| 105 | + v = ValueTracking.load_yaml(::File.read(temp_file.path)) |
| 106 | + expect(v.class).to eq Time |
| 107 | + expect(v.min).to eq 28 |
| 108 | + end |
| 109 | + end |
| 110 | + |
| 111 | + end |
| 112 | + end |
| 113 | +end end end |
0 commit comments