Skip to content

Commit

Permalink
Add tests for general section
Browse files Browse the repository at this point in the history
Tests for Pagination
Tests for Notion Response
  • Loading branch information
jonathangomz committed Jun 17, 2021
1 parent 21d7fa4 commit 819729d
Show file tree
Hide file tree
Showing 5 changed files with 2,326 additions and 61 deletions.
26 changes: 20 additions & 6 deletions lib/responses/notion_response.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ class NotionResponse {
/// The marker to know if the response is ok.
bool isOk = true;

get content {
if (this.isList) {
return this.pagination;
} else if (this.isDatabase) {
return this.database;
} else {
return this.pagination;
}
}

/// Returns true if the response is a database.
bool get isDatabase => this.object == ObjectTypes.Database;

Expand All @@ -47,16 +57,20 @@ class NotionResponse {
/// Returns true if the response is a page.
bool get isPage => this.object == ObjectTypes.Page;

/// Returns true if the response have no object type.
bool get isNone => this.object == ObjectTypes.None;

/// Main Notion response constructor.
///
/// Can receive the [object] type, the [status] code, the error [code] and the error [message].
///
/// By default the [object] is a list and the [status] is ok (200).
NotionResponse(
{this.object: ObjectTypes.List,
this.status: 200,
this.code,
this.message});
/// By default the [object] type is None and the [status] is zero.
NotionResponse({
this.object: ObjectTypes.None,
this.status: 0,
this.code,
this.message,
});

/// Map a new Notion response instance from a http [response].
///
Expand Down
25 changes: 13 additions & 12 deletions lib/responses/pagination.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,28 @@ class Pagination {
List<Block>? _blocks;
List<Database>? _databases;

/// TODO: made pages class
List<dynamic>? _pages;
get list {
if (this.isEmpty) {
return [];
} else if (this.isBlocksList) {
return this.blocks;
} else if (this.isDatabasesList) {
return this.databases;
}
}

/// The list of blocks for when the response is for blocks.
List<Block> get blocks => isEmpty ? [] : _blocks!;

/// The list of databases for when the response is for databases.
List<Database> get databases => isEmpty ? [] : _databases!;

/// The list of pages for when the response is for pages.
List<dynamic> get pages => isEmpty ? [] : _pages!;

/// Returns true if the result is a list of blocks.
bool get isBlocksList => _blocks != null;

/// Returns true if the result is a list of databases.
bool get isDatabasesList => _databases != null;

/// Returns true if the result is a list of pages.
bool get isPagesList => _pages != null;

/// Main pagination constructor.
///
/// Can receive the [nextCursor], if [hasMore] pages, if [isEmpty] and the corresponding list: [blocks], [databases] or [pages].
Expand All @@ -47,17 +48,17 @@ class Pagination {
this.isEmpty: false,
List<Block>? blocks,
List<Database>? databases,
List<dynamic>? pages,
});

/// Map a new pagination instance from a [json] map.
factory Pagination.fromJson(Map<String, dynamic> json,
{ObjectTypes? staticType}) {
Pagination pagination =
Pagination(hasMore: json['has_more'], nextCursor: json['next_cursor']);
Pagination pagination = Pagination(
hasMore: json['has_more'] ?? false, nextCursor: json['next_cursor']);

// Extract the type of the list
List listOfUnknowns = json['results'] as List;
List listOfUnknowns =
json['results'] != null ? json['results'] as List : [];
if (listOfUnknowns.length > 0) {
ObjectTypes autoType =
stringToObjectType(listOfUnknowns.first['object'] ?? '');
Expand Down
43 changes: 0 additions & 43 deletions test/notion_api_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import 'package:dotenv/dotenv.dart' show load, env, clean;
import 'package:notion_api/notion/blocks/heading.dart';
import 'package:notion_api/notion/blocks/paragraph.dart';
import 'package:notion_api/notion/blocks/todo.dart';
import 'package:notion_api/notion/general/property.dart';
import 'package:notion_api/notion/general/types/notion_types.dart';
import 'package:notion_api/notion/objects/children.dart';
import 'package:notion_api/notion/objects/pages.dart';
Expand Down Expand Up @@ -73,23 +72,6 @@ void main() {

expect(res.status, 200);
});

test('Invalid property', () async {
final NotionPagesClient pages = NotionPagesClient(token: token ?? '');

final Page page = Page(
parent: Parent.database(id: testDatabaseId ?? ''),
).addProperty(
name: 'TEST',
property: TitleProp(content: [Text('ABC')]),
);

var res = await pages.create(page);

expect(res.status, 400);
expect(res.isError, true);
expect(res.code, 'validation_error');
});
});

group('Notion Databases Client', () {
Expand Down Expand Up @@ -124,31 +106,6 @@ void main() {
expect(res.isOk, true);
});

test('Wrong uuid for block children', () async {
final NotionBlockClient blocks = NotionBlockClient(token: token ?? '');

NotionResponse res = await blocks.fetch(
testBlockId != null ? testBlockId!.replaceFirst('d', 'b') : '');

expect(res.status, 404);
expect(res.isOk, false);
expect(res.hasError, true);
expect(res.isError, true);
expect(res.code, 'object_not_found');
});

test('Wrong auth for block children', () async {
final NotionBlockClient blocks = NotionBlockClient(token: '');

NotionResponse res = await blocks.fetch(testBlockId ?? '');

expect(res.status, 401);
expect(res.isOk, false);
expect(res.hasError, true);
expect(res.isError, true);
expect(res.code, 'unauthorized');
});

test('Append heading & text', () async {
final NotionBlockClient blocks = NotionBlockClient(token: token ?? '');

Expand Down
Loading

0 comments on commit 819729d

Please sign in to comment.