directory_models_archive_reader_ut.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #include "directory_models_archive_reader.h"
  2. #include <library/cpp/testing/unittest/registar.h>
  3. #include <util/folder/tempdir.h>
  4. #include <util/string/cast.h>
  5. #include <util/stream/file.h>
  6. #include <util/system/tempfile.h>
  7. #include <util/memory/blob.h>
  8. class TDirectoryModelsArchiveReaderTest: public TTestBase {
  9. UNIT_TEST_SUITE(TDirectoryModelsArchiveReaderTest)
  10. UNIT_TEST(TestRead);
  11. UNIT_TEST_SUITE_END();
  12. private:
  13. void TestRead();
  14. };
  15. UNIT_TEST_SUITE_REGISTRATION(TDirectoryModelsArchiveReaderTest);
  16. const TString MAIN_DIR = "./dir";
  17. const TString SUBDIR = "/subdir";
  18. const TString SAMPLE_FILE1 = "/sample1";
  19. const TString SAMPLE_FILE2 = "/sample2";
  20. const TString TEST_TEXT = "Test Text.";
  21. void TDirectoryModelsArchiveReaderTest::TestRead() {
  22. TTempDir mainDir(MAIN_DIR);
  23. TTempDir subDir(MAIN_DIR + SUBDIR);
  24. TTempFileHandle file1(MAIN_DIR + SAMPLE_FILE1);
  25. TTempFileHandle file2(MAIN_DIR + SUBDIR + SAMPLE_FILE2);
  26. file1.Write(TEST_TEXT.data(), TEST_TEXT.size());
  27. file1.FlushData();
  28. TDirectoryModelsArchiveReader reader(MAIN_DIR, false);
  29. UNIT_ASSERT_EQUAL(reader.Count(), 2);
  30. UNIT_ASSERT(reader.Has(SAMPLE_FILE1));
  31. UNIT_ASSERT(reader.Has(SUBDIR + SAMPLE_FILE2));
  32. UNIT_ASSERT_EQUAL(reader.KeyByIndex(0), SAMPLE_FILE1);
  33. UNIT_ASSERT_EQUAL(reader.KeyByIndex(1), SUBDIR + SAMPLE_FILE2);
  34. TBlob blob = reader.BlobByKey(SAMPLE_FILE1);
  35. Cout << "'" << TString(blob.AsCharPtr(), blob.Size()) << "' - '" << TEST_TEXT << "'" << Endl;
  36. UNIT_ASSERT_VALUES_EQUAL(TString(blob.AsCharPtr(), blob.Size()), TString(TEST_TEXT));
  37. TAutoPtr<IInputStream> is = reader.ObjectByKey(SAMPLE_FILE1);
  38. const TString data = is->ReadAll();
  39. Cout << "'" << data << "' - '" << TEST_TEXT << "'" << Endl;
  40. UNIT_ASSERT_VALUES_EQUAL(data, TEST_TEXT);
  41. }