Best Python code snippet using pytest-django_python
test_pv_checks.py
Source:test_pv_checks.py
1import unittest2import src.pv_checks as pv3import src.db_parser.EPICS_collections as Ec4class PvChecksTest(unittest.TestCase):5 record_name_list = ["record1", "record2", "record3", "record4", "record5"]6 record_list = [Ec.Record("type", record_name_list[0], [], [], []),7 Ec.Record("type", record_name_list[1], [], [], []),8 Ec.Record("type", record_name_list[2], [], [], []),9 Ec.Record("type", record_name_list[3], [], [], []),10 Ec.Record("type", record_name_list[4], [], [], [])]11 field_name_list = ["field1", "field2", "field3", "field4", "field5"]12 value_list = [0, 1, 2, 3, 4]13 field_list = [Ec.Field(field_name_list[0], value_list[0]), Ec.Field(field_name_list[1], value_list[1]),14 Ec.Field(field_name_list[2], value_list[2]), Ec.Field(field_name_list[3], value_list[3]),15 Ec.Field(field_name_list[4], value_list[4])]16 def test_allowed_unit_standalone(self):17 # only two cases so can easily check both18 self.assertTrue(pv.allowed_unit("cdeg/ss"))19 self.assertTrue(pv.allowed_unit("uA hour"))20 def test_allowed_unit_over(self):21 # check properly handles unit over other unit22 self.assertTrue(pv.allowed_unit("bit/kbyte"))23 def test_allowed_unit_1_over(self):24 # check properly handles 1 over unit25 self.assertTrue(pv.allowed_unit("1/cm"))26 def test_allowed_unit_compound_over(self):27 # check properly handles unit over compound unit28 self.assertTrue(pv.allowed_unit("km/(m s)"))29 def test_allowed_unit_wrong_format_ms(self):30 self.assertTrue(pv.allowed_unit("m/s"))31 self.assertFalse(pv.allowed_unit("m s^-1"))32 def test_allowed_unit_bad_unit(self):33 # properly fails invalid unit34 self.assertFalse(pv.allowed_unit("not a unit"))35 def test_allowed_unit_bad_unit_over(self):36 self.assertFalse(pv.allowed_unit("k / (m s)"))37 def test_allowed_unit_catch_capitals(self):38 self.assertFalse(pv.allowed_unit("M"))39 def test_allowed_unit_wrong_format_1m2(self):40 self.assertTrue(pv.allowed_unit("1 / m ^ 2"))41 self.assertFalse(pv.allowed_unit("m^-2"))42 # need to know some valid macros of units to test that portion of allow units43 def test_get_multiple_instances_empty(self):44 test_db = Ec.Db("path", [])45 self.assertFalse(pv.get_multiple_instances(test_db))46 def test_get_multiple_instances_no(self):47 test_db = Ec.Db("path", self.record_list)48 self.assertFalse(pv.get_multiple_instances(test_db))49 def test_get_multiple_instances_1(self):50 test_db = Ec.Db("path", self.record_list + [Ec.Record("type", self.record_name_list[0], [], [], [])])51 self.assertEqual(len(pv.get_multiple_instances(test_db)), 1)52 def test_get_multiple_instances_more(self):53 test_db = Ec.Db("path", self.record_list + (5 * [Ec.Record("type", self.record_name_list[0], [], [], [])]))54 self.assertEqual(len(pv.get_multiple_instances(test_db)), 1)55 def test_get_multiple_properties_on_pvs_empty(self):56 test_db = Ec.Db("path", self.record_list)57 self.assertFalse(pv.get_multiple_properties_on_pvs(test_db))58 def test_get_multiple_properties_on_pvs_no(self):59 diff_records = [Ec.Record("type", name, [], self.field_list, []) for name in self.record_name_list]60 test_db = Ec.Db("path", diff_records)61 self.assertFalse(pv.get_multiple_properties_on_pvs(test_db))62 def test_get_multiple_properties_on_pvs_yes(self):63 diff_records = [Ec.Record("type", self.record_name_list[0], [], self.field_list + self.field_list, [])]64 test_db = Ec.Db("path", diff_records)65 self.assertEqual(len(pv.get_multiple_properties_on_pvs(test_db)), 1)66 def test_get_multiple_properties_on_pvs_yes_across_multiple(self):67 diff_records = [Ec.Record("type", name, [], self.field_list + self.field_list, []) for name in68 self.record_name_list69 ]70 test_db = Ec.Db("path", diff_records)71 self.assertEqual(len(pv.get_multiple_properties_on_pvs(test_db)), len(diff_records))72 def test_get_interest_units_empty_info(self):73 test_db = Ec.Db("path", [Ec.Record("longin", self.record_name_list[0], [], self.field_list, [])])74 self.assertFalse(pv.get_interest_units(test_db))75 def test_get_interest_units_not_interesting(self):76 test_db = Ec.Db("path", self.record_list + [77 Ec.Record("longin", self.record_name_list[0], [Ec.Field("BORING", 0)], self.field_list, [])])78 self.assertFalse(pv.get_interest_units(test_db))79 def test_get_interest_units_interesting_success(self):80 test_db = Ec.Db("path", [Ec.Record("longin", self.record_name_list[0], [Ec.Field("INTEREST", 0)],81 self.field_list + [Ec.Field("EGU", "bit/kbyte")], [])])82 self.assertFalse(pv.get_interest_units(test_db))83 def test_get_interest_units_interesting_success_out(self):84 test_db = Ec.Db("path", [Ec.Record("longout", self.record_name_list[0], [Ec.Field("INTEREST", 0)],85 self.field_list + [Ec.Field("EGU", "bit/kbyte")], [])])86 self.assertFalse(pv.get_interest_units(test_db))87 def test_get_interest_units_interesting_success_ai(self):88 test_db = Ec.Db("path", [Ec.Record("ai", self.record_name_list[0], [Ec.Field("INTEREST", 0)],89 self.field_list + [Ec.Field("EGU", "bit/kbyte")], [])])90 self.assertFalse(pv.get_interest_units(test_db))91 def test_get_interest_units_interesting_success_ao(self):92 test_db = Ec.Db("path", [Ec.Record("ao", self.record_name_list[0], [Ec.Field("INTEREST", 0)],93 self.field_list + [Ec.Field("EGU", "bit/kbyte")], [])])94 self.assertFalse(pv.get_interest_units(test_db))95 def test_get_interest_units_interesting_mult_success(self):96 test_db = Ec.Db("path", [Ec.Record("longin", name, [Ec.Field("INTEREST", 0)], self.field_list +97 [Ec.Field("EGU", "bit/kbyte")], [])98 for name in self.record_name_list])99 self.assertFalse(pv.get_interest_units(test_db))100 def test_get_interest_units_interesting_fail(self):101 test_db = Ec.Db("path",102 [Ec.Record("longin", self.record_name_list[0], [Ec.Field("INTEREST", 0)], self.field_list, [])])103 self.assertEqual(len(pv.get_interest_units(test_db)), 1)104 def test_get_interest_units_interesting_fail_out(self):105 test_db = Ec.Db("path", [106 Ec.Record("longout", self.record_name_list[0], [Ec.Field("INTEREST", 0)], self.field_list, [])])107 self.assertEqual(len(pv.get_interest_units(test_db)), 1)108 def test_get_interest_units_interesting_fail_ai(self):109 test_db = Ec.Db("path",110 [Ec.Record("ai", self.record_name_list[0], [Ec.Field("INTEREST", 0)], self.field_list, [])])111 self.assertEqual(len(pv.get_interest_units(test_db)), 1)112 def test_get_interest_units_interesting_fail_ao(self):113 test_db = Ec.Db("path", [Ec.Record("ao", self.record_name_list[0], [Ec.Field("INTEREST", 0)],114 self.field_list, [])])115 self.assertEqual(len(pv.get_interest_units(test_db)), 1)116 def test_get_interest_units_interesting_mult_fail(self):117 test_db = Ec.Db("path", [Ec.Record("longin", name, [Ec.Field("INTEREST", 0)], self.field_list, []) for name in118 self.record_name_list])119 self.assertEqual(len(pv.get_interest_units(test_db)), len(self.record_name_list))120 def test_get_interest_calc_readonly_empty_info(self):121 test_db = Ec.Db("path", [Ec.Record("calc", self.record_name_list[0], [], self.field_list, [])])122 self.assertFalse(pv.get_interest_calc_readonly(test_db))123 def test_get_interest_calc_readonly_not_interesting(self):124 test_db = Ec.Db("path", self.record_list + [125 Ec.Record("calc", self.record_name_list[0], [Ec.Field("BORING", 0)], [Ec.Field("ASG", "NOTREADONLY")] +126 self.field_list, [])])127 self.assertFalse(pv.get_interest_calc_readonly(test_db))128 def test_get_interest_calc_readonly_not_asg(self):129 test_db = Ec.Db("path", self.record_list + [130 Ec.Record("notcalc", self.record_name_list[0], [Ec.Field("INTEREST", 0)], [Ec.Field("ASG", "NOTREADONLY")] +131 self.field_list, [])])132 self.assertFalse(pv.get_interest_calc_readonly(test_db))133 def test_get_interest_calc_readonly_fail(self):134 test_db = Ec.Db("path", self.record_list + [135 Ec.Record("calc", self.record_name_list[0], [Ec.Field("INTEREST", 0)], [Ec.Field("ASG", "NOTREADONLY")] +136 self.field_list, [])])137 self.assertEqual(len(pv.get_interest_calc_readonly(test_db)), 1)138 def test_get_interest_calc_readonly_fail_multi(self):139 test_db = Ec.Db("path", [140 Ec.Record("calc", name, [Ec.Field("INTEREST", 0)], [Ec.Field("ASG", "NOTREADONLY")] +141 self.field_list, []) for name in self.record_name_list])142 self.assertEqual(len(pv.get_interest_calc_readonly(test_db)), len(self.record_name_list))143 def test_get_interest_calc_readonly_success(self):144 test_db = Ec.Db("path", self.record_list + [145 Ec.Record("calc", self.record_name_list[0], [Ec.Field("INTEREST", 0)], [Ec.Field("ASG", "READONLY")] +146 self.field_list, [])])147 self.assertFalse(pv.get_interest_calc_readonly(test_db))148 def test_get_interest_calc_readonly_success_multi(self):149 test_db = Ec.Db("path", [150 Ec.Record("calc", name, [Ec.Field("INTEREST", 0)], [Ec.Field("ASG", "READONLY")] + self.field_list, []) for151 name in self.record_name_list])152 self.assertFalse(pv.get_interest_calc_readonly(test_db))153 def test_get_desc_length_empty(self):154 test_db = Ec.Db("path", self.record_list)155 self.assertFalse(pv.get_desc_length(test_db))156 def test_get_desc_length_no_desc(self):157 test_db = Ec.Db("path", self.record_list + [158 Ec.Record("type", self.record_name_list[0], [], self.field_list, [])])159 self.assertFalse(pv.get_desc_length(test_db))160 def test_get_desc_length_success(self):161 test_db = Ec.Db("path", self.record_list + [162 Ec.Record("type", self.record_name_list[0], [], self.field_list + [Ec.Field("DESC", "Short")], [])])163 self.assertFalse(pv.get_desc_length(test_db))164 def test_get_desc_length_success_multi(self):165 test_db = Ec.Db("path", [166 Ec.Record("type", name, [], self.field_list + [Ec.Field("DESC", "Short")], []) for name in167 self.record_name_list])168 self.assertFalse(pv.get_desc_length(test_db))169 def test_get_desc_length_fail(self):170 test_db = Ec.Db("path", self.record_list + [171 Ec.Record("type", self.record_name_list[0], [], self.field_list + [Ec.Field("DESC", 41 * "0")], [])])172 self.assertEqual(len(pv.get_desc_length(test_db)), 1)173 def test_get_desc_length_fail_multi(self):174 test_db = Ec.Db("path", [175 Ec.Record("type", name, [], self.field_list + [Ec.Field("DESC", 41 * "0")], []) for name in176 self.record_name_list])177 self.assertEqual(len(pv.get_desc_length(test_db)), len(self.record_name_list))178 def test_get_units_valid_empty(self):179 test_db = Ec.Db("path", self.record_list)180 self.assertFalse(pv.get_units_valid(test_db))181 def test_get_units_valid_no_units(self):182 test_db = Ec.Db("path", [Ec.Record("type", self.record_name_list[0], [],183 [Ec.Field("ASG", "NOTREADONLY")] + self.field_list, [])])184 self.assertFalse(pv.get_units_valid(test_db))185 def test_get_units_valid_fail(self):186 test_db = Ec.Db("path", [Ec.Record("type", self.record_name_list[0], [],187 [Ec.Field("EGU", "NOTUNIT")] + self.field_list, [])])188 self.assertEqual(len(pv.get_units_valid(test_db)), 1)189 def test_get_units_valid_fail_multi(self):190 test_db = Ec.Db("path",191 [Ec.Record("type", name, [], [Ec.Field("EGU", "NOTUNIT")] + self.field_list, []) for name in192 self.record_name_list])193 self.assertEqual(len(pv.get_units_valid(test_db)), len(self.record_name_list))194 def test_get_units_valid_blank(self):195 test_db = Ec.Db("path", [Ec.Record("type", self.record_name_list[0], [],196 [Ec.Field("EGU", "")] + self.field_list, [])])197 self.assertFalse(pv.get_units_valid(test_db))198 def test_get_units_valid_success(self):199 test_db = Ec.Db("path", [Ec.Record("type", self.record_name_list[0], [],200 [Ec.Field("EGU", "cm")] + self.field_list, [])])201 self.assertFalse(pv.get_units_valid(test_db))202 def test_get_units_valid_success_multi(self):203 test_db = Ec.Db("path", [Ec.Record("type", name, [], [Ec.Field("EGU", "cm")] + self.field_list, []) for name in204 self.record_name_list])205 self.assertFalse(pv.get_units_valid(test_db))206 def test_get_interest_descriptions_empty(self):207 test_db = Ec.Db("path", self.record_list)208 self.assertFalse(pv.get_interest_descriptions(test_db))209 def test_get_interest_descriptions_not_interest(self):210 test_db = Ec.Db("path", [211 Ec.Record("type", self.record_name_list[0], [Ec.Field("NOTINTEREST", 0)], self.field_list, [])])212 self.assertFalse(pv.get_interest_descriptions(test_db))213 def test_get_interest_descriptions_fail_single(self):214 test_db = Ec.Db("path",215 [Ec.Record("type", self.record_name_list[0], [Ec.Field("INTEREST", 0)], self.field_list, [])])216 self.assertEqual(len(pv.get_interest_descriptions(test_db)), 1)217 def test_get_interest_descriptions_fail_multi(self):218 test_db = Ec.Db("path", [Ec.Record("type", name, [Ec.Field("INTEREST", 0)], self.field_list, []) for name in219 self.record_name_list])220 self.assertEqual(len(pv.get_interest_descriptions(test_db)), len(self.record_name_list))221 def test_get_interest_descriptions_success_single(self):222 test_db = Ec.Db("path", [Ec.Record("type", self.record_name_list[0], [Ec.Field("INTEREST", 0)],223 self.field_list + [Ec.Field("DESC", "Short")], [])])224 self.assertFalse(pv.get_interest_descriptions(test_db))225 def test_get_interest_descriptions_success_multi(self):226 test_db = Ec.Db("path", [227 Ec.Record("type", name, [Ec.Field("INTEREST", 0)], self.field_list + [Ec.Field("DESC", "Short")], []) for228 name in self.record_name_list])229 self.assertFalse(pv.get_interest_descriptions(test_db))230 def test_get_log_info_tags_empty(self):231 test_db = Ec.Db("path", self.record_list)232 self.assertFalse(pv.get_log_info_tags(test_db))233 def test_get_log_info_tags_no_logs(self):234 test_db = Ec.Db("path", [Ec.Record("type", name, self.field_list, self.field_list, []) for name in235 self.record_name_list])236 self.assertFalse(pv.get_log_info_tags(test_db))237 def test_get_log_info_tags_success_single_log(self):238 test_db = Ec.Db("path", [239 Ec.Record("type", self.record_name_list[0], self.field_list + [Ec.Field("log_test", 0)], self.field_list,240 [])])241 self.assertFalse(pv.get_log_info_tags(test_db))242 def test_get_log_info_tags_success_multi_log(self):243 test_db = Ec.Db("path", [244 Ec.Record("type", name, self.field_list + [Ec.Field("log_test_{}".format(name), 0)], self.field_list,245 [])246 for name in self.record_name_list])247 self.assertFalse(pv.get_log_info_tags(test_db))248 def test_get_log_info_tags_fail_multi_log_db(self):249 test_db = Ec.Db("path", [250 Ec.Record("type", name, self.field_list + [Ec.Field("log_test", 0)], self.field_list, [])251 for name in self.record_name_list])252 self.assertEqual(len(pv.get_log_info_tags(test_db)), len(self.record_name_list) - 1)253 def test_get_log_info_tags_fail_multi_log_record(self):254 test_db = Ec.Db("path", [Ec.Record("type", name, self.field_list +255 [Ec.Field("log_test_{}".format(name), 0),256 Ec.Field("log_test_{}".format(name), 0)], self.field_list, []) for name in257 self.record_name_list])258 self.assertEqual(len(pv.get_log_info_tags(test_db)), len(self.record_name_list))259 def test_get_log_info_tags_success_single_period(self):260 test_db = Ec.Db("path", [261 Ec.Record("type", self.record_name_list[0], self.field_list + [Ec.Field("log_period_seconds", 1)],262 self.field_list, [])])263 self.assertFalse(pv.get_log_info_tags(test_db))264 def test_get_log_info_tags_success_single_period_pv(self):265 test_db = Ec.Db("path", [266 Ec.Record("type", self.record_name_list[0], self.field_list + [Ec.Field("log_period_pv", 1)],267 self.field_list, [])])268 self.assertFalse(pv.get_log_info_tags(test_db))269 def test_get_log_info_tags_success_single_period_log(self):270 test_db = Ec.Db("path", [Ec.Record("type", self.record_name_list[0],271 self.field_list + [Ec.Field("test_log", 0), Ec.Field("log_period_pv", 1)],272 self.field_list, [])])273 self.assertFalse(pv.get_log_info_tags(test_db))274 def test_get_log_info_tags_success_multi_period_log(self):275 test_db = Ec.Db("path", [Ec.Record("type", self.record_name_list[0],276 self.field_list + [Ec.Field("test_log", 0), Ec.Field("log_period_pv", 1)],277 self.field_list, []),278 Ec.Record("type", self.record_name_list[0],279 self.field_list + [Ec.Field("test_log_1", 0)], self.field_list, [])])280 self.assertFalse(pv.get_log_info_tags(test_db))281 def test_get_log_info_tags_fail_redefine(self):282 test_db = Ec.Db("path", [Ec.Record("type", self.record_name_list[0],283 self.field_list + [Ec.Field("log_period_pv", 1),284 Ec.Field("log_period_seconds", 2)], self.field_list, [])])285 self.assertEqual(len(pv.get_log_info_tags(test_db)), 1)286 def test_get_log_info_tags_fail_redefine_multi(self):287 test_db = Ec.Db("path",288 [Ec.Record("type", name, self.field_list + [Ec.Field("log_period_pv", 1)], self.field_list, [])289 for name in self.record_name_list])...
test_db.py
Source:test_db.py
1import unittest2import logging3from src.db_manager import *4class TestDatabaseMethods(unittest.TestCase):5 def test_add_user(self): 6 logging.basicConfig()7 log = logging.getLogger("LOG")8 log.warning("Testing adding user")9 10 test_db = DatabaseManager()11 test_db.init_db("test.db")12 test_db.add_user("bob", "bobiscool1")13 self.assertTrue(test_db.password_is_correct("bob", "bobiscool1"))14 test_db.delete_db()15 def test_add_duplicate_user(self):16 logging.basicConfig()17 log = logging.getLogger("LOG")18 log.warning("Testing adding duplicate user")19 test_db = DatabaseManager()20 test_db.init_db("test.db")21 self.assertTrue(test_db.add_user("bob", "bobiscool1"))22 self.assertFalse(test_db.add_user("bob", "helloworld"))23 def test_add_bill(self):24 logging.basicConfig()25 log = logging.getLogger("LOG")26 log.warning("Testing adding bill")27 28 test_db = DatabaseManager()29 test_db.init_db("test.db")30 payment1 = Payment("Bob", 50, True, 0)31 payment2 = Payment("Jim", 45.98, False, 0)32 33 test_db.add_bill("Bob", "Steve", "maccas", 95.98, [payment1, payment2])34 bills = test_db.get_bills_from_username("Steve")35 self.assertEqual(len(bills), 1)36 self.assertEqual(bills[0].bill_id, 1)37 self.assertEqual(bills[0].title, 'maccas')38 self.assertEqual(bills[0].creator_username, 'Steve')39 self.assertEqual(bills[0].total, 95.98)40 specific_bill = test_db.get_bill_from_id(1)41 self.assertEqual(bills[0], specific_bill)42 test_db.delete_db()43 def test_get_payments(self):44 logging.basicConfig()45 log = logging.getLogger("LOG")46 log.warning("Testing getting payments")47 48 test_db = DatabaseManager()49 test_db.init_db("test.db")50 payment1 = Payment("Bob", 50, True, 0)51 52 payment2 = Payment("Jim", 45.98, False, 0)53 54 test_db.add_bill("Bob", "Bob", "maccas", 95.98, [payment1, payment2])55 payments = test_db.get_bills_from_username("Bob")[0].payments56 self.assertListEqual(payments, [Payment(*('Bob', 50.0, True, 1)), Payment(*('Jim', 45.98, False, 2))])57 test_db.delete_db()58 def test_pay_bill(self):59 logging.basicConfig()60 log = logging.getLogger("LOG")61 log.warning("Testing paying bill")62 test_db = DatabaseManager()63 test_db.init_db("test.db")64 payment1 = Payment("bob", 50, False, 0) 65 payment2 = Payment("jim", 45.98, False, 0)66 payment3 = Payment("tom", 10.00, False, 0)67 test_db.add_bill("bob", "bob", "maccas", 105.98, [payment1, payment2, payment3])68 payments = test_db.get_bills_from_username("bob")[0].payments69 70 self.assertEqual(test_db.get_payment_from_id(payments[0].payment_id).is_paid, 0)71 72 test_db.make_payment(2, True)73 test_db.make_payment(3, True)74 self.assertEqual(test_db.get_payment_from_id(payments[0].payment_id).is_paid, 0)75 test_db.make_payment(1, True)76 self.assertEqual(test_db.get_payment_from_id(payments[0].payment_id).is_paid, 1)77 test_db.delete_db()78if __name__ == '__main__':79 test_db = DatabaseManager()80 test_db.init_db("test.db")81 test_db.delete_db()...
test_database.py
Source:test_database.py
2import pandas as pd3import pytest4import pycmtensor as cmt5@pytest.fixture6def test_db():7 swissmetro = pd.read_csv("data/swissmetro.dat", sep="\t")8 db = cmt.Database(name="swissmetro", pandasDatabase=swissmetro, choiceVar="CHOICE")9 globals().update(db.variables)10 # Removing some observations11 # exclude = ((PURPOSE != 1) * (PURPOSE != 3) + (CHOICE == 0)) > 012 # db.remove(exclude)13 db.data.drop(db.data[db.data["CHOICE"] == 0].index, inplace=True)14 # additional steps to format database15 db.data["CHOICE"] -= 1 # set the first choice to 016 db.choices = sorted(db.data["CHOICE"].unique()) # save original choices17 db.autoscale(18 variables=["TRAIN_CO", "TRAIN_TT", "CAR_CO", "CAR_TT", "SM_CO", "SM_TT"],19 default=100.0,20 verbose=False,...
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!