Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

refactoring - TestLibrary class created

  • Loading branch information...
commit 6c177ff87be1d35fe3b6630ccf8982d954bf7115 1 parent cada368
Mika Hänninen mikahanninen authored

Showing 1 changed file with 108 additions and 81 deletions. Show diff stats Hide diff stats

  1. +108 81 rfgen.py
189 rfgen.py
@@ -17,15 +17,12 @@
17 17 import os
18 18 import random
19 19 import shutil
20   -import time
21 20 import sys
22 21 import sqlite3
23 22 from sqlite3 import OperationalError
24 23 import copy
25 24 from time import strftime
26   -from optparse import OptionParser
27 25 import urllib2
28   -import platform
29 26
30 27 ROOT = os.path.dirname(__file__)
31 28 lib = os.path.join(ROOT, '..', 'lib')
@@ -35,68 +32,90 @@
35 32 sys.path.insert(0, src)
36 33
37 34
38   -def _create_test_libraries(dirs, filecount = 10, keywords=10):
39   - global db_cursor, verbs, words
  35 +class TestResource:
  36 +
  37 + def __init__(self, path):
  38 + pass
  39 +
  40 +class TestLibrary:
  41 + def __init__(self, path):
  42 + self.lib_path = path
  43 + self.lib_prefix = "CustomLib"
  44 + self.lib_name = _get_random_name(self.lib_prefix)
  45 + _sql_insert("INSERT INTO source (path,type) VALUES ('%s','CUSTOMLIBRARY')" % self.lib_name)
  46 + self.lib_file = open("%s/%s.py" % (self.lib_path,self.lib_name),"w")
  47 + self.lib_doc = '\t"""Library documentation:\n' +\
  48 + '\t\t%s"""' % self.lib_name
  49 + self.write("import os,time\n\n" +\
  50 + "class %s:\n" % self.lib_name +\
  51 + "\tdef __init__(self):\n" +\
  52 + "\t%s\n" % self.lib_doc)
  53 + self.write("\t\t%s" % _select_functionality() + "\n")
  54 + self.verbs = copy.copy(verbs)
  55 + self.kw_count = 1
  56 +
  57 + def add_keyword(self):
  58 + if len(self.verbs) > 0:
  59 + kw_name_prefix = self.verbs.pop().capitalize()
  60 + else:
  61 + kw_name_prefix = "KW_%d" % self.kw_count
  62 + self.kw_count += 1
  63 + kw_name = kw_name_prefix + "_" + self.lib_prefix
  64 + _sql_insert("INSERT INTO keywords (name,source) VALUES ('%s','%s')" % (kw_name,self.lib_name))
  65 + kw_doc = '"""Keyword documentation for %s"""' % kw_name
  66 + self.write("\tdef %s(self):\n" % kw_name +\
  67 + "\t\t%s\n" % kw_doc +\
  68 + "\t\t%s\n" % _select_functionality())
  69 +
  70 + def write(self, text):
  71 + self.lib_file.write(text)
  72 +
  73 + def close(self):
  74 + global db_connection
  75 +
  76 + db_connection.commit()
  77 + self.write("myinstance = %s()" % self.lib_name)
  78 + self.lib_file.close()
  79 +
  80 +
  81 +def _select_functionality():
  82 + directory_looper = "for dirname, dirnames, filenames in os.walk('.'):\n" +\
  83 + "\t\t\tfor subdirname in dirnames:\n" +\
  84 + "\t\t\t\tprint os.path.join(dirname, subdirname)\n" +\
  85 + "\t\t\tfor filename in filenames:\n" +\
  86 + "\t\t\t\tprint os.path.join(dirname, filename)\n"
  87 + sleeper = "time.sleep(1)\n"
  88 + return random.choice([directory_looper, sleeper, "pass\n"])
  89 +
  90 +def _sql_insert(sqlString=""):
  91 + global db_cursor
  92 + db_cursor.execute(sqlString)
  93 +
  94 +def _sql_select(sqlString=""):
  95 + global db_cursor
  96 + return db_cursor.execute(sqlString)
  97 +
  98 +def _get_random_name(prefix=""):
  99 + global words
  100 +
  101 + random_name = random.choice(words).strip().capitalize()
  102 + return "%s%s" % (prefix, random_name)
  103 +
  104 +
  105 +def _get_random_verb(prefix=""):
  106 + global verbs
  107 +
40 108
  109 +def _create_test_libraries(dirs, filecount = 10, keywords=10):
