Esempio n. 1
0
    def __init__(self):
        logger.info("VMB %s starting, using %s core" %
                    (self.get_app_version(), self.get_core_version()))

        super(MainModel, self).__init__()
        self.bus = dbus.SystemBus()
        self.obj = None
        self.conf = config
        # we have to break MVC here :P
        self.ctrl = None
        # stats stuff
        self.is_3g_bearer = True  # we assume 3G
        self.prev_rx_bytes = 0
        self.prev_tx_bytes = 0
        self.start_time = None
        self.stop_time = None
        self.rx_bytes = self.tx_bytes = 0
        # DialStats SignalMatch
        self.stats_sm = None
        self.rssi_sm = None
        self.reginfo_sm = None
        self.dialer_manager = None
        self.dial_path = None
        self.device_opath = None
        self._we_dialed = None
        self.preferences_model = PreferencesModel(lambda: self.device)
        self.profiles_model = ProfilesModel(self)
        self.provider = UsageProvider(USAGE_DB)
        self._init_wader_object()
        # Per SIM stuff
        self.imsi = None
        self.msisdn = None
        # PIN in keyring stuff
        self.manage_pin = False
        self.keyring_available = self.is_keyring_available()
Esempio n. 2
0
 def setUp(self):
     self.provider = UsageProvider(':memory:')
