class HomeDirTest(unittest.TestCase):

    def setUp(self):
        self.temp_dir = TempDirectory(create=True)
        self.home = PathHomeDir(self.temp_dir.path)

    def tearDown(self):
        self.temp_dir.cleanup()

    def test_read(self):
        self.temp_dir.write("filename", "contents")
        self.assertEquals(self.home.read("filename"), "contents")

    def test_write(self):
        self.temp_dir.write("existing_file", "existing_contents")
        self.home.write("new_file", "contents")
        self.home.write("existing_file", "new_contents")
        self.assertEquals(self.temp_dir.read("existing_file"),
                          "new_contents")
        self.assertEquals(self.temp_dir.read("new_file"), "contents")

    def test_config_file(self):
        with collect_outputs() as outputs:
            self.home.write_config_file("new config")
            self.temp_dir.check(".cosmosrc")
            self.assertEquals(self.home.read_config_file(), "new config")
            self.assertIn("Settings saved", outputs.stdout.getvalue())
            file_mode = os.stat(self.temp_dir.getpath(".cosmosrc")).st_mode
            self.assertEquals(file_mode, stat.S_IFREG | stat.S_IRUSR | stat.S_IWUSR)

    def test_override_config_file(self):
        with collect_outputs():
            other_config = self.temp_dir.write("path/other", "config")
            self.assertEquals(
                self.home.read_config_file(filename_override=other_config),
                "config")

    def test_warn_on_unprotected_config_file(self):
        with collect_outputs() as outputs:
            self.home.write_config_file("new config")
            config_path = self.temp_dir.getpath(".cosmosrc")
            os.chmod(config_path, 0777)
            self.home.read_config_file()
            assertFunc = (self.assertNotIn if os.name=='nt' else self.assertIn)
            assertFunc("WARNING", outputs.stderr.getvalue())

    def test_last_cluster(self):
        self.home.write_last_cluster("0000000")
        self.temp_dir.check(".cosmoslast")
        self.assertEquals(self.home.read_last_cluster(), "0000000")
Beispiel #2
0
class HomeDirTest(unittest.TestCase):
    def setUp(self):
        self.temp_dir = TempDirectory(create=True)
        self.home = PathHomeDir(self.temp_dir.path)

    def tearDown(self):
        self.temp_dir.cleanup()

    def test_read(self):
        self.temp_dir.write("filename", "contents")
        self.assertEquals(self.home.read("filename"), "contents")

    def test_write(self):
        self.temp_dir.write("existing_file", "existing_contents")
        self.home.write("new_file", "contents")
        self.home.write("existing_file", "new_contents")
        self.assertEquals(self.temp_dir.read("existing_file"), "new_contents")
        self.assertEquals(self.temp_dir.read("new_file"), "contents")

    def test_config_file(self):
        with collect_outputs() as outputs:
            self.home.write_config_file("new config")
            self.temp_dir.check(".cosmosrc")
            self.assertEquals(self.home.read_config_file(), "new config")
            self.assertIn("Settings saved", outputs.stdout.getvalue())
            file_mode = os.stat(self.temp_dir.getpath(".cosmosrc")).st_mode
            self.assertEquals(file_mode,
                              stat.S_IFREG | stat.S_IRUSR | stat.S_IWUSR)

    def test_override_config_file(self):
        with collect_outputs():
            other_config = self.temp_dir.write("path/other", "config")
            self.assertEquals(
                self.home.read_config_file(filename_override=other_config),
                "config")

    def test_warn_on_unprotected_config_file(self):
        with collect_outputs() as outputs:
            self.home.write_config_file("new config")
            config_path = self.temp_dir.getpath(".cosmosrc")
            os.chmod(config_path, 0777)
            self.home.read_config_file()
            assertFunc = (self.assertNotIn
                          if os.name == 'nt' else self.assertIn)
            assertFunc("WARNING", outputs.stderr.getvalue())

    def test_last_cluster(self):
        self.home.write_last_cluster("0000000")
        self.temp_dir.check(".cosmoslast")
        self.assertEquals(self.home.read_last_cluster(), "0000000")
class TestPrepareTarget(TestCase):

    def setUp(self):
        self.dir = TempDirectory()
        self.addCleanup(self.dir.cleanup)
        replace = Replacer()
        replace('workfront.generate.TARGET_ROOT', self.dir.path)
        self.addCleanup(replace.restore)
        self.session = Session('test')

    def test_from_scratch(self):
        path = prepare_target(self.session)

        compare(path, expected=self.dir.getpath('unsupported.py'))
        self.dir.compare(expected=[])

    def test_everything(self):
        self.dir.write('unsupported.py', b'yy')
        path = prepare_target(self.session)

        compare(path, expected=self.dir.getpath('unsupported.py'))
        self.dir.compare(expected=['unsupported.py'])
        compare(self.dir.read('unsupported.py'), b"yy")

    def test_dots_in_version(self):
        path = prepare_target(Session('test', api_version='v4.0'))

        compare(path, expected=self.dir.getpath('v40.py'))
        self.dir.compare(expected=[])
    def test_activity(self, fake_session_mock, fake_s3_mock, fake_key_mock,
                      mock_sqs_message, mock_sqs_connect):
        directory = TempDirectory()

        for testdata in self.do_activity_passes:

            fake_session_mock.return_value = FakeSession(
                test_data.PreparePost_session_example(testdata["update_date"]))
            mock_sqs_connect.return_value = FakeSQSConn(directory)
            mock_sqs_message.return_value = FakeSQSMessage(directory)
            fake_s3_mock.return_value = FakeS3Connection()
            self.activity_PreparePostEIF.logger = mock.MagicMock()
            self.activity_PreparePostEIF.set_monitor_property = mock.MagicMock(
            )
            self.activity_PreparePostEIF.emit_monitor_event = mock.MagicMock()

            success = self.activity_PreparePostEIF.do_activity(
                test_data.PreparePostEIF_data)

            fake_sqs_queue = FakeSQSQueue(directory)
            data_written_in_test_queue = fake_sqs_queue.read(
                test_data.PreparePostEIF_test_dir)

            self.assertEqual(True, success)
            self.assertEqual(json.dumps(testdata["message"]),
                             data_written_in_test_queue)

            output_json = json.loads(
                directory.read(test_data.PreparePostEIF_test_dir))
            expected = testdata["expected"]
            self.assertDictEqual(output_json, expected)
Beispiel #5
0
class TestPrepareTarget(TestCase):
    def setUp(self):
        self.dir = TempDirectory()
        self.addCleanup(self.dir.cleanup)
        replace = Replacer()
        replace('workfront.generate.TARGET_ROOT', self.dir.path)
        self.addCleanup(replace.restore)
        self.session = Session('test')

    def test_from_scratch(self):
        path = prepare_target(self.session)

        compare(path, expected=self.dir.getpath('unsupported.py'))
        self.dir.compare(expected=[])

    def test_everything(self):
        self.dir.write('unsupported.py', b'yy')
        path = prepare_target(self.session)

        compare(path, expected=self.dir.getpath('unsupported.py'))
        self.dir.compare(expected=['unsupported.py'])
        compare(self.dir.read('unsupported.py'), b"yy")

    def test_dots_in_version(self):
        path = prepare_target(Session('test', api_version='v4.0'))

        compare(path, expected=self.dir.getpath('v40.py'))
        self.dir.compare(expected=[])
Beispiel #6
0
    def test_replace_file_contents(self):
        """ファイルの文字列を書き換える関数のテスト。"""
        # ファイルやディレクトリのテストでは、まず以下インスタンスを生成する。
        temp_dir = TempDirectory()

        # writeメソッドで、ファイル作成できる(以下のように、存在しないディレクトリも再帰的に作ってくれる)。
        # 一点注意すべきなのが、デフォルトだとbyte文字列しか書き込みできないこと。
        # 通常の文字列を書き込みたい場合、エンコーディングの指定が必須。
        # http://testfixtures.readthedocs.io/en/latest/api.html
        temp_file = temp_dir.write('foo/bar/sample.txt',
                                   'I love cat.\nMy cat\'s name is mike.\n',
                                   encoding='utf-8')

        # テストケース実施
        replace_file_contents(temp_dir.path + '/foo/bar', 'sample.txt', 'cat',
                              'dog')

        # ファイルの結果を確認する。
        self.assertEqual(
            'I love dog.\nMy dog\'s name is mike.\n',
            # readメソッド呼び出し時にも、エンコーディングの指定が要るので注意(省略した場合はバイト文字列)。
            temp_dir.read('foo/bar/sample.txt', encoding='utf-8'))

        # 以下を呼ぶことで、ディレクトリ以下の内容を再帰的にcleanしてくれる。
        temp_dir.cleanup()
    def test_activity(self, fake_session_mock, fake_s3_mock, fake_key_mock,
                      mock_sqs_message, mock_sqs_connect):
        directory = TempDirectory()

        for testdata in self.do_activity_passes:

            fake_session_mock.return_value = FakeSession(test_data.PreparePost_session_example(
                testdata["update_date"]))
            mock_sqs_connect.return_value = FakeSQSConn(directory)
            mock_sqs_message.return_value = FakeSQSMessage(directory)
            fake_s3_mock.return_value = FakeS3Connection()
            self.activity_PreparePostEIF.logger = mock.MagicMock()
            self.activity_PreparePostEIF.set_monitor_property = mock.MagicMock()
            self.activity_PreparePostEIF.emit_monitor_event = mock.MagicMock()

            success = self.activity_PreparePostEIF.do_activity(test_data.PreparePostEIF_data)

            fake_sqs_queue = FakeSQSQueue(directory)
            data_written_in_test_queue = fake_sqs_queue.read(test_data.PreparePostEIF_test_dir)

            self.assertEqual(True, success)
            self.assertEqual(json.dumps(testdata["message"]), data_written_in_test_queue)

            output_json = json.loads(directory.read(test_data.PreparePostEIF_test_dir))
            expected = testdata["expected"]
            self.assertDictEqual(output_json, expected)
