コード例 #1
0
    def test_compute(self):
        raw_hdr, hdr = mp3_header_test.VALID_MP3_HEADERS.items()[0]
        frame_data = raw_hdr + ("x" * (hdr.frame_size - len(raw_hdr)))

        for i, seq in enumerate((
            [ frame_data ],
            [ "junk", frame_data ],
            [ frame_data, "junk" ],
            [ "junk", frame_data, "junk" ],
            [ frame_data, frame_data ],
            [ "junk", frame_data, frame_data ],
            [ frame_data, "junk", frame_data ],
            [ frame_data, frame_data, "junk" ],
            [ "junk", frame_data, "junk", frame_data ],
            [ frame_data, "junk", frame_data, "junk" ],
            [ "junk", frame_data, frame_data, "junk" ],
            [ "junk", frame_data, "junk", frame_data, "junk" ],
            )):
            sha1_calc = hashlib.sha1()
            for data in seq:
                if data == frame_data:
                    sha1_calc.update(data)
            expected_fingerprint = sha1_calc.hexdigest()

            stream = cStringIO.StringIO(''.join(seq))
            actual_fingerprint = fingerprint.compute(stream)
            self.assertEqual(expected_fingerprint, actual_fingerprint,
                             msg="Case #%d failed" % i)
            self.assertTrue(fingerprint.is_valid(actual_fingerprint))

        # We return None if we cannot find any valid frames.
        stream = cStringIO.StringIO('no valid MPEG frames')
        self.assertTrue(fingerprint.compute(stream) is None)
コード例 #2
0
    def test_crawling(self):
        crawl = crawler.Crawler()
        crawl.add_root(os.path.join(TESTDATA, "test_tree"))

        seen_files = set()
        for au_file in crawl:
            # Each file has the filename in TPE1.
            tpe1 = au_file.mutagen_id3["TPE1"]
            self.assertTrue(au_file.path.endswith(tpe1.text[0]))
            # There should be a header.
            self.assertTrue(au_file.mp3_header is not None)
            # Make sure the fingerprint returned by the crawler matches
            # what is returned by fingerprint.compute().
            f_in = open(au_file.path)
            computed_fp = fingerprint.compute(f_in)
            self.assertEqual(computed_fp, au_file.fingerprint)
            # Each test file has the fingerprint in a UFID tag.
            # Make sure it matches.
            ufid = au_file.mutagen_id3["UFID:test"]
            self.assertEqual(ufid.data, au_file.fingerprint)
            # TODO(trow): We should have a better test of the frame count
            # and frame size.
            self.assertTrue(au_file.frame_count > 0)
            self.assertTrue(au_file.frame_size > 0)
            # Remember the basename of each file that we see.
            seen_files.add(os.path.basename(au_file.path))
        # Make sure we've seen all of the files we expect.
        self.assertEqual(15, len(seen_files))
        for a in ("A", "B", "C"):
            for b in range(5):
                self.assertTrue("%s%d.mp3" % (a, b) in seen_files)
        # Make sure we've seen the directories we expect.
        self.assertEqual(
            [os.path.join(TESTDATA, "test_tree", x) for x in ("A", "B", "C")],
            sorted(crawl.directories_seen))
        # We should have skipped one file.
        self.assertEqual(1, len(crawl.skipped_files))
        self.assertEqual(
            os.path.join(TESTDATA, "test_tree/A/invalid_file.mp3"),
            crawl.skipped_files[0][0])
コード例 #3
0
    def test_crawling(self):
        crawl = crawler.Crawler()
        crawl.add_root(os.path.join(TESTDATA, "test_tree"))

        seen_files = set()
        for au_file in crawl:
            # Each file has the filename in TPE1.
            tpe1 = au_file.mutagen_id3["TPE1"]
            self.assertTrue(au_file.path.endswith(tpe1.text[0]))
            # There should be a header.
            self.assertTrue(au_file.mp3_header is not None)
            # Make sure the fingerprint returned by the crawler matches
            # what is returned by fingerprint.compute().
            f_in = open(au_file.path)
            computed_fp = fingerprint.compute(f_in)
            self.assertEqual(computed_fp, au_file.fingerprint)
            # Each test file has the fingerprint in a UFID tag.
            # Make sure it matches.
            ufid = au_file.mutagen_id3["UFID:test"]
            self.assertEqual(ufid.data, au_file.fingerprint)
            # TODO(trow): We should have a better test of the frame count
            # and frame size.
            self.assertTrue(au_file.frame_count > 0)
            self.assertTrue(au_file.frame_size > 0)
            # Remember the basename of each file that we see.
            seen_files.add(os.path.basename(au_file.path))
        # Make sure we've seen all of the files we expect.
        self.assertEqual(15, len(seen_files))
        for a in ("A", "B", "C"):
            for b in range(5):
                self.assertTrue("%s%d.mp3" % (a, b) in seen_files)
        # Make sure we've seen the directories we expect.
        self.assertEqual(
            [os.path.join(TESTDATA, "test_tree", x) for x in ("A", "B", "C")], sorted(crawl.directories_seen)
        )
        # We should have skipped one file.
        self.assertEqual(1, len(crawl.skipped_files))
        self.assertEqual(os.path.join(TESTDATA, "test_tree/A/invalid_file.mp3"), crawl.skipped_files[0][0])