41 110 path = dirs[0]
42 111 libs = []
43 112
44 113 for x in range(filecount):
45   - lib_main = random.choice(words).strip().capitalize()
46   - lib_name = "CustomLib%s" % lib_main
47   - libs.append(lib_name)
48   - db_cursor.execute("INSERT INTO source (path,type) VALUES ('%s','CUSTOMLIBRARY')" % lib_name)
49   - libfile = open("%s/%s.py" % (path,lib_name),"w")
50   - lib_doc = '\t"""Library documentation:\n' + \
51   - '\t\t%s"""' % lib_name
52   - libfile.write("import os,time\n\n" + \
53   - "class %s:\n" % lib_name + \
54   - "\tdef __init__(self):\n" + \
55   - "\t%s\n" % lib_doc)
56   -
57   - directory_looper = "for dirname, dirnames, filenames in os.walk('.'):\n" + \
58   - "\t\t\tfor subdirname in dirnames:\n" + \
59   - "\t\t\t\tprint os.path.join(dirname, subdirname)\n" + \
60   - "\t\t\tfor filename in filenames:\n" + \
61   - "\t\t\t\tprint os.path.join(dirname, filename)\n"
62   - sleeper = "time.sleep(1)\n"
63   -
64   - libfile.write("\t\t%s" % random.choice([directory_looper, sleeper]) + "\n")
65   -
66   - temp_verb = copy.copy(verbs)
67   - counter = 1
  114 + lib = TestLibrary(path)
68 115 for x in range(keywords):
69   - if len(temp_verb) > 0:
70   - verb = temp_verb.pop().capitalize()
71   - else:
72   - verb = "KW_%d" % counter
73   - counter += 1
74   - kw_name = verb + "_" + lib_main
75   - db_cursor.execute("INSERT INTO keywords (name,source) VALUES ('%s','%s')" % (kw_name,lib_name))
76   - kw_doc = '"""Keyword documentation for %s"""' % kw_name
77   - libfile.write("\tdef %s(self):\n" % kw_name + \
78   - "\t\t%s\n" % kw_doc + \
79   - "\t\t%s\n" % random.choice([directory_looper, sleeper, "pass\n"]))
80   -
81   - libfile.write("myinstance = %s()" % lib_name)
82   - libfile.close()
83   -
84   - #initfile_lines = open("%s/__init__.txt" % path).readlines()
85   - index = 0
86   -
87   -# for line in initfile_lines:
88   -# if "*** Settings ***" in line:
89   -# index += 1
90   -# for lib_name in libs:
91   -# initfile_lines.insert(index, "Library\t%s.py\n" % lib_name)
92   -# index += 1
93   -# break
94   -# index += 1
95   -
96   -#fo = open("%s/__init__.txt" % path, "w")
97   -#for line in initfile_lines:
98   -# fo.write(line)
99   -#fo.close()
  116 + lib.add_keyword()
  117 + lib.close()
  118 + libs.append(lib)
