Skip to content

Commit

Permalink
Ussd models tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Luis Ciber committed Aug 21, 2021
1 parent 6ca1357 commit f35b984
Show file tree
Hide file tree
Showing 10 changed files with 326 additions and 9 deletions.
19 changes: 19 additions & 0 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
on: [push, pull_request, workflow_dispatch]
jobs:
tests:
runs-on: ubuntu-latest
steps:
- name: Setup Action
uses: actions/checkout@v2
- name: Setup Java
uses: actions/setup-java@v1
with:
java-version: '12.x'
- name: Setup Flutter
uses: subosito/flutter-action@v1
with:
channel: 'stable'
- name: Run Tests
run: |
flutter pub get
flutter test
15 changes: 8 additions & 7 deletions lib/app/data/models/ussd/category/ussd_category.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,19 @@ class UssdCategory extends UssdItem {
final List<UssdItem> items;

static List<UssdItem> _fieldsFromJson(
List<Map<String, dynamic>> fieldList,
List<dynamic> fieldList,
) =>
fieldList.map<UssdItem>(
(e) {
if (e['type'] == 'code') {
return UssdCode.fromJson(e);
} else if (e['type'] == 'category') {
return UssdCategory.fromJson(e);
final jsonMap = e as Map<String, dynamic>;
if (jsonMap['type'] == 'code') {
return UssdCode.fromJson(jsonMap);
} else if (jsonMap['type'] == 'category') {
return UssdCategory.fromJson(jsonMap);
} else {
throw ParseUssdCodeException(
message: "Unknown type ${e['type']}",
map: e,
message: "Unknown type ${jsonMap['type']}",
map: jsonMap,
);
}
},
Expand Down
3 changes: 1 addition & 2 deletions lib/app/data/models/ussd/category/ussd_category.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions test/fixtures/fixture_reader.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import 'dart:convert';
import 'dart:io';

import 'package:todo/app/app.dart';

String fixture(String name) => File('test/fixtures/$name').readAsStringSync();

String fixtureUssdCodes() => File('config/ussd_codes.json').readAsStringSync();

List<UssdItem> fixtureUssdCodesAsListUssdItem() {
final jsonString = File('config/ussd_codes.json').readAsStringSync();
final parsedJson = json.decode(jsonString) as Map<String, dynamic>;

parsedJson['name'] = '';
parsedJson['description'] = '';
parsedJson['icon'] = '';
parsedJson['type'] = 'category';

return UssdCategory.fromJson(parsedJson).items;
}
24 changes: 24 additions & 0 deletions test/fixtures/ussd_category.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "Adelantar Saldo",
"description": "Recarga con un adelanto de saldo",
"icon": "monetization_on",
"type": "category",
"items": [
{
"name": "25 CUP",
"description": "Adelanta con $1.00 CUC de saldo",
"icon": "monetization_on",
"type": "code",
"fields": [],
"code": "*234*3*1*1#"
},
{
"name": "50 CUP",
"description": "Adelanta con $2.00 CUC de saldo",
"icon": "monetization_on",
"type": "code",
"fields": [],
"code": "*234*3*1*2#"
}
]
}
21 changes: 21 additions & 0 deletions test/fixtures/ussd_code.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "Transferir Saldo",
"description": "Transfiere tu saldo",
"icon": "mobile_screen_share",
"type": "code",
"fields": [
{
"name": "telefono",
"type": "phone_number"
},
{
"name": "cantidad",
"type": "money"
},
{
"name": "clave",
"type": "key_number"
}
],
"code": "*234*1*{telefono}*{clave}*{cantidad}#"
}
4 changes: 4 additions & 0 deletions test/fixtures/ussd_code_field.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "telefono",
"type": "phone_number"
}
94 changes: 94 additions & 0 deletions test/models/ussd/ussd_category_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import 'dart:convert';

import 'package:flutter_test/flutter_test.dart';
import 'package:todo/app/app.dart';

import '../../fixtures/fixture_reader.dart';