Beispiel #8
0
 def test_evaulate_write(self):
     dir = TempDirectory()
     d = TestContainer('parsed', FileBlock('foo', 'content', 'write'))
     d.evaluate_with(Files('td'), globs={'td': dir})
     compare([C(FileResult, passed=True, expected=None, actual=None)],
             [r.evaluated for r in d])
     dir.compare(['foo'])
     compare(dir.read('foo', 'ascii'), 'content')
Beispiel #9
0
class TestCSVExporter(unittest.TestCase):
    def setUp(self):
        self.tmp = TempDirectory()
        self.dir = self.tmp.makedir('foobar')
        self.exp = CSVExporter()
    def tearDown(self):
        TempDirectory.cleanup_all()
    def test_creation_of_export_file(self):
        filename = 'foobar.csv'
        values = [lib.Sensorsegment._make([0] * 6)]
        status = self.exp.export(values, self.dir+filename)
        self.assertTrue(os.path.isfile(self.dir+filename))
        self.assertTrue(status)
    def test_content_of_export_file(self):
        filename = 'foobar.csv'
        values = [lib.Sensorsegment._make([0] * 6),
                lib.Sensorsegment._make([1] * 6)
                ]
        self.exp.export(values, self.dir+filename, False, False)
        res = self.tmp.read(self.dir+filename)
        ref = b'0,0,0,0,0,0\r\n1,1,1,1,1,1\r\n'
        self.assertEqual(ref, res)
    def test_content_with_header_with_indices(self):
        filename = 'foobar.csv'
        values = [lib.Sensorsegment._make([0] * 6)]
        self.exp.export(values, self.dir+filename, True, True)
        res = self.tmp.read(self.dir+filename)
        ref = b'Index,accelX,accelY,accelZ,gyroX,gyroY,gyroZ\r\n1,0,0,0,0,0,0\r\n'
        self.assertEqual(ref, res)
    def test_content_with_header_without_indices(self):
        filename = 'foobar.csv'
        values = [lib.Sensorsegment._make([0] * 6)]
        self.exp.export(values, self.dir+filename, True, False)
        res = self.tmp.read(self.dir+filename)
        ref = b'accelX,accelY,accelZ,gyroX,gyroY,gyroZ\r\n0,0,0,0,0,0\r\n'
        self.assertEqual(ref, res)
    def test_content_without_header_with_indices(self):
        filename = 'foobar.csv'
        values = [lib.Sensorsegment._make([0] * 6)]
        self.exp.export(values, self.dir+filename, False, True)
        res = self.tmp.read(self.dir+filename)
        ref = b'1,0,0,0,0,0,0\r\n'
        self.assertEqual(ref, res)
Beispiel #10
0
 def test_evaulate_write(self):
     dir = TempDirectory()
     d = TestContainer('parsed',FileBlock('foo','content','write'))
     d.evaluate_with(Files('td'),globs={'td':dir})
     compare([C(FileResult,
                passed=True,
                expected=None,
                actual=None)],
             [r.evaluated for r in d])
     dir.compare(['foo'])
     compare(dir.read('foo', 'ascii'), 'content')
    def test_activity(self, mock_set_monitor_property, mock_emit_monitor_event, fake_s3_mock, fake_key_mock):
        directory = TempDirectory()

        fake_key_mock.return_value = FakeKey(directory, data.bucket_dest_file_name,
                                             data.RewriteEIF_json_input_string)
        fake_s3_mock.return_value = FakeS3Connection()

        success = self.activity_PreparePostEIF.do_activity(data.RewriteEIF_data)
        self.assertEqual(True, success)

        output_json = json.loads(directory.read(data.bucket_dest_file_name))
        expected = data.RewriteEIF_json_output
        self.assertDictEqual(output_json, expected)
    def test_do_activity(self, fake_session_mock, fake_s3_mock, fake_key_mock, fake_get_article_xml_key, fake_add_update_date_to_json):
        directory = TempDirectory()

        #preparing Mocks
        fake_session_mock.return_value = FakeSession(data.session_example)
        fake_s3_mock.return_value = FakeS3Connection()
        fake_key_mock.return_value = FakeKey(directory, data.bucket_dest_file_name)
        fake_get_article_xml_key.return_value = FakeKey(directory), data.bucket_origin_file_name
        fake_add_update_date_to_json.return_value = data.json_output_return_example_string
        self.jats.emit_monitor_event = mock.MagicMock()
        self.jats.set_dashboard_properties = mock.MagicMock()

        success = self.jats.do_activity(testdata.ExpandArticle_data)
        self.assertEqual(success, True)
        output_json = json.loads(directory.read("test_dest.json"))
        expected = data.json_output_return_example
        self.assertDictEqual(output_json, expected)
    def test_activity(self, status_code, response_update_date, update_json, expected_update_date,
                      mock_sqs_message, mock_sqs_connect, mock_requests_put):
        directory = TempDirectory()

        mock_sqs_connect.return_value = FakeSQSConn(directory)
        mock_sqs_message.return_value = FakeSQSMessage(directory)
        mock_requests_put.return_value = classes_mock.FakeResponse(status_code, update_json)

        success = self.activity_ApprovePublication.do_activity(
            activity_data.ApprovePublication_data(response_update_date))

        fake_sqs_queue = FakeSQSQueue(directory)
        data_written_in_test_queue = fake_sqs_queue.read(activity_data.ApprovePublication_test_dir)

        self.assertEqual(True, success)

        output_json = json.loads(directory.read(activity_data.ApprovePublication_test_dir))
        expected = activity_data.ApprovePublication_json_output_return_example(expected_update_date)
        self.assertDictEqual(output_json, expected)
Beispiel #14
0
class DocumentServiceWriterTestCases(SegueApiTestCase):
    def setUp(self):
        super(DocumentServiceWriterTestCases, self).setUp()
        self.tmp_dir   = TempDirectory()
        self.out_dir   = self.tmp_dir.makedir("output")
        self.templates = os.path.join(os.path.dirname(__file__), 'fixtures')

        self.service = DocumentService(override_root=self.out_dir, template_root=self.templates, tmp_dir=self.tmp_dir.path)

    def tearDown(self):
        self.tmp_dir.cleanup()

    @skipIf("SNAP_CI" in os.environ, "inkscape is not available in CI")
    def test_svg_to_pdf(self):
        result = self.service.svg_to_pdf('templates/dummy.svg', 'certificate', "ABCD", { 'XONGA': 'bir<osca' })

        self.tmp_dir.check_dir('','certificate-ABCD.svg', 'output')
        self.tmp_dir.check_dir('output/certificate/AB', 'certificate-ABCD.pdf')

        contents_of_temp = self.tmp_dir.read("certificate-ABCD.svg")
        self.assertIn("bir&lt;osca da silva", contents_of_temp)

        self.assertEquals(result, 'certificate-ABCD.pdf')
