HCE Project Python language Distributed Tasks Manager Application, Distributed Crawler Application and client API bindings.  2.0.0-chaika
Hierarchical Cluster Engine Python language binding
ftest_Filters.py
Go to the documentation of this file.
1 '''
2 Created on Mar 18, 2015
3 
4 @author: scorp
5 '''
6 import ppath
7 
8 import unittest
9 import ConfigParser
10 import os
11 try:
12  import cPickle as pickle
13 except ImportError:
14  import pickle
15 import dc_crawler.DBTasksWrapper as DBTasksWrapper
16 from app.Filters import Filters
17 
18 CMD_SNEW_FILTERS = ("cd ../../bin && /usr/bin/python ./db-task.py --cfg=../ini/db-task.ini " +
19  "<../hce/ftests/db-task-data/site_new_filters.dat")
20 
21 
22 filtersTmp1 = [{"Pattern" : "%ContentType%", "Subject" : "text/xml", "OperationCode" : Filters.OC_RE,
23  "Stage" : Filters.STAGE_BEFORE_DOM_PRE, "Action" : 1},
24  {"Pattern" : "%ContentType%", "Subject" : "text/xml", "OperationCode" : Filters.OC_RE,
25  "Stage" : Filters.STAGE_AFTER_DOM, "Action" : 2},
26  {"Pattern" : "%ContentType%", "Subject" : "text/html", "OperationCode" : Filters.OC_RE,
27  "Stage" : Filters.STAGE_AFTER_PROCESSOR, "Action" : 3},
28  {"Pattern" : "%ContentType%", "Subject" : "application/pdf", "OperationCode" : Filters.OC_RE,
29  "Stage" : Filters.STAGE_BEFORE_DOM_PRE, "Action" : 4},
30  {"Pattern" : "%ContentType%", "Subject" : "text/xml", "OperationCode" : Filters.OC_RE,
31  "Stage" : Filters.STAGE_BEFORE_DOM_PRE, "Action" : 5},
32  {"Pattern" : "%url%", "Subject" : "text/xml", "OperationCode" : Filters.OC_RE,
33  "Stage" : Filters.STAGE_BEFORE_DOM_PRE, "Action" : 6},
34  {"Pattern" : "%url% and %ddl%", "Subject" : "text/xml and text/xml", "OperationCode" : Filters.OC_RE,
35  "Stage" : Filters.STAGE_AFTER_PROCESSOR, "Action" : 7},
36  {"Pattern" : "%url% and %redirect%", "Subject" : "text/xml and text/xml",
37  "OperationCode" : Filters.OC_RE, "Stage" : Filters.STAGE_AFTER_PROCESSOR, "Action" : 8}]
38 
39 class Test(unittest.TestCase):
40 
41  CFG_NAME = "../../ini/db-task.ini"
42  CONST_SITE_ID = "65f5d740b25e73d4c63d9f06e8f15b90"
43 
44  def setUp(self):
45  cfgParser = ConfigParser.ConfigParser()
46  cfgParser.read(self.CFG_NAME)
47  #self.wrapper = DBTasksWrapper.DBTasksWrapper(cfgParser)
48 
49 
50  def tearDown(self):
51  pass
52 
53 
54  def execCommand(self, command, step):
55  obj = None
56  print ">>> Start = " + str(command)
57  fd = os.popen(command)
58  if fd:
59  localStr = fd.read()
60  fd.close()
61  print ">>> Finish = " + str(command)
62  try:
63  obj = pickle.loads(localStr)
64  except EOFError:
65  self.assertTrue(False, "Step%s >>> Invalid return data" % str(step))
66  else:
67  print ">>> Bad FD " + + str(command)
68  return obj
69 
70 
71  def commonAssert_01(self, localFilters):
72  value = {"ContentType": "text/xml", "url": "text/xml", "MDd5" : "test/xml"}
73 
74  result = localFilters.filterAll(Filters.STAGE_AFTER_DOM, value, Filters.LOGIC_AND)
75  self.assertTrue(result == [True], ">>> Test01_A BAD")
76  result = localFilters.filterAll(Filters.STAGE_AFTER_DOM, value, Filters.LOGIC_OR)
77  self.assertTrue(result == [2], ">>> Test01_B BAD")
78 
79 
80  def commonAssert_02(self, localFilters):
81  value = {"ContentType": "application/dat", "url": "text/xml", "MDd5" : "test/xml"}
82 
83  result = localFilters.filterAll(Filters.STAGE_AFTER_DOM_PRE, value, Filters.LOGIC_AND)
84  self.assertTrue(result == [], ">>> Test02_A BAD")
85  result = localFilters.filterAll(Filters.STAGE_AFTER_DOM_PRE, value, Filters.LOGIC_OR)
86  self.assertTrue(result == [], ">>> Test02_B BAD")
87 
88 
89  def commonAssert_03(self, localFilters):
90  value = {"ContentType": "text/xml", "url": "text/xml", "MDd5" : "test/xml"}
91 
92  result = localFilters.filterAll(Filters.STAGE_BEFORE_DOM_PRE, value, Filters.LOGIC_AND)
93  self.assertTrue(result == [False], ">>> Test03_A BAD")
94  result = localFilters.filterAll(Filters.STAGE_BEFORE_DOM_PRE, value, Filters.LOGIC_OR)
95  self.assertTrue(result == [1, -4, 5, 6], ">>> Test03_B BAD")
96 
97  result = localFilters.filterAll(Filters.STAGE_COLLECT_URLS, value, Filters.LOGIC_AND)
98  self.assertTrue(result == [], ">>> Test03_C BAD")
99  result = localFilters.filterAll(Filters.STAGE_COLLECT_URLS, value, Filters.LOGIC_OR)
100  self.assertTrue(result == [], ">>> Test03_D BAD")
101 
102 
103  def commonAssert_04(self, localFilters):
104  value = {"ContentType": "text/html", "url": "text/xml", "ddl" : "test/xml"}
105 
106  result = localFilters.filterAll(Filters.STAGE_AFTER_PROCESSOR, value, Filters.LOGIC_AND)
107  self.assertTrue(result == [False], ">>> Test04_A BAD")
108  result = localFilters.filterAll(Filters.STAGE_AFTER_PROCESSOR, value, Filters.LOGIC_OR)
109  self.assertTrue(result == [3, -7, -8], ">>> Test04_B BAD")
110 
111 
112  def test_01(self):
113  localFilters = Filters(filtersTmp1)
114  self.commonAssert_01(localFilters)
115 
116  def test_02(self):
117  localFilters = Filters(filtersTmp1)
118  self.commonAssert_02(localFilters)
119 
120 
121  def test_03(self):
122  localFilters = Filters(filtersTmp1)
123  self.commonAssert_03(localFilters)
124 
125 
126  def test_04(self):
127  localFilters = Filters(filtersTmp1)
128  self.commonAssert_04(localFilters)
129 
130 
131  '''
132  def test_05(self):
133  self.execCommand(CMD_SNEW_FILTERS, 0)
134  localFilters = Filters(None, self.wrapper, self.CONST_SITE_ID)
135  self.commonAssert_01(localFilters)
136 
137 
138  def test_06(self):
139  localFilters = Filters(None, self.wrapper, self.CONST_SITE_ID)
140  self.commonAssert_02(localFilters)
141 
142 
143  def test_07(self):
144  localFilters = Filters(None, self.wrapper, self.CONST_SITE_ID)
145  self.commonAssert_03(localFilters)
146 
147 
148  def test_08(self):
149  localFilters = Filters(None, self.wrapper, self.CONST_SITE_ID)
150  self.commonAssert_04(localFilters)
151  '''
152 
153 
154  def test_09(self):
155  localUrl = "http://pbn.com/Brown-and-URI-slip-Bryant-and-RIC-gain-in-2015-08-college-rankings,99822"
156  localFilters = [{"Pattern" : "http://pbn.com/(.*)-%CUR_YEAR_FULL%-%CUR_MONTH_FULL%(.*)", "Subject" : localUrl,
157  "OperationCode" : Filters.OC_RE, "Stage" : Filters.STAGE_AFTER_PROCESSOR, "Action" : 1},
158  {"Pattern" : "http://pbn.com/(.*)-%CUR_YEAR_FULL%-%CUR_MONTH_FULL%(.*)", "Subject" : localUrl,
159  "OperationCode" : Filters.OC_RE, "Stage" : Filters.STAGE_BEFORE_DOM_PRE, "Action" : 2},
160  {"Pattern" : "http://pbn.com/(.*)-%ANY_DATA%(.*)", "Subject" : localUrl,
161  "OperationCode" : Filters.OC_RE, "Stage" : Filters.STAGE_AFTER_PROCESSOR, "Action" : 3},
162  {"Pattern" : "%DEPTH%", "Subject" : "10",
163  "OperationCode" : Filters.OC_EQMORE, "Stage" : Filters.STAGE_AFTER_PROCESSOR, "Action" : 4},]
164  localFilters = Filters(localFilters)
165 
166  value = {"ContentType": "text/xml", "url": "text/xml", "CUR_YEAR_FULL": "2015", "CUR_MONTH_FULL": "08",
167  "DEPTH": "1"}
168 
169  result = localFilters.filterAll(Filters.STAGE_AFTER_PROCESSOR, value, Filters.LOGIC_OR)
170  print result
171 
172 
173 if __name__ == "__main__":
174  #import sys;sys.argv = ['', 'Test.testName']
175  unittest.main()
def commonAssert_03(self, localFilters)
def commonAssert_02(self, localFilters)
def commonAssert_04(self, localFilters)
def commonAssert_01(self, localFilters)
def execCommand(self, command, step)