• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

tfcollins / telemetry / 10495659905

21 Aug 2024 06:45PM UTC coverage: 34.158% (-24.1%) from 58.234%
10495659905

push

github

tfcollins
Put exceptions back for missing backends

Signed-off-by: Travis F. Collins <travis.collins@analog.com>

0 of 2 new or added lines in 1 file covered. (0.0%)

211 existing lines in 13 files now uncovered.

428 of 1253 relevant lines covered (34.16%)

0.34 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

63.74
/telemetry/db.py
UNCOV
1
import sqlite3
×
UNCOV
2
from os import path
×
UNCOV
3
import logging
×
4

5
# logging.basicConfig(level=logging.DEBUG)
6

7

UNCOV
8
class db:
×
UNCOV
9
    schema = ""
×
10

UNCOV
11
    def __init__(self, db_filename="telemetry.db", skip_db_create=False):
×
12
        self.db_filename = db_filename
1✔
13
        self.table_name = "COMPANY"
1✔
14
        if not path.exists(self.db_filename):
1✔
15
            self.conn = sqlite3.connect(self.db_filename)
1✔
16
            if not skip_db_create:
1✔
17
                self.create_db()
1✔
18
        else:
19
            self.conn = sqlite3.connect(self.db_filename)
×
20

UNCOV
21
    def import_schema(self, json_filename):
×
22
        import json
1✔
23

24
        with open(json_filename) as json_raw:
1✔
25
            schema = json.load(json_raw)
1✔
26
        return schema
1✔
27

UNCOV
28
    def create_db_from_schema(self, schema):
×
29
        tbl = " ("
1✔
30
        fields = schema["fields"]
1✔
31
        for field in fields:
1✔
32
            tbl += field + " "
1✔
33
            for subfield in fields[field]:
1✔
34
                tbl += subfield + " "
1✔
35
            tbl = tbl[:-1] + ", "
1✔
36
        tbl = tbl[:-2] + ");"
1✔
37
        tbl = "CREATE TABLE " + schema["table_name"] + tbl
1✔
38
        logging.info(tbl)
1✔
39
        logging.info("Creating db")
1✔
40
        self.conn.execute(tbl)
1✔
41
        self.table_name = schema["table_name"]
1✔
42
        self.schema = schema
1✔
43
        logging.info("Created db")
1✔
44

UNCOV
45
    def create_db(self):
×
46
        logging.info("Creating db")
1✔
47
        self.conn.execute(
1✔
48
            """CREATE TABLE """
49
            + self.table_name
50
            + """
51
            (ID INT PRIMARY KEY     NOT NULL,
52
            NAME           TEXT    NOT NULL,
53
            AGE            INT     NOT NULL,
54
            ADDRESS        CHAR(50),
55
            SALARY         REAL);"""
56
        )
57
        logging.info("Created db")
1✔
58

UNCOV
59
    def check_if_exists(self, id):
×
60
        cur = self.conn.cursor()
1✔
61
        cur.execute("SELECT * FROM " + self.table_name + " WHERE ID=?", (id,))
1✔
62
        rows = cur.fetchall()
1✔
63
        logging.info("Found entries:")
1✔
64
        logging.info(rows)
1✔
65
        return len(rows) > 0
1✔
66

UNCOV
67
    def add_entry_example(self):
×
68
        id = 3
×
69
        if not self.check_if_exists(id):
×
70
            self.conn.execute(
×
71
                "INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) \
72
                    VALUES (3, 'Teddy', 23, 'Norway', 20000.00 )"
73
            )
74
            self.conn.commit()
×
75
        else:
76
            logging.warning("Entry already exists")
×
77

UNCOV
78
    def add_entry(self, entry):
×
79
        id = 3
1✔
80
        if not self.check_if_exists(id):
1✔
81
            fields = "ID, "
1✔
82
            values = "3, "
1✔
83
            for field in entry:
1✔
84
                fields += field + ", "
1✔
85
                if self.schema["fields"][field][0] in ["TEXT", "DATETIME"]:
1✔
86
                    values += "'" + str(entry[field]) + "', "
1✔
87
                else:
88
                    values += str(entry[field]) + ", "
1✔
89
            fields = fields[:-2]
1✔
90
            values = values[:-2]
1✔
91
            cmd = (
1✔
92
                "INSERT INTO "
93
                + self.table_name
94
                + " ("
95
                + fields
96
                + ") VALUES ("
97
                + values
98
                + ")"
99
            )
100
            logging.info(cmd)
1✔
101
            self.conn.execute(cmd)
1✔
102
            self.conn.commit()
1✔
103
        else:
104
            logging.warning("Entry already exists")
×
105

UNCOV
106
    def print_all(self):
×
107
        cursor = self.conn.execute("SELECT id, name, address, salary from COMPANY")
×
108
        for row in cursor:
×
109
            print("ID = ", row[0])
×
110
            print("NAME = ", row[1])
×
111
            print("ADDRESS = ", row[2])
×
112
            print("SALARY = ", row[3])
×
113

UNCOV
114
    def print_all_schema(self):
×
115
        fields = self.schema["fields"].keys()
1✔
116
        fields = [*fields]
1✔
117
        print(fields)
1✔
118
        all_fields = ", ".join(fields)
1✔
119
        cursor = self.conn.execute("SELECT " + all_fields + " from " + self.table_name)
1✔
120
        for row in cursor:
1✔
121
            for i, field in enumerate(fields):
1✔
122
                print(field, "=", row[i])
1✔
123

UNCOV
124
    def __del__(self):
×
125
        self.conn.close()
1✔
126

127

UNCOV
128
if __name__ == "__main__":
×
129
    d = db()
×
130
    d.add_entry()
×
131
    d.print_all()
×
132
    del d
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc