summaryrefslogtreecommitdiff
path: root/spec/PersonalRecords_spec.rb
blob: c23010b2aca146233581415c57b5c15db4545950 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#!/usr/bin/env ruby -w
# encoding: UTF-8
#
# = PostRunner_spec.rb -- PostRunner - Manage the data from your Garmin sport devices.
#
# Copyright (c) 2014, 2015, 2016 by Chris Schlaeger <cs@taskjuggler.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of version 2 of the GNU General Public License as
# published by the Free Software Foundation.
#

require 'spec_helper'
require 'perobs'

require 'fit4ruby/FileNameCoder'
require 'postrunner/FitFileStore'
require 'postrunner/PersonalRecords'

describe PostRunner::PersonalRecords do

  class Mock_Activity < PEROBS::Object

    po_attr :name, :fit_file_name

    def initialize(store, name = nil)
      super(store)
      init_attr(:name, name)
      init_attr(:fit_file_name, name)
    end

  end

  class Mock_FitFileStore < PEROBS::Object

    po_attr :activities

    def initialize(store)
      super
      init_attr(:activities, @store.new(PEROBS::Array))
    end

    def add_activity(a)
      @activities << a
    end

    def ref_by_activity(a)
      @activities.index(a) + 1
    end

  end

  before(:all) do
    @log = StringIO.new
    Fit4Ruby::Log.open(@log)
    @work_dir = tmp_dir_name(__FILE__)
    Dir.mkdir(@work_dir)

    # Create the FitFileStore
    @store = PEROBS::Store.new(File.join(@work_dir, 'db'))
    @store['config'] = @store.new(PEROBS::Hash)
    @store['config']['data_dir'] = @work_dir
    @ffs = @store['file_store'] = @store.new(Mock_FitFileStore)
    @records = @store['records'] = @store.new(PostRunner::PersonalRecords)
  end

  after(:all) do
    FileUtils.rm_rf(@work_dir)
  end

  it 'should initialize properly' do
    expect(@records.to_s).to eq('')
  end

  it 'should register a record' do
    a = @store.new(Mock_Activity, 'Activity 1')
    @ffs.add_activity(a)
    t = Time.parse('2014-11-08T09:16:00')
    expect(@records.register_result(a, 'running', 5000.0, 20 * 60,
                                    t)).to be true
    expect(@records.register_result(a, 'running', 5000.0, nil, t)).to be true
    expect(@records.activity_records(a).length).to eq(4)
    expect(tables_to_arrays(@records.to_s)).to eq([
      [["5 km", "0:20:00", "4:00", "1", "Activity 1", "2014-11-08"],
       ["Longest Distance", "5.000 km", "-", "1", "Activity 1", "2014-11-08"]],
      [["5 km", "0:20:00", "4:00", "1", "Activity 1", "2014-11-08"],
       ["Longest Distance", "5.000 km", "-", "1", "Activity 1", "2014-11-08"]]
    ])
  end

  it 'should register another record' do
    a = @store.new(Mock_Activity, 'Activity 2')
    @ffs.add_activity(a)
    t = Time.parse('2014-11-09T09:16:00')
    expect(@records.register_result(a, 'running', 10000.0,
                                    42 * 60, t)).to be true
    expect(@records.register_result(a, 'running', 10000.0,
                                    nil, t)).to be true
    expect(@records.activity_records(a).length).to eq(4)
    expect(tables_to_arrays(@records.to_s)).to eq([
      [["5 km", "0:20:00", "4:00", "1", "Activity 1", "2014-11-08"],
       ["10 km", "0:42:00", "4:12", "2", "Activity 2", "2014-11-09"],
       ["Longest Distance", "10.000 km", "-", "2", "Activity 2", "2014-11-09"]],
      [["5 km", "0:20:00", "4:00", "1", "Activity 1", "2014-11-08"],
       ["10 km", "0:42:00", "4:12", "2", "Activity 2", "2014-11-09"],
       ["Longest Distance", "10.000 km", "-", "2", "Activity 2", "2014-11-09"]],
    ])
  end

  it 'should replace an old record with a new one' do
    a = @store.new(Mock_Activity, 'Activity 3')
    @ffs.add_activity(a)
    t = Time.parse('2014-11-11T09:16:00')
    expect(@records.register_result(a, 'running', 5000.0,
                                    19 * 60, t)).to be true
    expect(@records.activity_records(a).length).to eq (2)
    expect(@records.activity_records(@ffs.activities[0]).length).to eq(0)
    expect(tables_to_arrays(@records.to_s)).to eq([
      [["5 km", "0:19:00", "3:47", "3", "Activity 3", "2014-11-11"],
       ["10 km", "0:42:00", "4:12", "2", "Activity 2", "2014-11-09"],
       ["Longest Distance", "10.000 km", "-", "2", "Activity 2", "2014-11-09"]],
      [["5 km", "0:19:00", "3:47", "3", "Activity 3", "2014-11-11"],
       ["10 km", "0:42:00", "4:12", "2", "Activity 2", "2014-11-09"],
       ["Longest Distance", "10.000 km", "-", "2", "Activity 2", "2014-11-09"]],
    ])
  end

  it 'should add a new table for a new year' do
    a = @store.new(Mock_Activity, 'Activity 4')
    @ffs.add_activity(a)
    t = Time.parse('2015-01-01T06:00:00')
    expect(@records.register_result(a, 'running', 5000.0,
                                    21 * 60, t)).to be true
    expect(@records.activity_records(a).length).to eq(1)
    expect(tables_to_arrays(@records.to_s)).to eq([
      [["5 km", "0:19:00", "3:47", "3", "Activity 3", "2014-11-11"],
       ["10 km", "0:42:00", "4:12", "2", "Activity 2", "2014-11-09"],
       ["Longest Distance", "10.000 km", "-", "2", "Activity 2", "2014-11-09"]],
      [["5 km", "0:21:00", "4:12", "4", "Activity 4", "2015-01-01"]],
      [["5 km", "0:19:00", "3:47", "3", "Activity 3", "2014-11-11"],
       ["10 km", "0:42:00", "4:12", "2", "Activity 2", "2014-11-09"],
       ["Longest Distance", "10.000 km", "-", "2", "Activity 2", "2014-11-09"]],
    ])
  end

  it 'should not add a new record for poor result' do
    a = @store.new(Mock_Activity, 'Activity 5')
    @ffs.add_activity(a)
    t = Time.parse('2015-01-02T10:00:00')
    expect(@records.register_result(a, 'running', 5000.0, 22 * 60,
                                    t)).to be false
    expect(@records.activity_records(a).length).to eq(0)
  end

  it 'should not delete a record for non-record activity' do
    expect(@records.delete_activity(@ffs.activities[0])).to be false
  end

  it 'should delete a record for a record activity' do
    expect(@records.delete_activity(@ffs.activities[2])).to be true
    expect(tables_to_arrays(@records.to_s)).to eq([
      [["10 km", "0:42:00", "4:12", "2", "Activity 2", "2014-11-09"],
       ["Longest Distance", "10.000 km", "-", "2", "Activity 2", "2014-11-09"]],
      [["5 km", "0:21:00", "4:12", "4", "Activity 4", "2015-01-01"]],
      [["10 km", "0:42:00", "4:12", "2", "Activity 2", "2014-11-09"],
       ["Longest Distance", "10.000 km", "-", "2", "Activity 2", "2014-11-09"]],
    ])
  end

  it 'should add a new distance record' do
    a = @store.new(Mock_Activity, 'Activity 6')
    @ffs.add_activity(a)
    t = Time.parse('2015-01-10T07:00:00')
    expect(@records.register_result(a, 'running', 15000.0, nil, t)).to be true
    expect(@records.activity_records(a).length).to eq(2)
    expect(tables_to_arrays(@records.to_s)).to eq([
      [ ["10 km", "0:42:00", "4:12", "2", "Activity 2", "2014-11-09"],
       ["Longest Distance", "15.000 km", "-", "6", "Activity 6", "2015-01-10"]],
      [["5 km", "0:21:00", "4:12", "4", "Activity 4", "2015-01-01"],
       ["Longest Distance", "15.000 km", "-", "6", "Activity 6", "2015-01-10"]],
      [["10 km", "0:42:00", "4:12", "2", "Activity 2", "2014-11-09"],
       ["Longest Distance", "10.000 km", "-", "2", "Activity 2", "2014-11-09"]],
    ])
  end

  it 'should not register a record for a bogus sport' do
    a = @store.new(Mock_Activity, 'Activity 5')
    @ffs.add_activity(a)
    t = Time.parse('2015-04-01T11:11:11')
    expect(@records.register_result(a, 'foobaring', 5000.0, 10 * 60,
                                    t)).to be false
  end

  it 'should not register a record for unknown distance' do
    a = @store.new(Mock_Activity, 'Activity 6')
    @ffs.add_activity(a)
    expect { @records.register_result(a, 'cycling', 42.0, 10 * 60,
             Time.parse('2015-04-01T11:11:11'))}.to raise_error(Fit4Ruby::Error)
  end

  it 'should delete all records' do
    @records.delete_all_records
    expect(@records.to_s).to eq('')
  end

end