Beispiel #15
0
class TestDecoratedObjectTypes(MockOpenHelper, TestCase):
    def setUp(self):
        super(TestDecoratedObjectTypes, self).setUp()
        self.dir = TempDirectory()
        self.addCleanup(self.dir.cleanup)

    def test_normal(self):
        base_url = 'https://test.attask-ondemand.com/attask/api/v4.0'
        session = Session('test', api_version='v4.0')
        self.server.add(
            url=base_url + '/metadata',
            params='method=GET',
            response=json.dumps(
                dict(data=dict(objects=dict(
                    SomeThing=dict(objCode='SMTHING', name='SomeThing'))))))
        expected = dict(objCode='SMTHING', name='SomeThing', stuff='a value')
        self.server.add(url=base_url + '/smthing/metadata',
                        params='method=GET',
                        response=json.dumps(dict(data=expected)))
        compare(decorated_object_types(session, None),
                expected=[('SomeThing', 'SMTHING', expected)])

    def test_cache_write(self):
        base_url = 'https://test.attask-ondemand.com/attask/api/v4.0'
        session = Session('test', api_version='v4.0')
        self.server.add(
            url=base_url + '/metadata',
            params='method=GET',
            response=json.dumps(
                dict(data=dict(objects=dict(
                    SomeThing=dict(objCode='SMTHING', name='SomeThing'))))))
        expected = dict(objCode='SMTHING', name='SomeThing', stuff='a value')
        self.server.add(url=base_url + '/smthing/metadata',
                        params='method=GET',
                        response=json.dumps(dict(data=expected)))
        compare(decorated_object_types(session, self.dir.path),
                expected=[('SomeThing', 'SMTHING', expected)])
        self.dir.compare(
            expected=['v4.0_metadata.json', 'v4.0_smthing_metadata.json'])
        compare(json.loads(
            self.dir.read('v4.0_metadata.json').decode('ascii')),
                expected=dict(objects=dict(
                    SomeThing=dict(objCode='SMTHING', name='SomeThing'))))
        compare(json.loads(
            self.dir.read('v4.0_smthing_metadata.json').decode('ascii')),
                expected=expected)

    def test_cache_read(self):
        expected = dict(objCode='SMTHING', name='SomeThing', stuff='a value')

        self.dir.write(
            'v4.0_metadata.json',
            json.dumps(
                dict(objects=dict(
                    SomeThing=dict(objCode='SMTHING', name='SomeThing')))),
            encoding='ascii')

        self.dir.write('v4.0_smthing_metadata.json',
                       json.dumps(expected),
                       encoding='ascii')

        session = Session('test', api_version='v4.0')
        compare(decorated_object_types(session, self.dir.path),
                expected=[('SomeThing', 'SMTHING', expected)])

    def test_unsupported(self):
        base_url = 'https://test.attask-ondemand.com/attask/api/unsupported'
        session = Session('test')
        self.server.add(
            url=base_url + '/metadata',
            params='method=GET',
            response=json.dumps(
                dict(data=dict(objects=dict(
                    SomeThing=dict(objCode='SMTHING', name='SomeThing'))))))
        expected = dict(objCode='SMTHING', name='SomeThing', stuff='a value')
        self.server.add(url=base_url + '/smthing/metadata',
                        params='method=GET',
                        response=json.dumps(dict(data=expected)))
        compare(decorated_object_types(session, None),
                expected=[('SomeThing', 'SMTHING', expected)])

    def test_name_override(self):
        base_url = 'https://test.attask-ondemand.com/attask/api/unsupported'
        session = Session('test')
        self.server.add(
            url=base_url + '/metadata',
            params='method=GET',
            response=json.dumps(
                dict(data=dict(objects=dict(
                    SomeThing=dict(objCode='OPTASK', name='SomeThing'))))))
        expected = dict(objCode='SMTHING', name='SomeThing', stuff='a value')
        self.server.add(url=base_url + '/optask/metadata',
                        params='method=GET',
                        response=json.dumps(dict(data=expected)))
        compare(decorated_object_types(session, None),
                expected=[('Issue', 'OPTASK', expected)])
Beispiel #16
0
class test_InputFileLoaderCheckerSaver(unittest.TestCase):
    """tests for InputFileLoaderCheckerSaver"""

    def setUp(self):
        self.dir = os.path.abspath(os.curdir)

        self.tempdir = TempDirectory()
        self.tempdir.write('inp1.py', 'a=4\nb=6', 'utf-8')
        self.tempdir.write('out0001.py', 'a=4\nb=6', 'utf-8')
        self.tempdir.write(('what', 'out0001.py'), 'a=4\nb=6', 'utf-8')
#        self.tempdir.write('a_005', b'some text a5')
#        self.tempdir.write('b_002.txt', b'some text b2')
#        self.tempdir.write('b_008.out', b'some text b8')
#        self.tempdir.write(('c_010', 'por'), b'some text c5por')
        os.chdir(self.tempdir.path)

    def tearDown(self):
        os.chdir(self.dir)
        self.tempdir.cleanup()

    def test_init_from_str(self):
        a = InputFileLoaderCheckerSaver()
        a._attributes = "a b".split()
        a._initialize_attributes()
        a.__init__('a=4\nb=6')

        assert_equal(a.a, 4)
        assert_equal(a.b, 6)
        assert_equal(a._input_text, 'a=4\nb=6')

    def test_init_from_fileobj(self):
        a = InputFileLoaderCheckerSaver()
        a._attributes = "a b".split()
        a._initialize_attributes()
        with open(os.path.join(self.tempdir.path, 'inp1.py'), 'r') as f:
            a.__init__(f)

        assert_equal(a.a, 4)
        assert_equal(a.b, 6)
        assert_equal(a._input_text, 'a=4\nb=6')


    def test_attribute_defaults(self):
        a = InputFileLoaderCheckerSaver()
        a._input_text = 'b=6'
        a._attributes = "a b".split()
        a._attribute_defaults = {'a': 24}
        a._initialize_attributes()
        a._transfer_attributes_from_inputfile()

        assert_equal(a.a, 24)
        assert_equal(a.b, 6)
        assert_equal(a._input_text, 'b=6')

    def test_check_attributes_that_should_be_lists(self):
        a = InputFileLoaderCheckerSaver()
        a.a=4
        a.b=6
        a._attributes_that_should_be_lists = ['b']
        a.check_input_attributes()

        assert_equal(a.a, 4)
        assert_equal(a.b, [6])

    def test_check_zero_or_all(self):
        a = InputFileLoaderCheckerSaver()
        a.a=4
        a.b=6
        a.c=None
        a._zero_or_all = ['a b c'.split()]

        assert_raises(ValueError, a.check_input_attributes)

    def test_check_at_least_one(self):
        a = InputFileLoaderCheckerSaver()
        a.c=None
        a._at_least_one = ['c'.split()]

        assert_raises(ValueError, a.check_input_attributes)

    def test_check_one_implies_others(self):
        a = InputFileLoaderCheckerSaver()
        a.a = 4
        a.c=None
        a._one_implies_others = ['a c'.split()]
        assert_raises(ValueError, a.check_input_attributes)

    def test_check_attributes_to_force_same_len(self):
        a = InputFileLoaderCheckerSaver()
        a.a = [4,5]
        a.c=None
        a._attributes_to_force_same_len = ['a c'.split()]
        a.check_input_attributes()

        assert_equal(a.c, [None, None])

    def test_check_attributes_that_should_have_same_x_limits(self):
        a = InputFileLoaderCheckerSaver()
        a.a = PolyLine([0,1], [2,5])
        a.c = PolyLine([0,7], [5,6])
        a._attributes_that_should_have_same_x_limits = ['a c'.split()]
        assert_raises(ValueError, a.check_input_attributes)

    def test_check_attributes_that_should_have_same_len_pairs(self):
        a = InputFileLoaderCheckerSaver()
        a.a = [2, 3]
        a.c = [3]
        a._attributes_that_should_have_same_len_pairs = ['a c'.split()]
        assert_raises(ValueError, a.check_input_attributes)

    def test_determine_output_stem_defaults(self):
        a = InputFileLoaderCheckerSaver()
        a._determine_output_stem()

        assert_equal(a._file_stem, '.\\out0002\\out0002')

    def test_determine_output_stem_overwrite(self):
        a = InputFileLoaderCheckerSaver()
        a.overwrite = True
        a._determine_output_stem()
        assert_equal(a._file_stem, '.\\out0001\\out0001')

    def test_determine_output_stem_create_directory(self):
        a = InputFileLoaderCheckerSaver()
        a.create_directory = False
        a._determine_output_stem()
        assert_equal(a._file_stem, '.\\out0002')

    def test_determine_output_stem_prefix(self):
        a = InputFileLoaderCheckerSaver()
        a.prefix = 'hello_'
        a._determine_output_stem()
        assert_equal(a._file_stem, '.\\hello_0001\\hello_0001')
    def test_determine_output_stem_directory(self):
        a = InputFileLoaderCheckerSaver()
        a.directory = os.path.join(self.tempdir.path, 'what')
        a._determine_output_stem()

        assert_equal(a._file_stem, os.path.join(self.tempdir.path,'what', 'out0002', 'out0002'))

    def test_save_data_parsed(self):
        a = InputFileLoaderCheckerSaver()
        a._attributes = "a b ".split()
        a.save_data_to_file=True
#        a._initialize_attributes()
        a.a=4
        a.b=6

        a._save_data()
#        print(os.listdir(self.tempdir.path))
#        print(os.listdir(os.path.join(self.tempdir.path,'out0002')))
        assert_equal(self.tempdir.read(
                ('out0002','out0002_input_parsed.py'), 'utf-8').strip().splitlines(),
                     'a = 4\nb = 6'.splitlines())

    def test_save_data_input_text(self):
        a = InputFileLoaderCheckerSaver()
        a._input_text= "hello"
        a.save_data_to_file=True
        a._save_data()
        assert_equal(self.tempdir.read(
                ('out0002','out0002_input_original.py'), 'utf-8').strip().splitlines(),
                     'hello'.splitlines())

    def test_save_data_input_ext(self):
        a = InputFileLoaderCheckerSaver()
        a._input_text= "hello"
        a.input_ext= '.txt'
        a.save_data_to_file=True
        a._save_data()

        ok_(os.path.isfile(os.path.join(
            self.tempdir.path, 'out0002','out0002_input_original.txt')))

    def test_save_data_grid_data_dicts(self):
        a = InputFileLoaderCheckerSaver()
        a._grid_data_dicts= {'data': np.arange(6).reshape(3,2)}
        a.save_data_to_file=True
        a._save_data()