Esempio n. 3
0
class TestUsageProvider(unittest.TestCase):
    def setUp(self):
        self.provider = UsageProvider(':memory:')

    def tearDown(self):
        self.provider.close()

    def test_add_usage_item(self):
        now = get_tz_aware_now()
        later = now + timedelta(minutes=30)
        umts, bytes_recv, bytes_sent = True, 12345460, 12333211
        item = self.provider.add_usage_item(now, later, bytes_recv,
                                            bytes_sent, umts)
        usage_items = self.provider.get_usage_for_day(date.today())
        self.assertIn(item, usage_items)
        self.provider.delete_usage_item(item)

    def test_delete_usage_item(self):
        now = get_tz_aware_now()
        later = now + timedelta(minutes=60)
        umts, bytes_recv, bytes_sent = True, 12345470, 12333212
        item = self.provider.add_usage_item(now, later, bytes_recv,
                                            bytes_sent, umts)
        usage_items = self.provider.get_usage_for_day(date.today())
        self.assertIn(item, usage_items)
        self.provider.delete_usage_item(item)
        # now check that it is indeed gone
        usage_items = self.provider.get_usage_for_day(date.today())
        self.assertNotIn(item, usage_items)

    def test_get_usage_for_day(self):
        # add one usage item for today (45m)
        now1 = get_tz_aware_now()
        later1 = now1 + timedelta(minutes=45)
        umts1, bytes_recv1, bytes_sent1 = True, 1200034, 124566
        item1 = self.provider.add_usage_item(now1, later1, bytes_recv1,
                                             bytes_sent1, umts1)
        # add another usage item for today (17m)  55 minutes later
        now2 = get_tz_aware_now() + timedelta(minutes=55)
        later2 = now1 + timedelta(minutes=17)
        umts2, bytes_recv2, bytes_sent2 = True, 12000, 1245
        item2 = self.provider.add_usage_item(now2, later2, bytes_recv2,
                                             bytes_sent2, umts2)

        # add another usage item for tomorrow (25m)
        now3 = get_tz_aware_now() + timedelta(days=1)
        later3 = now3 + timedelta(minutes=25)
        umts3, bytes_recv3, bytes_sent3 = True, 14000, 1785
        item3 = self.provider.add_usage_item(now3, later3, bytes_recv3,
                                             bytes_sent3, umts3)

        # now get the usage for today
        today_items = self.provider.get_usage_for_day(date.today())
        self.assertIn(item1, today_items)
        self.assertIn(item2, today_items)
        self.assertNotIn(item3, today_items)
        # get the usage for tomorrow
        tomorrow = date.today() + timedelta(days=1)
        tomorrow_items = self.provider.get_usage_for_day(tomorrow)
        self.assertNotIn(item1, tomorrow_items)
        self.assertNotIn(item2, tomorrow_items)
        self.assertIn(item3, tomorrow_items)

        # leave it as we found it
        for i in [item1, item2, item3]:
            self.provider.delete_usage_item(i)

    def test_get_usage_for_month(self):
        current_month = date.today().month
        current_year = date.today().year
        # add one usage item for day 12 of this month (45m)
        now1 = datetime(date.today().year, current_month, 12, 13, 10,
                        tzinfo=timezone('UTC'))
        later1 = now1 + timedelta(minutes=45)
        umts1, bytes_recv1, bytes_sent1 = True, 1200034, 124566
        item1 = self.provider.add_usage_item(now1, later1, bytes_recv1,
                                             bytes_sent1, umts1)
        # add another usage item for day 13 of this month (17m)
        now2 = datetime(date.today().year, current_month, 13, 15, 10,
                        tzinfo=timezone('UTC'))
        later2 = now1 + timedelta(minutes=17)
        umts2, bytes_recv2, bytes_sent2 = True, 12000, 1245
        item2 = self.provider.add_usage_item(now2, later2, bytes_recv2,
                                             bytes_sent2, umts2)
        # add another usage item for next month
        if current_month < 12:
            month = current_month + 1
            year = current_year
        else:
            month = 1
            year = current_year + 1

        # next month at 6.50am (25m)
        now3 = datetime(year, month, 2, 6, 50, tzinfo=timezone('UTC'))
        later3 = now3 + timedelta(minutes=25)
        umts3, bytes_recv3, bytes_sent3 = True, 14000, 1785
        item3 = self.provider.add_usage_item(now3, later3, bytes_recv3,
                                             bytes_sent3, umts3)
        # now get the usage for this month
        this_month_items = self.provider.get_usage_for_month(now1.date())
        self.assertIn(item1, this_month_items)
        self.assertIn(item2, this_month_items)
        self.assertNotIn(item3, this_month_items)
        # now get the usage for next month
        next_month_items = self.provider.get_usage_for_month(now3)
        self.assertNotIn(item1, next_month_items)
        self.assertNotIn(item2, next_month_items)
        self.assertIn(item3, next_month_items)
        # leave it as we found it
        for i in [item1, item2, item3]:
            self.provider.delete_usage_item(i)

    def test_get_total_usage(self):
        current_month = date.today().month
        current_year = date.today().year
        # add one usage item for day 12 of this month (45m)
        now1 = datetime(current_year, current_month, 12, 13, 10,
                        tzinfo=timezone('UTC'))
        later1 = now1 + timedelta(minutes=45)
        umts1, bytes_recv1, bytes_sent1 = True, 1200034, 124566
        item1 = self.provider.add_usage_item(now1, later1, bytes_recv1,
                                             bytes_sent1, umts1)
        # add another usage item for day 13 of this month (17m), one year ago
        now2 = datetime(current_year - 1, current_month, 13, 15, 10,
                        tzinfo=timezone('UTC'))
        later2 = now1 + timedelta(minutes=17)
        umts2, bytes_recv2, bytes_sent2 = True, 12000, 1245
        item2 = self.provider.add_usage_item(now2, later2, bytes_recv2,
                                             bytes_sent2, umts2)

        items = self.provider.get_total_usage()
        self.assertIn(item1, items)
        self.assertIn(item2, items)
        # leave it as we found it
        for i in [item1, item2]:
            self.provider.delete_usage_item(i)

    def test_get_total_usage_passing_a_date(self):
        current_month = date.today().month
        current_year = date.today().year
        # add one usage item for day 12 of this month (45m)
        now1 = datetime(current_year, current_month, 12, 13, 10,
                        tzinfo=timezone('UTC'))
        later1 = now1 + timedelta(minutes=45)
        umts1, bytes_recv1, bytes_sent1 = True, 1200034, 124566
        item1 = self.provider.add_usage_item(now1, later1, bytes_recv1,
                                             bytes_sent1, umts1)
        # add another usage item for day 13 of this month (17m), one year ago
        now2 = datetime(current_year - 1, current_month, 13, 15, 10,
                        tzinfo=timezone('UTC'))
        later2 = now1 + timedelta(minutes=17)
        umts2, bytes_recv2, bytes_sent2 = True, 12000, 1245
        item2 = self.provider.add_usage_item(now2, later2, bytes_recv2,
                                             bytes_sent2, umts2)

        items = self.provider.get_total_usage(now1.date())
        self.assertIn(item1, items)
        self.assertNotIn(item2, items)
        # leave it as we found it
        for i in [item1, item2]:
            self.provider.delete_usage_item(i)