void main() {
final tUssdCategoryModel = UssdCategory(
name: 'Adelantar Saldo',
description: 'Recarga con un adelanto de saldo',
icon: 'monetization_on',
type: 'category',
items: [
UssdCode.fromJson(const {
'name': '25 CUP',
'description': r'Adelanta con $1.00 CUC de saldo',
'icon': 'monetization_on',
'type': 'code',
'fields': [],
'code': '*234*3*1*1#'
}),
UssdCode.fromJson(const {
'name': '50 CUP',
'description': r'Adelanta con $2.00 CUC de saldo',
'icon': 'monetization_on',
'type': 'code',
'fields': [],
'code': '*234*3*1*2#'
})
],
);

group(
'+ fromJson"\n ',
() {
test(
'- Should return a valid model from JSON',
() {
// arrange
final jsonMap = json.decode(fixture('ussd_category.json'))
as Map<String, dynamic>;

// act
final result = UssdCategory.fromJson(jsonMap);

// assert
expect(result, tUssdCategoryModel);
},
);
},
);

group(
'toJson',
() {
test(
'Should return a JSON map containing the poper data',
() {
// act
final result = tUssdCategoryModel.toJson();

final expectedMap = {
'name': 'Adelantar Saldo',
'description': 'Recarga con un adelanto de saldo',
'icon': 'monetization_on',
'type': 'category',
'items': [
{
'name': '25 CUP',
'description': r'Adelanta con $1.00 CUC de saldo',
'icon': 'monetization_on',
'type': 'code',
'fields': [],
'code': '*234*3*1*1#'
},
{
'name': '50 CUP',
'description': r'Adelanta con $2.00 CUC de saldo',
'icon': 'monetization_on',
'type': 'code',
'fields': [],
'code': '*234*3*1*2#'
}
]
};

// assert
expect(result, expectedMap);
},
);
},
);
}
55 changes: 55 additions & 0 deletions test/models/ussd/ussd_code_field_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import 'dart:convert';

import 'package:flutter_test/flutter_test.dart';
import 'package:todo/app/app.dart';

import '../../fixtures/fixture_reader.dart';

void main() {
const tUssdCodeFieldModel = UssdCodeField(
name: 'telefono',
type: 'phone_number',
);

group(
'+ fromJson"\n ',
() {
test(
'- Should return a valid model from JSON',
() {
// arrange
final jsonMap = json.decode(
fixture('ussd_code_field.json'),
) as Map<String, dynamic>;

// act
final result = UssdCodeField.fromJson(jsonMap);

// assert
expect(result, tUssdCodeFieldModel);
},
);
},
);

group(
'toJson',
() {
test(
'Should return a JSON map containing the poper data',
() {
// act
final result = tUssdCodeFieldModel.toJson();

final expectedMap = {
'name': 'telefono',
'type': 'phone_number',
};

// assert
expect(result, expectedMap);
},
);
},
);
}
80 changes: 80 additions & 0 deletions test/models/ussd/ussd_code_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import 'dart:convert';

import 'package:flutter_test/flutter_test.dart';
import 'package:todo/app/app.dart';

import '../../fixtures/fixture_reader.dart';

void main() {
const tUssdCodeModel = UssdCode(
name: 'Transferir Saldo',
description: 'Transfiere tu saldo',
icon: 'mobile_screen_share',
type: 'code',
code: '*234*1*{telefono}*{clave}*{cantidad}#',
fields: [
UssdCodeField(
name: 'telefono',
type: 'phone_number',
),
UssdCodeField(
name: 'cantidad',
type: 'money',
),
UssdCodeField(
name: 'clave',
type: 'key_number',
)
],
);

group(
'+ fromJson"\n ',
() {
test(
'- Should return a valid model from JSON',
() {
// arrange
final jsonMap = json.decode(
fixture('ussd_code.json'),
) as Map<String, dynamic>;

// act
final result = UssdCode.fromJson(jsonMap);

// assert
expect(result, tUssdCodeModel);
},
);
},
);

group(
'toJson',
() {
test(
'Should return a JSON map containing the poper data',
() {
// act
final result = tUssdCodeModel.toJson();

final expectedMap = {
'name': 'Transferir Saldo',
'description': 'Transfiere tu saldo',
'icon': 'mobile_screen_share',
'type': 'code',
'fields': [
{'name': 'telefono', 'type': 'phone_number'},
{'name': 'cantidad', 'type': 'money'},
{'name': 'clave', 'type': 'key_number'}
],
'code': '*234*1*{telefono}*{clave}*{cantidad}#'
};

// assert
expect(result, expectedMap);
},
);
},
);
}

0 comments on commit f35b984

Please sign in to comment.