#        print(os.listdir(os.path.join(self.tempdir.path,'out0002')))
        ok_(os.path.isfile(os.path.join(
            self.tempdir.path, 'out0002','out0002.csv')))


    def test_save_data_grid_data_dicts_data_ext(self):
        a = InputFileLoaderCheckerSaver()
        a._grid_data_dicts= {'data': np.arange(6).reshape(3,2)}
        a.save_data_to_file=True
        a.data_ext = ".txt"
        a._save_data()

#        print(os.listdir(os.path.join(self.tempdir.path,'out0002')))
        ok_(os.path.isfile(os.path.join(
            self.tempdir.path, 'out0002','out0002.txt')))


    def test_save_figures(self):
        a = InputFileLoaderCheckerSaver()
        a.save_figures_to_file=True
        fig = matplotlib.pyplot.figure()
        ax = fig.add_subplot('111')
        ax.plot(4,5)
        fig.set_label('sing')
        a._figures=[fig]
        a._save_figures()
        a._figures=None
        matplotlib.pyplot.clf()
#        print(os.listdir(os.path.join(self.tempdir.path,'out0002')))
        ok_(os.path.isfile(os.path.join(
            self.tempdir.path, 'out0002','out0002_sing.eps')))

    def test_save_figures_figure_ext(self):
        a = InputFileLoaderCheckerSaver()
        a.save_figures_to_file=True
        a.figure_ext='.pdf'

        fig = matplotlib.pyplot.figure()
        ax = fig.add_subplot('111')
        ax.plot(4,5)
        fig.set_label('sing')
        a._figures=[fig]
        a._save_figures()
        a._figures=None
        matplotlib.pyplot.clf()
#        print(os.listdir(os.path.join(self.tempdir.path,'out0002')))
        ok_(os.path.isfile(os.path.join(
            self.tempdir.path, 'out0002','out0002_sing.pdf')))
Beispiel #17
0
class test_GenericInputFileArgParser(unittest.TestCase):
    """tests GenericInputFileArgParser"""

    def setUp(self):
        self.tempdir = TempDirectory()
        self.tempdir.write('a1.py', "a1", 'utf-8')
        self.tempdir.write('a2.py', "a2", 'utf-8')
        self.tempdir.write('b1.txt', "b1", 'utf-8')
        self.tempdir.write('b2.txt', "b2", 'utf-8')

    def tearDown(self):
        self.tempdir.cleanup()

    def _abc_fobj(self, fobj):
        """print file contents to out.zebra"""
        with open(os.path.join(self.tempdir.path, 'out.zebra'), 'a') as f:
            f.write(fobj.read()+'\n')
        return

    def _abc_path(self, path):
        """print file basename out.zebra"""
        with open(os.path.join(self.tempdir.path, 'out.zebra'), 'a') as f:
            f.write(os.path.basename(path)+'\n')
        return
#    def dog(self):
#        with open(os.path.join(self.tempdir.path, 'out.zebra'), 'a') as f:
#            f.write('dog\n')

    def test_directory_with_path(self):

        a = GenericInputFileArgParser(self._abc_path, False)
        args = '-d {0} -p'.format(self.tempdir.path).split()
        print(args)
        a.main(argv=args)

        assert_equal(self.tempdir.read(('out.zebra'), 'utf-8').splitlines(),
                            textwrap.dedent("""\
                            a1.py
                            a2.py""").splitlines())

    def test_directory_with_fobj(self):

        a = GenericInputFileArgParser(self._abc_fobj, True)
        args = '-d {0} -p'.format(self.tempdir.path).split()
#        print(args)
        a.main(argv=args)

        assert_equal(self.tempdir.read(('out.zebra'), 'utf-8').splitlines(),
                            textwrap.dedent("""\
                            a1
                            a2""").splitlines())

    def test_pattern_with_path(self):

        a = GenericInputFileArgParser(self._abc_path, False)
        args = '-d {0} -p *.txt'.format(self.tempdir.path).split()
#        print(args)
        a.main(argv=args)

        assert_equal(self.tempdir.read(('out.zebra'), 'utf-8').splitlines(),
                            textwrap.dedent("""\
                            b1.txt
                            b2.txt""").splitlines())

    def test_pattern_with_fobj(self):

        a = GenericInputFileArgParser(self._abc_fobj, True)
        args = '-d {0} -p *.txt'.format(self.tempdir.path).split()
#        print(args)
        a.main(argv=args)

        assert_equal(self.tempdir.read(('out.zebra'), 'utf-8').splitlines(),
                            textwrap.dedent("""\
                            b1
                            b2""").splitlines())

    def test_filename_with_fobj(self):

        a = GenericInputFileArgParser(self._abc_fobj, True)
        args = '-f {0} {1}'.format(
            os.path.join(self.tempdir.path, 'a1.py'),
            os.path.join(self.tempdir.path, 'b1.txt')
                                ).split()
#        print(args)
        a.main(argv=args)

        assert_equal(self.tempdir.read(('out.zebra'), 'utf-8').splitlines(),
                            textwrap.dedent("""\
                            a1
                            b1""").splitlines())


    def test_filename_with_path(self):

        a = GenericInputFileArgParser(self._abc_path, False)
        args = '-f {0} {1}'.format(
            os.path.join(self.tempdir.path, 'a1.py'),
            os.path.join(self.tempdir.path, 'b1.txt')
                                ).split()
#        print(args)
        a.main(argv=args)

        assert_equal(self.tempdir.read(('out.zebra'), 'utf-8').splitlines(),
                            textwrap.dedent("""\
                            a1.py
                            b1.txt""").splitlines())



    def test_default_directory(self):

        a = GenericInputFileArgParser(self._abc_path, False)
        args = '-d -p'.format(self.tempdir.path).split()

        with working_directory(self.tempdir.path):
            a.main(argv=args)

        assert_equal(self.tempdir.read(('out.zebra'), 'utf-8').splitlines(),
                            textwrap.dedent("""\
                            a1.py
                            a2.py""").splitlines())

    def test_methods_with_path(self):




        a = GenericInputFileArgParser(HelperForGenericInputFileArgParser,
                                      False, [('dog',[],{})])

        args = '-f {0} {1}'.format(
            os.path.join(self.tempdir.path, 'a1.py'),
            os.path.join(self.tempdir.path, 'b1.txt')
                                ).split()

        a.main(argv=args)

        assert_equal(self.tempdir.read(('out.zebra'), 'utf-8').splitlines(),
                            textwrap.dedent("""\
                            a1.py
                            dog
                            b1.txt
                            dog
                            """).splitlines())
Beispiel #18
0
class test_save_grid_data_to_file(unittest.TestCase):
    """tests for save_grid_data_to_file"""
