Best Python code snippet using molecule_python
test_properties.py
Source:test_properties.py
1# Licensed under the Apache License, Version 2.0 (the "License"); you may2# not use this file except in compliance with the License. You may obtain3# a copy of the License at4#5# http://www.apache.org/licenses/LICENSE-2.06#7# Unless required by applicable law or agreed to in writing, software8# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT9# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the10# License for the specific language governing permissions and limitations11# under the License.12from testtools import matchers13from toscaparser.common import exception14from toscaparser.elements.property_definition import PropertyDef15from toscaparser.nodetemplate import NodeTemplate16from toscaparser.properties import Property17from toscaparser.tests.base import TestCase18from toscaparser.utils.gettextutils import _19from toscaparser.utils import yamlparser20class PropertyTest(TestCase):21 def test_type(self):22 test_property_schema = {'type': 'string'}23 propertyInstance = Property('test_property', 'Hughes',24 test_property_schema)25 self.assertEqual('string', propertyInstance.type)26 def test_type_invalid(self):27 test_property_schema = {'type': 'Fish'}28 propertyInstance = Property('test_property', 'Hughes',29 test_property_schema)30 error = self.assertRaises(exception.InvalidTypeError,31 propertyInstance.validate)32 self.assertEqual(_('Type "Fish" is not a valid type.'), str(error))33 def test_list(self):34 test_property_schema = {'type': 'list'}35 propertyInstance = Property('test_property', ['a', 'b'],36 test_property_schema)37 self.assertIsNone(propertyInstance.validate())38 self.assertEqual(['a', 'b'], propertyInstance.value)39 def test_list_invalid(self):40 test_property_schema = {'type': 'list'}41 propertyInstance = Property('test_property', 'a',42 test_property_schema)43 error = self.assertRaises(ValueError, propertyInstance.validate)44 self.assertEqual(_('"a" is not a list.'), str(error))45 def test_list_entry_schema(self):46 test_property_schema = {'type': 'list',47 'entry_schema': {'type': 'string'}}48 propertyInstance = Property('test_property', ['a', 'b'],49 test_property_schema)50 self.assertIsNone(propertyInstance.validate())51 self.assertEqual(['a', 'b'], propertyInstance.value)52 schema_snippet = '''53 type: list54 entry_schema:55 type: string56 constraints:57 - min_length: 258 '''59 test_property_schema = yamlparser.simple_parse(schema_snippet)60 propertyInstance = Property('test_property', ['ab', 'cd'],61 test_property_schema)62 self.assertIsNone(propertyInstance.validate())63 self.assertEqual(['ab', 'cd'], propertyInstance.value)64 def test_list_entry_schema_invalid(self):65 test_property_schema = {'type': 'list',66 'entry_schema': {'type': 'integer'}}67 propertyInstance = Property('test_property', [1, 'b'],68 test_property_schema)69 error = self.assertRaises(ValueError, propertyInstance.validate)70 self.assertEqual(_('"b" is not an integer.'), str(error))71 def test_map(self):72 test_property_schema = {'type': 'map'}73 propertyInstance = Property('test_property', {'a': 'b'},74 test_property_schema)75 self.assertIsNone(propertyInstance.validate())76 self.assertEqual({'a': 'b'}, propertyInstance.value)77 def test_map_invalid(self):78 test_property_schema = {'type': 'map'}79 propertyInstance = Property('test_property', 12,80 test_property_schema)81 error = self.assertRaises(ValueError, propertyInstance.validate)82 self.assertEqual(_('"12" is not a map.'), str(error))83 def test_map_entry_schema(self):84 test_property_schema = {'type': 'map',85 'entry_schema': {'type': 'boolean'}}86 propertyInstance = Property('test_property',87 {'valid': True, 'required': True},88 test_property_schema)89 self.assertIsNone(propertyInstance.validate())90 self.assertEqual({'valid': True, 'required': True},91 propertyInstance.value)92 def test_map_entry_schema_invalid(self):93 test_property_schema = {'type': 'map',94 'entry_schema': {'type': 'boolean'}}95 propertyInstance = Property('test_property',96 {'valid': True, 'contact_name': 123},97 test_property_schema)98 error = self.assertRaises(ValueError, propertyInstance.validate)99 self.assertEqual(_('"123" is not a boolean.'), str(error))100 def test_boolean(self):101 test_property_schema = {'type': 'boolean'}102 propertyInstance = Property('test_property', 'true',103 test_property_schema)104 self.assertIsNone(propertyInstance.validate())105 propertyInstance = Property('test_property', True,106 test_property_schema)107 self.assertIsNone(propertyInstance.validate())108 self.assertEqual(True, propertyInstance.value)109 def test_boolean_invalid(self):110 test_property_schema = {'type': 'boolean'}111 propertyInstance = Property('test_property', 12,112 test_property_schema)113 error = self.assertRaises(ValueError, propertyInstance.validate)114 self.assertEqual(_('"12" is not a boolean.'), str(error))115 def test_float(self):116 test_property_schema = {'type': 'float'}117 propertyInstance = Property('test_property', 0.1,118 test_property_schema)119 self.assertIsNone(propertyInstance.validate())120 self.assertEqual(0.1, propertyInstance.value)121 def test_float_invalid(self):122 test_property_schema = {'type': 'float'}123 propertyInstance = Property('test_property', 12,124 test_property_schema)125 error = self.assertRaises(ValueError, propertyInstance.validate)126 self.assertEqual(_('"12" is not a float.'), str(error))127 def test_timestamp(self):128 test_property_schema = {'type': 'timestamp'}129 # canonical timestamp130 propertyInstance = Property('test_property', '2015-04-01T02:59:43.1Z',131 test_property_schema)132 self.assertIsNone(propertyInstance.validate())133 self.assertEqual("2015-04-01T02:59:43.1Z", propertyInstance.value)134 # iso8601 timestamp135 propertyInstance = Property('test_property',136 '2015-04-01t21:59:43.10-05:00',137 test_property_schema)138 self.assertIsNone(propertyInstance.validate())139 self.assertEqual("2015-04-01t21:59:43.10-05:00",140 propertyInstance.value)141 # space separated timestamp142 propertyInstance = Property('test_property',143 '2015-04-01 21:59:43.10 -5',144 test_property_schema)145 self.assertIsNone(propertyInstance.validate())146 self.assertEqual("2015-04-01 21:59:43.10 -5", propertyInstance.value)147 # no time zone timestamp148 propertyInstance = Property('test_property', '2015-04-01 21:59:43.10',149 test_property_schema)150 self.assertIsNone(propertyInstance.validate())151 self.assertEqual("2015-04-01 21:59:43.10", propertyInstance.value)152 # date (00:00:00Z)153 propertyInstance = Property('test_property', '2015-04-01',154 test_property_schema)155 self.assertIsNone(propertyInstance.validate())156 self.assertEqual("2015-04-01", propertyInstance.value)157 def test_timestamp_invalid(self):158 test_property_schema = {'type': 'timestamp'}159 # invalid timestamp - day out of range160 value = '2015-04-115T02:59:43.1Z'161 propertyInstance = Property('test_property', value,162 test_property_schema)163 error = self.assertRaises(ValueError, propertyInstance.validate)164 expected_message = (_('"%s" is not a valid timestamp.') % value)165 self.assertThat(str(error), matchers.StartsWith(expected_message))166 def test_required(self):167 test_property_schema = {'type': 'string'}168 propertyInstance = Property('test_property', 'Foo',169 test_property_schema)170 self.assertEqual(True, propertyInstance.required)171 def test_proprety_inheritance(self):172 tosca_custom_def = '''173 tosca.nodes.SoftwareComponent.MySoftware:174 derived_from: SoftwareComponent175 properties:176 install_path:177 required: false178 type: string179 default: /opt/mysoftware180 '''181 tosca_node_template = '''182 node_templates:183 mysoftware_instance:184 type: tosca.nodes.SoftwareComponent.MySoftware185 properties:186 component_version: 3.1187 '''188 expected_properties = ['component_version',189 'install_path']190 tpl = self._get_nodetemplate(tosca_node_template, tosca_custom_def)191 self.assertIsNone(tpl.validate())192 self.assertEqual(expected_properties,193 sorted(tpl.get_properties().keys()))194 def test_missing_property_type(self):195 tpl_snippet = '''196 properties:197 prop:198 typo: tosca.mytesttype.Test199 '''200 schema = yamlparser.simple_parse(tpl_snippet)201 error = self.assertRaises(exception.InvalidSchemaError, PropertyDef,202 'prop', None, schema['properties']['prop'])203 self.assertEqual(_('Schema definition of "prop" must have a "type" '204 'attribute.'), str(error))205 def test_invalid_required_value(self):206 tpl_snippet = '''207 properties:208 prop:209 type: tosca.mytesttype.Test210 required: dunno211 '''212 schema = yamlparser.simple_parse(tpl_snippet)213 error = self.assertRaises(exception.InvalidSchemaError, PropertyDef,214 'prop', None, schema['properties']['prop'])215 valid_values = ', '.join(PropertyDef.VALID_REQUIRED_VALUES)216 expected_message = (_('Schema definition of "prop" has "required" '217 'attribute with invalid value "dunno". The '218 'value must be one of "%s".') % valid_values)219 self.assertEqual(expected_message, str(error))220 def test_invalid_property_status(self):221 tpl_snippet = '''222 properties:223 prop:224 type: string225 status: unknown226 '''227 schema = yamlparser.simple_parse(tpl_snippet)228 error = self.assertRaises(exception.InvalidSchemaError, PropertyDef,229 'prop', None, schema['properties']['prop'])230 valid_values = ', '.join(PropertyDef.VALID_STATUS_VALUES)231 expected_message = (_('Schema definition of "prop" has "status" '232 'attribute with invalid value "unknown". The '233 'value must be one of "%s".') % valid_values)234 self.assertEqual(expected_message, str(error))235 def test_capability_proprety_inheritance(self):236 tosca_custom_def_example1 = '''237 tosca.capabilities.ScalableNew:238 derived_from: tosca.capabilities.Scalable239 properties:240 max_instances:241 type: integer242 default: 0243 required: no244 tosca.nodes.ComputeNew:245 derived_from: tosca.nodes.Compute246 capabilities:247 scalable:248 type: tosca.capabilities.ScalableNew249 '''250 tosca_node_template_example1 = '''251 node_templates:252 compute_instance:253 type: tosca.nodes.ComputeNew254 capabilities:255 scalable:256 properties:257 min_instances: 1258 '''259 tosca_custom_def_example2 = '''260 tosca.nodes.ComputeNew:261 derived_from: tosca.nodes.Compute262 capabilities:263 new_cap:264 type: tosca.capabilities.Scalable265 '''266 tosca_node_template_example2 = '''267 node_templates:268 db_server:269 type: tosca.nodes.ComputeNew270 capabilities:271 host:272 properties:273 num_cpus: 1274 '''275 tpl1 = self._get_nodetemplate(tosca_node_template_example1,276 tosca_custom_def_example1)277 self.assertIsNone(tpl1.validate())278 tpl2 = self._get_nodetemplate(tosca_node_template_example2,279 tosca_custom_def_example2)280 self.assertIsNone(tpl2.validate())281 def _get_nodetemplate(self, tpl_snippet,282 custom_def_snippet=None):283 nodetemplates = yamlparser.\284 simple_parse(tpl_snippet)['node_templates']285 custom_def = []286 if custom_def_snippet:287 custom_def = yamlparser.simple_parse(custom_def_snippet)288 name = list(nodetemplates.keys())[0]289 tpl = NodeTemplate(name, nodetemplates, custom_def)290 return tpl291 def test_explicit_relationship_proprety(self):292 tosca_node_template = '''293 node_templates:294 client_node:295 type: tosca.nodes.Compute296 requirements:297 - local_storage:298 node: my_storage299 relationship:300 type: AttachesTo301 properties:302 location: /mnt/disk303 my_storage:304 type: tosca.nodes.BlockStorage305 properties:306 size: 1 GB307 '''308 expected_properties = ['location']309 nodetemplates = yamlparser.\310 simple_parse(tosca_node_template)['node_templates']311 tpl = NodeTemplate('client_node', nodetemplates, [])312 self.assertIsNone(tpl.validate())313 rel_tpls = []314 for relationship, trgt in tpl.relationships.items():315 rel_tpls.extend(trgt.get_relationship_template())316 self.assertEqual(expected_properties,...
tests.py
Source:tests.py
1# Nathan2from django.test import TestCase3from django.contrib.auth.models import User4from users.models import *5from django.db import transaction6# Tests for the Profile Model7class ProfileModelTest(TestCase):8 def setUp(self):9 try:10 with transaction.atomic():11 test_user = User.objects.create_user(12 username = "test_user",13 password = "test_user",14 email = "123456@gmail.com",15 first_name = "hello",16 last_name = "hi"17 )18 test_user.full_clean()19 test_user.save()20 except:21 pass22 try:23 with transaction.atomic():24 test_user = User.objects.filter(username = "test_user")[0]25 test_profile = Profile.objects.create(26 user = test_user,27 image = "default.jpg",28 bio = "hello",29 pronouns = "she/they",30 on_grounds = Profile.ON_GROUNDS,31 display_profile = True32 )33 test_profile.full_clean()34 test_profile.save()35 except:36 pass37 def test_profile_str(self):38 print("Testing profile __str__() method...")39 test_user = User.objects.filter(username = "test_user")[0]40 test_profile = Profile.objects.filter(user__username = "test_user")[0]41 self.assertEqual(str(test_profile), "test_user\'s Profile")42 def test_profile_valid_graduation_year(self):43 print("Testing profile valid graduation year...")44 test_profile = Profile.objects.filter(user__username = "test_user")[0]45 test_profile.graduation_year = 202246 self.assertTrue(test_profile.is_valid_graduation_year())47 def test_profile_invalid_graduation_year(self):48 print("Testing profile invalid graduation year...")49 test_profile = Profile.objects.filter(user__username = "test_user")[0]50 test_profile.graduation_year = 204051 self.assertFalse(test_profile.is_valid_graduation_year())52 def test_profile_valid_age(self):53 print("Testing profile valid age...")54 test_profile = Profile.objects.filter(user__username = "test_user")[0]55 test_profile.age = 1956 self.assertTrue(test_profile.is_valid_age())57 def test_profile_invalid_age(self):58 print("Testing profile invalid age...")59 test_profile = Profile.objects.filter(user__username = "test_user")[0]60 test_profile.age = 204061 self.assertFalse(test_profile.is_valid_age())62 def test_profile_valid_phone_number1(self):63 print("Testing profile valid phone number 1...")64 test_profile = Profile.objects.filter(user__username = "test_user")[0]65 test_profile.phone_number = "472-555-0291"66 self.assertTrue(test_profile.is_valid_phone_number())67 68 def test_profile_valid_phone_number2(self):69 print("Testing profile valid phone number 2...")70 test_profile = Profile.objects.filter(user__username = "test_user")[0]71 test_profile.phone_number = "9471958362"72 self.assertTrue(test_profile.is_valid_phone_number())73 74 def test_profile_valid_phone_number3(self):75 print("Testing profile valid phone number 2...")76 test_profile = Profile.objects.filter(user__username = "test_user")[0]77 test_profile.phone_number = "2759%185036+"78 self.assertTrue(test_profile.is_valid_phone_number())79 def test_profile_invalid_phone_number1(self):80 print("Testing profile invalid phone number 1...")81 test_profile = Profile.objects.filter(user__username = "test_user")[0]82 test_profile.phone_number = "317-431-06941"83 self.assertFalse(test_profile.is_valid_phone_number())84 85 def test_profile_invalid_phone_number2(self):86 print("Testing profile invalid phone number 2...")87 test_profile = Profile.objects.filter(user__username = "test_user")[0]88 test_profile.phone_number = "38571058372"89 self.assertFalse(test_profile.is_valid_phone_number())90 91 def test_profile_invalid_phone_number3(self):92 print("Testing profile invalid phone number 3...")93 test_profile = Profile.objects.filter(user__username = "test_user")[0]94 test_profile.phone_number = "5039%0593812+"95 self.assertFalse(test_profile.is_valid_phone_number())96 def test_profile_valid_max_price(self):97 print("Testing profile valid max price...")98 test_profile = Profile.objects.filter(user__username = "test_user")[0]99 test_profile.max_price = 1500100 self.assertTrue(test_profile.is_valid_max_price())101 def test_profile_invalid_max_price(self):102 print("Testing profile invalid max price...")103 test_profile = Profile.objects.filter(user__username = "test_user")[0]104 test_profile.max_price = -500105 self.assertFalse(test_profile.is_valid_max_price())106 # 0 should be a valid price107 def test_profile_edge_case_max_price(self):108 print("Testing profile edge case max price...")109 test_profile = Profile.objects.filter(user__username = "test_user")[0]110 test_profile.max_price = 0111 self.assertTrue(test_profile.is_valid_max_price())112# Tests for the Property Model113class PropertyModelTest(TestCase):114 def setUp(self):115 try:116 with transaction.atomic():117 test_user = User.objects.create_user(118 username = "test_user",119 password = "test_user",120 email = "123456@gmail.com",121 first_name = "hello",122 last_name = "hi"123 )124 test_user.full_clean()125 test_user.save()126 except:127 pass128 try:129 with transaction.atomic():130 test_user = User.objects.filter(username = "test_user")[0]131 test_profile = Profile.objects.create(132 user = test_user,133 image = "default.jpg",134 bio = "hello",135 pronouns = "she/they",136 on_grounds = Profile.ON_GROUNDS,137 display_profile = True138 )139 test_profile.full_clean()140 test_profile.save()141 except:142 pass143 try:144 with transaction.atomic():145 test_profile = Profile.objects.filter(user__username = "test_user")[0]146 test_property = Property.objects.create(147 profile = test_profile,148 image = "default_property.jpg"149 )150 test_property.full_clean()151 test_property.save()152 except:153 pass154 def test_property_str(self):155 print("Testing property __str__() method...")156 test_property = Property.objects.filter(profile__user__username = "test_user")[0]157 self.assertEqual(str(test_property), "hello hi\'s Property")158 def test_property_valid_rent(self):159 print("Testing property valid rent...")160 test_property = Property.objects.filter(profile__user__username = "test_user")[0]161 test_property.rent = 1500162 self.assertTrue(test_property.is_valid_rent())163 def test_property_invalid_rent(self):164 print("Testing property invalid rent...")165 test_property = Property.objects.filter(profile__user__username = "test_user")[0]166 test_property.rent = -200167 self.assertFalse(test_property.is_valid_rent())168 # 0 is a valid rent number169 def test_property_edge_case_rent(self):170 print("Testing property edge case rent...")171 test_property = Property.objects.filter(profile__user__username = "test_user")[0]172 test_property.rent = 0173 self.assertTrue(test_property.is_valid_rent())174 def test_property_valid_curr_num_roommates(self):175 print("Testing property valid curr num roommates...")176 test_property = Property.objects.filter(profile__user__username = "test_user")[0]177 test_property.current_number_of_roommates = 2178 self.assertTrue(test_property.is_valid_curr_num_roommates())179 def test_property_invalid_curr_num_roommates(self):180 print("Testing property invalid curr num roommates...")181 test_property = Property.objects.filter(profile__user__username = "test_user")[0]182 test_property.current_number_of_roommates = 34183 self.assertFalse(test_property.is_valid_curr_num_roommates())184 # 0 is a valid current number of roommates185 def test_property_edge_case_curr_num_roommates(self):186 print("Testing property edge case curr num roommates...")187 test_property = Property.objects.filter(profile__user__username = "test_user")[0]188 test_property.current_number_of_roommates = 0189 self.assertTrue(test_property.is_valid_curr_num_roommates())190 def test_property_valid_num_roommates_seeking(self):191 print("Testing property valid num roommates seeking...")192 test_property = Property.objects.filter(profile__user__username = "test_user")[0]193 test_property.number_of_roommates_seeking = 2194 self.assertTrue(test_property.is_valid_num_roommates_seeking())195 # 0 is a NOT a valid number of roommates seeking196 def test_property_invalid_roommates_seeking(self):197 print("Testing property invalid num roommates seeking...")198 test_property = Property.objects.filter(profile__user__username = "test_user")[0]199 test_property.number_of_roommates_seeking = 0200 self.assertFalse(test_property.is_valid_num_roommates_seeking())201 # 1 is a valid current number of roommates202 def test_property_edge_case_roommates_seeking(self):203 print("Testing property edge case num roommates seeking...")204 test_property = Property.objects.filter(profile__user__username = "test_user")[0]205 test_property.number_of_roommates_seeking = 1206 self.assertTrue(test_property.is_valid_num_roommates_seeking())207 def test_property_valid_num_bedrooms(self):208 print("Testing property valid num bedrooms...")209 test_property = Property.objects.filter(profile__user__username = "test_user")[0]210 test_property.number_of_bedrooms = 3211 self.assertTrue(test_property.is_valid_number_of_bedrooms())212 def test_property_invalid_num_bedrooms(self):213 print("Testing property invalid num bedrooms...")214 test_property = Property.objects.filter(profile__user__username = "test_user")[0]215 test_property.number_of_bedrooms = 58216 self.assertFalse(test_property.is_valid_number_of_bedrooms())217 def test_property_valid_num_bathrooms(self):218 print("Testing property valid num bathrooms...")219 test_property = Property.objects.filter(profile__user__username = "test_user")[0]220 test_property.number_of_bathrooms = 3221 self.assertTrue(test_property.is_valid_number_of_bathrooms())222 def test_property_invalid_num_bathrooms(self):223 print("Testing property invalid num bathrooms...")224 test_property = Property.objects.filter(profile__user__username = "test_user")[0]225 test_property.number_of_bathrooms = 58226 self.assertFalse(test_property.is_valid_number_of_bathrooms())227 def test_property_valid_lease_duration(self):228 print("Testing property valid lease duration...")229 test_property = Property.objects.filter(profile__user__username = "test_user")[0]230 test_property.lease_duration = 3231 self.assertTrue(test_property.is_valid_lease_duration())232 def test_property_invalid_lease_duration(self):233 print("Testing property invalid lease duration...")234 test_property = Property.objects.filter(profile__user__username = "test_user")[0]235 test_property.lease_duration = 753...
test_python.py
Source:test_python.py
1import sys2import os3from typing import List4from python import EfkEfcProperty5def test_property(path: str, files: List[str]):6 f = open(path, 'rb')7 buf = f.read()8 result = EfkEfcProperty.loadEfkEfcInformation(buf)9 included_files = [df.path for df in result.dependentFiles]10 included_files.sort()11 files.sort()12 if files != included_files:13 raise "Not equal"14test_property('../../TestData/Effects/15/BasicRenderSettings_Blend.efkefc',15 ['../Textures/Particle03.png'])16test_property('../../TestData/Effects/15/Material_WorldPositionOffset.efkefc',17 ['../Materials/WorldPositionOffset.efkmat', '../Models/Sphare_Resized.efkmodel', '../Textures/Normal1.png', '../Textures/TestPattern.png'])18test_property('../../TestData/Effects/16/Curve01.efkefc',19 ['../Curves/Curve1.efkcurve'])20test_property('../../TestData/Effects/16/SoftParticle01.efkefc',21 ['../Materials/DepthFade.efkmat', '../Models/Sphare_Resized.efkmodel'])22test_property('../../TestData/Effects/17_Dependency/BasicRenderSettings_Blend.efkefc',23 ['../Textures/Particle03.png'])24test_property('../../TestData/Effects/17_Dependency/Material_WorldPositionOffset.efkefc',25 ['../Materials/WorldPositionOffset.efkmat', '../Models/Sphare_Resized.efkmodel', '../Textures/Normal1.png', '../Textures/TestPattern.png'])26test_property('../../TestData/Effects/17_Dependency/Curve01.efkefc',27 ['../Curves/Curve1.efkcurve'])28test_property('../../TestData/Effects/17_Dependency/SoftParticle01.efkefc',...
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!!