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

Go to the SVN repository for this file.

1 /* $Id: test_cached_taxon.cpp 68227 2015-07-22 11:14:39Z holmesbr $
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: Brad Holmes, NCBI
27 *
28 * File Description:
29 * Unit tests for the cached taxon
30 *
31 * ===========================================================================
32 */
33 
34 #include <ncbi_pch.hpp>
35 
36 #include <corelib/ncbi_system.hpp>
37 
38 
39 // This header must be included before all Boost.Test headers if there are any
40 #include <corelib/test_boost.hpp>
41 
44 
47 
48 class CMockTaxon : public ITaxon3
49 {
50 public:
51 
52  typedef list<CRef<CTaxon3_reply> > TReplies;
53 
55  const string& estr = kEmptyStr) :
56  m_replies(replies),
57  m_error_string(estr)
58  {
59  }
60 
61 private:
63  const string m_error_string;
64 
65 
66 public:
67 
68  virtual void Init(void) {}
69 
70  virtual void Init(const STimeout* timeout, unsigned reconnect_attempts /*= 5*/) {}
71 
72  virtual const string& GetLastError() const { return m_error_string; }
73 
75  {
76  CRef<CTaxon3_reply> v = m_replies.front();
77  m_replies.pop_front();
78  return v;
79  }
80 
81  virtual CRef<CTaxon3_reply> SendOrgRefList(const vector<CRef< COrg_ref> >& list)
82  {
83  CRef<CTaxon3_reply> v = m_replies.front();
84  m_replies.pop_front();
85  return v;
86  }
87 };
88 
90 {
92 
94  rq->SetTaxid(9606);
95 
96  request->SetRequest().push_back(rq);
97 
98  return request;
99 }
100 
102 {
104  CRef<CT3Reply> t3reply(new CT3Reply);
106  t3reply->SetError().SetMessage(message);
107  reply->SetReply().push_back(t3reply);
108  return reply;
109 }
110 
111 CRef<COrg_ref> s_CreateOrgRef(const int& taxid, const string& taxname)
112 {
113  CRef<COrg_ref> org_ref(new COrg_ref);
114  org_ref->SetTaxId(taxid);
115  org_ref->SetTaxname(taxname);
116  return org_ref;
117 }
118 
119 
120 BOOST_AUTO_TEST_CASE(create_safe_cached_taxon)
121 {
122 
123  CMockTaxon::TReplies replies;
124  const string message = "create_safe_cached_taxon";
125  replies.push_back(s_CreateReplyWithMessage(message));
126 
127 
128  AutoPtr<ITaxon3> mock_taxon(new CMockTaxon(replies, kEmptyStr));
129  AutoPtr<CCachedTaxon3> cached_taxon = CCachedTaxon3::Create(mock_taxon, 100);
130 
131  BOOST_CHECK_NO_THROW(cached_taxon->SendRequest(*s_GetDummyRequest()));
132 }
133 
134 BOOST_AUTO_TEST_CASE(send_request)
135 {
136 
137  CMockTaxon::TReplies replies;
138  const string message = "create_safe_cached_taxon";
139  replies.push_back(s_CreateReplyWithMessage(message));
140 
141 
142  AutoPtr<ITaxon3> mock_taxon(new CMockTaxon(replies, kEmptyStr));
143  AutoPtr<CCachedTaxon3> cached_taxon = CCachedTaxon3::Create(mock_taxon, 100);
144 
145  CRef<CTaxon3_reply> new_reply = cached_taxon->SendRequest(*s_GetDummyRequest());
146 
147  BOOST_CHECK_EQUAL(new_reply->GetReply().size(), 1);
148  BOOST_CHECK_EQUAL(new_reply->GetReply().front()->GetError().GetMessage(), message);
149 }
150 
151 
152 
153 BOOST_AUTO_TEST_CASE(create_unsafe_cached_taxon)
154 {
155  CMockTaxon::TReplies replies;
156 
157  const string message = "create_unsafe_cached_taxon";
158  replies.push_back(s_CreateReplyWithMessage(message));
159 
160  AutoPtr<ITaxon3> mock_taxon(new CMockTaxon(replies, kEmptyStr));
161  CCachedTaxon3* cached_taxon = CCachedTaxon3::CreateUnSafe(mock_taxon, 100);
162 
163  CRef<CTaxon3_reply> new_reply = cached_taxon->SendRequest(*s_GetDummyRequest());
164 
165  BOOST_CHECK_EQUAL(new_reply->GetReply().size(), 1);
166  BOOST_CHECK_EQUAL(new_reply->GetReply().front()->GetError().GetMessage(), message);
167 }
168 
169 
171 {
172  CMockTaxon::TReplies replies;
173 
174  replies.push_back(s_CreateReplyWithMessage("reply1"));
175  replies.push_back(s_CreateReplyWithMessage("reply2"));
176  replies.push_back(s_CreateReplyWithMessage("reply3"));
177 
178  AutoPtr<ITaxon3> mock_taxon(new CMockTaxon(replies, kEmptyStr));
179  AutoPtr<CCachedTaxon3> cached_taxon = CCachedTaxon3::Create(mock_taxon, 100);
180 
181  //Create org list to send.
182  vector<CRef< COrg_ref> > org_list;
183  org_list.push_back(s_CreateOrgRef(9606, "human"));
184  org_list.push_back(s_CreateOrgRef(9606, "human"));
185  org_list.push_back(s_CreateOrgRef(10900, "mouse"));
186 
187 
188  CRef<CTaxon3_reply> new_reply = cached_taxon->SendOrgRefList(org_list);
189 
190  BOOST_CHECK_EQUAL(new_reply->GetReply().size(), 3);
191  CTaxon3_reply::TReply::const_iterator it = new_reply->GetReply().begin();
192  BOOST_CHECK_EQUAL((*it)->GetError().GetMessage(), "reply1");
193  ++it;
194  BOOST_CHECK_EQUAL((*it)->GetError().GetMessage(), "reply1"); //cache hit
195  ++it;
196  BOOST_CHECK_EQUAL((*it)->GetError().GetMessage(), "reply2");
197 }
198 
199 
200 
static AutoPtr< CCachedTaxon3 > Create(AutoPtr< ITaxon3 > taxon, TSizeType capacity=100000)
virtual CRef< CTaxon3_reply > SendRequest(const CTaxon3_request &request)
static CCachedTaxon3 * CreateUnSafe(AutoPtr< ITaxon3 > taxon, TSizeType capacity=100000)
virtual const string & GetLastError() const
list< CRef< CTaxon3_reply > > TReplies
virtual void Init(const STimeout *timeout, unsigned reconnect_attempts)
CMockTaxon(TReplies replies, const string &estr=kEmptyStr)
virtual CRef< CTaxon3_reply > SendOrgRefList(const vector< CRef< COrg_ref > > &list)
virtual CRef< CTaxon3_reply > SendRequest(const CTaxon3_request &request)
virtual void Init(void)
TReplies m_replies
const string m_error_string
TTaxId SetTaxId(TTaxId tax_id)
Definition: Org_ref.cpp:93
CRef –.
Definition: ncbiobj.hpp:618
CT3Reply –.
Definition: T3Reply.hpp:66
CT3Request –.
Definition: T3Request.hpp:66
CTaxon3_reply –.
CTaxon3_request –.
#define kEmptyStr
Definition: ncbistr.hpp:123
void SetTaxname(const TTaxname &value)
Assign a value to Taxname data member.
Definition: Org_ref_.hpp:381
void SetLevel(TLevel value)
Assign a value to Level data member.
Definition: T3Error_.hpp:363
void SetMessage(const TMessage &value)
Assign a value to Message data member.
Definition: T3Error_.hpp:403
TError & SetError(void)
Select the variant.
Definition: T3Reply_.cpp:108
Timeout structure.
Definition: ncbi_types.h:76
Utility stuff for more convenient using of Boost.Test library.
USING_SCOPE(objects)
CRef< CTaxon3_request > s_GetDummyRequest()
CRef< CTaxon3_reply > s_CreateReplyWithMessage(const string &message)
CRef< COrg_ref > s_CreateOrgRef(const int &taxid, const string &taxname)
USING_NCBI_SCOPE
BOOST_AUTO_TEST_CASE(create_safe_cached_taxon)
Modified on Mon May 13 04:33:32 2024 by modify_doxy.py rev. 669887