100 119
101 120
102 121 def _create_test_suite(dirs, filecount = 1, testcount = 20, avg_test_depth = 5, test_validity = 1):
@@ -218,26 +237,32 @@ def _create_test_suite(dirs, filecount = 1, testcount = 20, avg_test_depth = 5,
218 237 tcfile.write(keywords_txt)
219 238 tcfile.close()
220 239
  240 +def _create_static_resource_files(target_dir, filename = "static_external_resource.txt", count = 1):
  241 + external_info = {}
221 242
222   -def _create_test_resources(dirs, resource_files, resources_in_file, external_resources, subdir = ""):
223   - global db_cursor, verbs, words
224   -
225   - path = dirs[0]
226   - ext_dir = dirs[1]
227   - if not os.path.exists(os.path.split(ext_dir)[0]):
228   - os.makedirs(os.path.split(ext_dir)[0])
  243 + if not os.path.exists(os.path.split(target_dir)[0]):
  244 + os.makedirs(os.path.split(target_dir)[0])
229 245
230   - static_external_resource_filename = "static_external_resource.txt"
231   - static_external_resource = open(ext_dir + static_external_resource_filename, "w")
  246 + static_external_resource_filename = filename
  247 + static_external_resource = open(target_dir + static_external_resource_filename, "w")
232 248 static_external_resource.write("*** Keywords ***\nMy Super KW\n\tNo Operation")
233 249 static_external_resource.close()
234   -
235   - external_resource_path = "%s%s" % (ext_dir, "ext_R%d_Resource.txt")
  250 + external_info['path'] = "%s%s" % (target_dir, "ext_R%d_Resource.txt")
236 251
237 252 if os.name == 'posix':
238   - external_resource_path_for_import = "%s%s%s%s" % (".." + os.sep, ".." + os.sep, "ext" + os.sep, "ext_R%d_Resource.txt")
  253 + external_info['import_path'] = "%s%s%s%s" % (".." + os.sep, ".." + os.sep, "ext" + os.sep, "ext_R%d_Resource.txt")
239 254 else:
240   - external_resource_path_for_import = "%s%s" % (ext_dir, "ext_R%d_Resource.txt")
  255 + external_info['import_path'] = "%s%s" % (target_dir, "ext_R%d_Resource.txt")
  256 + external_info['filename'] = static_external_resource_filename
  257 +
  258 + return external_info
  259 +
  260 +def _create_test_resources(dirs, resource_files, resources_in_file, external_resources, subdir = ""):
  261 + global db_cursor, verbs, words
  262 +
  263 + path = dirs[0]
  264 + ext_info = _create_static_resource_files(dirs[1])
  265 +
241 266 for resfile_index in range(resource_files):
242 267 basename = "R%d_Resource.txt" % (resfile_index+1)
243 268
@@ -252,32 +277,34 @@ def _create_test_resources(dirs, resource_files, resources_in_file, external_res
252 277 resfile_ondisk = open("%s%s" % (path + os.sep, basename) ,"w")
253 278 content = "*** Settings ***\n"
254 279 if external_resources > 0:
255   - content += "Resource\t" + (external_resource_path_for_import % (random.randint(0,external_resources) + 1)) + "\n"
  280 + content += "Resource\t" + (ext_info['import_path'] % (random.randint(0,external_resources) + 1)) + "\n"
256 281 #available_keywords = db_cursor.execute("SELECT * FROM keywords ORDER BY RANDOM()").fetchall()
257 282 content += "\n*** Variables ***\n"
258 283 for x in range(resources_in_file):
259 284 content += "%-25s%10s%d\n" % ("${%s%d}" % (random.choice(words).strip().capitalize(),x),"",
260 285 random.randint(1,1000))
261 286 content += "\n*** Keywords ***\n"
262   - kw_name = "User Kw %d" % (resfile_index+1)
263   - content += "%s\n\tNo Operation" % (kw_name)
264   - db_cursor.execute("INSERT INTO keywords (name,source) VALUES ('%s','%s')" % (kw_name,rf_resource_name))
  287 + word = random.choice(words).strip().capitalize()
  288 + for x in range(55):
  289 + kw_name = "Resource %s User Kw %d" % (word, x+1)
  290 + content += "%s\n\tNo Operation\n" % (kw_name)
  291 + db_cursor.execute("INSERT INTO keywords (name,source) VALUES ('%s','%s')" % (kw_name,rf_resource_name))
265 292 db_cursor.execute("INSERT INTO source (path,type) VALUES ('%s','RESOURCE')" % rf_resource_name)
266 293 resfile_ondisk.write(content)
267 294 resfile_ondisk.close()
268 295
269 296 for resfile_index in range(external_resources):
270 297 content = "*** Settings ***\n"
271   - content += "Resource\t%s\n" % static_external_resource_filename
  298 + content += "Resource\t%s\n" % ext_info['filename']
272 299 content += "\n*** Keywords ***\n"
273 300 kw_name = "External User Kw %d" % (resfile_index+1)
274 301 content += "%s\n\tNo Operation" % (kw_name)
275 302
276   - extfile_ondisk = open(external_resource_path % (resfile_index+1), "w")
  303 + extfile_ondisk = open(ext_info['path'] % (resfile_index+1), "w")
277 304 extfile_ondisk.write(content)
278 305 extfile_ondisk.close()
279   - db_cursor.execute("INSERT INTO keywords (name,source) VALUES ('%s','%s')" % (kw_name, external_resource_path_for_import % (resfile_index+1)))
280   - db_cursor.execute("INSERT INTO source (path,type) VALUES ('%s','EXT_RESOURCE')" % external_resource_path_for_import % (resfile_index+1))
  306 + db_cursor.execute("INSERT INTO keywords (name,source) VALUES ('%s','%s')" % (kw_name, ext_info['path'] % (resfile_index+1)))
  307 + db_cursor.execute("INSERT INTO source (path,type) VALUES ('%s','EXT_RESOURCE')" % ext_info['path'] % (resfile_index+1))
281 308
282 309
283 310 def _create_test_project(dirs,testlibs_count=5,keyword_count=10,testsuite_count=5,tests_in_suite=10,
@@ -474,4 +501,4 @@ def format_help(self, formatter=None):
474 501
475 502
476 503 if __name__ == '__main__':
477   - main()
  504 + main()

0 comments on commit 6c177ff

Please sign in to comment.
Something went wrong with that request. Please try again.