Skip to content

Commit

Permalink
✅ Added Unit testing
Browse files Browse the repository at this point in the history
  • Loading branch information
sumanrajpathak committed Apr 4, 2024
1 parent 0abf2cd commit bf72f0d
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 14 deletions.
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ dependencies:
dev_dependencies:
lints: ^1.0.0
test: ^1.16.0
mocktail: ^1.0.3
33 changes: 33 additions & 0 deletions test/data.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
String testIP = "202.XX.XX.19";
String testIPV6 = "2403:38XX:3249:47:XXXX:8692:XXXX:650b";
Map<String, dynamic> geoIpJson = {
"ip": "202.XX.XX.19",
"continent_code": "AS",
"country": "Nepal",
"country_code": "NP",
"country_code3": "NPL",
"region": "Province 2",
"region_code": "P2",
"city": "Jaleshwar",
"latitude": 26.0000,
'longitude': 85.0000,
"timezone": "Asia/Kathmandu",
"offset": 20700,
"asn": 4007,
"organization": "Subisu Cablenet Pvt Ltd, Baluwatar, Kathmandu, Nepal"
};
Map<String, dynamic> getAllDataJson = {
"continent_code": "AS",
"country": "Nepal",
"country_code": "NP",
"country_code3": "NPL",
"region": "Province 2",
"region_code": "P2",
"city": "Jaleshwar",
"latitude": 26.0000,
'longitude': 85.0000,
"timezone": "Asia/Kathmandu",
"offset": 20700,
"asn": 4007,
"organization": "Subisu Cablenet Pvt Ltd, Baluwatar, Kathmandu, Nepal"
};
14 changes: 0 additions & 14 deletions test/ip_address_test.dart

This file was deleted.

84 changes: 84 additions & 0 deletions test/src/ip_address_base_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import 'package:mocktail/mocktail.dart';
import 'package:public_ip_address/public_ip_address.dart';
import 'package:public_ip_address/src/ip_repository.dart';
import 'package:test/test.dart';

import '../data.dart';

class MockRepository extends Mock implements IpRepository {}

void main() {
late MockRepository repository;
setUpAll(() async {
repository = MockRepository();
});
group('IpAddress -', () {
test("test getAllData", () async {
when(() => repository.getAllData()).thenAnswer((_) async => geoIpJson);
Map<String, dynamic> data = await repository.getAllData();
expect(data, isNotNull);
expect(data, geoIpJson);
});
test("getAllDataFor", () async {
when(() => repository.getAllDataFor(testIP))
.thenAnswer((_) async => geoIpJson);
Map<String, dynamic> jsonData = await repository.getAllDataFor(testIP);
expect(jsonData, isNotNull);
expect(jsonData, geoIpJson);
});
test("getAsn", () async {
when(() => repository.getAsn()).thenAnswer((_) async => geoIpJson["asn"]);
int asn = await repository.getAsn();
expect(asn, isNotNull);
expect(asn, isA<int>());
expect(asn, geoIpJson["asn"]);
});
test("getAsnFor", () async {
when(() => repository.getAsnFor(geoIpJson["ip"]))
.thenAnswer((_) async => geoIpJson["asn"]);
int asn = await repository.getAsnFor(geoIpJson["ip"]);
expect(asn, isNotNull);
expect(asn, geoIpJson["asn"]);
});
test("getCity", () async {
when(() => repository.getCity())
.thenAnswer((_) async => geoIpJson["city"]);
String city = await repository.getCity();
expect(city, isNotNull);
expect(city, geoIpJson["city"]);
});
test("getCityFor", () async {
when(() => repository.getCityFor(geoIpJson["ip"]))
.thenAnswer((_) async => geoIpJson["city"]);
String city = await repository.getCityFor(geoIpJson["ip"]);
expect(city, isNotNull);
expect(city, geoIpJson["city"]);
});
test("getSeeIp", () async {
when(() => repository.getSeeIp("ip")).thenAnswer((_) async => testIP);
String ip = await repository.getSeeIp("ip");
expect(ip, isNotNull);
expect(ip, testIP);
});

test("getIP", () async {
when(() => repository.getIp(version: Ip.v4))
.thenAnswer((_) async => testIP);
String ip = await repository.getIp();
expect(ip, isNotNull);
expect(ip, testIP);
});
test("getIpv4", () async {
when(() => repository.getIpv4()).thenAnswer((_) async => testIP);
String ip = await repository.getIpv4();
expect(ip, isNotNull);
expect(ip, testIP);
});
test("getIpv6", () async {
when(() => repository.getIpv6()).thenAnswer((_) async => testIPV6);
String ip = await repository.getIpv6();
expect(ip, isNotNull);
expect(ip, testIPV6);
});
});
}

0 comments on commit bf72f0d

Please sign in to comment.