#    save_grid_data_to_file(directory=None, file_stem='out_000',
#                           create_directory=True, ext='.csv', *data_dicts)

    def setUp(self):
        self.tempdir = TempDirectory()


    def tearDown(self):
        self.tempdir.cleanup()

    def test_defaults(self):

        data = np.arange(6).reshape(3,2)
        save_grid_data_to_file({'data': data},
                               directory=self.tempdir.path)
        assert_equal(self.tempdir.read(('out_000','out_000.csv'), 'utf-8').splitlines(),
                            textwrap.dedent("""\
                            idex,0,1
                            0,0,1
                            1,2,3
                            2,4,5""").splitlines())
    def test_directory(self):

        data = np.arange(6).reshape(3,2)
        save_grid_data_to_file({'data': data},
                               directory=os.path.join(self.tempdir.path,'g'))
        assert_equal(self.tempdir.read(('g','out_000','out_000.csv'), 'utf-8').splitlines(),
                            textwrap.dedent("""\
                            idex,0,1
                            0,0,1
                            1,2,3
                            2,4,5""").splitlines())
    def test_file_stem(self):

        data = np.arange(6).reshape(3,2)
        save_grid_data_to_file({'data': data},
                               directory=self.tempdir.path,
                               file_stem="ppp")
        assert_equal(self.tempdir.read(('ppp','ppp.csv'), 'utf-8').splitlines(),
                            textwrap.dedent("""\
                            idex,0,1
                            0,0,1
                            1,2,3
                            2,4,5""").splitlines())
    def test_ext(self):

        data = np.arange(6).reshape(3,2)
        save_grid_data_to_file({'data': data},
                               directory=self.tempdir.path,
                               ext=".out")
        assert_equal(self.tempdir.read(('out_000','out_000.out'), 'utf-8').splitlines(),
                            textwrap.dedent("""\
                            idex,0,1
                            0,0,1
                            1,2,3
                            2,4,5""").splitlines())
    def test_create_directory(self):

        data = np.arange(6).reshape(3,2)
        save_grid_data_to_file({'data': data},
                               directory=self.tempdir.path,
                               create_directory=False)
        assert_equal(self.tempdir.read('out_000.csv', 'utf-8').splitlines(),
                            textwrap.dedent("""\
                            idex,0,1
                            0,0,1
                            1,2,3
                            2,4,5""").splitlines())
    def test_data_dict_header(self):

        data = np.arange(6).reshape(3,2)
        save_grid_data_to_file({'data': data, 'header':'hello header'},
                               directory=self.tempdir.path)
        assert_equal(self.tempdir.read(('out_000','out_000.csv'), 'utf-8').splitlines(),
                            textwrap.dedent("""\
                            hello header
                            idex,0,1
                            0,0,1
                            1,2,3
                            2,4,5""").splitlines())
    def test_data_dict_name(self):

        data = np.arange(6).reshape(3,2)
        save_grid_data_to_file({'data': data, 'name':'xx'},
                               directory=self.tempdir.path)
        assert_equal(self.tempdir.read(('out_000','out_000xx.csv'), 'utf-8').splitlines(),
                            textwrap.dedent("""\
                            idex,0,1
                            0,0,1
                            1,2,3
                            2,4,5""").splitlines())

    def test_data_dict_row_labels(self):

        data = np.arange(6).reshape(3,2)
        save_grid_data_to_file({'data': data, 'row_labels':[8,12,6]},
                               directory=self.tempdir.path)
        assert_equal(self.tempdir.read(('out_000','out_000.csv'), 'utf-8').splitlines(),
                            textwrap.dedent("""\
                            idex,item,0,1
                            0,8,0,1
                            1,12,2,3
                            2,6,4,5""").splitlines())
    def test_data_dict_row_labels_label(self):

        data = np.arange(6).reshape(3,2)
        save_grid_data_to_file({'data': data, 'row_labels':[8,12,6],
                                'row_labels_label':'yyy'},
                               directory=self.tempdir.path)
        assert_equal(self.tempdir.read(('out_000','out_000.csv'), 'utf-8').splitlines(),
                            textwrap.dedent("""\
                            idex,yyy,0,1
                            0,8,0,1
                            1,12,2,3
                            2,6,4,5""").splitlines())
    def test_data_dict_column_labels(self):

        data = np.arange(6).reshape(3,2)
        save_grid_data_to_file({'data': data, 'column_labels':['a', 'b']},
                               directory=self.tempdir.path)
        assert_equal(self.tempdir.read(('out_000','out_000.csv'), 'utf-8').splitlines(),
                            textwrap.dedent("""\
                            idex,a,b
                            0,0,1
                            1,2,3
                            2,4,5""").splitlines())
    def test_two_files(self):

        data = np.arange(6).reshape(3,2)
        save_grid_data_to_file([{'data': data, 'name':1},
                                {'data': 2*data, 'name':2}],
                               directory=self.tempdir.path,
                               file_stem="qqq")
        assert_equal(self.tempdir.read(('qqq','qqq1.csv'), 'utf-8').splitlines(),
                            textwrap.dedent("""\
                            idex,0,1
                            0,0,1
                            1,2,3
                            2,4,5""").splitlines())
        assert_equal(self.tempdir.read(('qqq','qqq2.csv'), 'utf-8').splitlines(),
                            textwrap.dedent("""\
                            idex,0,1
                            0,0,2
                            1,4,6
                            2,8,10""").splitlines())
Beispiel #19
0
 def test_write_config(self):
     d = TempDirectory()
     ConfigManager.write_config(os.path.join(d.path,"t1.txt"), 1)
     compare(d.read("t1.txt"), b'1')
     d.cleanup()
Beispiel #20
0
 def test_initialise_file(self):
     d = TempDirectory()
     task_index = ConfigManager.read_config(os.path.join(d.path,"t1.txt"))
     compare(d.read("t1.txt"), b'0')
     d.cleanup()
Beispiel #21
0
class FunctionalTest(MockOpenHelper, TestCase):

    base = 'https://api-cl01.attask-ondemand.com/attask/api/unsupported'

    def setUp(self):
        super(FunctionalTest, self).setUp()
        self.log = LogCapture()
        self.addCleanup(self.log.uninstall)
        self.dir = TempDirectory()
        self.addCleanup(self.dir.cleanup)
        self.replace('logging.basicConfig', Mock())
        self.replace('workfront.generate.TARGET_ROOT', self.dir.path)

    def test_functional(self):
        self.replace('sys.argv', ['x'])

        self.server.add(
            url='/metadata',
            params='method=GET',
            response=json.dumps(
                dict(data=dict(objects=dict(
                    SomeThing=dict(objCode='BAR', name='SomeThing'),
                    OtherThing=dict(objCode='FOO', name='OtherThing'),
                )))))

        self.server.add(url='/foo/metadata',
                        params='method=GET',
                        response=json.dumps(
                            dict(data=dict(
                                objCode='FOO',
                                name='OtherThing',
                                fields={
                                    "ID": {},
                                    "anotherField": {}
                                },
                                references={},
                                collections={},
                                actions={},
                            ))))

        self.server.add(
            url='/bar/metadata',
            params='method=GET',
            response=json.dumps(
                dict(data=dict(objCode='BAR',
                               name='SomeThing',
                               fields={
                                   "ID": {},
                                   "theField": {}
                               },
                               references={"accessRules": {}},
                               collections={"assignedTo": {}},
                               actions={
                                   "doSomething": {
                                       "arguments": [{
                                           "name": "anOption",
                                           "type": "Task"
                                       }, {
                                           "name": "options",
                                           "type": "string[]"
                                       }],
                                       "resultType":
                                       "string",
                                       "label":
                                       "doSomething"
                                   }
                               }))))

        with OutputCapture() as output:
            output.disable()
            main()

        output.compare("")

        self.dir.compare(expected=['unsupported.py'])

        compare(self.dir.read('unsupported.py').decode('ascii'),
                expected=u'''\
# generated from https://api-cl01.attask-ondemand.com/attask/api/unsupported/metadata
from ..meta import APIVersion, Object, Field, Reference, Collection

api = APIVersion('unsupported')


class OtherThing(Object):
    code = 'FOO'
    another_field = Field('anotherField')

api.register(OtherThing)


class SomeThing(Object):
    code = 'BAR'
    the_field = Field('theField')
    access_rules = Reference('accessRules')
    assigned_to = Collection('assignedTo')

    def do_something(self, an_option=None, options=None):
        """
        The ``doSomething`` action.

        :param an_option: anOption (type: ``Task``)
        :param options: options (type: ``string[]``)
        :return: ``string``
        """
        params = {}
        if an_option is not None: params['anOption'] = an_option
        if options is not None: params['options'] = options
        data = self.session.put(self.api_url()+'/doSomething', params)
        return data['result']

api.register(SomeThing)
''',
                trailing_whitespace=False)
Beispiel #22
0
class test_save_grid_data_to_file(unittest.TestCase):
    """tests for save_grid_data_to_file"""

    #    save_grid_data_to_file(directory=None, file_stem='out_000',
    #                           create_directory=True, ext='.csv', *data_dicts)

    def setUp(self):
        self.tempdir = TempDirectory()

    def tearDown(self):
        self.tempdir.cleanup()

    def test_defaults(self):

        data = np.arange(6).reshape(3, 2)
        save_grid_data_to_file({'data': data}, directory=self.tempdir.path)
        assert_equal(
            self.tempdir.read(('out_000', 'out_000.csv'),
                              'utf-8').splitlines(),
            textwrap.dedent("""\
                            idex,0,1
                            0,0,1
                            1,2,3
                            2,4,5""").splitlines())

    def test_directory(self):

        data = np.arange(6).reshape(3, 2)
        save_grid_data_to_file({'data': data},
                               directory=os.path.join(self.tempdir.path, 'g'))
        assert_equal(
            self.tempdir.read(('g', 'out_000', 'out_000.csv'),
                              'utf-8').splitlines(),
            textwrap.dedent("""\
                            idex,0,1
                            0,0,1
                            1,2,3
                            2,4,5""").splitlines())

    def test_file_stem(self):

        data = np.arange(6).reshape(3, 2)
        save_grid_data_to_file({'data': data},
                               directory=self.tempdir.path,
                               file_stem="ppp")
        assert_equal(
            self.tempdir.read(('ppp', 'ppp.csv'), 'utf-8').splitlines(),
            textwrap.dedent("""\
                            idex,0,1
                            0,0,1
                            1,2,3
                            2,4,5""").splitlines())

    def test_ext(self):

        data = np.arange(6).reshape(3, 2)
        save_grid_data_to_file({'data': data},
                               directory=self.tempdir.path,
                               ext=".out")
        assert_equal(
            self.tempdir.read(('out_000', 'out_000.out'),
                              'utf-8').splitlines(),
            textwrap.dedent("""\
                            idex,0,1
                            0,0,1
                            1,2,3
                            2,4,5""").splitlines())

    def test_create_directory(self):

        data = np.arange(6).reshape(3, 2)
        save_grid_data_to_file({'data': data},
                               directory=self.tempdir.path,
                               create_directory=False)
        assert_equal(
            self.tempdir.read('out_000.csv', 'utf-8').splitlines(),
            textwrap.dedent("""\
                            idex,0,1
                            0,0,1
                            1,2,3
                            2,4,5""").splitlines())

    def test_data_dict_header(self):

        data = np.arange(6).reshape(3, 2)
        save_grid_data_to_file({
            'data': data,
            'header': 'hello header'
        },
                               directory=self.tempdir.path)
        assert_equal(
            self.tempdir.read(('out_000', 'out_000.csv'),
                              'utf-8').splitlines(),
            textwrap.dedent("""\
                            hello header
                            idex,0,1
                            0,0,1
                            1,2,3
                            2,4,5""").splitlines())

    def test_data_dict_name(self):

        data = np.arange(6).reshape(3, 2)
        save_grid_data_to_file({
            'data': data,
            'name': 'xx'
        },
                               directory=self.tempdir.path)
        assert_equal(
            self.tempdir.read(('out_000', 'out_000xx.csv'),
                              'utf-8').splitlines(),
            textwrap.dedent("""\
                            idex,0,1
                            0,0,1
                            1,2,3
                            2,4,5""").splitlines())

    def test_data_dict_row_labels(self):

        data = np.arange(6).reshape(3, 2)
        save_grid_data_to_file({
            'data': data,
            'row_labels': [8, 12, 6]
        },
                               directory=self.tempdir.path)
        assert_equal(
            self.tempdir.read(('out_000', 'out_000.csv'),
                              'utf-8').splitlines(),
            textwrap.dedent("""\
                            idex,item,0,1
                            0,8,0,1
                            1,12,2,3
                            2,6,4,5""").splitlines())

    def test_data_dict_row_labels_label(self):

        data = np.arange(6).reshape(3, 2)
        save_grid_data_to_file(
            {
                'data': data,
                'row_labels': [8, 12, 6],
                'row_labels_label': 'yyy'
            },
            directory=self.tempdir.path)
        assert_equal(
            self.tempdir.read(('out_000', 'out_000.csv'),
                              'utf-8').splitlines(),
            textwrap.dedent("""\
                            idex,yyy,0,1
                            0,8,0,1
                            1,12,2,3
                            2,6,4,5""").splitlines())

    def test_data_dict_column_labels(self):

        data = np.arange(6).reshape(3, 2)
        save_grid_data_to_file({
            'data': data,
            'column_labels': ['a', 'b']
        },
                               directory=self.tempdir.path)
        assert_equal(
            self.tempdir.read(('out_000', 'out_000.csv'),
                              'utf-8').splitlines(),
            textwrap.dedent("""\
                            idex,a,b
                            0,0,1
                            1,2,3
                            2,4,5""").splitlines())

    def test_two_files(self):

        data = np.arange(6).reshape(3, 2)
        save_grid_data_to_file([{
            'data': data,
            'name': 1
        }, {
            'data': 2 * data,
            'name': 2
        }],
                               directory=self.tempdir.path,
                               file_stem="qqq")
        assert_equal(
            self.tempdir.read(('qqq', 'qqq1.csv'), 'utf-8').splitlines(),
            textwrap.dedent("""\
                            idex,0,1
                            0,0,1
                            1,2,3
                            2,4,5""").splitlines())
        assert_equal(
            self.tempdir.read(('qqq', 'qqq2.csv'), 'utf-8').splitlines(),
            textwrap.dedent("""\
                            idex,0,1
                            0,0,2
                            1,4,6
                            2,8,10""").splitlines())
