def test_collect_unique_enums__inconsistent_duplicates_detected(self): e0 = Enum('age', [11, 33]) e1 = Enum('age', [11, 22, 33]) e2 = Enum('name', ['Jack', 'Alice', 'Joe']) with pytest.raises(EventFactory.ServerError) as e: self.renderer.collect_unique_enums([ Mock( request_query=Mock(enums=[e0]), request_body=Mock(enums=[]), response=Mock(enums=[])), Mock( request_query=Mock(enums=[]), request_body=Mock(enums=[e1, e2]), response=Mock(enums=[])), ]) assert e.value.data == { '@event': 'INCONSISTENT_ENUMS_DETECTED', '@type': 'error', 'inconsistent_enums': [ { 'enum.0': { 'name': 'Age', 'values': [33, 11], }, 'enum.1': { 'name': 'Age', 'values': [33, 11, 22], }, }, ], }
def test_collect_unique_enums__all_unique(self): e0 = Enum('age', [11, 33]) e1 = Enum('name', ['Jack', 'Alice', 'Joe']) e2 = Enum('category', ['AA', 'YY', 'XX']) assert self.renderer.collect_unique_enums([ Mock( request_query=Mock(enums=[e0, e1]), request_body=Mock(enums=[]), response=Mock(enums=[])), Mock( request_query=Mock(enums=[]), request_body=Mock(enums=[e2]), response=Mock(enums=[])), ]) == [e0, e2, e1]
def test_append_enum__no_duplicates(self): interface = Interface('READ_CARDS', Interface.TYPES.RESPONSE, {}, '') e0 = interface.append_enum('age', [19, 91]) assert len(interface.enums) == 1 assert e0 == Enum('age', [19, 91], None)
def test_render__numerical(self): enum0 = Enum('position', [0, 1]) enum1 = Enum('position', ['0', '1', '2']) assert enum0.render() == normalize_indentation(''' export enum Position { VALUE_0 = 0, VALUE_1 = 1, } ''', 0) assert enum1.render() == normalize_indentation(''' export enum Position { VALUE_0 = '0', VALUE_1 = '1', VALUE_2 = '2', } ''', 0)
def test_append_enum__same_name_different_values(self): interface = Interface('X', Interface.TYPES.RESPONSE, {}, '') e0 = interface.append_enum('age', [19, 91]) # noqa e1 = interface.append_enum('age', [19]) assert len(interface.enums) == 2 assert e1 == Enum('age', [19], 1)
def test_render__removes_duplicates(self): enum = Enum('class', ['AA', 'BB', 'AA']) assert enum.render() == normalize_indentation(''' export enum Class { AA = 'AA', BB = 'BB', } ''', 0)
def test_render(self): enum = Enum('age', ['AA', 'BB']) assert enum.render() == normalize_indentation(''' export enum Age { AA = 'AA', BB = 'BB', } ''', 0)
def test_render__starts_from_number(self): enum = Enum('quality', ['240p', '720p', '1080p']) assert enum.render() == normalize_indentation(''' export enum Quality { VALUE_1080P = '1080p', VALUE_240P = '240p', VALUE_720P = '720p', } ''', 0)
def test_name__empty_name(self): with pytest.raises(EventFactory.BrokenRequest) as e: Enum('', ['what']) assert e.value.data == { '@event': 'ENUMS_WITHOUT_NAME_DETECTED', '@type': 'error', 'name': '', 'values': ['what'], }
def test_render__sorts_values(self): enum = Enum('category', ['XX', 'AA', 'BB']) assert enum.render() == normalize_indentation(''' export enum Category { AA = 'AA', BB = 'BB', XX = 'XX', } ''', 0)
def test_render_models_ts__with_enums(self): paths_path = str(self.domains_dir.mkdir('paths')) domain = Domain('paths', 'Path Management') commands = [ Mock( request_query=Mock( render=Mock(return_value=[ 'request 1', [Enum('Type', ['A'])]])), request_body=Mock( render=Mock(return_value=['', []])), response=Mock( render=Mock(return_value=[ 'response 1', [Enum('Jack', []), Enum('Type', [])]])), ), ] assert os.listdir(paths_path) == [] self.renderer.render_models_ts(domain, commands) assert os.listdir(paths_path) == ['paths.models.ts'] with open(os.path.join(paths_path, 'paths.models.ts'), 'r') as f: assert f.read() == normalize_indentation(''' /** * THIS FILE WAS AUTOGENERATED, ALL MANUAL CHANGES CAN BE * OVERWRITTEN */ import { Jack, Type } from '../../shared/enums'; /** * Path Management Domain Models */ request 1 response 1 ''', 0)
def test_render__contains_extra_characters(self): enum = Enum( 'content_type', ['image/svg+xml', 'video/png', 'audio/ogg-what']) assert enum.render() == normalize_indentation(''' export enum ContentType { AUDIO_OGG_WHAT = 'audio/ogg-what', IMAGE_SVG_XML = 'image/svg+xml', VIDEO_PNG = 'video/png', } ''', 0)
def test_render_enums_ts(self): self.mocker.patch.object( self.renderer, 'collect_unique_enums' ).return_value = [ Enum('age', [11, 33]), Enum('category', ['AA', 'YY', 'XX']), Enum('name', ['Jack', 'Alice', 'Joe']), ] shared_path = str(self.src_dir.mkdir('shared')) self.renderer.render_enums_ts(shared_path, Mock()) assert sorted(os.listdir(self.src_dir)) == sorted( ['domains', 'services', 'shared']) assert ( self.src_dir.join('shared/enums.ts').read() == normalize_indentation(''' export enum Age { VALUE_11 = 11, VALUE_33 = 33, } export enum Category { AA = 'AA', XX = 'XX', YY = 'YY', } export enum Name { ALICE = 'Alice', JACK = 'Jack', JOE = 'Joe', } ''', 0)) # noqa
def test__eq__(self): # -- identical assert ( Enum('category', ['XX', 'AA', 'BB']) == Enum('category', ['XX', 'AA', 'BB'])) # -- the same, but with some duplicates and different order assert ( Enum('category', ['XX', 'AA', 'BB']) == Enum('category', ['XX', 'BB', 'AA', 'BB'])) # -- different name assert ( Enum('categories', ['XX', 'AA']) != Enum('category', ['XX', 'AA'])) # -- different values assert ( Enum('category', ['XX', 'AA']) != Enum('category', ['XX', 'YY']))
def test_name(self): assert Enum('age', []).name == 'Age' assert Enum('a_ge', []).name == 'AGe'
'type': 'string', }, }, }, None, normalize_indentation(''' /** * http://here */ export interface ReadCardsResponse { occupation: OccupationType; surname?: string; } ''', 0), [Enum('OccupationType', ['AA', 'BB'])], ), # -- case 6 - enum with const value ( { 'type': 'object', 'properties': { 'employees': { 'type': 'array', 'items': { 'oneOf': [ { 'type': 'object', 'properties': { 'name': {