Skip to content

Commit

Permalink
Merge pull request #25 from esquerbatua/feature/tests
Browse files Browse the repository at this point in the history
Feature/tests
  • Loading branch information
JalonSolov authored Aug 20, 2024
2 parents 10f1a27 + 02231c9 commit b537842
Show file tree
Hide file tree
Showing 25 changed files with 248 additions and 102 deletions.
42 changes: 32 additions & 10 deletions .github/workflows/ci.yml_ → .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@ on:
jobs:
tests:
runs-on: ubuntu-latest
strategy:
matrix:
mongodb-version: ['4.2', '4.4', '5.0', '6.0', '7.0']
steps:
- name: Checkout V
uses: actions/checkout@v2
with:
repository: vlang/v
- name: Checkout Mongo
uses: actions/checkout@v2
with:
path: vlib/mongo
- name: Install V dependencies
run: |
sudo apt-get update
Expand All @@ -28,19 +27,42 @@ jobs:
run: |
make
sudo ./v symlink
- name: Checkout Mongo
uses: actions/checkout@v2
with:
path: vlib/mongo
- name: Install Mongo dependencies
run: |
cd ./vlib/mongo/
sudo apt update
sudo make install
- name: Start MongoDB
uses: supercharge/mongodb-github-action@1.10.0
with:
mongodb-version: ${{ matrix.mongodb-version }}
- name: Test
run: |
cd ./vlib/mongo/
v -stats test .
- name: Build Mongo shared lib
run: |
cd ./vlib/mongo/
v -shared .
make test
- name: Build Mongo example
run: |
cd ./vlib/mongo/examples/
v -prod example.v
v -prod example.v
build:
runs-on: ubuntu-latest
container: thevlang/vlang:debian-dev
steps:
- name: Checkout Mongo
uses: actions/checkout@v2
with:
path: vlib/mongo
- name: Install Mongo dependencies
run: |
cd ./vlib/mongo/
apt update
make install
- name: Build Mongo shared lib
run: |
cd ./vlib/mongo/
make build
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
./*.so
*.so
mongo-c-driver*
mongo.so
v
9 changes: 9 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FROM thevlang/vlang:debian-dev AS builder

WORKDIR /app
RUN v up
RUN apt update
COPY . .
RUN make install
RUN make build
RUN make test
11 changes: 9 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,18 @@ build_mongo_c_driver:
cmake -DENABLE_AUTOMATIC_INIT_AND_CLEANUP=OFF .. && \
make && \
make install

install:
apt install libbson-1.0-0 libmongoc-1.0-0
apt install -y libbson-dev libmongoc-dev

dev:
v -cg -shared watch .

build:
v -shared .
v -shared -prod .

test:
v -stats test .

test_watch:
v -stats -keepc -cg watch test . --only-watch=*.v,*.h
2 changes: 1 addition & 1 deletion examples/find-lean-example.v
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ fn main() {
client := mongo.new_client(url)

mut dt := sw.elapsed().microseconds()
println('Elapsed time (new_client): $dt uS') // Elapsed time (new_client): 38 uS
println('Elapsed time (new_client): ${dt} uS') // Elapsed time (new_client): 38 uS

collection := client.get_collection('test', 'mongo-test')

Expand Down
4 changes: 2 additions & 2 deletions examples/insert-example.v
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ fn main() {
boolean: true
}

struct_bson := mongo.new_bson_from<Test>(test)
struct_bson := mongo.new_bson_from[Test](test)
json_bson := mongo.new_from_json('{"str":"string","number":2,"float":2.1,"boolean":true}')

child := json2.Any({
Expand All @@ -37,7 +37,7 @@ fn main() {
'foo': child
})

collection.insert_one_from<Test>(test)
collection.insert_one_from(test)
collection.insert_one_from_bson_t(struct_bson)
collection.insert_one_from_bson_t(json_bson)

Expand Down
Binary file removed mongo.so
Binary file not shown.
10 changes: 6 additions & 4 deletions src/bson.v
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ pub fn new_bson() &C.bson_t {

@[inline]
pub fn new_bson_oid_filter(_oid string) &C.bson_t {
return new_from_json('{"_id": {"\$oid": "$_oid"}}')
return new_from_json('{"_id": {"\$oid": "${_oid}"}}')
}

@[inline]
pub fn new_bson_from<T>(t T) &C.bson_t {
pub fn new_bson_from[T](t T) &C.bson_t {
json_data := json.encode(t)
error := C.bson_error_t{}
bson := C.bson_new_from_json(json_data.str, json_data.len, &error)
Expand All @@ -37,10 +37,12 @@ pub fn (document &C.bson_t) reinit() {
C.bson_reinit(document)
}

@[unsafe]
pub fn (document &C.bson_t) destroy() {
C.bson_destroy(document)
}

@[unsafe]
pub fn free(mem voidptr) {
C.bson_free(mem)
}
Expand Down Expand Up @@ -109,8 +111,8 @@ pub fn (document &C.bson_t) equal(b &C.bson_t) bool {
return C.bson_equal(document, b)
}

pub fn (document &C.bson_t) to<T>() ?T {
pub fn (document &C.bson_t) to[T]() ?T {
doc := document.str()
println('tp: $doc')
println('tp: ${doc}')
return json.decode(T, doc)
}
8 changes: 8 additions & 0 deletions src/bson.c_fn.v → src/bson_fn.c.v
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
module mongo

#flag -I @VMODROOT/thirdparty/libbson-1.0
#flag -I @VMODROOT/thirdparty/libmongoc-1.0
#flag -l mongoc-1.0
#flag -l bson-1.0

#include "mongoc/mongoc.h"
#include "bson/bson.h"

fn C.bson_new() &C.bson_t
fn C.bson_new_from_json(byteptr, int, &C.bson_error_t) &C.bson_t
fn C.bson_init_from_json(&C.bson_t, byteptr, int, &C.bson_error_t) bool
Expand Down
16 changes: 15 additions & 1 deletion src/bson.c_structs.v → src/bson_structs.c.v
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
module mongo

#flag -I @VMODROOT/thirdparty/libbson-1.0
#flag -I @VMODROOT/thirdparty/libmongoc-1.0
#flag -l mongoc-1.0
#flag -l bson-1.0

#include "mongoc/mongoc.h"
#include "bson/bson.h"

// http://mongoc.org/libbson/current/bson_t.html
@[typedef]
pub struct C.bson_t {
flags u32 // Internal flags for the bson_t.
len u32 // Length of BSON data.
padding u8 // Padding for stack allocation.
padding u8 // Padding for stack allocation.
}

// http://mongoc.org/libbson/current/bson_oid_t.html
Expand All @@ -15,21 +23,27 @@ pub struct C.bson_oid_t {
}

// http://mongoc.org/libbson/current/bson_subtype_t.html
@[typedef]
pub struct C.bson_subtype_t {}

// http://mongoc.org/libbson/current/bson_context_t.html
@[typedef]
pub struct C.bson_context_t {}

// http://mongoc.org/libbson/current/bson_type_t.html
@[typedef]
pub struct C.bson_type_t {}

// http://mongoc.org/libbson/current/bson_decimal128_t.html
@[typedef]
pub struct C.bson_decimal128_t {}

// http://mongoc.org/libbson/current/bson_iter_t.html
@[typedef]
pub struct C.bson_iter_t {}

// http://mongoc.org/libbson/current/bson_value_t.html
@[typedef]
pub struct C.bson_value_t {}

// http://mongoc.org/libbson/current/bson_error_t.html
Expand Down
2 changes: 1 addition & 1 deletion src/bson_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ fn test_new_bson_from() {
float: 0.5
boolean: true
}
text := new_bson_from<Test>(test)
text := new_bson_from[Test](test)
assert text.as_json() == '{ "str" : "test", "number" : 1, "float" : 0.5, "boolean" : true }'
}

Expand Down
File renamed without changes.
1 change: 0 additions & 1 deletion src/bulk_operation.v

This file was deleted.

13 changes: 13 additions & 0 deletions src/c_enums.c.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module mongo

// http://mongoc.org/libmongoc/1.17.0/mongoc_query_flags_t.html
pub enum QueryFlags {
@none = 0 // Specify no query flags.
tailable_cursor = 1 // Cursor will not be closed when the last data is retrieved. You can resume this cursor later.
slave_ok = 2 // Allow query of replica set secondaries.
oplog_replay = 3 // Used internally by MongoDB.
no_cursor_timeout = 4 // The server normally times out an idle cursor after an inactivity period (10 minutes). This prevents that.
await_data = 5 // Use with MONGOC_QUERY_TAILABLE_CURSOR. Block rather than returning no data. After a period, time out.
exhaust = 6 // Stream the data down full blast in multiple “reply” packets. Faster when you are pulling down a lot of data and you know you want to retrieve it all. Only applies to cursors created from a find operation (i.e. mongoc_collection_find()).
partial = 7 // Get partial results from mongos if some shards are down (instead of throwing an error).
}
13 changes: 0 additions & 13 deletions src/c_enums.v

This file was deleted.

9 changes: 8 additions & 1 deletion src/client.v
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
module mongo

pub fn new_client(uri string) &C.mongoc_client_t {
mongoc_uri := C.mongoc_uri_new(uri.str)
return C.mongoc_client_new_from_uri(mongoc_uri)
}

/*
pub fn new_client_(uri string) &C.mongoc_client_t {
mongoc_uri := C.mongoc_uri_new(uri.str)
mut error := C.bson_error_t{}
return C.mongoc_client_new_from_uri_with_error(mongoc_uri, &error)
}
*/

pub fn (client &C.mongoc_client_t) set_appname(name string) bool {
return C.mongoc_client_set_appname(client, name.str)
}

pub fn (client &C.mongoc_client_t) get_database(db_name string) &C.mongoc_database_t {
return C.mongoc_client_get_database(client, db_name.str)
return C.mongoc_client_get_database(client, &char(db_name.str))
}

pub fn (client &C.mongoc_client_t) get_collection(db_name string, collection_name string) &C.mongoc_collection_t {
Expand Down
Loading

0 comments on commit b537842

Please sign in to comment.