Beispiel #23
0
class test_InputFileLoaderCheckerSaver(unittest.TestCase):
    """tests for InputFileLoaderCheckerSaver"""
    def setUp(self):
        self.dir = os.path.abspath(os.curdir)

        self.tempdir = TempDirectory()
        self.tempdir.write('inp1.py', 'a=4\nb=6', 'utf-8')
        self.tempdir.write('out0001.py', 'a=4\nb=6', 'utf-8')
        self.tempdir.write(('what', 'out0001.py'), 'a=4\nb=6', 'utf-8')
        #        self.tempdir.write('a_005', b'some text a5')
        #        self.tempdir.write('b_002.txt', b'some text b2')
        #        self.tempdir.write('b_008.out', b'some text b8')
        #        self.tempdir.write(('c_010', 'por'), b'some text c5por')
        os.chdir(self.tempdir.path)

    def tearDown(self):
        os.chdir(self.dir)
        self.tempdir.cleanup()

    def test_init_from_str(self):
        a = InputFileLoaderCheckerSaver()
        a._attributes = "a b".split()
        a._initialize_attributes()
        a.__init__('a=4\nb=6')

        assert_equal(a.a, 4)
        assert_equal(a.b, 6)
        assert_equal(a._input_text, 'a=4\nb=6')

    def test_init_from_fileobj(self):
        a = InputFileLoaderCheckerSaver()
        a._attributes = "a b".split()
        a._initialize_attributes()
        with open(os.path.join(self.tempdir.path, 'inp1.py'), 'r') as f:
            a.__init__(f)

        assert_equal(a.a, 4)
        assert_equal(a.b, 6)
        assert_equal(a._input_text, 'a=4\nb=6')

    def test_attribute_defaults(self):
        a = InputFileLoaderCheckerSaver()
        a._input_text = 'b=6'
        a._attributes = "a b".split()
        a._attribute_defaults = {'a': 24}
        a._initialize_attributes()
        a._transfer_attributes_from_inputfile()

        assert_equal(a.a, 24)
        assert_equal(a.b, 6)
        assert_equal(a._input_text, 'b=6')

    def test_check_attributes_that_should_be_lists(self):
        a = InputFileLoaderCheckerSaver()
        a.a = 4
        a.b = 6
        a._attributes_that_should_be_lists = ['b']
        a.check_input_attributes()

        assert_equal(a.a, 4)
        assert_equal(a.b, [6])

    def test_check_zero_or_all(self):
        a = InputFileLoaderCheckerSaver()
        a.a = 4
        a.b = 6
        a.c = None
        a._zero_or_all = ['a b c'.split()]

        assert_raises(ValueError, a.check_input_attributes)

    def test_check_at_least_one(self):
        a = InputFileLoaderCheckerSaver()
        a.c = None
        a._at_least_one = ['c'.split()]

        assert_raises(ValueError, a.check_input_attributes)

    def test_check_one_implies_others(self):
        a = InputFileLoaderCheckerSaver()
        a.a = 4
        a.c = None
        a._one_implies_others = ['a c'.split()]
        assert_raises(ValueError, a.check_input_attributes)

    def test_check_attributes_to_force_same_len(self):
        a = InputFileLoaderCheckerSaver()
        a.a = [4, 5]
        a.c = None
        a._attributes_to_force_same_len = ['a c'.split()]
        a.check_input_attributes()

        assert_equal(a.c, [None, None])

    def test_check_attributes_that_should_have_same_x_limits(self):
        a = InputFileLoaderCheckerSaver()
        a.a = PolyLine([0, 1], [2, 5])
        a.c = PolyLine([0, 7], [5, 6])
        a._attributes_that_should_have_same_x_limits = ['a c'.split()]
        assert_raises(ValueError, a.check_input_attributes)

    def test_check_attributes_that_should_have_same_len_pairs(self):
        a = InputFileLoaderCheckerSaver()
        a.a = [2, 3]
        a.c = [3]
        a._attributes_that_should_have_same_len_pairs = ['a c'.split()]
        assert_raises(ValueError, a.check_input_attributes)

    def test_determine_output_stem_defaults(self):
        a = InputFileLoaderCheckerSaver()
        a._determine_output_stem()

        assert_equal(a._file_stem, '.\\out0002\\out0002')

    def test_determine_output_stem_overwrite(self):
        a = InputFileLoaderCheckerSaver()
        a.overwrite = True
        a._determine_output_stem()
        assert_equal(a._file_stem, '.\\out0001\\out0001')

    def test_determine_output_stem_create_directory(self):
        a = InputFileLoaderCheckerSaver()
        a.create_directory = False
        a._determine_output_stem()
        assert_equal(a._file_stem, '.\\out0002')

    def test_determine_output_stem_prefix(self):
        a = InputFileLoaderCheckerSaver()
        a.prefix = 'hello_'
        a._determine_output_stem()
        assert_equal(a._file_stem, '.\\hello_0001\\hello_0001')

    def test_determine_output_stem_directory(self):
        a = InputFileLoaderCheckerSaver()
        a.directory = os.path.join(self.tempdir.path, 'what')
        a._determine_output_stem()

        assert_equal(
            a._file_stem,
            os.path.join(self.tempdir.path, 'what', 'out0002', 'out0002'))

    def test_save_data_parsed(self):
        a = InputFileLoaderCheckerSaver()
        a._attributes = "a b ".split()
        a.save_data_to_file = True
        #        a._initialize_attributes()
        a.a = 4
        a.b = 6

        a._save_data()
        #        print(os.listdir(self.tempdir.path))
        #        print(os.listdir(os.path.join(self.tempdir.path,'out0002')))
        assert_equal(
            self.tempdir.read(('out0002', 'out0002_input_parsed.py'),
                              'utf-8').strip().splitlines(),
            'a = 4\nb = 6'.splitlines())

    def test_save_data_input_text(self):
        a = InputFileLoaderCheckerSaver()
        a._input_text = "hello"
        a.save_data_to_file = True
        a._save_data()
        assert_equal(
            self.tempdir.read(('out0002', 'out0002_input_original.py'),
                              'utf-8').strip().splitlines(),
            'hello'.splitlines())

    def test_save_data_input_ext(self):
        a = InputFileLoaderCheckerSaver()
        a._input_text = "hello"
        a.input_ext = '.txt'
        a.save_data_to_file = True
        a._save_data()

        ok_(
            os.path.isfile(
                os.path.join(self.tempdir.path, 'out0002',
                             'out0002_input_original.txt')))

    def test_save_data_grid_data_dicts(self):
        a = InputFileLoaderCheckerSaver()
        a._grid_data_dicts = {'data': np.arange(6).reshape(3, 2)}
        a.save_data_to_file = True
        a._save_data()

        #        print(os.listdir(os.path.join(self.tempdir.path,'out0002')))
        ok_(
            os.path.isfile(
                os.path.join(self.tempdir.path, 'out0002', 'out0002.csv')))

    def test_save_data_grid_data_dicts_data_ext(self):
        a = InputFileLoaderCheckerSaver()
        a._grid_data_dicts = {'data': np.arange(6).reshape(3, 2)}
        a.save_data_to_file = True
        a.data_ext = ".txt"
        a._save_data()

        #        print(os.listdir(os.path.join(self.tempdir.path,'out0002')))
        ok_(
            os.path.isfile(
                os.path.join(self.tempdir.path, 'out0002', 'out0002.txt')))

    def test_save_figures(self):
        a = InputFileLoaderCheckerSaver()
        a.save_figures_to_file = True
        fig = matplotlib.pyplot.figure()
        ax = fig.add_subplot('111')
        ax.plot(4, 5)
        fig.set_label('sing')
        a._figures = [fig]
        a._save_figures()
        a._figures = None
        matplotlib.pyplot.clf()
        #        print(os.listdir(os.path.join(self.tempdir.path,'out0002')))
        ok_(
            os.path.isfile(
                os.path.join(self.tempdir.path, 'out0002',
                             'out0002_sing.eps')))

    def test_save_figures_figure_ext(self):
        a = InputFileLoaderCheckerSaver()
        a.save_figures_to_file = True
        a.figure_ext = '.pdf'

        fig = matplotlib.pyplot.figure()
        ax = fig.add_subplot('111')
        ax.plot(4, 5)
        fig.set_label('sing')
        a._figures = [fig]
        a._save_figures()
        a._figures = None
        matplotlib.pyplot.clf()
        #        print(os.listdir(os.path.join(self.tempdir.path,'out0002')))
        ok_(
            os.path.isfile(
                os.path.join(self.tempdir.path, 'out0002',
                             'out0002_sing.pdf')))
