NCBI C++ ToolKit
flatfile_parser_integration_tests.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: flatfile_parser_integration_tests.cpp 95492 2021-11-19 13:37:10Z foleyjp $
2 * ===========================================================================
3 *
4 * PUBLIC DOMAIN NOTICE
5 * National Center for Biotechnology Information
6 *
7 * This software/database is a "United States Government Work" under the
8 * terms of the United States Copyright Act. It was written as part of
9 * the author's official duties as a United States Government employee and
10 * thus cannot be copyrighted. This software/database is freely available
11 * to the public for use. The National Library of Medicine and the U.S.
12 * Government have not placed any restriction on its use or reproduction.
13 *
14 * Although all reasonable efforts have been taken to ensure the accuracy
15 * and reliability of the software and data, the NLM and the U.S.
16 * Government do not and cannot warrant the performance or results that
17 * may be obtained by using this software or data. The NLM and the U.S.
18 * Government disclaim all warranties, express or implied, including
19 * warranties of performance, merchantability or fitness for any particular
20 * purpose.
21 *
22 * Please cite the author in any work or product based on this material.
23 *
24 * ===========================================================================
25 *
26 * Author: Justin Foley
27 *
28 * File Description:
29 * Unit tests for code processing secondary accessions
30 *
31 */
32 
33 #include <ncbi_pch.hpp>
34 
35 // This header must be included before all Boost.Test headers if there are any
36 #include <corelib/test_boost.hpp>
37 #include <corelib/ncbifile.hpp>
40 #include <common/test_assert.h> /* This header must go last */
41 
42 
45 
46 
47 BOOST_AUTO_TEST_CASE(TestIstreamInterface)
48 {
49  string test_data_dir {"test_data"};
50 
51  unique_ptr<Parser> pConfig(new Parser());
52  pConfig->mode = Parser::EMode::Relaxed;
53  pConfig->output_format = Parser::EOutput::Seqsubmit;
54  string format {"genbank"};
55  string source {"ncbi"};
57 
58  string filestub = CDir::ConcatPath(test_data_dir, "TP53_MH011443");
59  string inputFile = filestub + ".gb";
60  BOOST_REQUIRE(CDirEntry(inputFile).Exists());
61 
62  auto pIstr = make_unique<CNcbiIfstream>(inputFile);
63  CFlatFileParser ffparser(nullptr);
64  auto pResult = ffparser.Parse(*pConfig, *pIstr);
65  BOOST_REQUIRE(pResult.NotNull());
66 
67  const string& outputName = CDirEntry::GetTmpName();
68  CNcbiOfstream ofstr(outputName);
69  ofstr << MSerial_AsnText << *pResult;
70  ofstr.close();
71 
72  CFile goldenFile(filestub + ".asn");
73  BOOST_REQUIRE(goldenFile.Exists());
74 
75  bool success = goldenFile.CompareTextContents(outputName, CFile::eIgnoreWs);
76 
77  if (!success) {
78  const auto& args = CNcbiApplication::Instance()->GetArgs();
79  if (args["keep-diffs"]) {
80  CDirEntry outputFile(outputName);
81  outputFile.Copy(filestub + ".istr_interface");
82  }
83  }
84 
85  BOOST_REQUIRE(success);
86 }
87 
88 
89 BOOST_AUTO_TEST_CASE(TestFileInterface)
90 {
91  string test_data_dir {"test_data"};
92 
93  unique_ptr<Parser> pConfig(new Parser());
94  pConfig->mode = Parser::EMode::Relaxed;
95  pConfig->output_format = Parser::EOutput::Seqsubmit;
96  string format {"genbank"};
97  string source {"ncbi"};
99 
100  string filestub = CDir::ConcatPath(test_data_dir, "TP53_MH011443");
101  string inputFile = filestub + ".gb";
102  BOOST_REQUIRE(CDirEntry(inputFile).Exists());
103 
104  CFlatFileParser ffparser(nullptr);
105  auto pResult = ffparser.Parse(*pConfig, inputFile);
106  BOOST_REQUIRE(pResult.NotNull());
107 
108  const string& outputName = CDirEntry::GetTmpName();
109  CNcbiOfstream ofstr(outputName);
110  ofstr << MSerial_AsnText << *pResult;
111  ofstr.close();
112 
113  CFile goldenFile(filestub + ".asn");
114  BOOST_REQUIRE(goldenFile.Exists());
115 
116  bool success = goldenFile.CompareTextContents(outputName, CFile::eIgnoreWs);
117 
118  if (!success) {
119  const auto& args = CNcbiApplication::Instance()->GetArgs();
120  if (args["keep-diffs"]) {
121  CDirEntry outputFile(outputName);
122  outputFile.Copy(filestub + ".file_interface");
123  }
124  }
125 
126  BOOST_REQUIRE(success);
127 }
128 
130 {
131  string test_data_dir {"test_data"};
132 
133  unique_ptr<Parser> pConfig(new Parser());
134  pConfig->mode = Parser::EMode::Release;
135  pConfig->output_format = Parser::EOutput::Seqsubmit;
136  string format {"embl"};
137  string source {"embl"};
138  fta_set_format_source(*pConfig, format, source);
139 
140  string filestub = CDir::ConcatPath(test_data_dir, "AJ000076");
141  string inputFile = filestub + ".embl";
142  BOOST_REQUIRE(CDirEntry(inputFile).Exists());
143 
144  CFlatFileParser ffparser(nullptr);
145  auto pResult = ffparser.Parse(*pConfig, inputFile);
146  BOOST_REQUIRE(pResult.NotNull());
147 
148  const string& outputName = CDirEntry::GetTmpName();
149  CNcbiOfstream ofstr(outputName);
150  ofstr << MSerial_AsnText << *pResult;
151  ofstr.close();
152 
153  CFile goldenFile(filestub + ".asn");
154  BOOST_REQUIRE(goldenFile.Exists());
155 
156  bool success = goldenFile.CompareTextContents(outputName, CFile::eIgnoreWs);
157 
158  if (!success) {
159  const auto& args = CNcbiApplication::Instance()->GetArgs();
160  if (args["keep-diffs"]) {
161  CDirEntry outputFile(outputName);
162  outputFile.Copy(filestub + ".new");
163  }
164  }
165 
166  BOOST_REQUIRE(success);
167 }
168 
169 
171 {
172  argDescrs->AddFlag("keep-diffs",
173  "Keep output files that are different from expected.",
174  true);
175 }
176 
CDirEntry –.
Definition: ncbifile.hpp:262
CFile –.
Definition: ncbifile.hpp:1605
CRef< CSerialObject > Parse(Parser &parseInfo)
Definition: ftamain.cpp:721
static CNcbiApplication * Instance(void)
Singleton method.
Definition: ncbiapp.cpp:264
USING_SCOPE(objects)
NCBITEST_INIT_CMDLINE(argDescrs)
BOOST_AUTO_TEST_CASE(TestIstreamInterface)
bool fta_set_format_source(Parser &pp, const std::string &format, const std::string &source)
virtual const CArgs & GetArgs(void) const
Get parsed command line arguments.
Definition: ncbiapp.cpp:305
bool CompareTextContents(const string &file, ECompareText mode, size_t buf_size=0) const
Compare files contents in text form.
Definition: ncbifile.cpp:3565
static string GetTmpName(ETmpFileCreationMode mode=eTmpFileGetName)
Get temporary file name.
Definition: ncbifile.cpp:2903
static string ConcatPath(const string &first, const string &second)
Concatenate two parts of the path for the current OS.
Definition: ncbifile.cpp:776
virtual bool Copy(const string &new_path, TCopyFlags flags=fCF_Default, size_t buf_size=0) const
Copy the entry to a location specified by "new_path".
Definition: ncbifile.cpp:2428
virtual bool Exists(void) const
Check existence of file.
Definition: ncbifile.hpp:4039
@ eIgnoreWs
Definition: ncbifile.hpp:1669
#define MSerial_AsnText
I/O stream manipulators –.
Definition: serialbase.hpp:696
IO_PREFIX::ofstream CNcbiOfstream
Portable alias for ofstream.
Definition: ncbistre.hpp:500
const CharType(& source)[N]
Definition: pointer.h:1149
Defines classes: CDirEntry, CFile, CDir, CSymLink, CMemoryFile, CFileUtil, CFileLock,...
static Format format
Definition: njn_ioutil.cpp:53
Utility stuff for more convenient using of Boost.Test library.
Modified on Fri Sep 20 14:57:10 2024 by modify_doxy.py rev. 669887
HHS Vulnerability Disclosure