|
| 1 | +import 'package:flutter_test/flutter_test.dart'; |
| 2 | +import 'package:opso/modals/GSoC/Gsoc.dart'; |
| 3 | + |
| 4 | +void main() { |
| 5 | + group('Project', () { |
| 6 | + test('fromJson constructs correctly', () { |
| 7 | + final json = { |
| 8 | + 'title': 'Test Project', |
| 9 | + 'short_description': 'Short desc', |
| 10 | + 'description': 'Full description', |
| 11 | + 'student_name': 'Alice', |
| 12 | + 'code_url': 'https://github.com/alice/project', |
| 13 | + 'project_url': 'https://summerofcode.withgoogle.com/project', |
| 14 | + }; |
| 15 | + final project = Project.fromJson(json); |
| 16 | + expect(project.title, 'Test Project'); |
| 17 | + expect(project.shortDescription, 'Short desc'); |
| 18 | + expect(project.description, 'Full description'); |
| 19 | + expect(project.studentName, 'Alice'); |
| 20 | + expect(project.codeUrl, 'https://github.com/alice/project'); |
| 21 | + expect(project.projectUrl, 'https://summerofcode.withgoogle.com/project'); |
| 22 | + }); |
| 23 | + |
| 24 | + test('fromJson handles missing fields with empty string defaults', () { |
| 25 | + final project = Project.fromJson({}); |
| 26 | + expect(project.title, ''); |
| 27 | + expect(project.shortDescription, ''); |
| 28 | + expect(project.description, ''); |
| 29 | + expect(project.studentName, ''); |
| 30 | + expect(project.codeUrl, ''); |
| 31 | + expect(project.projectUrl, ''); |
| 32 | + }); |
| 33 | + }); |
| 34 | + |
| 35 | + group('Organization', () { |
| 36 | + final orgJson = { |
| 37 | + 'name': 'TestOrg', |
| 38 | + 'image_url': 'https://example.com/image.png', |
| 39 | + 'image_background_color': '#FFFFFF', |
| 40 | + 'description': 'A test organization', |
| 41 | + 'url': 'https://testorg.com', |
| 42 | + 'num_projects': 5, |
| 43 | + 'category': 'programming languages and development tools', |
| 44 | + 'projects_url': 'https://testorg.com/projects', |
| 45 | + 'irc_channel': '#testorg', |
| 46 | + 'contact_email': 'contact@testorg.com', |
| 47 | + 'mailing_list': 'list@testorg.com', |
| 48 | + 'twitter_url': 'https://twitter.com/testorg', |
| 49 | + 'blog_url': 'https://blog.testorg.com', |
| 50 | + 'topics': ['machine learning', 'AI'], |
| 51 | + 'technologies': ['Python', 'TensorFlow'], |
| 52 | + 'projects': [], |
| 53 | + }; |
| 54 | + |
| 55 | + test('fromJson constructs correctly', () { |
| 56 | + final org = Organization.fromJson(orgJson); |
| 57 | + expect(org.name, 'TestOrg'); |
| 58 | + expect(org.numProjects, 5); |
| 59 | + expect(org.topics, ['machine learning', 'AI']); |
| 60 | + expect(org.technologies, ['Python', 'TensorFlow']); |
| 61 | + expect(org.projects, isEmpty); |
| 62 | + }); |
| 63 | + |
| 64 | + test('fromJson handles missing fields with defaults', () { |
| 65 | + final org = Organization.fromJson({}); |
| 66 | + expect(org.name, ''); |
| 67 | + expect(org.numProjects, 0); |
| 68 | + expect(org.topics, isEmpty); |
| 69 | + expect(org.technologies, isEmpty); |
| 70 | + expect(org.projects, isEmpty); |
| 71 | + }); |
| 72 | + |
| 73 | + test('fromJson parses nested projects', () { |
| 74 | + final jsonWithProjects = Map<String, dynamic>.from(orgJson); |
| 75 | + jsonWithProjects['projects'] = [ |
| 76 | + { |
| 77 | + 'title': 'Nested Project', |
| 78 | + 'short_description': '', |
| 79 | + 'description': '', |
| 80 | + 'student_name': 'Bob', |
| 81 | + 'code_url': '', |
| 82 | + 'project_url': '', |
| 83 | + } |
| 84 | + ]; |
| 85 | + final org = Organization.fromJson(jsonWithProjects); |
| 86 | + expect(org.projects, hasLength(1)); |
| 87 | + expect(org.projects.first.title, 'Nested Project'); |
| 88 | + expect(org.projects.first.studentName, 'Bob'); |
| 89 | + }); |
| 90 | + }); |
| 91 | + |
| 92 | + group('Gsoc', () { |
| 93 | + test('fromJson constructs correctly', () { |
| 94 | + final json = { |
| 95 | + 'year': 2023, |
| 96 | + 'archive_url': 'https://summerofcode.withgoogle.com/archive', |
| 97 | + 'organizations': [], |
| 98 | + }; |
| 99 | + final gsoc = Gsoc.fromJson(json); |
| 100 | + expect(gsoc.year, 2023); |
| 101 | + expect(gsoc.archiveUrl, 'https://summerofcode.withgoogle.com/archive'); |
| 102 | + expect(gsoc.organizations, isEmpty); |
| 103 | + }); |
| 104 | + |
| 105 | + test('fromJson handles missing archive_url', () { |
| 106 | + final json = { |
| 107 | + 'year': 2022, |
| 108 | + 'organizations': [], |
| 109 | + }; |
| 110 | + final gsoc = Gsoc.fromJson(json); |
| 111 | + expect(gsoc.archiveUrl, ''); |
| 112 | + }); |
| 113 | + |
| 114 | + test('fromJson parses nested organizations', () { |
| 115 | + final json = { |
| 116 | + 'year': 2021, |
| 117 | + 'archive_url': '', |
| 118 | + 'organizations': [ |
| 119 | + { |
| 120 | + 'name': 'Org1', |
| 121 | + 'image_url': '', |
| 122 | + 'image_background_color': '', |
| 123 | + 'description': '', |
| 124 | + 'url': '', |
| 125 | + 'num_projects': 2, |
| 126 | + 'category': '', |
| 127 | + 'projects_url': '', |
| 128 | + 'irc_channel': '', |
| 129 | + 'contact_email': '', |
| 130 | + 'mailing_list': '', |
| 131 | + 'twitter_url': '', |
| 132 | + 'blog_url': '', |
| 133 | + 'topics': [], |
| 134 | + 'technologies': [], |
| 135 | + 'projects': [], |
| 136 | + } |
| 137 | + ], |
| 138 | + }; |
| 139 | + final gsoc = Gsoc.fromJson(json); |
| 140 | + expect(gsoc.organizations, hasLength(1)); |
| 141 | + expect(gsoc.organizations.first.name, 'Org1'); |
| 142 | + expect(gsoc.organizations.first.numProjects, 2); |
| 143 | + }); |
| 144 | + }); |
| 145 | +} |
0 commit comments