Beispiel #24
0
class test_GenericInputFileArgParser(unittest.TestCase):
    """tests GenericInputFileArgParser"""
    def setUp(self):
        self.tempdir = TempDirectory()
        self.tempdir.write('a1.py', "a1", 'utf-8')
        self.tempdir.write('a2.py', "a2", 'utf-8')
        self.tempdir.write('b1.txt', "b1", 'utf-8')
        self.tempdir.write('b2.txt', "b2", 'utf-8')

    def tearDown(self):
        self.tempdir.cleanup()

    def _abc_fobj(self, fobj):
        """print file contents to out.zebra"""
        with open(os.path.join(self.tempdir.path, 'out.zebra'), 'a') as f:
            f.write(fobj.read() + '\n')
        return

    def _abc_path(self, path):
        """print file basename out.zebra"""
        with open(os.path.join(self.tempdir.path, 'out.zebra'), 'a') as f:
            f.write(os.path.basename(path) + '\n')
        return


#    def dog(self):
#        with open(os.path.join(self.tempdir.path, 'out.zebra'), 'a') as f:
#            f.write('dog\n')

    def test_directory_with_path(self):

        a = GenericInputFileArgParser(self._abc_path, False)
        args = '-d {0} -p'.format(self.tempdir.path).split()
        print(args)
        a.main(argv=args)

        assert_equal(
            self.tempdir.read(('out.zebra'), 'utf-8').splitlines(),
            textwrap.dedent("""\
                            a1.py
                            a2.py""").splitlines())

    def test_directory_with_fobj(self):

        a = GenericInputFileArgParser(self._abc_fobj, True)
        args = '-d {0} -p'.format(self.tempdir.path).split()
        #        print(args)
        a.main(argv=args)

        assert_equal(
            self.tempdir.read(('out.zebra'), 'utf-8').splitlines(),
            textwrap.dedent("""\
                            a1
                            a2""").splitlines())

    def test_pattern_with_path(self):

        a = GenericInputFileArgParser(self._abc_path, False)
        args = '-d {0} -p *.txt'.format(self.tempdir.path).split()
        #        print(args)
        a.main(argv=args)

        assert_equal(
            self.tempdir.read(('out.zebra'), 'utf-8').splitlines(),
            textwrap.dedent("""\
                            b1.txt
                            b2.txt""").splitlines())

    def test_pattern_with_fobj(self):

        a = GenericInputFileArgParser(self._abc_fobj, True)
        args = '-d {0} -p *.txt'.format(self.tempdir.path).split()
        #        print(args)
        a.main(argv=args)

        assert_equal(
            self.tempdir.read(('out.zebra'), 'utf-8').splitlines(),
            textwrap.dedent("""\
                            b1
                            b2""").splitlines())

    def test_filename_with_fobj(self):

        a = GenericInputFileArgParser(self._abc_fobj, True)
        args = '-f {0} {1}'.format(os.path.join(self.tempdir.path, 'a1.py'),
                                   os.path.join(self.tempdir.path,
                                                'b1.txt')).split()
        #        print(args)
        a.main(argv=args)

        assert_equal(
            self.tempdir.read(('out.zebra'), 'utf-8').splitlines(),
            textwrap.dedent("""\
                            a1
                            b1""").splitlines())

    def test_filename_with_path(self):

        a = GenericInputFileArgParser(self._abc_path, False)
        args = '-f {0} {1}'.format(os.path.join(self.tempdir.path, 'a1.py'),
                                   os.path.join(self.tempdir.path,
                                                'b1.txt')).split()
        #        print(args)
        a.main(argv=args)

        assert_equal(
            self.tempdir.read(('out.zebra'), 'utf-8').splitlines(),
            textwrap.dedent("""\
                            a1.py
                            b1.txt""").splitlines())

    def test_default_directory(self):

        a = GenericInputFileArgParser(self._abc_path, False)
        args = '-d -p'.format(self.tempdir.path).split()

        with working_directory(self.tempdir.path):
            a.main(argv=args)

        assert_equal(
            self.tempdir.read(('out.zebra'), 'utf-8').splitlines(),
            textwrap.dedent("""\
                            a1.py
                            a2.py""").splitlines())

    def test_methods_with_path(self):

        a = GenericInputFileArgParser(HelperForGenericInputFileArgParser,
                                      False, [('dog', [], {})])

        args = '-f {0} {1}'.format(os.path.join(self.tempdir.path, 'a1.py'),
                                   os.path.join(self.tempdir.path,
                                                'b1.txt')).split()

        a.main(argv=args)

        assert_equal(
            self.tempdir.read(('out.zebra'), 'utf-8').splitlines(),
            textwrap.dedent("""\
                            a1.py
                            dog
                            b1.txt
                            dog
                            """).splitlines())
class FunctionalTest(MockOpenHelper, TestCase):

    base = 'https://api-cl01.attask-ondemand.com/attask/api/unsupported'

    def setUp(self):
        super(FunctionalTest, self).setUp()
        self.log = LogCapture()
        self.addCleanup(self.log.uninstall)
        self.dir = TempDirectory()
        self.addCleanup(self.dir.cleanup)
        self.replace('logging.basicConfig', Mock())
        self.replace('workfront.generate.TARGET_ROOT', self.dir.path)

    def test_functional(self):
        self.replace('sys.argv', ['x'])

        self.server.add(
            url='/metadata',
            params='method=GET',
            response=json.dumps(dict(data=dict(objects=dict(
                SomeThing=dict(objCode='BAR', name='SomeThing'),
                OtherThing=dict(objCode='FOO', name='OtherThing'),
            ))))
        )

        self.server.add(
            url='/foo/metadata',
            params='method=GET',
            response=json.dumps(dict(data=dict(
                objCode='FOO',
                name='OtherThing',
                fields={"ID": {}, "anotherField": {}},
                references={},
                collections={},
                actions={},
            )))
        )

        self.server.add(
            url='/bar/metadata',
            params='method=GET',
            response=json.dumps(dict(data=dict(
                objCode='BAR',
                name='SomeThing',
                fields={"ID": {}, "theField": {}},
                references={"accessRules": {}},
                collections={"assignedTo": {}},
                actions={"doSomething": {
                    "arguments": [
                        {
                            "name": "anOption",
                            "type": "Task"
                        },
                        {
                            "name": "options",
                            "type": "string[]"
                        }
                    ],
                    "resultType": "string",
                    "label": "doSomething"
                }}
            )))
        )

        with OutputCapture() as output:
            output.disable()
            main()

        output.compare("")

        self.dir.compare(expected=['unsupported.py'])

        compare(self.dir.read('unsupported.py').decode('ascii'), expected=u'''\
# generated from https://api-cl01.attask-ondemand.com/attask/api/unsupported/metadata
from ..meta import APIVersion, Object, Field, Reference, Collection

api = APIVersion('unsupported')


class OtherThing(Object):
    code = 'FOO'
    another_field = Field('anotherField')

api.register(OtherThing)


class SomeThing(Object):
    code = 'BAR'
    the_field = Field('theField')
    access_rules = Reference('accessRules')
    assigned_to = Collection('assignedTo')

    def do_something(self, an_option=None, options=None):
        """
        The ``doSomething`` action.

        :param an_option: anOption (type: ``Task``)
        :param options: options (type: ``string[]``)
        :return: ``string``
        """
        params = {}
        if an_option is not None: params['anOption'] = an_option
        if options is not None: params['options'] = options
        data = self.session.put(self.api_url()+'/doSomething', params)
        return data['result']

api.register(SomeThing)
''',
                trailing_whitespace=False)
