How to use test_set_header method in tox

Best Python code snippet using tox_python

tst_reader.py

Source:tst_reader.py Github

copy

Full Screen

1# -*- coding: utf-8 -*-2#3# Copyright 2012-2022 BigML4#5# Licensed under the Apache License, Version 2.0 (the "License"); you may6# not use this file except in compliance with the License. You may obtain7# a copy of the License at8#9# http://www.apache.org/licenses/LICENSE-2.010#11# Unless required by applicable law or agreed to in writing, software12# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT13# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the14# License for the specific language governing permissions and limitations15# under the License.16"""TestReader class17 Manages the test input data, its headers and checks them against the18 model fields data to build the dict input_data.19"""20import sys21import os22from bigml.util import get_csv_delimiter, is_image23from bigml.io import UnicodeReader24from bigmler.utils import BIGML_SYS_ENCODING25from bigmler.checkpoint import file_number_of_lines26from bigmler.utf8recoder import UTF8Recoder27from bigmler.folderreader import FolderReader28def is_folder(folder):29 """Checking if the test_set is a folder """30 if isinstance(folder, str) and os.path.isdir(folder) and \31 os.path.exists(folder):32 return True33 return False34def contains_csv(folder):35 """Checking whether a folder contains a CSV file"""36 files = os.listdir(folder)37 return any(os.path.splitext(filename)[1].replace(".", "").lower() == "csv"38 for filename in files)39class TstReader():40 """Retrieves csv info and builds a input data dict41 """42 def __init__(self, test_set, test_set_header, fields, objective_field,43 test_separator=None):44 """Builds a generator from a csv file and the fields' model structure45 `test_set`: path to the test data file46 `test_set_header`: boolean, True means that headers are first row47 in the file48 `fields`: Fields object with the expected fields structure.49 `objective_field`: field_id of the objective field50 """51 self.test_set = test_set52 if isinstance(self.test_set, str) and os.path.isdir(self.test_set) \53 and os.path.exists(self.test_set) and \54 not self.test_set.endswith(os.sep):55 self.test_set += os.sep56 self.directory = None57 if test_set.__class__.__name__ == "StringIO":58 self.test_set = UTF8Recoder(test_set, BIGML_SYS_ENCODING)59 else:60 self.directory = os.path.dirname(self.test_set)61 self.test_set_header = test_set_header62 self.fields = fields63 if (objective_field is not None and64 not objective_field in fields.fields):65 try:66 objective_field = fields.field_id(objective_field)67 except ValueError as exc:68 sys.exit(exc)69 self.objective_field = objective_field70 self.image_fields = []71 try:72 self.image_fields = [column for column in73 sorted(fields.fields_by_column_number.keys())74 if fields.fields[fields.field_id(column)].get(75 "optype") == "image"]76 except ValueError as exc:77 sys.exit(exc)78 self.test_separator = (test_separator79 if test_separator is not None80 else get_csv_delimiter())81 if is_folder(self.test_set) and \82 self.image_fields and len(self.image_fields) == 1 \83 and not contains_csv(self.test_set):84 # The test_set points to a directory where images are stored.85 # Only images are read.86 image_field_name = fields.fields[87 fields.field_id(self.image_fields[0])].get(88 "name") if test_set_header else None89 self.test_reader = FolderReader(90 self.test_set,91 filter_fn=is_image,92 header=image_field_name).open_reader()93 else:94 if len(self.test_separator) > 1:95 sys.exit("Only one character can be used as test data "96 "separator.")97 try:98 self.test_reader = UnicodeReader(99 self.test_set, delimiter=self.test_separator,100 lineterminator="\n").open_reader()101 except IOError:102 sys.exit("Error: cannot read test %s" % test_set)103 self.headers = None104 self.raw_headers = None105 self.exclude = []106 if test_set_header:107 self.headers = next(self.test_reader)108 # validate headers against model fields excluding objective_field,109 # that may be present or not110 if objective_field is not None:111 objective_field = fields.field_column_number(objective_field)112 try:113 fields_names = [fields.fields[fields.field_id(i)]114 ['name'] for i in115 sorted(fields.fields_by_column_number.keys())116 if objective_field is None or117 i != objective_field]118 except ValueError as exc:119 sys.exit(exc)120 self.raw_headers = self.headers[:]121 self.exclude = [i for i in range(len(self.headers))122 if not self.headers[i] in fields_names]123 self.exclude.reverse()124 if self.exclude:125 if len(self.headers) > len(self.exclude):126 for index in self.exclude:127 del self.headers[index]128 else:129 raise Exception(("No test field matches the model fields."130 "\nThe expected fields are:\n\n%s\n\n"131 "while "132 "the headers found in the test file are:"133 "\n\n%s\n\n"134 "Use --no-test-header flag if first li"135 "ne should not be interpreted as"136 " headers." %137 (",".join(fields_names),138 ",".join(self.headers))).encode("utf-8"))139 else:140 columns = fields.fields_columns[:]141 if objective_field is not None:142 if fields.field_column_number(objective_field) in columns:143 columns.remove(fields.field_column_number(objective_field))144 self.headers = [fields.fields_by_column_number[column] for145 column in columns]146 self.raw_headers = self.headers147 def __iter__(self):148 """Iterator method149 """150 return self151 def __next__(self):152 """Returns the next row153 """154 row = next(self.test_reader)155 if self.directory and self.image_fields:156 for index, row_item in enumerate(row):157 if index in self.image_fields:158 row[index] = os.path.join(self.directory, row_item)159 return row160 def dict(self, row, filtering=True):161 """Returns the row in a dict format according to the given headers162 """163 new_row = row[:]164 if not filtering:165 if self.test_set_header:166 headers = self.raw_headers167 else:168 headers = [self.fields.fields_by_column_number[column] for169 column in self.fields.fields_columns]170 return dict(list(zip(headers, new_row)))171 for index in self.exclude:172 del new_row[index]173 return self.fields.pair(new_row, self.headers, self.objective_field)174 def number_of_tests(self):175 """Returns the number of tests in the test file176 """177 tests = file_number_of_lines(self.test_set)178 if self.test_set_header:179 tests -= 1180 return tests181 def has_headers(self):182 """Returns whether the test set file has a headers row183 """184 return self.test_set_header185 def close(self):186 """Closing file handler187 """188 try:189 self.test_reader.close_reader()190 except AttributeError:...

Full Screen

Full Screen

test_header_setter.py

Source:test_header_setter.py Github

copy

Full Screen

...8 9 def tearDown(self):10 super(ContentHeaderSetterTest, self).tearDown()11 12 def test_set_header(self):13 result = self.simulate_get('/test')14 print(result._headers)...

Full Screen

Full Screen

test_sets.py

Source:test_sets.py Github

copy

Full Screen

1from wye.utils.sets import set_header2def test_set_header():...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run tox automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful