def test_file_already_exits(self):
        bag_file_name="{}{}{}".format(self.tmp_dir.name, os.sep, BAGNAME)

        bag1 = baggie.Baggie(bag_file_name, mode="w")

        with self.assertRaises(baggie.BaggieException):
            bag2 = baggie.Baggie(bag_file_name, mode="w")
    def test_illegal_override_types(self):
        bag_file_name="{}{}{}".format(self.tmp_dir.name, os.sep, BAGNAME)

        with self.assertRaises(TypeError):
            bag = baggie.Baggie(bag_file_name, mode="w", storage_opts="foo")

        with self.assertRaises(TypeError):
            bag = baggie.Baggie(bag_file_name, mode="w", converter_opts=100)

        with self.assertRaises(TypeError):
            bag = baggie.Baggie(bag_file_name, mode="w", compression_opts=1.0)
    def test_compressed(self):
        bag_file_name="{}{}{}".format(self.tmp_dir.name, os.sep, BAGNAME)
        bag = baggie.Baggie(bag_file_name, mode="w", compress=True)

        for i in range(N_MSGS):
            msg = Int32()
            msg.data = i
            bag.write(TOPIC_NAME, msg)
            time.sleep(1./N_MSGS)
    def test_readonly_write(self):
        bag_file_name = "{}{}{}".format(self.tmp_dir.name, os.sep,
                                        BAGNAME_COMPRESSED)

        bag = baggie.Baggie(bag_file_name, mode="r")
        int_msg = Int32()
        int_msg.data = 1000

        with self.assertRaises(baggie.BaggieException):
            bag.write(TOPIC_INT, int_msg)
    def test_meta(self):
        bag_file_name = "{}{}{}".format(self.tmp_dir.name, os.sep, BAGNAME)

        bag = baggie.Baggie(bag_file_name, mode="r")
        meta = bag.meta()

        self.assertIsInstance(meta, baggie._BagMetadata)
        self.assertIsInstance(meta.duration, datetime.timedelta)
        self.assertIsInstance(meta.starting_time, datetime.datetime)
        self.assertEqual(meta.message_count, N_MSGS * 2)
    def test_compressed_reader(self):
        bag_file_name = "{}{}{}".format(self.tmp_dir.name, os.sep,
                                        BAGNAME_COMPRESSED)

        bag = baggie.Baggie(bag_file_name, mode="r")

        bag = baggie.Baggie(bag_file_name, mode="r")
        i = 0
        last_time = Time(nanoseconds=0)
        for topic, msg, ts in bag.read_messages():
            if topic == TOPIC_INT:
                self.assertIsInstance(msg, Int32)
            elif topic == TOPIC_STR:
                self.assertIsInstance(msg, String)

            self.assertIsInstance(ts, Time)
            self.assertTrue(last_time <= ts)
            last_time = ts
            i += 1

        self.assertEqual(i, N_MSGS * 2)
    def test_reader_topic_filtered(self):
        bag_file_name = "{}{}{}".format(self.tmp_dir.name, os.sep, BAGNAME)

        bag = baggie.Baggie(bag_file_name, mode="r")
        i = 0
        last_time = Time(nanoseconds=0)
        for topic, msg, ts in bag.read_messages(topics=[TOPIC_INT]):
            self.assertIsInstance(msg, Int32)
            self.assertIsInstance(ts, Time)
            self.assertTrue(last_time <= ts)
            last_time = ts
            i += 1

        self.assertEqual(i, N_MSGS)
    def setUp(self):
        self.tmp_dir = tempfile.TemporaryDirectory()

        bag_file_name = "{}{}{}".format(self.tmp_dir.name, os.sep, BAGNAME)
        bag = baggie.Baggie(bag_file_name, mode="w", compress=False)

        bag_comp_file_name = "{}{}{}".format(self.tmp_dir.name, os.sep,
                                             BAGNAME_COMPRESSED)
        bag_comp = baggie.Baggie(bag_comp_file_name, mode="w", compress=True)

        for i in range(N_MSGS):
            int_msg = Int32()
            int_msg.data = i

            str_msg = String()
            str_msg.data = "The count is: %s" % i

            bag.write(TOPIC_INT, int_msg)
            bag.write(TOPIC_STR, str_msg)

            bag_comp.write(TOPIC_INT, int_msg)
            bag_comp.write(TOPIC_STR, str_msg)

            time.sleep(1. / N_MSGS)
    def test_legal_override_types(self):
        bag_file_name="{}{}{}".format(self.tmp_dir.name, os.sep, BAGNAME)

        s_opt = baggie._StorageOptions()
        s_opt.storage_id = baggie.Baggie.DEFAULT_STORAGE_ID

        c_opt = baggie._ConverterOptions()
        c_opt.input_serialization_format = \
          baggie.Baggie.DEFAULT_SERIALIZATION_FORMAT
        c_opt.output_serialization_format = \
          baggie.Baggie.DEFAULT_SERIALIZATION_FORMAT

        comp_opt = baggie._CompressionOptions()
        comp_opt.compression_format = baggie.Baggie.DEFAULT_COMPRESSION_FORMAT
        comp_opt.compression_mode = baggie.Baggie.DEFAULT_COMPRESSION_MODE

        bag = baggie.Baggie(bag_file_name, mode="w",
                            storage_opts=s_opt,
                            converter_opts=c_opt,
                            compression_opts=comp_opt)
    def test_file_does_not_exist(self):
        bag_file_name = "{}{}{}".format(self.tmp_dir.name, os.sep,
                                        BAGNAME + "_foo")

        with self.assertRaises(baggie.BaggieException):
            bag = baggie.Baggie(bag_file_name, mode="r")