Beispiel #26
0
 def test_init_with_user(self, tmpdir: TempDirectory):
     Git(tmpdir.getpath('foo')).init(
         User(name='Foo Bar', email='*****@*****.**'))
     config = tmpdir.read('foo/.git/config')
     assert b'name = Foo Bar' in config
     assert b'email = [email protected]' in config
class TestDecoratedObjectTypes(MockOpenHelper, TestCase):

    def setUp(self):
        super(TestDecoratedObjectTypes, self).setUp()
        self.dir = TempDirectory()
        self.addCleanup(self.dir.cleanup)

    def test_normal(self):
        base_url = 'https://test.attask-ondemand.com/attask/api/v4.0'
        session = Session('test', api_version='v4.0')
        self.server.add(
            url=base_url+'/metadata',
            params='method=GET',
            response=json.dumps(dict(data=dict(objects=dict(
                SomeThing=dict(objCode='SMTHING', name='SomeThing')
            ))))
        )
        expected = dict(
            objCode='SMTHING',
            name='SomeThing',
            stuff='a value'
        )
        self.server.add(
            url=base_url+'/smthing/metadata',
            params='method=GET',
            response=json.dumps(dict(data=expected))
        )
        compare(decorated_object_types(session, None),
                expected=[('SomeThing', 'SMTHING', expected)])

    def test_cache_write(self):
        base_url = 'https://test.attask-ondemand.com/attask/api/v4.0'
        session = Session('test', api_version='v4.0')
        self.server.add(
            url=base_url+'/metadata',
            params='method=GET',
            response=json.dumps(dict(data=dict(objects=dict(
                SomeThing=dict(objCode='SMTHING', name='SomeThing')
            ))))
        )
        expected = dict(
            objCode='SMTHING',
            name='SomeThing',
            stuff='a value'
        )
        self.server.add(
            url=base_url+'/smthing/metadata',
            params='method=GET',
            response=json.dumps(dict(data=expected))
        )
        compare(decorated_object_types(session, self.dir.path),
                expected=[('SomeThing', 'SMTHING', expected)])
        self.dir.compare(expected=[
            'v4.0_metadata.json', 'v4.0_smthing_metadata.json'
        ])
        compare(
            json.loads(self.dir.read('v4.0_metadata.json').decode('ascii')),
            expected=dict(objects=dict(
                SomeThing=dict(objCode='SMTHING', name='SomeThing')
            )))
        compare(
            json.loads(
                self.dir.read('v4.0_smthing_metadata.json').decode('ascii')
            ),
            expected=expected
        )

    def test_cache_read(self):
        expected = dict(
            objCode='SMTHING',
            name='SomeThing',
            stuff='a value'
        )

        self.dir.write('v4.0_metadata.json', json.dumps(dict(objects=dict(
                SomeThing=dict(objCode='SMTHING', name='SomeThing')
        ))), encoding='ascii')

        self.dir.write('v4.0_smthing_metadata.json',
                       json.dumps(expected),
                       encoding='ascii')

        session = Session('test', api_version='v4.0')
        compare(decorated_object_types(session, self.dir.path),
                expected=[('SomeThing', 'SMTHING', expected)])

    def test_unsupported(self):
        base_url = 'https://test.attask-ondemand.com/attask/api/unsupported'
        session = Session('test')
        self.server.add(
            url=base_url+'/metadata',
            params='method=GET',
            response=json.dumps(dict(data=dict(objects=dict(
                SomeThing=dict(objCode='SMTHING', name='SomeThing')
            ))))
        )
        expected = dict(
            objCode='SMTHING',
            name='SomeThing',
            stuff='a value'
        )
        self.server.add(
            url=base_url+'/smthing/metadata',
            params='method=GET',
            response=json.dumps(dict(data=expected))
        )
        compare(decorated_object_types(session, None),
                expected=[('SomeThing', 'SMTHING', expected)])

    def test_name_override(self):
        base_url = 'https://test.attask-ondemand.com/attask/api/unsupported'
        session = Session('test')
        self.server.add(
            url=base_url+'/metadata',
            params='method=GET',
            response=json.dumps(dict(data=dict(objects=dict(
                SomeThing=dict(objCode='OPTASK', name='SomeThing')
            ))))
        )
        expected = dict(
            objCode='SMTHING',
            name='SomeThing',
            stuff='a value'
        )
        self.server.add(
            url=base_url+'/optask/metadata',
            params='method=GET',
            response=json.dumps(dict(data=expected))
        )
        compare(decorated_object_types(session, None),
                expected=[('Issue', 'OPTASK', expected)])
class TestStocksStore(unittest.TestCase):
    def setUp(self):
        self.test_dir = TempDirectory()
        pass

    def tearDown(self):
        self.test_dir.cleanup()
        pass

    def rowToByte(self, row):
        return bytes(','.join(row) + '\r\n', 'UTF-8')

    def test_creation_object(self):
        path = self.test_dir.path
        file = 'file'
        storeObject = StoreService(path, file)

        self.assertEqual(storeObject.path, path)
        self.assertEqual(storeObject.file_name, file)

    def test_creation_with_empty_values(self):
        path = ''
        file = ''
        storeObject = StoreService(path, file)

        self.assertEqual(storeObject.path, os.getcwd())
        self.assertEqual(storeObject.file_name, 'StoreServiceFile')

    def test_check_real_directory_exists(self):
        path = self.test_dir.path
        file = ''
        storeObject = StoreService(path, file)

        self.assertEqual(storeObject.check_directory(), True)

    def test_check_fake_directory_not_exists(self):
        path = 'xjxjxj'
        file = ''
        storeObject = StoreService(path, file)

        self.assertEqual(storeObject.check_directory(), False)

    def test_file_is_create(self):
        path = self.test_dir.path
        file = 'testFile'
        storeObject = StoreService(path, file)
        storeObject.open_file()
        storeObject.close_file()

        self.assertEqual(os.path.isfile(os.path.join(path, file)), True)

    def test_write_list_to_file(self):
        path = self.test_dir.path
        file = 'testFile.csv'
        storeObject = StoreService(path, file)
        storeObject.open_file()
        row = ['dato', '01', '08']
        storeObject.write_row(row)
        storeObject.close_file()

        compare(self.test_dir.read(file), self.rowToByte(row))

    def test_write_string_to_file(self):
        path = self.test_dir.path
        file = 'testFile.csv'
        storeObject = StoreService(path, file)
        storeObject.open_file()
        row = 'hola'
        storeObject.write_row(row)
        storeObject.close_file()

        compare(self.test_dir.read(file), self.rowToByte(row))
Beispiel #29
0
class TestLocalFile(unittest.TestCase):
    """
    python -m unittest -v test.util.test_file_strategies.TestLocalFile
    """
    def setUp(self):
        self.tmp_dir = TempDirectory()
        self.tmp_dir.write('foo.txt', 'bar', encoding='utf-8')

    def tearDown(self):
        self.tmp_dir.cleanup()

    def test_attributes(self):
        """should build attribute data to describe file"""
        path = os.path.join(self.tmp_dir.path, 'foo.txt')
        subject = LocalFile(path)

        self.assertEqual(subject.protocol, 'file://')
        self.assertEqual(subject.basename, 'foo.txt')
        self.assertEqual(subject.dir_path, self.tmp_dir.path)

    def test_get_contents(self):
        """should return contents as bytes from local file"""
        path = os.path.join(self.tmp_dir.path, 'foo.txt')
        subject = LocalFile(path)

        actual = subject.get_contents().getvalue()
        expected = b'bar'

        self.assertEqual(actual, expected)

    def test_put_byte_contents(self):
        """should store contents passed in as bytes to local file"""
        path = os.path.join(self.tmp_dir.path, 'my_dir', 'foo.txt')
        subject = LocalFile(path)

        subject.put_contents(b'baz')
        actual = self.tmp_dir.read('my_dir/foo.txt', encoding='utf-8')
        expected = 'baz'

        self.assertEqual(actual, expected)

    def test_put_str_contents(self):
        """should store contents passed in as str to local file"""
        path = os.path.join(self.tmp_dir.path, 'my_dir', 'foo.txt')
        subject = LocalFile(path)

        subject.put_contents('baz')
        actual = self.tmp_dir.read('my_dir/foo.txt', encoding='utf-8')
        expected = 'baz'

        self.assertEqual(actual, expected)

    def test_exists(self):
        """should determine if a local file exists or not"""
        present_path = os.path.join(self.tmp_dir.path, 'foo.txt')
        absent_path = os.path.join(self.tmp_dir.path, 'missing.txt')
        present_subject = LocalFile(present_path)
        absent_subject = LocalFile(absent_path)

        self.assertTrue(present_subject.exists())
        self.assertFalse(absent_subject.exists())