Refactor code as suggested and add tests.

This commit is contained in:
Christian Rösch 2019-08-10 00:04:12 +02:00 committed by Thomas Lauf
parent f3280966e0
commit 3def4b10c4
2 changed files with 46 additions and 25 deletions

View file

@ -41,7 +41,7 @@ int CmdContinue (
// Gather IDs and TAGs.
std::set <int> ids = cli.getIds();
auto tags = cli.getTags();
auto filter = getFilter (cli);
if (ids.size() > 1)
{
@ -66,33 +66,14 @@ int CmdContinue (
assert (intervals.size () == 1);
to_copy = intervals.front ();
}
else if (!tags.empty())
else if (!filter.tags().empty())
{
// Load the data.
// Note: There is no filter.
Interval filter;
auto tracked = getTracked (database, rules, filter);
for (int i = tracked.size() -1; i >= 0; --i)
{
bool allTagsFound = true;
for (unsigned int t = 0; t < tags.size(); ++t)
{
if (!tracked[i].hasTag(tags[t]))
{
allTagsFound = false;
break;
}
}
if (allTagsFound)
{
to_copy = tracked[i];
break;
}
}
if (tracked.empty())
throw format ("Tags '{1}' do not correspond to any tracking.", joinQuotedIfNeeded (", ", filter.tags()));
if (to_copy.empty())
throw format ("Tags '{1}' do not correspond to any tracking.", joinQuotedIfNeeded (", ", tags));
to_copy = tracked[0];
}
else
{
@ -111,7 +92,6 @@ int CmdContinue (
to_copy = latest;
}
auto filter = cli.getFilter ();
Datetime start_time;
Datetime end_time;

View file

@ -124,6 +124,21 @@ class TestContinue(TestCase):
self.assertIn("Recorded FOO\n", out)
self.assertIn("Tracking BAR FOO\n", out)
def test_continue_with_one_tag_and_intervals_with_multiple_tags(self):
"""Verify that 'continue' with one tag works to specify a matching interval^"""
code, out, err = self.t("start meeting phone 2h ago")
self.assertIn("Tracking meeting phone\n", out)
code, out, err = self.t("start travel customer 1h ago")
self.assertIn("Tracking customer travel\n", out)
code, out, err = self.t("start training travel 30min ago")
self.assertIn("Tracking training travel\n", out)
code, out, err = self.t("continue customer")
self.assertIn("Recorded training travel\n", out)
self.assertIn("Tracking customer travel\n", out)
def test_continue_with_invalid_tag(self):
"""Verify that 'continue' with invalid tag is an error"""
code, out, err = self.t("start FOO 1h ago")
@ -161,6 +176,32 @@ class TestContinue(TestCase):
self.assertIn("Recorded BAR\n", out)
self.assertIn("Tracking FOO\n", out)
def test_continue_with_tag_with_active_tracking(self):
"""Verify that continuing a specified interval stops active tracking"""
code, out, err = self.t("start FOO 1h ago")
self.assertIn("Tracking FOO\n", out)
code, out, err = self.t("start BAR 30min ago")
self.assertIn("Tracking BAR\n", out)
code, out, err = self.t("continue FOO")
self.assertIn("Recorded BAR\n", out)
self.assertIn("Tracking FOO\n", out)
def test_continue_with_tag_and_id_continues_interval_with_id(self):
"""Verify that continuing an interval by specifying its id and some tags works"""
code, out, err = self.t("start FOO 1h ago")
self.assertIn("Tracking FOO\n", out)
code, out, err = self.t("start BAR 30min ago")
self.assertIn("Tracking BAR\n", out)
code, out, err = self.t("stop 15min ago")
self.assertIn("Recorded BAR\n", out)
code, out, err = self.t("continue @2 FOO")
self.assertIn("Tracking FOO\n", out)
def test_continue_with_id_and_date(self):
"""Verify that continuing a specified interval with date continues at given date"""
now_utc = datetime.now().utcnow()