-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathflagship_pool_queue_test.dart
146 lines (122 loc) · 4.72 KB
/
flagship_pool_queue_test.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import 'package:flagship/hits/event.dart';
import 'package:flagship/hits/item.dart';
import 'package:flagship/hits/page.dart';
import 'package:flagship/hits/transaction.dart';
import 'package:flagship/tracking/Batching/pool_queue.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/mockito.dart';
FlagshipPoolQueue poolTest = FlagshipPoolQueue(80);
class MockFlagshipPoolQueueDelegate extends Mock
implements FlagshipPoolQueueDelegate {
@override
void onPoolSizeMaxReached() {
poolTest.fsQueue.clear();
}
}
void main() {
test("pool_queue", () async {
poolTest.delegate = MockFlagshipPoolQueueDelegate();
// create 100 hits
for (int i = 0; i < 100; i++) {
Event testEvent = Event(
action: "testPool" + "$i", category: EventCategory.Action_Tracking);
testEvent.visitorId = "user_" + "$i";
poolTest.addNewTrackElement(testEvent);
// check the creattion id after adding the hit in the pool
expect(testEvent.id.contains(testEvent.visitorId), true);
}
// check the size limiation
expect(poolTest.fsQueue.length, 20);
var extractedList = poolTest.extractXElementFromQueue(20);
// Check the axtraction length
expect(extractedList.length, 20);
// Check the empty
expect(poolTest.isEmpty(), true);
});
test("removeWithvisitorId", () {
// create 100 hits
for (int i = 0; i < 20; i++) {
Event testEvent = Event(
action: "testPool" + "$i", category: EventCategory.Action_Tracking);
testEvent.visitorId = "user_" + "$i";
testEvent.anonymousId = null;
poolTest.addNewTrackElement(testEvent);
// check the creattion id after adding the hit in the pool
expect(testEvent.id.contains(testEvent.visitorId), true);
}
var listWithVisitorId = poolTest.extractHitsWithVisitorId("user_10");
// check length for the returned result
expect(listWithVisitorId.length, 1);
var hitElement = listWithVisitorId.first;
// Check the visitor id for the extract
expect(hitElement.visitorId, "user_10");
// Check the pool length
expect(poolTest.fsQueue.length, 20);
poolTest.removeHitsForVisitorId("user_10");
// Check the pool length
expect(poolTest.fsQueue.length, 19);
});
test("flushPool", () {
poolTest.flushAllTrackFromQueue();
for (int i = 0; i < 100; i++) {
Event testEvent = Event(
action: "testPool" + "$i", category: EventCategory.Action_Tracking);
testEvent.visitorId = "user_" + "$i";
poolTest.addNewTrackElement(testEvent);
// check the creattion id after adding the hit in the pool
expect(testEvent.id.contains(testEvent.visitorId), true);
}
poolTest.flushAllTrackFromQueue();
expect(poolTest.fsQueue.length, 0);
});
test("flushPoolWithConsent", () {
poolTest.flushAllTrackFromQueue();
for (int i = 0; i < 50; i++) {
Event testEvent = Event(
action: "testPool" + "$i", category: EventCategory.Action_Tracking);
testEvent.visitorId = "userTest";
poolTest.addNewTrackElement(testEvent);
}
// Create the consent one
Consent consent = Consent(hasConsented: false);
consent.visitorId = "userTest";
poolTest.addNewTrackElement(consent);
poolTest.flushTrackAndKeepConsent("userTest");
expect(poolTest.fsQueue.length, 1);
poolTest.flushAllTrackFromQueue();
expect(poolTest.fsQueue.length, 0);
});
test("add_hits", () async {
poolTest.delegate = MockFlagshipPoolQueueDelegate();
// create 100 hits
for (int i = 0; i < 5; i++) {
Event testEvent = Event(
action: "testPool" + "$i", category: EventCategory.Action_Tracking);
testEvent.visitorId = "user_" + "$i";
poolTest.addNewTrackElement(testEvent);
// Create transac
Transaction testTransac = Transaction(
transactionId: "user_" + "$i", affiliation: "testAffiliation");
testTransac.visitorId = "user_" + "$i";
poolTest.addNewTrackElement(testTransac);
// create Pageview
Page testPage = Page(location: "testPage");
testPage.visitorId = "user_" + "$i";
poolTest.addNewTrackElement(testPage);
// Create Item
Item testItem = Item(
transactionId: "user_" + "$i", name: "testItem", code: "testCode");
testItem.visitorId = "user_" + "$i";
poolTest.addNewTrackElement(testItem);
// check the creattion id after adding the hit in the pool
expect(testEvent.id.contains(testEvent.visitorId), true);
}
// check the size limiation
expect(poolTest.fsQueue.length, 20);
var extractedList = poolTest.extractXElementFromQueue(20);
// Check the axtraction length
expect(extractedList.length, 20);
// Check the empty
expect(poolTest.isEmpty(), true);
});
}