Tests: Convert import.t to python

This commit is contained in:
Renato Alves 2015-06-06 00:05:51 +01:00
parent 78525f509d
commit c4ac7dc4c2

View file

@ -1,98 +1,99 @@
#! /usr/bin/env perl #!/usr/bin/env python2.7
################################################################################ # -*- coding: utf-8 -*-
## ###############################################################################
## Copyright 2006 - 2015, Paul Beckingham, Federico Hernandez. #
## # Copyright 2006 - 2015, Paul Beckingham, Federico Hernandez.
## Permission is hereby granted, free of charge, to any person obtaining a copy #
## of this software and associated documentation files (the "Software"), to deal # Permission is hereby granted, free of charge, to any person obtaining a copy
## in the Software without restriction, including without limitation the rights # of this software and associated documentation files (the "Software"), to deal
## to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # in the Software without restriction, including without limitation the rights
## copies of the Software, and to permit persons to whom the Software is # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
## furnished to do so, subject to the following conditions: # copies of the Software, and to permit persons to whom the Software is
## # furnished to do so, subject to the following conditions:
## The above copyright notice and this permission notice shall be included #
## in all copies or substantial portions of the Software. # The above copyright notice and this permission notice shall be included
## # in all copies or substantial portions of the Software.
## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS #
## OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
## FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
## THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
## LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
## OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
## SOFTWARE. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
## # SOFTWARE.
## http://www.opensource.org/licenses/mit-license.php #
## # http://www.opensource.org/licenses/mit-license.php
################################################################################ #
###############################################################################
use strict; import sys
use warnings; import os
use Test::More tests => 8; import unittest
# Ensure python finds the local simpletap module
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
# Ensure environment has no influence. from basetest import Task, TestCase
delete $ENV{'TASKDATA'}; from basetest.utils import mkstemp
delete $ENV{'TASKRC'};
# Create the rc file.
if (open my $fh, '>', 'import.rc')
{
print $fh "data.location=.\n",
"dateformat=m/d/Y\n";
close $fh;
}
# Create import file. class TestImport(TestCase):
if (open my $fh, '>', 'import.txt') def setUp(self):
{ self.t = Task()
print $fh <<EOF; self.t.config("dateformat", "m/d/Y")
{"uuid":"00000000-0000-0000-0000-000000000000","description":"zero","project":"A","status":"pending","entry":"1234567889"}
self.data1 = """{"uuid":"00000000-0000-0000-0000-000000000000","description":"zero","project":"A","status":"pending","entry":"1234567889"}
{"uuid":"11111111-1111-1111-1111-111111111111","description":"one","project":"B","status":"pending","entry":"1234567889"} {"uuid":"11111111-1111-1111-1111-111111111111","description":"one","project":"B","status":"pending","entry":"1234567889"}
{"uuid":"22222222-2222-2222-2222-222222222222","description":"two","status":"completed","entry":"1234524689","end":"1234524690"} {"uuid":"22222222-2222-2222-2222-222222222222","description":"two","status":"completed","entry":"1234524689","end":"1234524690"}
EOF """
close $fh;
}
my $output = qx{../src/task rc:import.rc import import.txt 2>&1 >/dev/null}; self.data2 = """{"uuid":"44444444-4444-4444-4444-444444444444","description":"three","status":"pending","entry":"1234567889"}
like ($output, qr/Imported 3 tasks\./, 'no errors'); """
# Imported 3 tasks successfully.
$output = qx{../src/task rc:import.rc list 2>&1}; def assertData1(self):
# ID Project Pri Due Active Age Description code, out, err = self.t(("list",))
# -- ------- --- --- ------ ------- ----------- self.assertRegexpMatches(out, "1.+A.+zero")
# 1 A 1.5 yrs zero self.assertRegexpMatches(out, "2.+B.+one")
# 2 B 1.5 yrs one self.assertNotIn("two", out)
#
# 2 tasks
like ($output, qr/1.+A.+zero/, 't1 present'); code, out, err = self.t(("completed",))
like ($output, qr/2.+B.+one/, 't2 present'); self.assertNotIn("zero", out)
unlike ($output, qr/3.+two/, 't3 missing'); self.assertNotIn("one", out)
# complete has completion date as 1st column
self.assertRegexpMatches(out, "2/13/2009.+two")
$output = qx{../src/task rc:import.rc completed 2>&1}; def assertData2(self):
# Complete Project Pri Age Description code, out, err = self.t(("list",))
# --------- ------- --- ------- ----------- self.assertRegexpMatches(out, "3.+three")
# 2/13/2009 1.5 yrs two
#
# 1 task
unlike ($output, qr/1.+A.+zero/, 't1 missing'); def test_import_stdin(self):
unlike ($output, qr/2.+B.+one/, 't2 missing'); """Import from stdin"""
like ($output, qr/2\/13\/2009.+two/, 't3 present'); code, out, err = self.t(("import", "-"), input=self.data1)
self.assertIn("Imported 3 tasks", err)
# Create import file. self.assertData1()
if (open my $fh, '>', 'import2.txt')
{
print $fh <<EOF;
{"uuid":"44444444-4444-4444-4444-444444444444","description":"three","status":"pending","entry":"1234567889"}
EOF
close $fh;
}
$output = qx{../src/task rc:import.rc import import2.txt 2>&1 >/dev/null}; def test_import_file(self):
like ($output, qr/Imported 1 tasks\./, 'no errors'); """Import from a file"""
# Imported 1 tasks successfully. filename = mkstemp(self.data1)
# Cleanup. code, out, err = self.t(("import", filename))
unlink qw(import.txt import2.txt pending.data completed.data undo.data backlog.data import.rc); self.assertIn("Imported 3 tasks", err)
exit 0;
self.assertData1()
def test_double_import(self):
"""Multiple imports persist data"""
code, out, err = self.t(("import", "-"), input=self.data1)
self.assertIn("Imported 3 tasks", err)
code, out, err = self.t(("import", "-"), input=self.data2)
self.assertIn("Imported 1 tasks", err)
self.assertData1()
self.assertData2()
if __name__ == "__main__":
from simpletap import TAPTestRunner
unittest.main(testRunner=TAPTestRunner())
# vim: ai sts=4 et sw=4